Skip to content

Commit 9d616f0

Browse files
committed
Use identifier argument for .update(), add lots of comments for future readers
1 parent f8f35ac commit 9d616f0

File tree

7 files changed

+52
-31
lines changed

7 files changed

+52
-31
lines changed

readthedocs/doc_builder/director.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ def setup_vcs(self):
9494
environment=self.vcs_environment,
9595
verbose_name=self.data.version.verbose_name,
9696
version_type=self.data.version.type,
97-
version_identifier=self.data.version.identifier,
9897
)
9998

10099
# We can't do too much on ``pre_checkout`` because we haven't
@@ -214,10 +213,10 @@ def build(self):
214213
def checkout(self):
215214
"""Checkout Git repo and load build config file."""
216215

217-
log.info("Cloning repository.")
218-
self.vcs_repository.update()
219-
220216
identifier = self.data.build_commit or self.data.version.identifier
217+
log.info("Cloning and fetching.", identifier=identifier)
218+
self.vcs_repository.update(identifier=identifier)
219+
221220
log.info("Checking out.", identifier=identifier)
222221
self.vcs_repository.checkout(identifier)
223222

readthedocs/projects/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,11 @@ def has_htmlzip(self, version_slug=LATEST, version_type=None):
993993
)
994994

995995
def vcs_repo(
996-
self, environment, version=LATEST, verbose_name=None, version_type=None
996+
self,
997+
environment,
998+
version=LATEST,
999+
verbose_name=None,
1000+
version_type=None,
9971001
):
9981002
"""
9991003
Return a Backend object for this project able to handle VCS commands.

readthedocs/vcs_support/backends/bzr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class Backend(BaseVCS):
1616
supports_tags = True
1717
fallback_branch = ''
1818

19-
def update(self):
20-
super().update()
19+
def update(self, identifier=None):
20+
super().update(identifier=identifier)
2121
if self.repo_exists():
2222
return self.up()
2323
return self.clone()

readthedocs/vcs_support/backends/git.py

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class Backend(BaseVCS):
3535
repo_depth = 50
3636

3737
def __init__(self, *args, **kwargs):
38-
self.version_identifier = kwargs.pop("version_identifier", None)
3938
super().__init__(*args, **kwargs)
4039
self.token = kwargs.get('token')
4140
self.repo_url = self._get_clone_url()
@@ -56,9 +55,15 @@ def _get_clone_url(self):
5655
def set_remote_url(self, url):
5756
return self.run('git', 'remote', 'set-url', 'origin', url)
5857

59-
def update(self):
60-
"""Clone or update the repository."""
61-
super().update()
58+
def update(self, identifier=None):
59+
"""
60+
Clone or update the repository.
61+
62+
:param identifier: This is the optional identifier for git fetch - a branch or tag name.
63+
PR references are generated automatically for certain Git providers.
64+
:return:
65+
"""
66+
super().update(identifier=identifier)
6267

6368
if self.use_clone_fetch_checkout_pattern():
6469

@@ -67,7 +72,7 @@ def update(self):
6772
self.clone_ng()
6873
# New behavior: No confusing return value. We are not using return values
6974
# in the callers.
70-
self.fetch_ng()
75+
self.fetch_ng(identifier=identifier)
7176

7277
else:
7378
if self.repo_exists():
@@ -80,52 +85,65 @@ def update(self):
8085
return self.fetch()
8186
return self.clone()
8287

83-
def get_remote_reference(self):
88+
def get_remote_fetch_reference(self, identifier):
89+
"""
90+
Gets a valid remote reference for the identifier.
91+
92+
:param identifier: Should be a branch or tag name when building branches or tags.
93+
:return: A reference valid for fetch operation
94+
"""
8495
# Tags and branches have the tag/branch identifier set by the caller who instantiated the
8596
# Git backend -- this means that the build process needs to know this from build data,
8697
# essentially from an incoming webhook call.
8798
if self.version_type in (BRANCH, TAG):
88-
return self.version_identifier
99+
return identifier
89100
if self.version_type == EXTERNAL:
101+
102+
# TODO: We should be able to resolve this without looking up in oauth registry
90103
git_provider_name = self.project.git_provider_name
104+
105+
# TODO: Why are these our only patterns?
91106
if git_provider_name == GITHUB_BRAND:
92107
return GITHUB_PR_PULL_PATTERN.format(id=self.verbose_name)
93108
if self.project.git_provider_name == GITLAB_BRAND:
94-
return GITLAB_MR_PULL_PATTERN
109+
return GITLAB_MR_PULL_PATTERN.format(id=self.verbose_name)
95110

96111
# This seems to be the default behavior when we don't know the remote
97112
# reference for a PR/MR: Just fetch everything
113+
# TODO: Provide more information about fetch operations without references
98114
return None
99115

100116
def clone_ng(self):
101117
# If the repository is already cloned, we don't do anything.
102-
# TODO: Why is it already cloned?
118+
# This is likely legacy, but we may want to be able to call .update()
119+
# several times in the same build
103120
if self.repo_exists():
104121
return
105122

106123
# --no-checkout: Makes it explicit what we are doing here. Nothing is checked out
107-
# until it's explcitly done.
124+
# until it's explicitly done.
108125
# --depth 1: Shallow clone, fetch as little data as possible.
109-
cmd = ["git", "clone", "--no-checkout", "--depth", "1"]
126+
cmd = ["git", "clone", "--no-checkout", "--depth", "1", self.repo_url, "."]
110127

111128
code, stdout, stderr = self.run(*cmd)
112129
return code, stdout, stderr
113130

114-
def fetch_ng(self):
131+
def fetch_ng(self, identifier):
115132
"""Implementation for new clone+fetch+checkout pattern."""
116133

117-
# --force: ?
134+
# --force: Likely legacy, it seems to be irrelevant to this usage
118135
# --tags: We need to fetch tags in order to resolve these references in the checkout
119-
# --prune: ?
120-
# --prune-tags: ?
136+
# --prune: Likely legacy, we don't expect a previous fetch command to have run
137+
# --prune-tags: Likely legacy, we don't expect a previous fetch command to have run
138+
# --tags: This flag was used in the previous approach such that all tags were fetched
139+
# in order to checkout a tag afterwards.
121140
cmd = ["git", "fetch", "origin", "--force", "--tags", "--prune", "--prune-tags"]
122-
remote_reference = self.get_remote_reference()
141+
remote_reference = self.get_remote_fetch_reference(identifier)
123142

124143
if remote_reference:
125-
# TODO: If we are fetching just one reference, what should the depth be?
144+
# TODO: We are still fetching the latest 50 commits.
126145
# A PR might have another commit added after the build has started...
127-
cmd.append("--depth 1")
128-
cmd.append(remote_reference)
146+
cmd.extend(["--depth", self.repo_depth, remote_reference])
129147

130148
code, stdout, stderr = self.run(*cmd)
131149
return code, stdout, stderr

readthedocs/vcs_support/backends/hg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class Backend(BaseVCS):
1212
supports_branches = True
1313
fallback_branch = 'default'
1414

15-
def update(self):
16-
super().update()
15+
def update(self, identifier=None):
16+
super().update(identifier=identifier)
1717
if self.repo_exists():
1818
return self.pull()
1919
return self.clone()

readthedocs/vcs_support/backends/svn.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def __init__(self, *args, **kwargs):
2626
else:
2727
self.base_url = self.repo_url
2828

29-
def update(self):
30-
super().update()
29+
def update(self, identifier=None):
30+
super().update(identifier=identifier)
3131
if self.repo_exists():
3232
return self.up()
3333
return self.co()

readthedocs/vcs_support/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def make_clean_working_dir(self):
8383
safe_rmtree(self.working_dir, ignore_errors=True)
8484
self.check_working_dir()
8585

86-
def update(self):
86+
def update(self, identifier=None):
8787
"""
8888
Update a local copy of the repository in self.working_dir.
8989

0 commit comments

Comments
 (0)