-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathgit.py
355 lines (290 loc) · 11.7 KB
/
git.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""Git-related utilities."""
import re
import git
import structlog
from django.core.exceptions import ValidationError
from git.exc import BadName, InvalidGitRepositoryError, NoSuchPathError
from gitdb.util import hex_to_bin
from readthedocs.builds.constants import EXTERNAL
from readthedocs.config import ALL
from readthedocs.projects.constants import (
GITHUB_BRAND,
GITHUB_PR_PULL_PATTERN,
GITLAB_BRAND,
GITLAB_MR_PULL_PATTERN,
)
from readthedocs.projects.exceptions import RepositoryError
from readthedocs.projects.validators import validate_submodule_url
from readthedocs.vcs_support.base import BaseVCS, VCSVersion
log = structlog.get_logger(__name__)
class Backend(BaseVCS):
"""Git VCS backend."""
supports_tags = True
supports_branches = True
supports_submodules = True
supports_lsremote = True
fallback_branch = 'master' # default branch
repo_depth = 50
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.token = kwargs.get('token')
self.repo_url = self._get_clone_url()
def _get_clone_url(self):
if '://' in self.repo_url:
hacked_url = self.repo_url.split('://')[1]
hacked_url = re.sub('.git$', '', hacked_url)
clone_url = 'https://%s' % hacked_url
if self.token:
clone_url = 'https://{}@{}'.format(self.token, hacked_url)
return clone_url
# Don't edit URL because all hosts aren't the same
# else:
# clone_url = 'git://%s' % (hacked_url)
return self.repo_url
def set_remote_url(self, url):
return self.run('git', 'remote', 'set-url', 'origin', url)
def update(self):
"""Clone or update the repository."""
super().update()
if self.repo_exists():
self.set_remote_url(self.repo_url)
return self.fetch()
self.make_clean_working_dir()
# A fetch is always required to get external versions properly
if self.version_type == EXTERNAL:
self.clone()
return self.fetch()
return self.clone()
def repo_exists(self):
try:
self._repo
except (InvalidGitRepositoryError, NoSuchPathError):
return False
return True
@property
def _repo(self):
"""Get a `git.Repo` instance from the current `self.working_dir`."""
return git.Repo(self.working_dir, expand_vars=False)
def are_submodules_available(self, config):
"""Test whether git submodule checkout step should be performed."""
submodules_in_config = (
config.submodules.exclude != ALL or config.submodules.include
)
if not submodules_in_config:
return False
# Keep compatibility with previous projects
return bool(self.submodules)
def validate_submodules(self, config):
"""
Returns the submodules and check that its URLs are valid.
.. note::
Always call after `self.are_submodules_available`.
:returns: tuple(bool, list)
Returns `True` if all required submodules URLs are valid.
Returns a list of all required submodules:
- Include is `ALL`, returns all submodules available.
- Include is a list, returns just those.
- Exclude is `ALL` - this should never happen.
- Exclude is a list, returns all available submodules
but those from the list.
Returns `False` if at least one submodule is invalid.
Returns the list of invalid submodules.
"""
submodules = {sub.path: sub for sub in self.submodules}
for sub_path in config.submodules.exclude:
path = sub_path.rstrip('/')
if path in submodules:
del submodules[path]
if config.submodules.include != ALL and config.submodules.include:
submodules_include = {}
for sub_path in config.submodules.include:
path = sub_path.rstrip('/')
submodules_include[path] = submodules[path]
submodules = submodules_include
invalid_submodules = []
for path, submodule in submodules.items():
try:
validate_submodule_url(submodule.url)
except ValidationError:
invalid_submodules.append(path)
if invalid_submodules:
return False, invalid_submodules
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):
# --force lets us checkout branches that are not fast-forwarded
# https://github.com/readthedocs/readthedocs.org/issues/6097
cmd = ['git', 'fetch', 'origin',
'--force', '--tags', '--prune', '--prune-tags']
if self.use_shallow_clone():
cmd.extend(['--depth', str(self.repo_depth)])
if self.verbose_name and self.version_type == EXTERNAL:
if self.project.git_provider_name == GITHUB_BRAND:
cmd.append(
GITHUB_PR_PULL_PATTERN.format(id=self.verbose_name)
)
if self.project.git_provider_name == GITLAB_BRAND:
cmd.append(
GITLAB_MR_PULL_PATTERN.format(id=self.verbose_name)
)
code, stdout, stderr = self.run(*cmd)
return code, stdout, stderr
def checkout_revision(self, revision=None):
if not revision:
branch = self.default_branch or self.fallback_branch
revision = 'origin/%s' % branch
try:
code, out, err = self.run('git', 'checkout', '--force', revision)
return [code, out, err]
except RepositoryError:
raise RepositoryError(
RepositoryError.FAILED_TO_CHECKOUT.format(revision),
)
def clone(self):
"""Clones the repository."""
cmd = ['git', 'clone', '--no-single-branch']
if self.use_shallow_clone():
cmd.extend(['--depth', str(self.repo_depth)])
cmd.extend([self.repo_url, '.'])
try:
code, stdout, stderr = self.run(*cmd)
return code, stdout, stderr
except RepositoryError:
raise RepositoryError(RepositoryError.CLONE_ERROR)
@property
def lsremote(self):
"""
Use ``git ls-remote`` to list branches and tags without cloning the repository.
:returns: tuple containing a list of branch and tags
"""
cmd = ['git', 'ls-remote', self.repo_url]
self.check_working_dir()
code, stdout, stderr = self.run(*cmd)
tags = []
branches = []
for line in stdout.splitlines()[1:]: # skip HEAD
commit, ref = line.split()
if ref.startswith('refs/heads/'):
branch = ref.replace('refs/heads/', '')
branches.append(VCSVersion(self, branch, branch))
if ref.startswith('refs/tags/'):
tag = ref.replace('refs/tags/', '')
if tag.endswith('^{}'):
# skip annotated tags since they are duplicated
continue
tags.append(VCSVersion(self, commit, tag))
return branches, tags
@property
def tags(self):
versions = []
repo = self._repo
# Build a cache of tag -> commit
# GitPython is not very optimized for reading large numbers of tags
ref_cache = {} # 'ref/tags/<tag>' -> hexsha
# This code is the same that is executed for each tag in gitpython,
# we execute it only once for all tags.
for hexsha, ref in git.TagReference._iter_packed_refs(repo):
gitobject = git.Object.new_from_sha(repo, hex_to_bin(hexsha))
if gitobject.type == 'commit':
ref_cache[ref] = str(gitobject)
elif gitobject.type == 'tag' and gitobject.object.type == 'commit':
ref_cache[ref] = str(gitobject.object)
for tag in repo.tags:
if tag.path in ref_cache:
hexsha = ref_cache[tag.path]
else:
try:
hexsha = str(tag.commit)
except ValueError:
# ValueError: Cannot resolve commit as tag TAGNAME points to a
# blob object - use the `.object` property instead to access it
# This is not a real tag for us, so we skip it
# https://github.com/rtfd/readthedocs.org/issues/4440
log.warning('Git tag skipped.', tag=tag, exc_info=True)
continue
versions.append(VCSVersion(self, hexsha, str(tag)))
return versions
@property
def branches(self):
repo = self._repo
versions = []
branches = []
# ``repo.remotes.origin.refs`` returns remote branches
if repo.remotes:
branches += repo.remotes.origin.refs
for branch in branches:
verbose_name = branch.name
if verbose_name.startswith('origin/'):
verbose_name = verbose_name.replace('origin/', '')
if verbose_name == 'HEAD':
continue
versions.append(VCSVersion(self, str(branch), verbose_name))
return versions
@property
def commit(self):
if self.repo_exists():
_, stdout, _ = self.run('git', 'rev-parse', 'HEAD', record=False)
return stdout.strip()
return None
@property
def submodules(self):
return list(self._repo.submodules)
def checkout(self, identifier=None):
"""Checkout to identifier or latest."""
super().checkout()
# Find proper identifier
if not identifier:
identifier = self.default_branch or self.fallback_branch
identifier = self.find_ref(identifier)
# Checkout the correct identifier for this branch.
code, out, err = self.checkout_revision(identifier)
# Clean any remains of previous checkouts
self.run('git', 'clean', '-d', '-f', '-f')
return code, out, err
def update_submodules(self, config):
if self.are_submodules_available(config):
valid, submodules = self.validate_submodules(config)
if valid:
self.checkout_submodules(submodules, config)
else:
raise RepositoryError(
RepositoryError.INVALID_SUBMODULES.format(submodules),
)
def checkout_submodules(self, submodules, config):
"""Checkout all repository submodules."""
self.run('git', 'submodule', 'sync')
cmd = [
'git',
'submodule',
'update',
'--init',
'--force',
]
if config.submodules.recursive:
cmd.append('--recursive')
cmd += submodules
self.run(*cmd)
def find_ref(self, ref):
# Check if ref starts with 'origin/'
if ref.startswith('origin/'):
return ref
# Check if ref is a branch of the origin remote
if self.ref_exists('remotes/origin/' + ref):
return 'origin/' + ref
return ref
def ref_exists(self, ref):
try:
if self._repo.commit(ref):
return True
except (BadName, ValueError):
return False
return False