Skip to content

Commit 9392c3f

Browse files
stephenfinericholscher
authored andcommitted
Add temporary method for disabling shallow cloning (#5031)
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 <[email protected]>
1 parent fa5d272 commit 9392c3f

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

readthedocs/projects/models.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,7 @@ def add_features(sender, **kwargs):
10131013
DONT_OVERWRITE_SPHINX_CONTEXT = 'dont_overwrite_sphinx_context'
10141014
ALLOW_V2_CONFIG_FILE = 'allow_v2_config_file'
10151015
MKDOCS_THEME_RTD = 'mkdocs_theme_rtd'
1016+
DONT_SHALLOW_CLONE = 'dont_shallow_clone'
10161017

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

10301033
projects = models.ManyToManyField(

readthedocs/rtd_tests/tests/test_backend.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,27 @@ def test_skip_submodule_checkout(self):
149149
repo.update()
150150
repo.checkout('submodule')
151151
self.assertTrue(repo.are_submodules_available(self.dummy_conf))
152-
feature = fixture.get(
152+
fixture.get(
153153
Feature,
154154
projects=[self.project],
155155
feature_id=Feature.SKIP_SUBMODULES,
156156
)
157157
self.assertTrue(self.project.has_feature(Feature.SKIP_SUBMODULES))
158158
self.assertFalse(repo.are_submodules_available(self.dummy_conf))
159159

160+
def test_use_shallow_clone(self):
161+
repo = self.project.vcs_repo()
162+
repo.update()
163+
repo.checkout('submodule')
164+
self.assertTrue(repo.use_shallow_clone())
165+
fixture.get(
166+
Feature,
167+
projects=[self.project],
168+
feature_id=Feature.DONT_SHALLOW_CLONE,
169+
)
170+
self.assertTrue(self.project.has_feature(Feature.DONT_SHALLOW_CLONE))
171+
self.assertFalse(repo.use_shallow_clone())
172+
160173
def test_check_submodule_urls(self):
161174
repo = self.project.vcs_repo()
162175
repo.update()

readthedocs/vcs_support/backends/git.py

+27-8
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,26 @@ def validate_submodules(self, config):
129129
return False, []
130130
return True, submodules.keys()
131131

132+
def use_shallow_clone(self):
133+
"""
134+
Test whether shallow clone should be performed.
135+
136+
.. note::
137+
138+
Temporarily, we support skipping this option as builds that rely on
139+
git history can fail if using shallow clones. This should
140+
eventually be configurable via the web UI.
141+
"""
142+
from readthedocs.projects.models import Feature
143+
return not self.project.has_feature(Feature.DONT_SHALLOW_CLONE)
144+
132145
def fetch(self):
133-
code, stdout, stderr = self.run(
134-
'git', 'fetch', '--depth', str(self.repo_depth),
135-
'--tags', '--prune', '--prune-tags',
136-
)
146+
cmd = ['git', 'fetch', '--tags', '--prune', '--prune-tags']
147+
148+
if self.use_shallow_clone():
149+
cmd.extend(['--depth', str(self.repo_depth)])
150+
151+
code, stdout, stderr = self.run(*cmd)
137152
if code != 0:
138153
raise RepositoryError
139154
return code, stdout, stderr
@@ -150,10 +165,14 @@ def checkout_revision(self, revision=None):
150165

151166
def clone(self):
152167
"""Clones the repository."""
153-
code, stdout, stderr = self.run(
154-
'git', 'clone', '--depth', str(self.repo_depth),
155-
'--no-single-branch', self.repo_url, '.'
156-
)
168+
cmd = ['git', 'clone', '--no-single-branch']
169+
170+
if self.use_shallow_clone():
171+
cmd.extend(['--depth', str(self.repo_depth)])
172+
173+
cmd.extend([self.repo_url, '.'])
174+
175+
code, stdout, stderr = self.run(*cmd)
157176
if code != 0:
158177
raise RepositoryError
159178
return code, stdout, stderr

0 commit comments

Comments
 (0)