Skip to content

Commit

Permalink
Add temporary method for disabling shallow cloning (#5031)
Browse files Browse the repository at this point in the history
This adds a project feature that allows us to use a standard clone and
fetch rather than the shallow clone/fetch introduced in #4939.
Eventually we should move this to the web UI, but doing so requires some
work to make sure, for example, that git options are only show when
'Project.repo_type' is 'git'.

Signed-off-by: Stephen Finucane <stephen@that.guru>
  • Loading branch information
stephenfin authored and ericholscher committed Dec 26, 2018
1 parent fa5d272 commit 9392c3f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
5 changes: 4 additions & 1 deletion readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,7 @@ def add_features(sender, **kwargs):
DONT_OVERWRITE_SPHINX_CONTEXT = 'dont_overwrite_sphinx_context'
ALLOW_V2_CONFIG_FILE = 'allow_v2_config_file'
MKDOCS_THEME_RTD = 'mkdocs_theme_rtd'
DONT_SHALLOW_CLONE = 'dont_shallow_clone'

FEATURES = (
(USE_SPHINX_LATEST, _('Use latest version of Sphinx')),
Expand All @@ -1021,10 +1022,12 @@ def add_features(sender, **kwargs):
(PIP_ALWAYS_UPGRADE, _('Always run pip install --upgrade')),
(SKIP_SUBMODULES, _('Skip git submodule checkout')),
(DONT_OVERWRITE_SPHINX_CONTEXT, _(
'Do not overwrite context vars in conf.py with Read the Docs context',)),
'Do not overwrite context vars in conf.py with Read the Docs context')),
(ALLOW_V2_CONFIG_FILE, _(
'Allow to use the v2 of the configuration file')),
(MKDOCS_THEME_RTD, _('Use Read the Docs theme for MkDocs as default theme')),
(DONT_SHALLOW_CLONE, _(
'Do not shallow clone when cloning git repos')),
)

projects = models.ManyToManyField(
Expand Down
15 changes: 14 additions & 1 deletion readthedocs/rtd_tests/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,27 @@ def test_skip_submodule_checkout(self):
repo.update()
repo.checkout('submodule')
self.assertTrue(repo.are_submodules_available(self.dummy_conf))
feature = fixture.get(
fixture.get(
Feature,
projects=[self.project],
feature_id=Feature.SKIP_SUBMODULES,
)
self.assertTrue(self.project.has_feature(Feature.SKIP_SUBMODULES))
self.assertFalse(repo.are_submodules_available(self.dummy_conf))

def test_use_shallow_clone(self):
repo = self.project.vcs_repo()
repo.update()
repo.checkout('submodule')
self.assertTrue(repo.use_shallow_clone())
fixture.get(
Feature,
projects=[self.project],
feature_id=Feature.DONT_SHALLOW_CLONE,
)
self.assertTrue(self.project.has_feature(Feature.DONT_SHALLOW_CLONE))
self.assertFalse(repo.use_shallow_clone())

def test_check_submodule_urls(self):
repo = self.project.vcs_repo()
repo.update()
Expand Down
35 changes: 27 additions & 8 deletions readthedocs/vcs_support/backends/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,26 @@ def validate_submodules(self, config):
return False, []
return True, submodules.keys()

def use_shallow_clone(self):
"""
Test whether shallow clone should be performed.
.. note::
Temporarily, we support skipping this option as builds that rely on
git history can fail if using shallow clones. This should
eventually be configurable via the web UI.
"""
from readthedocs.projects.models import Feature
return not self.project.has_feature(Feature.DONT_SHALLOW_CLONE)

def fetch(self):
code, stdout, stderr = self.run(
'git', 'fetch', '--depth', str(self.repo_depth),
'--tags', '--prune', '--prune-tags',
)
cmd = ['git', 'fetch', '--tags', '--prune', '--prune-tags']

if self.use_shallow_clone():
cmd.extend(['--depth', str(self.repo_depth)])

code, stdout, stderr = self.run(*cmd)
if code != 0:
raise RepositoryError
return code, stdout, stderr
Expand All @@ -150,10 +165,14 @@ def checkout_revision(self, revision=None):

def clone(self):
"""Clones the repository."""
code, stdout, stderr = self.run(
'git', 'clone', '--depth', str(self.repo_depth),
'--no-single-branch', self.repo_url, '.'
)
cmd = ['git', 'clone', '--no-single-branch']

if self.use_shallow_clone():
cmd.extend(['--depth', str(self.repo_depth)])

cmd.extend([self.repo_url, '.'])

code, stdout, stderr = self.run(*cmd)
if code != 0:
raise RepositoryError
return code, stdout, stderr
Expand Down

0 comments on commit 9392c3f

Please sign in to comment.