blob: fe88a505e2c6ebfbdb1d4b8eeb79357a1b302e3d [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001# Copyright (C) 2008 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -080015import errno
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070016import filecmp
Wink Saville4c426ef2015-06-03 08:05:17 -070017import glob
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018import os
Shawn O. Pearcec325dc32011-10-03 08:30:24 -070019import random
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020import re
21import shutil
22import stat
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -070023import subprocess
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070024import sys
Julien Campergue335f5ef2013-10-16 11:02:35 +020025import tarfile
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +080026import tempfile
Shawn O. Pearcec325dc32011-10-03 08:30:24 -070027import time
Mike Frysingeracf63b22019-06-13 02:24:21 -040028import urllib.parse
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -070029
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070030from color import Coloring
Dave Borowitzb42b4742012-10-31 12:27:27 -070031from git_command import GitCommand, git_require
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -070032from git_config import GitConfig, IsId, GetSchemeFromUrl, GetUrlCookieFile, \
33 ID_RE
Remy Bohmer16c13282020-09-10 10:38:04 +020034from error import GitError, UploadError, DownloadError
Mike Frysingere6a202f2019-08-02 15:57:57 -040035from error import ManifestInvalidRevisionError, ManifestInvalidPathError
Conley Owens75ee0572012-11-15 17:33:11 -080036from error import NoManifestException
Renaud Paquayd5cec5e2016-11-01 11:24:03 -070037import platform_utils
Mike Frysinger70d861f2019-08-26 15:22:36 -040038import progress
Mike Frysinger8a11f6f2019-08-27 00:26:15 -040039from repo_trace import IsTrace, Trace
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040
Mike Frysinger21b7fbe2020-02-26 23:53:36 -050041from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M, R_WORKTREE_M
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070042
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -070043
George Engelbrecht9bc283e2020-04-02 12:36:09 -060044# Maximum sleep time allowed during retries.
45MAXIMUM_RETRY_SLEEP_SEC = 3600.0
46# +-10% random jitter is added to each Fetches retry sleep duration.
47RETRY_JITTER_PERCENT = 0.1
48
49
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -070050def _lwrite(path, content):
51 lock = '%s.lock' % path
52
Remy Bohmer169b0212020-11-21 10:57:52 +010053 # Maintain Unix line endings on all OS's to match git behavior.
54 with open(lock, 'w', newline='\n') as fd:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -070055 fd.write(content)
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -070056
57 try:
Renaud Paquayad1abcb2016-11-01 11:34:55 -070058 platform_utils.rename(lock, path)
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -070059 except OSError:
Renaud Paquay010fed72016-11-11 14:25:29 -080060 platform_utils.remove(lock)
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -070061 raise
62
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -070063
Shawn O. Pearce48244782009-04-16 08:25:57 -070064def _error(fmt, *args):
65 msg = fmt % args
Sarah Owenscecd1d82012-11-01 22:59:27 -070066 print('error: %s' % msg, file=sys.stderr)
Shawn O. Pearce48244782009-04-16 08:25:57 -070067
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -070068
David Pursehousef33929d2015-08-24 14:39:14 +090069def _warn(fmt, *args):
70 msg = fmt % args
71 print('warn: %s' % msg, file=sys.stderr)
72
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -070073
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070074def not_rev(r):
75 return '^' + r
76
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -070077
Shawn O. Pearceb54a3922009-01-05 16:18:58 -080078def sq(r):
79 return "'" + r.replace("'", "'\''") + "'"
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080080
David Pursehouse819827a2020-02-12 15:20:19 +090081
Jonathan Nieder93719792015-03-17 11:29:58 -070082_project_hook_list = None
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -070083
84
Jonathan Nieder93719792015-03-17 11:29:58 -070085def _ProjectHooks():
86 """List the hooks present in the 'hooks' directory.
87
88 These hooks are project hooks and are copied to the '.git/hooks' directory
89 of all subprojects.
90
91 This function caches the list of hooks (based on the contents of the
92 'repo/hooks' directory) on the first call.
93
94 Returns:
95 A list of absolute paths to all of the files in the hooks directory.
96 """
97 global _project_hook_list
98 if _project_hook_list is None:
Renaud Paquay227ad2e2016-11-01 14:37:13 -070099 d = platform_utils.realpath(os.path.abspath(os.path.dirname(__file__)))
Jonathan Nieder93719792015-03-17 11:29:58 -0700100 d = os.path.join(d, 'hooks')
Renaud Paquaybed8b622018-09-27 10:46:58 -0700101 _project_hook_list = [os.path.join(d, x) for x in platform_utils.listdir(d)]
Jonathan Nieder93719792015-03-17 11:29:58 -0700102 return _project_hook_list
103
104
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700105class DownloadedChange(object):
106 _commit_cache = None
107
108 def __init__(self, project, base, change_id, ps_id, commit):
109 self.project = project
110 self.base = base
111 self.change_id = change_id
112 self.ps_id = ps_id
113 self.commit = commit
114
115 @property
116 def commits(self):
117 if self._commit_cache is None:
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700118 self._commit_cache = self.project.bare_git.rev_list('--abbrev=8',
119 '--abbrev-commit',
120 '--pretty=oneline',
121 '--reverse',
122 '--date-order',
123 not_rev(self.base),
124 self.commit,
125 '--')
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700126 return self._commit_cache
127
128
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700129class ReviewableBranch(object):
130 _commit_cache = None
Mike Frysinger6da17752019-09-11 18:43:17 -0400131 _base_exists = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700132
133 def __init__(self, project, branch, base):
134 self.project = project
135 self.branch = branch
136 self.base = base
137
138 @property
139 def name(self):
140 return self.branch.name
141
142 @property
143 def commits(self):
144 if self._commit_cache is None:
Mike Frysinger6da17752019-09-11 18:43:17 -0400145 args = ('--abbrev=8', '--abbrev-commit', '--pretty=oneline', '--reverse',
146 '--date-order', not_rev(self.base), R_HEADS + self.name, '--')
147 try:
148 self._commit_cache = self.project.bare_git.rev_list(*args)
149 except GitError:
150 # We weren't able to probe the commits for this branch. Was it tracking
151 # a branch that no longer exists? If so, return no commits. Otherwise,
152 # rethrow the error as we don't know what's going on.
153 if self.base_exists:
154 raise
155
156 self._commit_cache = []
157
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700158 return self._commit_cache
159
160 @property
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800161 def unabbrev_commits(self):
162 r = dict()
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700163 for commit in self.project.bare_git.rev_list(not_rev(self.base),
164 R_HEADS + self.name,
165 '--'):
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800166 r[commit[0:8]] = commit
167 return r
168
169 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700170 def date(self):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700171 return self.project.bare_git.log('--pretty=format:%cd',
172 '-n', '1',
173 R_HEADS + self.name,
174 '--')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700175
Mike Frysinger6da17752019-09-11 18:43:17 -0400176 @property
177 def base_exists(self):
178 """Whether the branch we're tracking exists.
179
180 Normally it should, but sometimes branches we track can get deleted.
181 """
182 if self._base_exists is None:
183 try:
184 self.project.bare_git.rev_parse('--verify', not_rev(self.base))
185 # If we're still here, the base branch exists.
186 self._base_exists = True
187 except GitError:
188 # If we failed to verify, the base branch doesn't exist.
189 self._base_exists = False
190
191 return self._base_exists
192
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700193 def UploadForReview(self, people,
Mike Frysinger819cc812020-02-19 02:27:22 -0500194 dryrun=False,
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700195 auto_topic=False,
Mike Frysinger84685ba2020-02-19 02:22:22 -0500196 hashtags=(),
Mike Frysingerfc1b18a2020-02-24 15:38:07 -0500197 labels=(),
Changcheng Xiao87984c62017-08-02 16:55:03 +0200198 private=False,
Vadim Bendeburybd8f6582018-10-31 13:48:01 -0700199 notify=None,
Changcheng Xiao87984c62017-08-02 16:55:03 +0200200 wip=False,
Łukasz Gardońbed59ce2017-08-08 10:18:11 +0200201 dest_branch=None,
Masaya Suzuki305a2d02017-11-13 10:48:34 -0800202 validate_certs=True,
203 push_options=None):
Mike Frysinger819cc812020-02-19 02:27:22 -0500204 self.project.UploadForReview(branch=self.name,
205 people=people,
206 dryrun=dryrun,
Brian Harring435370c2012-07-28 15:37:04 -0700207 auto_topic=auto_topic,
Mike Frysinger84685ba2020-02-19 02:22:22 -0500208 hashtags=hashtags,
Mike Frysingerfc1b18a2020-02-24 15:38:07 -0500209 labels=labels,
Changcheng Xiao87984c62017-08-02 16:55:03 +0200210 private=private,
Vadim Bendeburybd8f6582018-10-31 13:48:01 -0700211 notify=notify,
Changcheng Xiao87984c62017-08-02 16:55:03 +0200212 wip=wip,
Łukasz Gardońbed59ce2017-08-08 10:18:11 +0200213 dest_branch=dest_branch,
Masaya Suzuki305a2d02017-11-13 10:48:34 -0800214 validate_certs=validate_certs,
215 push_options=push_options)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700216
Ficus Kirkpatrickbc7ef672009-05-04 12:45:11 -0700217 def GetPublishedRefs(self):
218 refs = {}
219 output = self.project.bare_git.ls_remote(
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700220 self.branch.remote.SshReviewUrl(self.project.UserEmail),
221 'refs/changes/*')
Ficus Kirkpatrickbc7ef672009-05-04 12:45:11 -0700222 for line in output.split('\n'):
223 try:
224 (sha, ref) = line.split()
225 refs[sha] = ref
226 except ValueError:
227 pass
228
229 return refs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700230
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700231
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700232class StatusColoring(Coloring):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700233
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700234 def __init__(self, config):
Mike Frysinger5d9c4972021-02-19 13:34:09 -0500235 super().__init__(config, 'status')
Anthony King7bdac712014-07-16 12:56:40 +0100236 self.project = self.printer('header', attr='bold')
237 self.branch = self.printer('header', attr='bold')
238 self.nobranch = self.printer('nobranch', fg='red')
239 self.important = self.printer('important', fg='red')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700240
Anthony King7bdac712014-07-16 12:56:40 +0100241 self.added = self.printer('added', fg='green')
242 self.changed = self.printer('changed', fg='red')
243 self.untracked = self.printer('untracked', fg='red')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700244
245
246class DiffColoring(Coloring):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700247
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700248 def __init__(self, config):
Mike Frysinger5d9c4972021-02-19 13:34:09 -0500249 super().__init__(config, 'diff')
Anthony King7bdac712014-07-16 12:56:40 +0100250 self.project = self.printer('header', attr='bold')
Mike Frysinger0a9265e2019-09-30 23:59:27 -0400251 self.fail = self.printer('fail', fg='red')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700252
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700253
Jack Neus6ea0cae2021-07-20 20:52:33 +0000254class Annotation(object):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700255
James W. Mills24c13082012-04-12 15:04:13 -0500256 def __init__(self, name, value, keep):
257 self.name = name
258 self.value = value
259 self.keep = keep
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700260
Jack Neus6ea0cae2021-07-20 20:52:33 +0000261 def __eq__(self, other):
262 if not isinstance(other, Annotation):
263 return False
264 return self.__dict__ == other.__dict__
265
266 def __lt__(self, other):
267 # This exists just so that lists of Annotation objects can be sorted, for
268 # use in comparisons.
269 if not isinstance(other, Annotation):
270 raise ValueError('comparison is not between two Annotation objects')
271 if self.name == other.name:
272 if self.value == other.value:
273 return self.keep < other.keep
274 return self.value < other.value
275 return self.name < other.name
276
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700277
Mike Frysingere6a202f2019-08-02 15:57:57 -0400278def _SafeExpandPath(base, subpath, skipfinal=False):
279 """Make sure |subpath| is completely safe under |base|.
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700280
Mike Frysingere6a202f2019-08-02 15:57:57 -0400281 We make sure no intermediate symlinks are traversed, and that the final path
282 is not a special file (e.g. not a socket or fifo).
283
284 NB: We rely on a number of paths already being filtered out while parsing the
285 manifest. See the validation logic in manifest_xml.py for more details.
286 """
Mike Frysingerd9254592020-02-19 22:36:26 -0500287 # Split up the path by its components. We can't use os.path.sep exclusively
288 # as some platforms (like Windows) will convert / to \ and that bypasses all
289 # our constructed logic here. Especially since manifest authors only use
290 # / in their paths.
291 resep = re.compile(r'[/%s]' % re.escape(os.path.sep))
292 components = resep.split(subpath)
Mike Frysingere6a202f2019-08-02 15:57:57 -0400293 if skipfinal:
294 # Whether the caller handles the final component itself.
295 finalpart = components.pop()
296
297 path = base
298 for part in components:
299 if part in {'.', '..'}:
300 raise ManifestInvalidPathError(
301 '%s: "%s" not allowed in paths' % (subpath, part))
302
303 path = os.path.join(path, part)
304 if platform_utils.islink(path):
305 raise ManifestInvalidPathError(
306 '%s: traversing symlinks not allow' % (path,))
307
308 if os.path.exists(path):
309 if not os.path.isfile(path) and not platform_utils.isdir(path):
310 raise ManifestInvalidPathError(
311 '%s: only regular files & directories allowed' % (path,))
312
313 if skipfinal:
314 path = os.path.join(path, finalpart)
315
316 return path
317
318
319class _CopyFile(object):
320 """Container for <copyfile> manifest element."""
321
322 def __init__(self, git_worktree, src, topdir, dest):
323 """Register a <copyfile> request.
324
325 Args:
326 git_worktree: Absolute path to the git project checkout.
327 src: Relative path under |git_worktree| of file to read.
328 topdir: Absolute path to the top of the repo client checkout.
329 dest: Relative path under |topdir| of file to write.
330 """
331 self.git_worktree = git_worktree
332 self.topdir = topdir
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700333 self.src = src
334 self.dest = dest
335
336 def _Copy(self):
Mike Frysingere6a202f2019-08-02 15:57:57 -0400337 src = _SafeExpandPath(self.git_worktree, self.src)
338 dest = _SafeExpandPath(self.topdir, self.dest)
339
340 if platform_utils.isdir(src):
341 raise ManifestInvalidPathError(
342 '%s: copying from directory not supported' % (self.src,))
343 if platform_utils.isdir(dest):
344 raise ManifestInvalidPathError(
345 '%s: copying to directory not allowed' % (self.dest,))
346
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700347 # copy file if it does not exist or is out of date
348 if not os.path.exists(dest) or not filecmp.cmp(src, dest):
349 try:
350 # remove existing file first, since it might be read-only
351 if os.path.exists(dest):
Renaud Paquay010fed72016-11-11 14:25:29 -0800352 platform_utils.remove(dest)
Matthew Buckett2daf6672009-07-11 09:43:47 -0400353 else:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200354 dest_dir = os.path.dirname(dest)
Renaud Paquaybed8b622018-09-27 10:46:58 -0700355 if not platform_utils.isdir(dest_dir):
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200356 os.makedirs(dest_dir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700357 shutil.copy(src, dest)
358 # make the file read-only
359 mode = os.stat(dest)[stat.ST_MODE]
360 mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
361 os.chmod(dest, mode)
362 except IOError:
Shawn O. Pearce48244782009-04-16 08:25:57 -0700363 _error('Cannot copy file %s to %s', src, dest)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700364
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700365
Anthony King7bdac712014-07-16 12:56:40 +0100366class _LinkFile(object):
Mike Frysingere6a202f2019-08-02 15:57:57 -0400367 """Container for <linkfile> manifest element."""
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700368
Mike Frysingere6a202f2019-08-02 15:57:57 -0400369 def __init__(self, git_worktree, src, topdir, dest):
370 """Register a <linkfile> request.
371
372 Args:
373 git_worktree: Absolute path to the git project checkout.
374 src: Target of symlink relative to path under |git_worktree|.
375 topdir: Absolute path to the top of the repo client checkout.
376 dest: Relative path under |topdir| of symlink to create.
377 """
Wink Saville4c426ef2015-06-03 08:05:17 -0700378 self.git_worktree = git_worktree
Mike Frysingere6a202f2019-08-02 15:57:57 -0400379 self.topdir = topdir
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500380 self.src = src
381 self.dest = dest
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500382
Wink Saville4c426ef2015-06-03 08:05:17 -0700383 def __linkIt(self, relSrc, absDest):
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500384 # link file if it does not exist or is out of date
Renaud Paquay227ad2e2016-11-01 14:37:13 -0700385 if not platform_utils.islink(absDest) or (platform_utils.readlink(absDest) != relSrc):
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500386 try:
387 # remove existing file first, since it might be read-only
Dan Willemsene1e0bd12015-11-18 16:49:38 -0800388 if os.path.lexists(absDest):
Renaud Paquay010fed72016-11-11 14:25:29 -0800389 platform_utils.remove(absDest)
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500390 else:
Wink Saville4c426ef2015-06-03 08:05:17 -0700391 dest_dir = os.path.dirname(absDest)
Renaud Paquaybed8b622018-09-27 10:46:58 -0700392 if not platform_utils.isdir(dest_dir):
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500393 os.makedirs(dest_dir)
Renaud Paquayd5cec5e2016-11-01 11:24:03 -0700394 platform_utils.symlink(relSrc, absDest)
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500395 except IOError:
Wink Saville4c426ef2015-06-03 08:05:17 -0700396 _error('Cannot link file %s to %s', relSrc, absDest)
397
398 def _Link(self):
Mike Frysingere6a202f2019-08-02 15:57:57 -0400399 """Link the self.src & self.dest paths.
400
401 Handles wild cards on the src linking all of the files in the source in to
402 the destination directory.
Wink Saville4c426ef2015-06-03 08:05:17 -0700403 """
Mike Frysinger07392ed2020-02-10 21:35:48 -0500404 # Some people use src="." to create stable links to projects. Lets allow
405 # that but reject all other uses of "." to keep things simple.
406 if self.src == '.':
407 src = self.git_worktree
408 else:
409 src = _SafeExpandPath(self.git_worktree, self.src)
Mike Frysingere6a202f2019-08-02 15:57:57 -0400410
Angel Petkovdbfbcb12020-05-02 23:16:20 +0300411 if not glob.has_magic(src):
412 # Entity does not contain a wild card so just a simple one to one link operation.
Mike Frysingere6a202f2019-08-02 15:57:57 -0400413 dest = _SafeExpandPath(self.topdir, self.dest, skipfinal=True)
414 # dest & src are absolute paths at this point. Make sure the target of
415 # the symlink is relative in the context of the repo client checkout.
416 relpath = os.path.relpath(src, os.path.dirname(dest))
417 self.__linkIt(relpath, dest)
Wink Saville4c426ef2015-06-03 08:05:17 -0700418 else:
Mike Frysingere6a202f2019-08-02 15:57:57 -0400419 dest = _SafeExpandPath(self.topdir, self.dest)
Angel Petkovdbfbcb12020-05-02 23:16:20 +0300420 # Entity contains a wild card.
Mike Frysingere6a202f2019-08-02 15:57:57 -0400421 if os.path.exists(dest) and not platform_utils.isdir(dest):
422 _error('Link error: src with wildcard, %s must be a directory', dest)
Wink Saville4c426ef2015-06-03 08:05:17 -0700423 else:
Mike Frysingere6a202f2019-08-02 15:57:57 -0400424 for absSrcFile in glob.glob(src):
Wink Saville4c426ef2015-06-03 08:05:17 -0700425 # Create a releative path from source dir to destination dir
426 absSrcDir = os.path.dirname(absSrcFile)
Mike Frysingere6a202f2019-08-02 15:57:57 -0400427 relSrcDir = os.path.relpath(absSrcDir, dest)
Wink Saville4c426ef2015-06-03 08:05:17 -0700428
429 # Get the source file name
430 srcFile = os.path.basename(absSrcFile)
431
432 # Now form the final full paths to srcFile. They will be
433 # absolute for the desintaiton and relative for the srouce.
Mike Frysingere6a202f2019-08-02 15:57:57 -0400434 absDest = os.path.join(dest, srcFile)
Wink Saville4c426ef2015-06-03 08:05:17 -0700435 relSrc = os.path.join(relSrcDir, srcFile)
436 self.__linkIt(relSrc, absDest)
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500437
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700438
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700439class RemoteSpec(object):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700440
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700441 def __init__(self,
442 name,
Anthony King7bdac712014-07-16 12:56:40 +0100443 url=None,
Steve Raed6480452016-08-10 15:00:00 -0700444 pushUrl=None,
Anthony King7bdac712014-07-16 12:56:40 +0100445 review=None,
Dan Willemsen96c2d652016-04-06 16:03:54 -0700446 revision=None,
David Rileye0684ad2017-04-05 00:02:59 -0700447 orig_name=None,
448 fetchUrl=None):
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700449 self.name = name
450 self.url = url
Steve Raed6480452016-08-10 15:00:00 -0700451 self.pushUrl = pushUrl
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700452 self.review = review
Anthony King36ea2fb2014-05-06 11:54:01 +0100453 self.revision = revision
Dan Willemsen96c2d652016-04-06 16:03:54 -0700454 self.orig_name = orig_name
David Rileye0684ad2017-04-05 00:02:59 -0700455 self.fetchUrl = fetchUrl
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700456
Ian Kasprzak0286e312021-02-05 10:06:18 -0800457
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700458class Project(object):
Kevin Degi384b3c52014-10-16 16:02:58 -0600459 # These objects can be shared between several working trees.
460 shareable_files = ['description', 'info']
461 shareable_dirs = ['hooks', 'objects', 'rr-cache', 'svn']
462 # These objects can only be used by a single working tree.
463 working_tree_files = ['config', 'packed-refs', 'shallow']
464 working_tree_dirs = ['logs', 'refs']
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700465
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700466 def __init__(self,
467 manifest,
468 name,
469 remote,
470 gitdir,
David James8d201162013-10-11 17:03:19 -0700471 objdir,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700472 worktree,
473 relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700474 revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800475 revisionId,
Anthony King7bdac712014-07-16 12:56:40 +0100476 rebase=True,
477 groups=None,
478 sync_c=False,
479 sync_s=False,
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +0900480 sync_tags=True,
Anthony King7bdac712014-07-16 12:56:40 +0100481 clone_depth=None,
482 upstream=None,
483 parent=None,
Mike Frysinger979d5bd2020-02-09 02:28:34 -0500484 use_git_worktrees=False,
Anthony King7bdac712014-07-16 12:56:40 +0100485 is_derived=False,
David Pursehouseb1553542014-09-04 21:28:09 +0900486 dest_branch=None,
Simran Basib9a1b732015-08-20 12:19:28 -0700487 optimized_fetch=False,
George Engelbrecht9bc283e2020-04-02 12:36:09 -0600488 retry_fetches=0,
Simran Basib9a1b732015-08-20 12:19:28 -0700489 old_revision=None):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800490 """Init a Project object.
491
492 Args:
493 manifest: The XmlManifest object.
494 name: The `name` attribute of manifest.xml's project element.
495 remote: RemoteSpec object specifying its remote's properties.
496 gitdir: Absolute path of git directory.
David James8d201162013-10-11 17:03:19 -0700497 objdir: Absolute path of directory to store git objects.
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800498 worktree: Absolute path of git working tree.
499 relpath: Relative path of git working tree to repo's top directory.
500 revisionExpr: The `revision` attribute of manifest.xml's project element.
501 revisionId: git commit id for checking out.
502 rebase: The `rebase` attribute of manifest.xml's project element.
503 groups: The `groups` attribute of manifest.xml's project element.
504 sync_c: The `sync-c` attribute of manifest.xml's project element.
505 sync_s: The `sync-s` attribute of manifest.xml's project element.
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +0900506 sync_tags: The `sync-tags` attribute of manifest.xml's project element.
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800507 upstream: The `upstream` attribute of manifest.xml's project element.
508 parent: The parent Project object.
Mike Frysinger979d5bd2020-02-09 02:28:34 -0500509 use_git_worktrees: Whether to use `git worktree` for this project.
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800510 is_derived: False if the project was explicitly defined in the manifest;
511 True if the project is a discovered submodule.
Bryan Jacobsf609f912013-05-06 13:36:24 -0400512 dest_branch: The branch to which to push changes for review by default.
David Pursehouseb1553542014-09-04 21:28:09 +0900513 optimized_fetch: If True, when a project is set to a sha1 revision, only
514 fetch from the remote if the sha1 is not present locally.
George Engelbrecht9bc283e2020-04-02 12:36:09 -0600515 retry_fetches: Retry remote fetches n times upon receiving transient error
516 with exponential backoff and jitter.
Simran Basib9a1b732015-08-20 12:19:28 -0700517 old_revision: saved git commit id for open GITC projects.
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800518 """
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -0400519 self.client = self.manifest = manifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700520 self.name = name
521 self.remote = remote
Anthony Newnamdf14a702011-01-09 17:31:57 -0800522 self.gitdir = gitdir.replace('\\', '/')
David James8d201162013-10-11 17:03:19 -0700523 self.objdir = objdir.replace('\\', '/')
Shawn O. Pearce0ce6ca92011-01-10 13:26:01 -0800524 if worktree:
Renaud Paquayfef9f212016-11-01 18:28:01 -0700525 self.worktree = os.path.normpath(worktree).replace('\\', '/')
Shawn O. Pearce0ce6ca92011-01-10 13:26:01 -0800526 else:
527 self.worktree = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700528 self.relpath = relpath
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700529 self.revisionExpr = revisionExpr
530
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700531 if revisionId is None \
532 and revisionExpr \
533 and IsId(revisionExpr):
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700534 self.revisionId = revisionExpr
535 else:
536 self.revisionId = revisionId
537
Mike Pontillod3153822012-02-28 11:53:24 -0800538 self.rebase = rebase
Colin Cross5acde752012-03-28 20:15:45 -0700539 self.groups = groups
Anatol Pomazau79770d22012-04-20 14:41:59 -0700540 self.sync_c = sync_c
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800541 self.sync_s = sync_s
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +0900542 self.sync_tags = sync_tags
David Pursehouseede7f122012-11-27 22:25:30 +0900543 self.clone_depth = clone_depth
Brian Harring14a66742012-09-28 20:21:57 -0700544 self.upstream = upstream
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800545 self.parent = parent
Mike Frysinger979d5bd2020-02-09 02:28:34 -0500546 # NB: Do not use this setting in __init__ to change behavior so that the
547 # manifest.git checkout can inspect & change it after instantiating. See
548 # the XmlManifest init code for more info.
549 self.use_git_worktrees = use_git_worktrees
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800550 self.is_derived = is_derived
David Pursehouseb1553542014-09-04 21:28:09 +0900551 self.optimized_fetch = optimized_fetch
George Engelbrecht9bc283e2020-04-02 12:36:09 -0600552 self.retry_fetches = max(0, retry_fetches)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800553 self.subprojects = []
Mike Pontillod3153822012-02-28 11:53:24 -0800554
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700555 self.snapshots = {}
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700556 self.copyfiles = []
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500557 self.linkfiles = []
James W. Mills24c13082012-04-12 15:04:13 -0500558 self.annotations = []
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700559 self.config = GitConfig.ForRepository(gitdir=self.gitdir,
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -0400560 defaults=self.client.globalConfig)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700561
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800562 if self.worktree:
David James8d201162013-10-11 17:03:19 -0700563 self.work_git = self._GitGetByExec(self, bare=False, gitdir=gitdir)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800564 else:
565 self.work_git = None
David James8d201162013-10-11 17:03:19 -0700566 self.bare_git = self._GitGetByExec(self, bare=True, gitdir=gitdir)
Shawn O. Pearced237b692009-04-17 18:49:50 -0700567 self.bare_ref = GitRefs(gitdir)
David James8d201162013-10-11 17:03:19 -0700568 self.bare_objdir = self._GitGetByExec(self, bare=True, gitdir=objdir)
Bryan Jacobsf609f912013-05-06 13:36:24 -0400569 self.dest_branch = dest_branch
Simran Basib9a1b732015-08-20 12:19:28 -0700570 self.old_revision = old_revision
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700571
Doug Anderson37282b42011-03-04 11:54:18 -0800572 # This will be filled in if a project is later identified to be the
573 # project containing repo hooks.
574 self.enabled_repo_hooks = []
575
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700576 @property
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800577 def Derived(self):
578 return self.is_derived
579
580 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700581 def Exists(self):
Renaud Paquaybed8b622018-09-27 10:46:58 -0700582 return platform_utils.isdir(self.gitdir) and platform_utils.isdir(self.objdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700583
584 @property
585 def CurrentBranch(self):
586 """Obtain the name of the currently checked out branch.
Mike Frysingerc8290ad2019-10-01 01:07:11 -0400587
588 The branch name omits the 'refs/heads/' prefix.
589 None is returned if the project is on a detached HEAD, or if the work_git is
590 otheriwse inaccessible (e.g. an incomplete sync).
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700591 """
Mike Frysingerc8290ad2019-10-01 01:07:11 -0400592 try:
593 b = self.work_git.GetHead()
594 except NoManifestException:
595 # If the local checkout is in a bad state, don't barf. Let the callers
596 # process this like the head is unreadable.
597 return None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700598 if b.startswith(R_HEADS):
599 return b[len(R_HEADS):]
600 return None
601
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700602 def IsRebaseInProgress(self):
Mike Frysinger4b0eb5a2020-02-23 23:22:34 -0500603 return (os.path.exists(self.work_git.GetDotgitPath('rebase-apply')) or
604 os.path.exists(self.work_git.GetDotgitPath('rebase-merge')) or
605 os.path.exists(os.path.join(self.worktree, '.dotest')))
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200606
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700607 def IsDirty(self, consider_untracked=True):
608 """Is the working directory modified in some way?
609 """
610 self.work_git.update_index('-q',
611 '--unmerged',
612 '--ignore-missing',
613 '--refresh')
David Pursehouse8f62fb72012-11-14 12:09:38 +0900614 if self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700615 return True
616 if self.work_git.DiffZ('diff-files'):
617 return True
618 if consider_untracked and self.work_git.LsOthers():
619 return True
620 return False
621
622 _userident_name = None
623 _userident_email = None
624
625 @property
626 def UserName(self):
627 """Obtain the user's personal name.
628 """
629 if self._userident_name is None:
630 self._LoadUserIdentity()
631 return self._userident_name
632
633 @property
634 def UserEmail(self):
635 """Obtain the user's email address. This is very likely
636 to be their Gerrit login.
637 """
638 if self._userident_email is None:
639 self._LoadUserIdentity()
640 return self._userident_email
641
642 def _LoadUserIdentity(self):
David Pursehousec1b86a22012-11-14 11:36:51 +0900643 u = self.bare_git.var('GIT_COMMITTER_IDENT')
644 m = re.compile("^(.*) <([^>]*)> ").match(u)
645 if m:
646 self._userident_name = m.group(1)
647 self._userident_email = m.group(2)
648 else:
649 self._userident_name = ''
650 self._userident_email = ''
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700651
652 def GetRemote(self, name):
653 """Get the configuration for a single remote.
654 """
655 return self.config.GetRemote(name)
656
657 def GetBranch(self, name):
658 """Get the configuration for a single branch.
659 """
660 return self.config.GetBranch(name)
661
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700662 def GetBranches(self):
663 """Get all existing local branches.
664 """
665 current = self.CurrentBranch
David Pursehouse8a68ff92012-09-24 12:15:13 +0900666 all_refs = self._allrefs
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700667 heads = {}
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700668
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530669 for name, ref_id in all_refs.items():
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700670 if name.startswith(R_HEADS):
671 name = name[len(R_HEADS):]
672 b = self.GetBranch(name)
673 b.current = name == current
674 b.published = None
David Pursehouse8a68ff92012-09-24 12:15:13 +0900675 b.revision = ref_id
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700676 heads[name] = b
677
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530678 for name, ref_id in all_refs.items():
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700679 if name.startswith(R_PUB):
680 name = name[len(R_PUB):]
681 b = heads.get(name)
682 if b:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900683 b.published = ref_id
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700684
685 return heads
686
Colin Cross5acde752012-03-28 20:15:45 -0700687 def MatchesGroups(self, manifest_groups):
688 """Returns true if the manifest groups specified at init should cause
689 this project to be synced.
690 Prefixing a manifest group with "-" inverts the meaning of a group.
Conley Owensbb1b5f52012-08-13 13:11:18 -0700691 All projects are implicitly labelled with "all".
Conley Owens971de8e2012-04-16 10:36:08 -0700692
693 labels are resolved in order. In the example case of
Conley Owensbb1b5f52012-08-13 13:11:18 -0700694 project_groups: "all,group1,group2"
Conley Owens971de8e2012-04-16 10:36:08 -0700695 manifest_groups: "-group1,group2"
696 the project will be matched.
David Holmer0a1c6a12012-11-14 19:19:00 -0500697
698 The special manifest group "default" will match any project that
699 does not have the special project group "notdefault"
Colin Cross5acde752012-03-28 20:15:45 -0700700 """
David Holmer0a1c6a12012-11-14 19:19:00 -0500701 expanded_manifest_groups = manifest_groups or ['default']
Conley Owensbb1b5f52012-08-13 13:11:18 -0700702 expanded_project_groups = ['all'] + (self.groups or [])
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700703 if 'notdefault' not in expanded_project_groups:
David Holmer0a1c6a12012-11-14 19:19:00 -0500704 expanded_project_groups += ['default']
Conley Owensbb1b5f52012-08-13 13:11:18 -0700705
Conley Owens971de8e2012-04-16 10:36:08 -0700706 matched = False
Conley Owensbb1b5f52012-08-13 13:11:18 -0700707 for group in expanded_manifest_groups:
708 if group.startswith('-') and group[1:] in expanded_project_groups:
Conley Owens971de8e2012-04-16 10:36:08 -0700709 matched = False
Conley Owensbb1b5f52012-08-13 13:11:18 -0700710 elif group in expanded_project_groups:
Conley Owens971de8e2012-04-16 10:36:08 -0700711 matched = True
Colin Cross5acde752012-03-28 20:15:45 -0700712
Conley Owens971de8e2012-04-16 10:36:08 -0700713 return matched
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700714
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700715# Status Display ##
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700716 def UncommitedFiles(self, get_all=True):
717 """Returns a list of strings, uncommitted files in the git tree.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700718
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700719 Args:
720 get_all: a boolean, if True - get information about all different
721 uncommitted files. If False - return as soon as any kind of
722 uncommitted files is detected.
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500723 """
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700724 details = []
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500725 self.work_git.update_index('-q',
726 '--unmerged',
727 '--ignore-missing',
728 '--refresh')
729 if self.IsRebaseInProgress():
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700730 details.append("rebase in progress")
731 if not get_all:
732 return details
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500733
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700734 changes = self.work_git.DiffZ('diff-index', '--cached', HEAD).keys()
735 if changes:
736 details.extend(changes)
737 if not get_all:
738 return details
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500739
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700740 changes = self.work_git.DiffZ('diff-files').keys()
741 if changes:
742 details.extend(changes)
743 if not get_all:
744 return details
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500745
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700746 changes = self.work_git.LsOthers()
747 if changes:
748 details.extend(changes)
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500749
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700750 return details
751
752 def HasChanges(self):
753 """Returns true if there are uncommitted changes.
754 """
755 if self.UncommitedFiles(get_all=False):
756 return True
757 else:
758 return False
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500759
Andrew Wheeler4d5bb682012-02-27 13:52:22 -0600760 def PrintWorkTreeStatus(self, output_redir=None, quiet=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700761 """Prints the status of the repository to stdout.
Terence Haddock4655e812011-03-31 12:33:34 +0200762
763 Args:
Rostislav Krasny0bcc2d22020-01-25 14:49:14 +0200764 output_redir: If specified, redirect the output to this object.
Andrew Wheeler4d5bb682012-02-27 13:52:22 -0600765 quiet: If True then only print the project name. Do not print
766 the modified files, branch name, etc.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700767 """
Renaud Paquaybed8b622018-09-27 10:46:58 -0700768 if not platform_utils.isdir(self.worktree):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700769 if output_redir is None:
Terence Haddock4655e812011-03-31 12:33:34 +0200770 output_redir = sys.stdout
Sarah Owenscecd1d82012-11-01 22:59:27 -0700771 print(file=output_redir)
772 print('project %s/' % self.relpath, file=output_redir)
773 print(' missing (run "repo sync")', file=output_redir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700774 return
775
776 self.work_git.update_index('-q',
777 '--unmerged',
778 '--ignore-missing',
779 '--refresh')
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700780 rb = self.IsRebaseInProgress()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700781 di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD)
782 df = self.work_git.DiffZ('diff-files')
783 do = self.work_git.LsOthers()
Ali Utku Selen76abcc12012-01-25 10:51:12 +0100784 if not rb and not di and not df and not do and not self.CurrentBranch:
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700785 return 'CLEAN'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700786
787 out = StatusColoring(self.config)
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700788 if output_redir is not None:
Terence Haddock4655e812011-03-31 12:33:34 +0200789 out.redirect(output_redir)
Jakub Vrana0402cd82014-09-09 15:39:15 -0700790 out.project('project %-40s', self.relpath + '/ ')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700791
Andrew Wheeler4d5bb682012-02-27 13:52:22 -0600792 if quiet:
793 out.nl()
794 return 'DIRTY'
795
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700796 branch = self.CurrentBranch
797 if branch is None:
798 out.nobranch('(*** NO BRANCH ***)')
799 else:
800 out.branch('branch %s', branch)
801 out.nl()
802
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700803 if rb:
804 out.important('prior sync failed; rebase still in progress')
805 out.nl()
806
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700807 paths = list()
808 paths.extend(di.keys())
809 paths.extend(df.keys())
810 paths.extend(do)
811
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530812 for p in sorted(set(paths)):
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900813 try:
814 i = di[p]
815 except KeyError:
816 i = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700817
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900818 try:
819 f = df[p]
820 except KeyError:
821 f = None
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200822
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900823 if i:
824 i_status = i.status.upper()
825 else:
826 i_status = '-'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700827
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900828 if f:
829 f_status = f.status.lower()
830 else:
831 f_status = '-'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700832
833 if i and i.src_path:
Shawn O. Pearcefe086752009-03-03 13:49:48 -0800834 line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700835 i.src_path, p, i.level)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700836 else:
837 line = ' %s%s\t%s' % (i_status, f_status, p)
838
839 if i and not f:
840 out.added('%s', line)
841 elif (i and f) or (not i and f):
842 out.changed('%s', line)
843 elif not i and not f:
844 out.untracked('%s', line)
845 else:
846 out.write('%s', line)
847 out.nl()
Terence Haddock4655e812011-03-31 12:33:34 +0200848
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700849 return 'DIRTY'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700850
Mike Frysinger69b4a9c2021-02-16 18:18:01 -0500851 def PrintWorkTreeDiff(self, absolute_paths=False, output_redir=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700852 """Prints the status of the repository to stdout.
853 """
854 out = DiffColoring(self.config)
Mike Frysinger69b4a9c2021-02-16 18:18:01 -0500855 if output_redir:
856 out.redirect(output_redir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700857 cmd = ['diff']
858 if out.is_on:
859 cmd.append('--color')
860 cmd.append(HEAD)
pelyad67872d2012-03-28 14:49:58 +0300861 if absolute_paths:
862 cmd.append('--src-prefix=a/%s/' % self.relpath)
863 cmd.append('--dst-prefix=b/%s/' % self.relpath)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700864 cmd.append('--')
Mike Frysinger0a9265e2019-09-30 23:59:27 -0400865 try:
866 p = GitCommand(self,
867 cmd,
868 capture_stdout=True,
869 capture_stderr=True)
Mike Frysinger84230002021-02-16 17:08:35 -0500870 p.Wait()
Mike Frysinger0a9265e2019-09-30 23:59:27 -0400871 except GitError as e:
872 out.nl()
873 out.project('project %s/' % self.relpath)
874 out.nl()
875 out.fail('%s', str(e))
876 out.nl()
877 return False
Mike Frysinger84230002021-02-16 17:08:35 -0500878 if p.stdout:
879 out.nl()
880 out.project('project %s/' % self.relpath)
881 out.nl()
Mike Frysinger9888acc2021-03-09 11:31:14 -0500882 out.write('%s', p.stdout)
Mike Frysinger0a9265e2019-09-30 23:59:27 -0400883 return p.Wait() == 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700884
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -0700885# Publish / Upload ##
David Pursehouse8a68ff92012-09-24 12:15:13 +0900886 def WasPublished(self, branch, all_refs=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700887 """Was the branch published (uploaded) for code review?
888 If so, returns the SHA-1 hash of the last published
889 state for the branch.
890 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700891 key = R_PUB + branch
David Pursehouse8a68ff92012-09-24 12:15:13 +0900892 if all_refs is None:
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700893 try:
894 return self.bare_git.rev_parse(key)
895 except GitError:
896 return None
897 else:
898 try:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900899 return all_refs[key]
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700900 except KeyError:
901 return None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700902
David Pursehouse8a68ff92012-09-24 12:15:13 +0900903 def CleanPublishedCache(self, all_refs=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700904 """Prunes any stale published refs.
905 """
David Pursehouse8a68ff92012-09-24 12:15:13 +0900906 if all_refs is None:
907 all_refs = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700908 heads = set()
909 canrm = {}
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530910 for name, ref_id in all_refs.items():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700911 if name.startswith(R_HEADS):
912 heads.add(name)
913 elif name.startswith(R_PUB):
David Pursehouse8a68ff92012-09-24 12:15:13 +0900914 canrm[name] = ref_id
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700915
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530916 for name, ref_id in canrm.items():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700917 n = name[len(R_PUB):]
918 if R_HEADS + n not in heads:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900919 self.bare_git.DeleteRef(name, ref_id)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700920
Mandeep Singh Bainesd6c93a22011-05-26 10:34:11 -0700921 def GetUploadableBranches(self, selected_branch=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700922 """List any branches which can be uploaded for review.
923 """
924 heads = {}
925 pubed = {}
926
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530927 for name, ref_id in self._allrefs.items():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700928 if name.startswith(R_HEADS):
David Pursehouse8a68ff92012-09-24 12:15:13 +0900929 heads[name[len(R_HEADS):]] = ref_id
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700930 elif name.startswith(R_PUB):
David Pursehouse8a68ff92012-09-24 12:15:13 +0900931 pubed[name[len(R_PUB):]] = ref_id
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700932
933 ready = []
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530934 for branch, ref_id in heads.items():
David Pursehouse8a68ff92012-09-24 12:15:13 +0900935 if branch in pubed and pubed[branch] == ref_id:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700936 continue
Mandeep Singh Bainesd6c93a22011-05-26 10:34:11 -0700937 if selected_branch and branch != selected_branch:
938 continue
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700939
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800940 rb = self.GetUploadableBranch(branch)
941 if rb:
942 ready.append(rb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700943 return ready
944
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800945 def GetUploadableBranch(self, branch_name):
946 """Get a single uploadable branch, or None.
947 """
948 branch = self.GetBranch(branch_name)
949 base = branch.LocalMerge
950 if branch.LocalMerge:
951 rb = ReviewableBranch(self, branch, base)
952 if rb.commits:
953 return rb
954 return None
955
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700956 def UploadForReview(self, branch=None,
Anthony King7bdac712014-07-16 12:56:40 +0100957 people=([], []),
Mike Frysinger819cc812020-02-19 02:27:22 -0500958 dryrun=False,
Brian Harring435370c2012-07-28 15:37:04 -0700959 auto_topic=False,
Mike Frysinger84685ba2020-02-19 02:22:22 -0500960 hashtags=(),
Mike Frysingerfc1b18a2020-02-24 15:38:07 -0500961 labels=(),
Changcheng Xiao87984c62017-08-02 16:55:03 +0200962 private=False,
Vadim Bendeburybd8f6582018-10-31 13:48:01 -0700963 notify=None,
Changcheng Xiao87984c62017-08-02 16:55:03 +0200964 wip=False,
Łukasz Gardońbed59ce2017-08-08 10:18:11 +0200965 dest_branch=None,
Masaya Suzuki305a2d02017-11-13 10:48:34 -0800966 validate_certs=True,
967 push_options=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700968 """Uploads the named branch for code review.
969 """
970 if branch is None:
971 branch = self.CurrentBranch
972 if branch is None:
973 raise GitError('not currently on a branch')
974
975 branch = self.GetBranch(branch)
976 if not branch.LocalMerge:
977 raise GitError('branch %s does not track a remote' % branch.name)
978 if not branch.remote.review:
979 raise GitError('remote %s has no review url' % branch.remote.name)
980
Bryan Jacobsf609f912013-05-06 13:36:24 -0400981 if dest_branch is None:
982 dest_branch = self.dest_branch
983 if dest_branch is None:
984 dest_branch = branch.merge
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700985 if not dest_branch.startswith(R_HEADS):
986 dest_branch = R_HEADS + dest_branch
987
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800988 if not branch.remote.projectname:
989 branch.remote.projectname = self.name
990 branch.remote.Save()
991
Łukasz Gardońbed59ce2017-08-08 10:18:11 +0200992 url = branch.remote.ReviewUrl(self.UserEmail, validate_certs)
Shawn O. Pearcec9571422012-01-11 14:58:54 -0800993 if url is None:
994 raise UploadError('review not configured')
995 cmd = ['push']
Mike Frysinger819cc812020-02-19 02:27:22 -0500996 if dryrun:
997 cmd.append('-n')
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800998
Shawn O. Pearcec9571422012-01-11 14:58:54 -0800999 if url.startswith('ssh://'):
Jonathan Nieder713c5872018-11-05 13:21:52 -08001000 cmd.append('--receive-pack=gerrit receive-pack')
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -07001001
Masaya Suzuki305a2d02017-11-13 10:48:34 -08001002 for push_option in (push_options or []):
1003 cmd.append('-o')
1004 cmd.append(push_option)
1005
Shawn O. Pearcec9571422012-01-11 14:58:54 -08001006 cmd.append(url)
Shawn O. Pearceb54a3922009-01-05 16:18:58 -08001007
Shawn O. Pearcec9571422012-01-11 14:58:54 -08001008 if dest_branch.startswith(R_HEADS):
1009 dest_branch = dest_branch[len(R_HEADS):]
Brian Harring435370c2012-07-28 15:37:04 -07001010
Mike Frysingerb0fbc7f2020-02-25 02:58:04 -05001011 ref_spec = '%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch)
David Pursehousef25a3702018-11-14 19:01:22 -08001012 opts = []
Shawn O. Pearcec9571422012-01-11 14:58:54 -08001013 if auto_topic:
David Pursehousef25a3702018-11-14 19:01:22 -08001014 opts += ['topic=' + branch.name]
Mike Frysinger84685ba2020-02-19 02:22:22 -05001015 opts += ['t=%s' % p for p in hashtags]
Mike Frysingerfc1b18a2020-02-24 15:38:07 -05001016 opts += ['l=%s' % p for p in labels]
Changcheng Xiao87984c62017-08-02 16:55:03 +02001017
David Pursehousef25a3702018-11-14 19:01:22 -08001018 opts += ['r=%s' % p for p in people[0]]
Jonathan Nieder713c5872018-11-05 13:21:52 -08001019 opts += ['cc=%s' % p for p in people[1]]
Vadim Bendeburybd8f6582018-10-31 13:48:01 -07001020 if notify:
1021 opts += ['notify=' + notify]
Jonathan Nieder713c5872018-11-05 13:21:52 -08001022 if private:
1023 opts += ['private']
1024 if wip:
1025 opts += ['wip']
1026 if opts:
1027 ref_spec = ref_spec + '%' + ','.join(opts)
Shawn O. Pearcec9571422012-01-11 14:58:54 -08001028 cmd.append(ref_spec)
1029
Anthony King7bdac712014-07-16 12:56:40 +01001030 if GitCommand(self, cmd, bare=True).Wait() != 0:
Shawn O. Pearcec9571422012-01-11 14:58:54 -08001031 raise UploadError('Upload failed')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001032
Mike Frysingerd7f86832020-11-19 19:18:46 -05001033 if not dryrun:
1034 msg = "posted to %s for %s" % (branch.remote.review, dest_branch)
1035 self.bare_git.UpdateRef(R_PUB + branch.name,
1036 R_HEADS + branch.name,
1037 message=msg)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001038
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07001039# Sync ##
Julien Campergue335f5ef2013-10-16 11:02:35 +02001040 def _ExtractArchive(self, tarpath, path=None):
1041 """Extract the given tar on its current location
1042
1043 Args:
1044 - tarpath: The path to the actual tar file
1045
1046 """
1047 try:
1048 with tarfile.open(tarpath, 'r') as tar:
1049 tar.extractall(path=path)
1050 return True
1051 except (IOError, tarfile.TarError) as e:
David Pursehousef33929d2015-08-24 14:39:14 +09001052 _error("Cannot extract archive %s: %s", tarpath, str(e))
Julien Campergue335f5ef2013-10-16 11:02:35 +02001053 return False
1054
Shawn O. Pearcee02ac0a2012-03-14 15:36:59 -07001055 def Sync_NetworkHalf(self,
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07001056 quiet=False,
Mike Frysinger521d01b2020-02-17 01:51:49 -05001057 verbose=False,
Mike Frysinger7b586f22021-02-23 18:38:39 -05001058 output_redir=None,
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07001059 is_new=None,
Mike Frysinger73561142021-05-03 01:10:09 -04001060 current_branch_only=None,
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07001061 force_sync=False,
1062 clone_bundle=True,
Mike Frysingerd68ed632021-05-03 01:21:35 -04001063 tags=None,
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07001064 archive=False,
1065 optimized_fetch=False,
George Engelbrecht9bc283e2020-04-02 12:36:09 -06001066 retry_fetches=0,
Martin Kellye4e94d22017-03-21 16:05:12 -07001067 prune=False,
Xin Li745be2e2019-06-03 11:24:30 -07001068 submodules=False,
Mike Frysinger19e409c2021-05-05 19:44:35 -04001069 ssh_proxy=None,
Raman Tennetif32f2432021-04-12 20:57:25 -07001070 clone_filter=None,
Raman Tenneticd89ec12021-04-22 09:18:14 -07001071 partial_clone_exclude=set()):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001072 """Perform only the network IO portion of the sync process.
1073 Local working directory/branch state is not affected.
1074 """
Julien Campergue335f5ef2013-10-16 11:02:35 +02001075 if archive and not isinstance(self, MetaProject):
1076 if self.remote.url.startswith(('http://', 'https://')):
David Pursehousef33929d2015-08-24 14:39:14 +09001077 _error("%s: Cannot fetch archives from http/https remotes.", self.name)
Julien Campergue335f5ef2013-10-16 11:02:35 +02001078 return False
1079
1080 name = self.relpath.replace('\\', '/')
1081 name = name.replace('/', '_')
1082 tarpath = '%s.tar' % name
1083 topdir = self.manifest.topdir
1084
1085 try:
1086 self._FetchArchive(tarpath, cwd=topdir)
1087 except GitError as e:
David Pursehousef33929d2015-08-24 14:39:14 +09001088 _error('%s', e)
Julien Campergue335f5ef2013-10-16 11:02:35 +02001089 return False
1090
1091 # From now on, we only need absolute tarpath
1092 tarpath = os.path.join(topdir, tarpath)
1093
1094 if not self._ExtractArchive(tarpath, path=topdir):
1095 return False
1096 try:
Renaud Paquay010fed72016-11-11 14:25:29 -08001097 platform_utils.remove(tarpath)
Julien Campergue335f5ef2013-10-16 11:02:35 +02001098 except OSError as e:
David Pursehousef33929d2015-08-24 14:39:14 +09001099 _warn("Cannot remove archive %s: %s", tarpath, str(e))
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001100 self._CopyAndLinkFiles()
Julien Campergue335f5ef2013-10-16 11:02:35 +02001101 return True
Mike Frysinger76844ba2021-02-28 17:08:55 -05001102
1103 # If the shared object dir already exists, don't try to rebootstrap with a
1104 # clone bundle download. We should have the majority of objects already.
1105 if clone_bundle and os.path.exists(self.objdir):
1106 clone_bundle = False
1107
Raman Tennetif32f2432021-04-12 20:57:25 -07001108 if self.name in partial_clone_exclude:
1109 clone_bundle = True
1110 clone_filter = None
1111
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -07001112 if is_new is None:
1113 is_new = not self.Exists
Shawn O. Pearce88443382010-10-08 10:02:09 +02001114 if is_new:
David Pursehousee8ace262020-02-13 12:41:15 +09001115 self._InitGitDir(force_sync=force_sync, quiet=quiet)
Jimmie Westera0444582012-10-24 13:44:42 +02001116 else:
David Pursehousee8ace262020-02-13 12:41:15 +09001117 self._UpdateHooks(quiet=quiet)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001118 self._InitRemote()
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001119
1120 if is_new:
1121 alt = os.path.join(self.gitdir, 'objects/info/alternates')
1122 try:
Mike Frysinger3164d402019-11-11 05:40:22 -05001123 with open(alt) as fd:
Samuel Hollandbaa00092018-01-22 10:57:29 -06001124 # This works for both absolute and relative alternate directories.
1125 alt_dir = os.path.join(self.objdir, 'objects', fd.readline().rstrip())
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001126 except IOError:
1127 alt_dir = None
1128 else:
1129 alt_dir = None
1130
Mike Frysingere50b6a72020-02-19 01:45:48 -05001131 if (clone_bundle
1132 and alt_dir is None
1133 and self._ApplyCloneBundle(initial=is_new, quiet=quiet, verbose=verbose)):
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001134 is_new = False
1135
Mike Frysinger73561142021-05-03 01:10:09 -04001136 if current_branch_only is None:
Shawn O. Pearce6ba6ba02012-05-24 09:46:50 -07001137 if self.sync_c:
1138 current_branch_only = True
1139 elif not self.manifest._loaded:
1140 # Manifest cannot check defaults until it syncs.
1141 current_branch_only = False
1142 elif self.manifest.default.sync_c:
1143 current_branch_only = True
1144
Mike Frysingerd68ed632021-05-03 01:21:35 -04001145 if tags is None:
1146 tags = self.sync_tags
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +09001147
Aymen Bouaziz6c594462016-10-25 18:03:51 +02001148 if self.clone_depth:
1149 depth = self.clone_depth
1150 else:
1151 depth = self.manifest.manifestProject.config.GetString('repo.depth')
1152
Mike Frysinger521d01b2020-02-17 01:51:49 -05001153 # See if we can skip the network fetch entirely.
1154 if not (optimized_fetch and
1155 (ID_RE.match(self.revisionExpr) and
1156 self._CheckForImmutableRevision())):
1157 if not self._RemoteFetch(
Mike Frysinger7b586f22021-02-23 18:38:39 -05001158 initial=is_new,
1159 quiet=quiet, verbose=verbose, output_redir=output_redir,
1160 alt_dir=alt_dir, current_branch_only=current_branch_only,
Mike Frysingerc58ec4d2020-02-17 14:36:08 -05001161 tags=tags, prune=prune, depth=depth,
David Pursehouse3cceda52020-02-18 14:11:39 +09001162 submodules=submodules, force_sync=force_sync,
Mike Frysinger19e409c2021-05-05 19:44:35 -04001163 ssh_proxy=ssh_proxy,
George Engelbrecht9bc283e2020-04-02 12:36:09 -06001164 clone_filter=clone_filter, retry_fetches=retry_fetches):
Mike Frysinger521d01b2020-02-17 01:51:49 -05001165 return False
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001166
Nikolai Merinov09f0abb2018-10-19 15:07:05 +05001167 mp = self.manifest.manifestProject
1168 dissociate = mp.config.GetBoolean('repo.dissociate')
1169 if dissociate:
1170 alternates_file = os.path.join(self.gitdir, 'objects/info/alternates')
1171 if os.path.exists(alternates_file):
1172 cmd = ['repack', '-a', '-d']
Mike Frysinger7b586f22021-02-23 18:38:39 -05001173 p = GitCommand(self, cmd, bare=True, capture_stdout=bool(output_redir),
1174 merge_output=bool(output_redir))
1175 if p.stdout and output_redir:
Mike Frysinger3c093122021-03-03 11:17:27 -05001176 output_redir.write(p.stdout)
Mike Frysinger7b586f22021-02-23 18:38:39 -05001177 if p.Wait() != 0:
Nikolai Merinov09f0abb2018-10-19 15:07:05 +05001178 return False
1179 platform_utils.remove(alternates_file)
1180
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001181 if self.worktree:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001182 self._InitMRef()
1183 else:
1184 self._InitMirrorHead()
1185 try:
Renaud Paquay010fed72016-11-11 14:25:29 -08001186 platform_utils.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001187 except OSError:
1188 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001189 return True
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001190
1191 def PostRepoUpgrade(self):
1192 self._InitHooks()
1193
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001194 def _CopyAndLinkFiles(self):
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -04001195 if self.client.isGitcClient:
Simran Basib9a1b732015-08-20 12:19:28 -07001196 return
David Pursehouse8a68ff92012-09-24 12:15:13 +09001197 for copyfile in self.copyfiles:
1198 copyfile._Copy()
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001199 for linkfile in self.linkfiles:
1200 linkfile._Link()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001201
Julien Camperguedd654222014-01-09 16:21:37 +01001202 def GetCommitRevisionId(self):
1203 """Get revisionId of a commit.
1204
1205 Use this method instead of GetRevisionId to get the id of the commit rather
1206 than the id of the current git object (for example, a tag)
1207
1208 """
1209 if not self.revisionExpr.startswith(R_TAGS):
1210 return self.GetRevisionId(self._allrefs)
1211
1212 try:
1213 return self.bare_git.rev_list(self.revisionExpr, '-1')[0]
1214 except GitError:
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07001215 raise ManifestInvalidRevisionError('revision %s in %s not found' %
1216 (self.revisionExpr, self.name))
Julien Camperguedd654222014-01-09 16:21:37 +01001217
David Pursehouse8a68ff92012-09-24 12:15:13 +09001218 def GetRevisionId(self, all_refs=None):
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001219 if self.revisionId:
1220 return self.revisionId
1221
1222 rem = self.GetRemote(self.remote.name)
1223 rev = rem.ToLocal(self.revisionExpr)
1224
David Pursehouse8a68ff92012-09-24 12:15:13 +09001225 if all_refs is not None and rev in all_refs:
1226 return all_refs[rev]
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001227
1228 try:
1229 return self.bare_git.rev_parse('--verify', '%s^0' % rev)
1230 except GitError:
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07001231 raise ManifestInvalidRevisionError('revision %s in %s not found' %
1232 (self.revisionExpr, self.name))
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001233
Raman Tenneti6a872c92021-01-14 19:17:50 -08001234 def SetRevisionId(self, revisionId):
Xin Li0e776a52021-06-29 21:42:34 +00001235 if self.revisionExpr:
Xin Liaabf79d2021-04-29 01:50:38 -07001236 self.upstream = self.revisionExpr
1237
Raman Tenneti6a872c92021-01-14 19:17:50 -08001238 self.revisionId = revisionId
1239
Martin Kellye4e94d22017-03-21 16:05:12 -07001240 def Sync_LocalHalf(self, syncbuf, force_sync=False, submodules=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001241 """Perform only the local IO portion of the sync process.
1242 Network access is not required.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001243 """
Mike Frysingerb610b852019-11-11 05:10:03 -05001244 if not os.path.exists(self.gitdir):
1245 syncbuf.fail(self,
1246 'Cannot checkout %s due to missing network sync; Run '
1247 '`repo sync -n %s` first.' %
1248 (self.name, self.name))
1249 return
1250
Martin Kellye4e94d22017-03-21 16:05:12 -07001251 self._InitWorkTree(force_sync=force_sync, submodules=submodules)
David Pursehouse8a68ff92012-09-24 12:15:13 +09001252 all_refs = self.bare_ref.all
1253 self.CleanPublishedCache(all_refs)
1254 revid = self.GetRevisionId(all_refs)
Skyler Kaufman835cd682011-03-08 12:14:41 -08001255
Mike Frysinger0458faa2021-03-10 23:35:44 -05001256 # Special case the root of the repo client checkout. Make sure it doesn't
1257 # contain files being checked out to dirs we don't allow.
1258 if self.relpath == '.':
1259 PROTECTED_PATHS = {'.repo'}
1260 paths = set(self.work_git.ls_tree('-z', '--name-only', '--', revid).split('\0'))
1261 bad_paths = paths & PROTECTED_PATHS
1262 if bad_paths:
1263 syncbuf.fail(self,
1264 'Refusing to checkout project that writes to protected '
1265 'paths: %s' % (', '.join(bad_paths),))
1266 return
1267
David Pursehouse1d947b32012-10-25 12:23:11 +09001268 def _doff():
1269 self._FastForward(revid)
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001270 self._CopyAndLinkFiles()
David Pursehouse1d947b32012-10-25 12:23:11 +09001271
Martin Kellye4e94d22017-03-21 16:05:12 -07001272 def _dosubmodules():
1273 self._SyncSubmodules(quiet=True)
1274
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001275 head = self.work_git.GetHead()
1276 if head.startswith(R_HEADS):
1277 branch = head[len(R_HEADS):]
1278 try:
David Pursehouse8a68ff92012-09-24 12:15:13 +09001279 head = all_refs[head]
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001280 except KeyError:
1281 head = None
1282 else:
1283 branch = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001284
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001285 if branch is None or syncbuf.detach_head:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001286 # Currently on a detached HEAD. The user is assumed to
1287 # not have any local modifications worth worrying about.
1288 #
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -07001289 if self.IsRebaseInProgress():
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001290 syncbuf.fail(self, _PriorSyncFailedError())
1291 return
1292
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001293 if head == revid:
1294 # No changes; don't do anything further.
Florian Vallee7cf1b362012-06-07 17:11:42 +02001295 # Except if the head needs to be detached
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001296 #
Florian Vallee7cf1b362012-06-07 17:11:42 +02001297 if not syncbuf.detach_head:
Dan Willemsen029eaf32015-09-03 12:52:28 -07001298 # The copy/linkfile config may have changed.
1299 self._CopyAndLinkFiles()
Florian Vallee7cf1b362012-06-07 17:11:42 +02001300 return
1301 else:
1302 lost = self._revlist(not_rev(revid), HEAD)
1303 if lost:
1304 syncbuf.info(self, "discarding %d commits", len(lost))
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001305
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001306 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001307 self._Checkout(revid, quiet=True)
Martin Kellye4e94d22017-03-21 16:05:12 -07001308 if submodules:
1309 self._SyncSubmodules(quiet=True)
Sarah Owensa5be53f2012-09-09 15:37:57 -07001310 except GitError as e:
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001311 syncbuf.fail(self, e)
1312 return
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001313 self._CopyAndLinkFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001314 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001315
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001316 if head == revid:
1317 # No changes; don't do anything further.
1318 #
Dan Willemsen029eaf32015-09-03 12:52:28 -07001319 # The copy/linkfile config may have changed.
1320 self._CopyAndLinkFiles()
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001321 return
1322
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001323 branch = self.GetBranch(branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001324
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001325 if not branch.LocalMerge:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001326 # The current branch has no tracking configuration.
Anatol Pomazau2a32f6a2011-08-30 10:52:33 -07001327 # Jump off it to a detached HEAD.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001328 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001329 syncbuf.info(self,
1330 "leaving %s; does not track upstream",
1331 branch.name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001332 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001333 self._Checkout(revid, quiet=True)
Martin Kellye4e94d22017-03-21 16:05:12 -07001334 if submodules:
1335 self._SyncSubmodules(quiet=True)
Sarah Owensa5be53f2012-09-09 15:37:57 -07001336 except GitError as e:
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001337 syncbuf.fail(self, e)
1338 return
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001339 self._CopyAndLinkFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001340 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001341
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001342 upstream_gain = self._revlist(not_rev(HEAD), revid)
Mike Frysinger2ba5a1e2019-09-23 17:42:23 -04001343
1344 # See if we can perform a fast forward merge. This can happen if our
1345 # branch isn't in the exact same state as we last published.
1346 try:
1347 self.work_git.merge_base('--is-ancestor', HEAD, revid)
1348 # Skip the published logic.
1349 pub = False
1350 except GitError:
1351 pub = self.WasPublished(branch.name, all_refs)
1352
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001353 if pub:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001354 not_merged = self._revlist(not_rev(revid), pub)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001355 if not_merged:
1356 if upstream_gain:
1357 # The user has published this branch and some of those
1358 # commits are not yet merged upstream. We do not want
1359 # to rewrite the published commits so we punt.
1360 #
Daniel Sandler4c50dee2010-03-02 15:38:03 -05001361 syncbuf.fail(self,
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07001362 "branch %s is published (but not merged) and is now "
1363 "%d commits behind" % (branch.name, len(upstream_gain)))
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001364 return
Shawn O. Pearce05f66b62009-04-21 08:26:32 -07001365 elif pub == head:
1366 # All published commits are merged, and thus we are a
1367 # strict subset. We can fast-forward safely.
Shawn O. Pearcea54c5272008-10-30 11:03:00 -07001368 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001369 syncbuf.later1(self, _doff)
Martin Kellye4e94d22017-03-21 16:05:12 -07001370 if submodules:
1371 syncbuf.later1(self, _dosubmodules)
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001372 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001373
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001374 # Examine the local commits not in the remote. Find the
1375 # last one attributed to this user, if any.
1376 #
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001377 local_changes = self._revlist(not_rev(revid), HEAD, format='%H %ce')
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001378 last_mine = None
1379 cnt_mine = 0
1380 for commit in local_changes:
Mike Frysinger600f4922019-08-03 02:14:28 -04001381 commit_id, committer_email = commit.split(' ', 1)
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001382 if committer_email == self.UserEmail:
1383 last_mine = commit_id
1384 cnt_mine += 1
1385
Shawn O. Pearceda88ff42009-06-03 11:09:12 -07001386 if not upstream_gain and cnt_mine == len(local_changes):
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001387 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001388
1389 if self.IsDirty(consider_untracked=False):
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001390 syncbuf.fail(self, _DirtyError())
1391 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001392
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001393 # If the upstream switched on us, warn the user.
1394 #
1395 if branch.merge != self.revisionExpr:
1396 if branch.merge and self.revisionExpr:
1397 syncbuf.info(self,
1398 'manifest switched %s...%s',
1399 branch.merge,
1400 self.revisionExpr)
1401 elif branch.merge:
1402 syncbuf.info(self,
1403 'manifest no longer tracks %s',
1404 branch.merge)
1405
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001406 if cnt_mine < len(local_changes):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001407 # Upstream rebased. Not everything in HEAD
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001408 # was created by this user.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001409 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001410 syncbuf.info(self,
1411 "discarding %d commits removed from upstream",
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001412 len(local_changes) - cnt_mine)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001413
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001414 branch.remote = self.GetRemote(self.remote.name)
Anatol Pomazaucd7c5de2012-03-20 13:45:00 -07001415 if not ID_RE.match(self.revisionExpr):
1416 # in case of manifest sync the revisionExpr might be a SHA1
1417 branch.merge = self.revisionExpr
Conley Owens04f2f0e2014-10-01 17:22:46 -07001418 if not branch.merge.startswith('refs/'):
1419 branch.merge = R_HEADS + branch.merge
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001420 branch.Save()
1421
Mike Pontillod3153822012-02-28 11:53:24 -08001422 if cnt_mine > 0 and self.rebase:
Martin Kellye4e94d22017-03-21 16:05:12 -07001423 def _docopyandlink():
1424 self._CopyAndLinkFiles()
1425
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001426 def _dorebase():
Anthony King7bdac712014-07-16 12:56:40 +01001427 self._Rebase(upstream='%s^1' % last_mine, onto=revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001428 syncbuf.later2(self, _dorebase)
Martin Kellye4e94d22017-03-21 16:05:12 -07001429 if submodules:
1430 syncbuf.later2(self, _dosubmodules)
1431 syncbuf.later2(self, _docopyandlink)
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001432 elif local_changes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001433 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001434 self._ResetHard(revid)
Martin Kellye4e94d22017-03-21 16:05:12 -07001435 if submodules:
1436 self._SyncSubmodules(quiet=True)
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001437 self._CopyAndLinkFiles()
Sarah Owensa5be53f2012-09-09 15:37:57 -07001438 except GitError as e:
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001439 syncbuf.fail(self, e)
1440 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001441 else:
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001442 syncbuf.later1(self, _doff)
Martin Kellye4e94d22017-03-21 16:05:12 -07001443 if submodules:
1444 syncbuf.later1(self, _dosubmodules)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001445
Mike Frysingere6a202f2019-08-02 15:57:57 -04001446 def AddCopyFile(self, src, dest, topdir):
1447 """Mark |src| for copying to |dest| (relative to |topdir|).
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001448
Mike Frysingere6a202f2019-08-02 15:57:57 -04001449 No filesystem changes occur here. Actual copying happens later on.
1450
1451 Paths should have basic validation run on them before being queued.
1452 Further checking will be handled when the actual copy happens.
1453 """
1454 self.copyfiles.append(_CopyFile(self.worktree, src, topdir, dest))
1455
1456 def AddLinkFile(self, src, dest, topdir):
1457 """Mark |dest| to create a symlink (relative to |topdir|) pointing to |src|.
1458
1459 No filesystem changes occur here. Actual linking happens later on.
1460
1461 Paths should have basic validation run on them before being queued.
1462 Further checking will be handled when the actual link happens.
1463 """
1464 self.linkfiles.append(_LinkFile(self.worktree, src, topdir, dest))
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001465
James W. Mills24c13082012-04-12 15:04:13 -05001466 def AddAnnotation(self, name, value, keep):
Jack Neus6ea0cae2021-07-20 20:52:33 +00001467 self.annotations.append(Annotation(name, value, keep))
James W. Mills24c13082012-04-12 15:04:13 -05001468
Shawn O. Pearce632768b2008-10-23 11:58:52 -07001469 def DownloadPatchSet(self, change_id, patch_id):
1470 """Download a single patch set of a single change to FETCH_HEAD.
1471 """
1472 remote = self.GetRemote(self.remote.name)
1473
1474 cmd = ['fetch', remote.name]
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07001475 cmd.append('refs/changes/%2.2d/%d/%d'
Shawn O. Pearce632768b2008-10-23 11:58:52 -07001476 % (change_id % 100, change_id, patch_id))
Shawn O. Pearce632768b2008-10-23 11:58:52 -07001477 if GitCommand(self, cmd, bare=True).Wait() != 0:
1478 return None
1479 return DownloadedChange(self,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001480 self.GetRevisionId(),
Shawn O. Pearce632768b2008-10-23 11:58:52 -07001481 change_id,
1482 patch_id,
1483 self.bare_git.rev_parse('FETCH_HEAD'))
1484
Mike Frysingerc0d18662020-02-19 19:19:18 -05001485 def DeleteWorktree(self, quiet=False, force=False):
1486 """Delete the source checkout and any other housekeeping tasks.
1487
1488 This currently leaves behind the internal .repo/ cache state. This helps
1489 when switching branches or manifest changes get reverted as we don't have
1490 to redownload all the git objects. But we should do some GC at some point.
1491
1492 Args:
1493 quiet: Whether to hide normal messages.
1494 force: Always delete tree even if dirty.
1495
1496 Returns:
1497 True if the worktree was completely cleaned out.
1498 """
1499 if self.IsDirty():
1500 if force:
1501 print('warning: %s: Removing dirty project: uncommitted changes lost.' %
1502 (self.relpath,), file=sys.stderr)
1503 else:
1504 print('error: %s: Cannot remove project: uncommitted changes are '
1505 'present.\n' % (self.relpath,), file=sys.stderr)
1506 return False
1507
1508 if not quiet:
1509 print('%s: Deleting obsolete checkout.' % (self.relpath,))
1510
1511 # Unlock and delink from the main worktree. We don't use git's worktree
1512 # remove because it will recursively delete projects -- we handle that
1513 # ourselves below. https://crbug.com/git/48
1514 if self.use_git_worktrees:
1515 needle = platform_utils.realpath(self.gitdir)
1516 # Find the git worktree commondir under .repo/worktrees/.
1517 output = self.bare_git.worktree('list', '--porcelain').splitlines()[0]
1518 assert output.startswith('worktree '), output
1519 commondir = output[9:]
1520 # Walk each of the git worktrees to see where they point.
1521 configs = os.path.join(commondir, 'worktrees')
1522 for name in os.listdir(configs):
1523 gitdir = os.path.join(configs, name, 'gitdir')
1524 with open(gitdir) as fp:
1525 relpath = fp.read().strip()
1526 # Resolve the checkout path and see if it matches this project.
1527 fullpath = platform_utils.realpath(os.path.join(configs, name, relpath))
1528 if fullpath == needle:
1529 platform_utils.rmtree(os.path.join(configs, name))
1530
1531 # Delete the .git directory first, so we're less likely to have a partially
1532 # working git repository around. There shouldn't be any git projects here,
1533 # so rmtree works.
1534
1535 # Try to remove plain files first in case of git worktrees. If this fails
1536 # for any reason, we'll fall back to rmtree, and that'll display errors if
1537 # it can't remove things either.
1538 try:
1539 platform_utils.remove(self.gitdir)
1540 except OSError:
1541 pass
1542 try:
1543 platform_utils.rmtree(self.gitdir)
1544 except OSError as e:
1545 if e.errno != errno.ENOENT:
1546 print('error: %s: %s' % (self.gitdir, e), file=sys.stderr)
1547 print('error: %s: Failed to delete obsolete checkout; remove manually, '
1548 'then run `repo sync -l`.' % (self.relpath,), file=sys.stderr)
1549 return False
1550
1551 # Delete everything under the worktree, except for directories that contain
1552 # another git project.
1553 dirs_to_remove = []
1554 failed = False
1555 for root, dirs, files in platform_utils.walk(self.worktree):
1556 for f in files:
1557 path = os.path.join(root, f)
1558 try:
1559 platform_utils.remove(path)
1560 except OSError as e:
1561 if e.errno != errno.ENOENT:
1562 print('error: %s: Failed to remove: %s' % (path, e), file=sys.stderr)
1563 failed = True
1564 dirs[:] = [d for d in dirs
1565 if not os.path.lexists(os.path.join(root, d, '.git'))]
1566 dirs_to_remove += [os.path.join(root, d) for d in dirs
1567 if os.path.join(root, d) not in dirs_to_remove]
1568 for d in reversed(dirs_to_remove):
1569 if platform_utils.islink(d):
1570 try:
1571 platform_utils.remove(d)
1572 except OSError as e:
1573 if e.errno != errno.ENOENT:
1574 print('error: %s: Failed to remove: %s' % (d, e), file=sys.stderr)
1575 failed = True
1576 elif not platform_utils.listdir(d):
1577 try:
1578 platform_utils.rmdir(d)
1579 except OSError as e:
1580 if e.errno != errno.ENOENT:
1581 print('error: %s: Failed to remove: %s' % (d, e), file=sys.stderr)
1582 failed = True
1583 if failed:
1584 print('error: %s: Failed to delete obsolete checkout.' % (self.relpath,),
1585 file=sys.stderr)
1586 print(' Remove manually, then run `repo sync -l`.', file=sys.stderr)
1587 return False
1588
1589 # Try deleting parent dirs if they are empty.
1590 path = self.worktree
1591 while path != self.manifest.topdir:
1592 try:
1593 platform_utils.rmdir(path)
1594 except OSError as e:
1595 if e.errno != errno.ENOENT:
1596 break
1597 path = os.path.dirname(path)
1598
1599 return True
1600
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07001601# Branch Management ##
Theodore Dubois60fdc5c2019-07-30 12:14:25 -07001602 def StartBranch(self, name, branch_merge='', revision=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001603 """Create a new branch off the manifest's revision.
1604 """
Simran Basib9a1b732015-08-20 12:19:28 -07001605 if not branch_merge:
1606 branch_merge = self.revisionExpr
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001607 head = self.work_git.GetHead()
1608 if head == (R_HEADS + name):
1609 return True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001610
David Pursehouse8a68ff92012-09-24 12:15:13 +09001611 all_refs = self.bare_ref.all
Anthony King7bdac712014-07-16 12:56:40 +01001612 if R_HEADS + name in all_refs:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001613 return GitCommand(self,
Shawn O. Pearce89e717d2009-04-18 15:04:41 -07001614 ['checkout', name, '--'],
Anthony King7bdac712014-07-16 12:56:40 +01001615 capture_stdout=True,
1616 capture_stderr=True).Wait() == 0
Shawn O. Pearce0a389e92009-04-10 16:21:18 -07001617
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001618 branch = self.GetBranch(name)
1619 branch.remote = self.GetRemote(self.remote.name)
Simran Basib9a1b732015-08-20 12:19:28 -07001620 branch.merge = branch_merge
1621 if not branch.merge.startswith('refs/') and not ID_RE.match(branch_merge):
1622 branch.merge = R_HEADS + branch_merge
Theodore Dubois60fdc5c2019-07-30 12:14:25 -07001623
1624 if revision is None:
1625 revid = self.GetRevisionId(all_refs)
1626 else:
1627 revid = self.work_git.rev_parse(revision)
Shawn O. Pearce0a389e92009-04-10 16:21:18 -07001628
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001629 if head.startswith(R_HEADS):
1630 try:
David Pursehouse8a68ff92012-09-24 12:15:13 +09001631 head = all_refs[head]
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001632 except KeyError:
1633 head = None
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001634 if revid and head and revid == head:
Mike Frysinger746e7f62020-02-19 15:49:02 -05001635 ref = R_HEADS + name
1636 self.work_git.update_ref(ref, revid)
1637 self.work_git.symbolic_ref(HEAD, ref)
1638 branch.Save()
1639 return True
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001640
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001641 if GitCommand(self,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001642 ['checkout', '-b', branch.name, revid],
Anthony King7bdac712014-07-16 12:56:40 +01001643 capture_stdout=True,
1644 capture_stderr=True).Wait() == 0:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001645 branch.Save()
1646 return True
1647 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001648
Wink Saville02d79452009-04-10 13:01:24 -07001649 def CheckoutBranch(self, name):
1650 """Checkout a local topic branch.
Doug Anderson3ba5f952011-04-07 12:51:04 -07001651
1652 Args:
1653 name: The name of the branch to checkout.
1654
1655 Returns:
1656 True if the checkout succeeded; False if it didn't; None if the branch
1657 didn't exist.
Wink Saville02d79452009-04-10 13:01:24 -07001658 """
Shawn O. Pearce89e717d2009-04-18 15:04:41 -07001659 rev = R_HEADS + name
1660 head = self.work_git.GetHead()
1661 if head == rev:
1662 # Already on the branch
1663 #
1664 return True
Wink Saville02d79452009-04-10 13:01:24 -07001665
David Pursehouse8a68ff92012-09-24 12:15:13 +09001666 all_refs = self.bare_ref.all
Wink Saville02d79452009-04-10 13:01:24 -07001667 try:
David Pursehouse8a68ff92012-09-24 12:15:13 +09001668 revid = all_refs[rev]
Shawn O. Pearce89e717d2009-04-18 15:04:41 -07001669 except KeyError:
1670 # Branch does not exist in this project
1671 #
Doug Anderson3ba5f952011-04-07 12:51:04 -07001672 return None
Wink Saville02d79452009-04-10 13:01:24 -07001673
Shawn O. Pearce89e717d2009-04-18 15:04:41 -07001674 if head.startswith(R_HEADS):
1675 try:
David Pursehouse8a68ff92012-09-24 12:15:13 +09001676 head = all_refs[head]
Shawn O. Pearce89e717d2009-04-18 15:04:41 -07001677 except KeyError:
1678 head = None
1679
1680 if head == revid:
1681 # Same revision; just update HEAD to point to the new
1682 # target branch, but otherwise take no other action.
1683 #
Mike Frysinger9f91c432020-02-23 23:24:40 -05001684 _lwrite(self.work_git.GetDotgitPath(subpath=HEAD),
1685 'ref: %s%s\n' % (R_HEADS, name))
Shawn O. Pearce89e717d2009-04-18 15:04:41 -07001686 return True
1687
1688 return GitCommand(self,
1689 ['checkout', name, '--'],
Anthony King7bdac712014-07-16 12:56:40 +01001690 capture_stdout=True,
1691 capture_stderr=True).Wait() == 0
Wink Saville02d79452009-04-10 13:01:24 -07001692
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -08001693 def AbandonBranch(self, name):
1694 """Destroy a local topic branch.
Doug Andersondafb1d62011-04-07 11:46:59 -07001695
1696 Args:
1697 name: The name of the branch to abandon.
1698
1699 Returns:
1700 True if the abandon succeeded; False if it didn't; None if the branch
1701 didn't exist.
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -08001702 """
Shawn O. Pearce552ac892009-04-18 15:15:24 -07001703 rev = R_HEADS + name
David Pursehouse8a68ff92012-09-24 12:15:13 +09001704 all_refs = self.bare_ref.all
1705 if rev not in all_refs:
Doug Andersondafb1d62011-04-07 11:46:59 -07001706 # Doesn't exist
1707 return None
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -08001708
Shawn O. Pearce552ac892009-04-18 15:15:24 -07001709 head = self.work_git.GetHead()
1710 if head == rev:
1711 # We can't destroy the branch while we are sitting
1712 # on it. Switch to a detached HEAD.
1713 #
David Pursehouse8a68ff92012-09-24 12:15:13 +09001714 head = all_refs[head]
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -08001715
David Pursehouse8a68ff92012-09-24 12:15:13 +09001716 revid = self.GetRevisionId(all_refs)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001717 if head == revid:
Mike Frysinger9f91c432020-02-23 23:24:40 -05001718 _lwrite(self.work_git.GetDotgitPath(subpath=HEAD), '%s\n' % revid)
Shawn O. Pearce552ac892009-04-18 15:15:24 -07001719 else:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001720 self._Checkout(revid, quiet=True)
Shawn O. Pearce552ac892009-04-18 15:15:24 -07001721
1722 return GitCommand(self,
1723 ['branch', '-D', name],
Anthony King7bdac712014-07-16 12:56:40 +01001724 capture_stdout=True,
1725 capture_stderr=True).Wait() == 0
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -08001726
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001727 def PruneHeads(self):
1728 """Prune any topic branches already merged into upstream.
1729 """
1730 cb = self.CurrentBranch
1731 kill = []
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -08001732 left = self._allrefs
1733 for name in left.keys():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001734 if name.startswith(R_HEADS):
1735 name = name[len(R_HEADS):]
1736 if cb is None or name != cb:
1737 kill.append(name)
1738
Mike Frysingera3794e92021-03-11 23:24:01 -05001739 # Minor optimization: If there's nothing to prune, then don't try to read
1740 # any project state.
1741 if not kill and not cb:
1742 return []
1743
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001744 rev = self.GetRevisionId(left)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001745 if cb is not None \
1746 and not self._revlist(HEAD + '...' + rev) \
Anthony King7bdac712014-07-16 12:56:40 +01001747 and not self.IsDirty(consider_untracked=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001748 self.work_git.DetachHead(HEAD)
1749 kill.append(cb)
1750
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001751 if kill:
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001752 old = self.bare_git.GetHead()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001753
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001754 try:
1755 self.bare_git.DetachHead(rev)
1756
1757 b = ['branch', '-d']
1758 b.extend(kill)
1759 b = GitCommand(self, b, bare=True,
1760 capture_stdout=True,
1761 capture_stderr=True)
1762 b.Wait()
1763 finally:
Dan Willemsen1a799d12015-12-15 13:40:05 -08001764 if ID_RE.match(old):
1765 self.bare_git.DetachHead(old)
1766 else:
1767 self.bare_git.SetHead(old)
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -08001768 left = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001769
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -08001770 for branch in kill:
1771 if (R_HEADS + branch) not in left:
1772 self.CleanPublishedCache()
1773 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001774
1775 if cb and cb not in kill:
1776 kill.append(cb)
Shawn O. Pearce7c6c64d2009-03-02 12:38:13 -08001777 kill.sort()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001778
1779 kept = []
1780 for branch in kill:
Anthony King7bdac712014-07-16 12:56:40 +01001781 if R_HEADS + branch in left:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001782 branch = self.GetBranch(branch)
1783 base = branch.LocalMerge
1784 if not base:
1785 base = rev
1786 kept.append(ReviewableBranch(self, branch, base))
1787 return kept
1788
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07001789# Submodule Management ##
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001790 def GetRegisteredSubprojects(self):
1791 result = []
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07001792
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001793 def rec(subprojects):
1794 if not subprojects:
1795 return
1796 result.extend(subprojects)
1797 for p in subprojects:
1798 rec(p.subprojects)
1799 rec(self.subprojects)
1800 return result
1801
1802 def _GetSubmodules(self):
1803 # Unfortunately we cannot call `git submodule status --recursive` here
1804 # because the working tree might not exist yet, and it cannot be used
1805 # without a working tree in its current implementation.
1806
1807 def get_submodules(gitdir, rev):
1808 # Parse .gitmodules for submodule sub_paths and sub_urls
1809 sub_paths, sub_urls = parse_gitmodules(gitdir, rev)
1810 if not sub_paths:
1811 return []
1812 # Run `git ls-tree` to read SHAs of submodule object, which happen to be
1813 # revision of submodule repository
1814 sub_revs = git_ls_tree(gitdir, rev, sub_paths)
1815 submodules = []
1816 for sub_path, sub_url in zip(sub_paths, sub_urls):
1817 try:
1818 sub_rev = sub_revs[sub_path]
1819 except KeyError:
1820 # Ignore non-exist submodules
1821 continue
1822 submodules.append((sub_rev, sub_path, sub_url))
1823 return submodules
1824
Sebastian Schuberth41a26832019-03-11 13:53:38 +01001825 re_path = re.compile(r'^submodule\.(.+)\.path=(.*)$')
1826 re_url = re.compile(r'^submodule\.(.+)\.url=(.*)$')
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07001827
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001828 def parse_gitmodules(gitdir, rev):
1829 cmd = ['cat-file', 'blob', '%s:.gitmodules' % rev]
1830 try:
Anthony King7bdac712014-07-16 12:56:40 +01001831 p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True,
1832 bare=True, gitdir=gitdir)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001833 except GitError:
1834 return [], []
1835 if p.Wait() != 0:
1836 return [], []
1837
1838 gitmodules_lines = []
1839 fd, temp_gitmodules_path = tempfile.mkstemp()
1840 try:
Mike Frysinger163d42e2020-02-11 03:35:24 -05001841 os.write(fd, p.stdout.encode('utf-8'))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001842 os.close(fd)
1843 cmd = ['config', '--file', temp_gitmodules_path, '--list']
Anthony King7bdac712014-07-16 12:56:40 +01001844 p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True,
1845 bare=True, gitdir=gitdir)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001846 if p.Wait() != 0:
1847 return [], []
1848 gitmodules_lines = p.stdout.split('\n')
1849 except GitError:
1850 return [], []
1851 finally:
Renaud Paquay010fed72016-11-11 14:25:29 -08001852 platform_utils.remove(temp_gitmodules_path)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001853
1854 names = set()
1855 paths = {}
1856 urls = {}
1857 for line in gitmodules_lines:
1858 if not line:
1859 continue
1860 m = re_path.match(line)
1861 if m:
1862 names.add(m.group(1))
1863 paths[m.group(1)] = m.group(2)
1864 continue
1865 m = re_url.match(line)
1866 if m:
1867 names.add(m.group(1))
1868 urls[m.group(1)] = m.group(2)
1869 continue
1870 names = sorted(names)
1871 return ([paths.get(name, '') for name in names],
1872 [urls.get(name, '') for name in names])
1873
1874 def git_ls_tree(gitdir, rev, paths):
1875 cmd = ['ls-tree', rev, '--']
1876 cmd.extend(paths)
1877 try:
Anthony King7bdac712014-07-16 12:56:40 +01001878 p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True,
1879 bare=True, gitdir=gitdir)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001880 except GitError:
1881 return []
1882 if p.Wait() != 0:
1883 return []
1884 objects = {}
1885 for line in p.stdout.split('\n'):
1886 if not line.strip():
1887 continue
1888 object_rev, object_path = line.split()[2:4]
1889 objects[object_path] = object_rev
1890 return objects
1891
1892 try:
1893 rev = self.GetRevisionId()
1894 except GitError:
1895 return []
1896 return get_submodules(self.gitdir, rev)
1897
1898 def GetDerivedSubprojects(self):
1899 result = []
1900 if not self.Exists:
1901 # If git repo does not exist yet, querying its submodules will
1902 # mess up its states; so return here.
1903 return result
1904 for rev, path, url in self._GetSubmodules():
1905 name = self.manifest.GetSubprojectName(self, path)
David James8d201162013-10-11 17:03:19 -07001906 relpath, worktree, gitdir, objdir = \
1907 self.manifest.GetSubprojectPaths(self, name, path)
1908 project = self.manifest.paths.get(relpath)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001909 if project:
1910 result.extend(project.GetDerivedSubprojects())
1911 continue
David James8d201162013-10-11 17:03:19 -07001912
Shouheng Zhang02c0ee62017-12-08 09:54:58 +08001913 if url.startswith('..'):
1914 url = urllib.parse.urljoin("%s/" % self.remote.url, url)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001915 remote = RemoteSpec(self.remote.name,
Anthony King7bdac712014-07-16 12:56:40 +01001916 url=url,
Steve Raed6480452016-08-10 15:00:00 -07001917 pushUrl=self.remote.pushUrl,
Anthony King7bdac712014-07-16 12:56:40 +01001918 review=self.remote.review,
1919 revision=self.remote.revision)
1920 subproject = Project(manifest=self.manifest,
1921 name=name,
1922 remote=remote,
1923 gitdir=gitdir,
1924 objdir=objdir,
1925 worktree=worktree,
1926 relpath=relpath,
Aymen Bouaziz2598ed02016-06-24 14:34:08 +02001927 revisionExpr=rev,
Anthony King7bdac712014-07-16 12:56:40 +01001928 revisionId=rev,
1929 rebase=self.rebase,
1930 groups=self.groups,
1931 sync_c=self.sync_c,
1932 sync_s=self.sync_s,
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +09001933 sync_tags=self.sync_tags,
Anthony King7bdac712014-07-16 12:56:40 +01001934 parent=self,
1935 is_derived=True)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001936 result.append(subproject)
1937 result.extend(subproject.GetDerivedSubprojects())
1938 return result
1939
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07001940# Direct Git Commands ##
Mike Frysingerf81c72e2020-02-19 15:50:00 -05001941 def EnableRepositoryExtension(self, key, value='true', version=1):
1942 """Enable git repository extension |key| with |value|.
1943
1944 Args:
1945 key: The extension to enabled. Omit the "extensions." prefix.
1946 value: The value to use for the extension.
1947 version: The minimum git repository version needed.
1948 """
1949 # Make sure the git repo version is new enough already.
1950 found_version = self.config.GetInt('core.repositoryFormatVersion')
1951 if found_version is None:
1952 found_version = 0
1953 if found_version < version:
1954 self.config.SetString('core.repositoryFormatVersion', str(version))
1955
1956 # Enable the extension!
1957 self.config.SetString('extensions.%s' % (key,), value)
1958
Mike Frysinger50a81de2020-09-06 15:51:21 -04001959 def ResolveRemoteHead(self, name=None):
1960 """Find out what the default branch (HEAD) points to.
1961
1962 Normally this points to refs/heads/master, but projects are moving to main.
1963 Support whatever the server uses rather than hardcoding "master" ourselves.
1964 """
1965 if name is None:
1966 name = self.remote.name
1967
1968 # The output will look like (NB: tabs are separators):
1969 # ref: refs/heads/master HEAD
1970 # 5f6803b100bb3cd0f534e96e88c91373e8ed1c44 HEAD
1971 output = self.bare_git.ls_remote('-q', '--symref', '--exit-code', name, 'HEAD')
1972
1973 for line in output.splitlines():
1974 lhs, rhs = line.split('\t', 1)
1975 if rhs == 'HEAD' and lhs.startswith('ref:'):
1976 return lhs[4:].strip()
1977
1978 return None
1979
Zac Livingstone4332262017-06-16 08:56:09 -06001980 def _CheckForImmutableRevision(self):
Chris AtLee2fb64662014-01-16 21:32:33 -05001981 try:
1982 # if revision (sha or tag) is not present then following function
1983 # throws an error.
Ian Kasprzak0286e312021-02-05 10:06:18 -08001984 self.bare_git.rev_list('-1', '--missing=allow-any',
1985 '%s^0' % self.revisionExpr, '--')
Xin Li0e776a52021-06-29 21:42:34 +00001986 if self.upstream:
1987 rev = self.GetRemote(self.remote.name).ToLocal(self.upstream)
1988 self.bare_git.rev_list('-1', '--missing=allow-any',
1989 '%s^0' % rev, '--')
Xin Li8e983bb2021-07-20 20:15:30 +00001990 self.bare_git.merge_base('--is-ancestor', self.revisionExpr, rev)
Chris AtLee2fb64662014-01-16 21:32:33 -05001991 return True
1992 except GitError:
1993 # There is no such persistent revision. We have to fetch it.
1994 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001995
Julien Campergue335f5ef2013-10-16 11:02:35 +02001996 def _FetchArchive(self, tarpath, cwd=None):
1997 cmd = ['archive', '-v', '-o', tarpath]
1998 cmd.append('--remote=%s' % self.remote.url)
1999 cmd.append('--prefix=%s/' % self.relpath)
2000 cmd.append(self.revisionExpr)
2001
2002 command = GitCommand(self, cmd, cwd=cwd,
2003 capture_stdout=True,
2004 capture_stderr=True)
2005
2006 if command.Wait() != 0:
2007 raise GitError('git archive %s: %s' % (self.name, command.stderr))
2008
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07002009 def _RemoteFetch(self, name=None,
2010 current_branch_only=False,
Shawn O. Pearce16614f82010-10-29 12:05:43 -07002011 initial=False,
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07002012 quiet=False,
Mike Frysinger521d01b2020-02-17 01:51:49 -05002013 verbose=False,
Mike Frysinger7b586f22021-02-23 18:38:39 -05002014 output_redir=None,
Mitchel Humpherys597868b2012-10-29 10:18:34 -07002015 alt_dir=None,
Mike Frysingerc58ec4d2020-02-17 14:36:08 -05002016 tags=True,
Aymen Bouaziz6c594462016-10-25 18:03:51 +02002017 prune=False,
Martin Kellye4e94d22017-03-21 16:05:12 -07002018 depth=None,
Mike Frysingere57f1142019-03-18 21:27:54 -04002019 submodules=False,
Mike Frysinger19e409c2021-05-05 19:44:35 -04002020 ssh_proxy=None,
Xin Li745be2e2019-06-03 11:24:30 -07002021 force_sync=False,
George Engelbrecht9bc283e2020-04-02 12:36:09 -06002022 clone_filter=None,
2023 retry_fetches=2,
2024 retry_sleep_initial_sec=4.0,
2025 retry_exp_factor=2.0):
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07002026 is_sha1 = False
2027 tag_name = None
David Pursehouse9bc422f2014-04-15 10:28:56 +09002028 # The depth should not be used when fetching to a mirror because
2029 # it will result in a shallow repository that cannot be cloned or
2030 # fetched from.
Aymen Bouaziz6c594462016-10-25 18:03:51 +02002031 # The repo project should also never be synced with partial depth.
2032 if self.manifest.IsMirror or self.relpath == '.repo/repo':
2033 depth = None
David Pursehouse9bc422f2014-04-15 10:28:56 +09002034
Shawn Pearce69e04d82014-01-29 12:48:54 -08002035 if depth:
2036 current_branch_only = True
2037
Nasser Grainawi909d58b2014-09-19 12:13:04 -06002038 if ID_RE.match(self.revisionExpr) is not None:
2039 is_sha1 = True
2040
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07002041 if current_branch_only:
Nasser Grainawi909d58b2014-09-19 12:13:04 -06002042 if self.revisionExpr.startswith(R_TAGS):
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07002043 # this is a tag and its sha1 value should never change
2044 tag_name = self.revisionExpr[len(R_TAGS):]
2045
2046 if is_sha1 or tag_name is not None:
Zac Livingstone4332262017-06-16 08:56:09 -06002047 if self._CheckForImmutableRevision():
Mike Frysinger521d01b2020-02-17 01:51:49 -05002048 if verbose:
Tim Schumacher1f1596b2019-04-15 11:17:08 +02002049 print('Skipped fetching project %s (already have persistent ref)'
2050 % self.name)
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07002051 return True
Bertrand SIMONNET3000cda2014-11-25 16:19:29 -08002052 if is_sha1 and not depth:
2053 # When syncing a specific commit and --depth is not set:
2054 # * if upstream is explicitly specified and is not a sha1, fetch only
2055 # upstream as users expect only upstream to be fetch.
2056 # Note: The commit might not be in upstream in which case the sync
2057 # will fail.
2058 # * otherwise, fetch all branches to make sure we end up with the
2059 # specific commit.
Aymen Bouaziz037040f2016-06-28 12:27:23 +02002060 if self.upstream:
2061 current_branch_only = not ID_RE.match(self.upstream)
2062 else:
2063 current_branch_only = False
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07002064
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002065 if not name:
2066 name = self.remote.name
Shawn O. Pearcefb231612009-04-10 18:53:46 -07002067
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -07002068 remote = self.GetRemote(name)
Mike Frysinger339f2df2021-05-06 00:44:42 -04002069 if not remote.PreConnectFetch(ssh_proxy):
2070 ssh_proxy = None
Shawn O. Pearcefb231612009-04-10 18:53:46 -07002071
Shawn O. Pearce88443382010-10-08 10:02:09 +02002072 if initial:
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07002073 if alt_dir and 'objects' == os.path.basename(alt_dir):
2074 ref_dir = os.path.dirname(alt_dir)
Shawn O. Pearce88443382010-10-08 10:02:09 +02002075 packed_refs = os.path.join(self.gitdir, 'packed-refs')
Shawn O. Pearce88443382010-10-08 10:02:09 +02002076
David Pursehouse8a68ff92012-09-24 12:15:13 +09002077 all_refs = self.bare_ref.all
2078 ids = set(all_refs.values())
Shawn O. Pearce88443382010-10-08 10:02:09 +02002079 tmp = set()
2080
Chirayu Desai217ea7d2013-03-01 19:14:38 +05302081 for r, ref_id in GitRefs(ref_dir).all.items():
David Pursehouse8a68ff92012-09-24 12:15:13 +09002082 if r not in all_refs:
Shawn O. Pearce88443382010-10-08 10:02:09 +02002083 if r.startswith(R_TAGS) or remote.WritesTo(r):
David Pursehouse8a68ff92012-09-24 12:15:13 +09002084 all_refs[r] = ref_id
2085 ids.add(ref_id)
Shawn O. Pearce88443382010-10-08 10:02:09 +02002086 continue
2087
David Pursehouse8a68ff92012-09-24 12:15:13 +09002088 if ref_id in ids:
Shawn O. Pearce88443382010-10-08 10:02:09 +02002089 continue
2090
David Pursehouse8a68ff92012-09-24 12:15:13 +09002091 r = 'refs/_alt/%s' % ref_id
2092 all_refs[r] = ref_id
2093 ids.add(ref_id)
Shawn O. Pearce88443382010-10-08 10:02:09 +02002094 tmp.add(r)
2095
heping3d7bbc92017-04-12 19:51:47 +08002096 tmp_packed_lines = []
2097 old_packed_lines = []
Shawn O. Pearce88443382010-10-08 10:02:09 +02002098
Chirayu Desai217ea7d2013-03-01 19:14:38 +05302099 for r in sorted(all_refs):
David Pursehouse8a68ff92012-09-24 12:15:13 +09002100 line = '%s %s\n' % (all_refs[r], r)
heping3d7bbc92017-04-12 19:51:47 +08002101 tmp_packed_lines.append(line)
Shawn O. Pearce88443382010-10-08 10:02:09 +02002102 if r not in tmp:
heping3d7bbc92017-04-12 19:51:47 +08002103 old_packed_lines.append(line)
Shawn O. Pearce88443382010-10-08 10:02:09 +02002104
heping3d7bbc92017-04-12 19:51:47 +08002105 tmp_packed = ''.join(tmp_packed_lines)
2106 old_packed = ''.join(old_packed_lines)
Shawn O. Pearce88443382010-10-08 10:02:09 +02002107 _lwrite(packed_refs, tmp_packed)
Shawn O. Pearce88443382010-10-08 10:02:09 +02002108 else:
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07002109 alt_dir = None
Shawn O. Pearce88443382010-10-08 10:02:09 +02002110
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08002111 cmd = ['fetch']
Doug Anderson30d45292011-05-04 15:01:04 -07002112
Xin Li745be2e2019-06-03 11:24:30 -07002113 if clone_filter:
2114 git_require((2, 19, 0), fail=True, msg='partial clones')
2115 cmd.append('--filter=%s' % clone_filter)
Mike Frysingerf81c72e2020-02-19 15:50:00 -05002116 self.EnableRepositoryExtension('partialclone', self.remote.name)
Xin Li745be2e2019-06-03 11:24:30 -07002117
Conley Owensf97e8382015-01-21 11:12:46 -08002118 if depth:
Doug Anderson30d45292011-05-04 15:01:04 -07002119 cmd.append('--depth=%s' % depth)
Dan Willemseneeab6862015-08-03 13:11:53 -07002120 else:
2121 # If this repo has shallow objects, then we don't know which refs have
2122 # shallow objects or not. Tell git to unshallow all fetched refs. Don't
2123 # do this with projects that don't have shallow objects, since it is less
2124 # efficient.
2125 if os.path.exists(os.path.join(self.gitdir, 'shallow')):
2126 cmd.append('--depth=2147483647')
Doug Anderson30d45292011-05-04 15:01:04 -07002127
Mike Frysinger4847e052020-02-22 00:07:35 -05002128 if not verbose:
Shawn O. Pearce16614f82010-10-29 12:05:43 -07002129 cmd.append('--quiet')
Mike Frysinger4847e052020-02-22 00:07:35 -05002130 if not quiet and sys.stdout.isatty():
2131 cmd.append('--progress')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08002132 if not self.worktree:
2133 cmd.append('--update-head-ok')
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07002134 cmd.append(name)
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07002135
Mike Frysingere57f1142019-03-18 21:27:54 -04002136 if force_sync:
2137 cmd.append('--force')
2138
David Pursehouse74cfd272015-10-14 10:50:15 +09002139 if prune:
2140 cmd.append('--prune')
2141
Martin Kellye4e94d22017-03-21 16:05:12 -07002142 if submodules:
2143 cmd.append('--recurse-submodules=on-demand')
2144
Kuang-che Wu6856f982019-11-25 12:37:55 +08002145 spec = []
Brian Harring14a66742012-09-28 20:21:57 -07002146 if not current_branch_only:
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07002147 # Fetch whole repo
Conley Owens80b87fe2014-05-09 17:13:44 -07002148 spec.append(str((u'+refs/heads/*:') + remote.ToLocal('refs/heads/*')))
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07002149 elif tag_name is not None:
Conley Owens80b87fe2014-05-09 17:13:44 -07002150 spec.append('tag')
2151 spec.append(tag_name)
Nasser Grainawi04e52d62014-09-30 13:34:52 -06002152
Chirayu Desaif7b64e32020-02-04 17:50:57 +05302153 if self.manifest.IsMirror and not current_branch_only:
2154 branch = None
2155 else:
2156 branch = self.revisionExpr
Kuang-che Wu6856f982019-11-25 12:37:55 +08002157 if (not self.manifest.IsMirror and is_sha1 and depth
David Pursehouseabdf7502020-02-12 14:58:39 +09002158 and git_require((1, 8, 3))):
Kuang-che Wu6856f982019-11-25 12:37:55 +08002159 # Shallow checkout of a specific commit, fetch from that commit and not
2160 # the heads only as the commit might be deeper in the history.
2161 spec.append(branch)
Xin Liaabf79d2021-04-29 01:50:38 -07002162 if self.upstream:
2163 spec.append(self.upstream)
Kuang-che Wu6856f982019-11-25 12:37:55 +08002164 else:
2165 if is_sha1:
2166 branch = self.upstream
2167 if branch is not None and branch.strip():
2168 if not branch.startswith('refs/'):
2169 branch = R_HEADS + branch
2170 spec.append(str((u'+%s:' % branch) + remote.ToLocal(branch)))
2171
2172 # If mirroring repo and we cannot deduce the tag or branch to fetch, fetch
2173 # whole repo.
2174 if self.manifest.IsMirror and not spec:
2175 spec.append(str((u'+refs/heads/*:') + remote.ToLocal('refs/heads/*')))
2176
2177 # If using depth then we should not get all the tags since they may
2178 # be outside of the depth.
Mike Frysingerc58ec4d2020-02-17 14:36:08 -05002179 if not tags or depth:
Kuang-che Wu6856f982019-11-25 12:37:55 +08002180 cmd.append('--no-tags')
2181 else:
2182 cmd.append('--tags')
2183 spec.append(str((u'+refs/tags/*:') + remote.ToLocal('refs/tags/*')))
2184
Conley Owens80b87fe2014-05-09 17:13:44 -07002185 cmd.extend(spec)
2186
George Engelbrecht9bc283e2020-04-02 12:36:09 -06002187 # At least one retry minimum due to git remote prune.
2188 retry_fetches = max(retry_fetches, 2)
2189 retry_cur_sleep = retry_sleep_initial_sec
2190 ok = prune_tried = False
2191 for try_n in range(retry_fetches):
Mike Frysinger31990f02020-02-17 01:35:18 -05002192 gitcmd = GitCommand(self, cmd, bare=True, ssh_proxy=ssh_proxy,
Mike Frysinger7b586f22021-02-23 18:38:39 -05002193 merge_output=True, capture_stdout=quiet or bool(output_redir))
2194 if gitcmd.stdout and not quiet and output_redir:
2195 output_redir.write(gitcmd.stdout)
John L. Villalovos126e2982015-01-29 21:58:12 -08002196 ret = gitcmd.Wait()
Brian Harring14a66742012-09-28 20:21:57 -07002197 if ret == 0:
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07002198 ok = True
2199 break
George Engelbrecht9bc283e2020-04-02 12:36:09 -06002200
2201 # Retry later due to HTTP 429 Too Many Requests.
Mike Frysinger92304bf2021-02-23 03:58:43 -05002202 elif (gitcmd.stdout and
2203 'error:' in gitcmd.stdout and
2204 'HTTP 429' in gitcmd.stdout):
Mike Frysinger6823bc22021-04-15 02:06:28 -04002205 # Fallthru to sleep+retry logic at the bottom.
2206 pass
George Engelbrecht9bc283e2020-04-02 12:36:09 -06002207
Mike Frysinger6823bc22021-04-15 02:06:28 -04002208 # Try to prune remote branches once in case there are conflicts.
2209 # For example, if the remote had refs/heads/upstream, but deleted that and
2210 # now has refs/heads/upstream/foo.
2211 elif (gitcmd.stdout and
Mike Frysinger92304bf2021-02-23 03:58:43 -05002212 'error:' in gitcmd.stdout and
2213 'git remote prune' in gitcmd.stdout and
George Engelbrecht9bc283e2020-04-02 12:36:09 -06002214 not prune_tried):
2215 prune_tried = True
John L. Villalovos126e2982015-01-29 21:58:12 -08002216 prunecmd = GitCommand(self, ['remote', 'prune', name], bare=True,
John L. Villalovos9c76f672015-03-16 20:49:10 -07002217 ssh_proxy=ssh_proxy)
John L. Villalovose30f46b2015-02-25 14:27:02 -08002218 ret = prunecmd.Wait()
John L. Villalovose30f46b2015-02-25 14:27:02 -08002219 if ret:
John L. Villalovos126e2982015-01-29 21:58:12 -08002220 break
Mike Frysingerb16b9d22021-05-20 01:48:17 -04002221 print('retrying fetch after pruning remote branches', file=output_redir)
Mike Frysinger6823bc22021-04-15 02:06:28 -04002222 # Continue right away so we don't sleep as we shouldn't need to.
John L. Villalovos126e2982015-01-29 21:58:12 -08002223 continue
Brian Harring14a66742012-09-28 20:21:57 -07002224 elif current_branch_only and is_sha1 and ret == 128:
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07002225 # Exit code 128 means "couldn't find the ref you asked for"; if we're
2226 # in sha1 mode, we just tried sync'ing from the upstream field; it
2227 # doesn't exist, thus abort the optimization attempt and do a full sync.
Brian Harring14a66742012-09-28 20:21:57 -07002228 break
Colin Crossc4b301f2015-05-13 00:10:02 -07002229 elif ret < 0:
2230 # Git died with a signal, exit immediately
2231 break
Mike Frysinger6823bc22021-04-15 02:06:28 -04002232
2233 # Figure out how long to sleep before the next attempt, if there is one.
Mike Frysingerb16b9d22021-05-20 01:48:17 -04002234 if not verbose and gitcmd.stdout:
2235 print('\n%s:\n%s' % (self.name, gitcmd.stdout), end='', file=output_redir)
Mike Frysinger6823bc22021-04-15 02:06:28 -04002236 if try_n < retry_fetches - 1:
Mike Frysingerb16b9d22021-05-20 01:48:17 -04002237 print('%s: sleeping %s seconds before retrying' % (self.name, retry_cur_sleep),
2238 file=output_redir)
Mike Frysinger6823bc22021-04-15 02:06:28 -04002239 time.sleep(retry_cur_sleep)
2240 retry_cur_sleep = min(retry_exp_factor * retry_cur_sleep,
2241 MAXIMUM_RETRY_SLEEP_SEC)
2242 retry_cur_sleep *= (1 - random.uniform(-RETRY_JITTER_PERCENT,
2243 RETRY_JITTER_PERCENT))
Shawn O. Pearce88443382010-10-08 10:02:09 +02002244
2245 if initial:
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07002246 if alt_dir:
Shawn O. Pearce88443382010-10-08 10:02:09 +02002247 if old_packed != '':
2248 _lwrite(packed_refs, old_packed)
2249 else:
Renaud Paquay010fed72016-11-11 14:25:29 -08002250 platform_utils.remove(packed_refs)
Shawn O. Pearce88443382010-10-08 10:02:09 +02002251 self.bare_git.pack_refs('--all', '--prune')
Brian Harring14a66742012-09-28 20:21:57 -07002252
Aymen Bouaziz6c594462016-10-25 18:03:51 +02002253 if is_sha1 and current_branch_only:
Brian Harring14a66742012-09-28 20:21:57 -07002254 # We just synced the upstream given branch; verify we
2255 # got what we wanted, else trigger a second run of all
2256 # refs.
Zac Livingstone4332262017-06-16 08:56:09 -06002257 if not self._CheckForImmutableRevision():
Mike Frysinger521d01b2020-02-17 01:51:49 -05002258 # Sync the current branch only with depth set to None.
2259 # We always pass depth=None down to avoid infinite recursion.
2260 return self._RemoteFetch(
Mike Frysinger7b586f22021-02-23 18:38:39 -05002261 name=name, quiet=quiet, verbose=verbose, output_redir=output_redir,
Mike Frysinger521d01b2020-02-17 01:51:49 -05002262 current_branch_only=current_branch_only and depth,
2263 initial=False, alt_dir=alt_dir,
Mike Frysinger19e409c2021-05-05 19:44:35 -04002264 depth=None, ssh_proxy=ssh_proxy, clone_filter=clone_filter)
Brian Harring14a66742012-09-28 20:21:57 -07002265
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07002266 return ok
Shawn O. Pearce88443382010-10-08 10:02:09 +02002267
Mike Frysingere50b6a72020-02-19 01:45:48 -05002268 def _ApplyCloneBundle(self, initial=False, quiet=False, verbose=False):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07002269 if initial and \
2270 (self.manifest.manifestProject.config.GetString('repo.depth') or
2271 self.clone_depth):
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07002272 return False
2273
2274 remote = self.GetRemote(self.remote.name)
2275 bundle_url = remote.url + '/clone.bundle'
2276 bundle_url = GitConfig.ForUser().UrlInsteadOf(bundle_url)
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07002277 if GetSchemeFromUrl(bundle_url) not in ('http', 'https',
2278 'persistent-http',
2279 'persistent-https'):
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07002280 return False
2281
2282 bundle_dst = os.path.join(self.gitdir, 'clone.bundle')
2283 bundle_tmp = os.path.join(self.gitdir, 'clone.bundle.tmp')
2284
2285 exist_dst = os.path.exists(bundle_dst)
2286 exist_tmp = os.path.exists(bundle_tmp)
2287
2288 if not initial and not exist_dst and not exist_tmp:
2289 return False
2290
2291 if not exist_dst:
Mike Frysingere50b6a72020-02-19 01:45:48 -05002292 exist_dst = self._FetchBundle(bundle_url, bundle_tmp, bundle_dst, quiet,
2293 verbose)
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07002294 if not exist_dst:
2295 return False
2296
2297 cmd = ['fetch']
Mike Frysinger4847e052020-02-22 00:07:35 -05002298 if not verbose:
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07002299 cmd.append('--quiet')
Mike Frysinger4847e052020-02-22 00:07:35 -05002300 if not quiet and sys.stdout.isatty():
2301 cmd.append('--progress')
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07002302 if not self.worktree:
2303 cmd.append('--update-head-ok')
2304 cmd.append(bundle_dst)
2305 for f in remote.fetch:
2306 cmd.append(str(f))
Xin Li6e538442018-12-10 11:33:16 -08002307 cmd.append('+refs/tags/*:refs/tags/*')
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07002308
2309 ok = GitCommand(self, cmd, bare=True).Wait() == 0
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -07002310 if os.path.exists(bundle_dst):
Renaud Paquay010fed72016-11-11 14:25:29 -08002311 platform_utils.remove(bundle_dst)
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -07002312 if os.path.exists(bundle_tmp):
Renaud Paquay010fed72016-11-11 14:25:29 -08002313 platform_utils.remove(bundle_tmp)
Shawn O. Pearce88443382010-10-08 10:02:09 +02002314 return ok
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002315
Mike Frysingere50b6a72020-02-19 01:45:48 -05002316 def _FetchBundle(self, srcUrl, tmpPath, dstPath, quiet, verbose):
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -07002317 if os.path.exists(dstPath):
Renaud Paquay010fed72016-11-11 14:25:29 -08002318 platform_utils.remove(dstPath)
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -07002319
Matt Gumbel2dc810c2012-08-30 09:39:36 -07002320 cmd = ['curl', '--fail', '--output', tmpPath, '--netrc', '--location']
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -07002321 if quiet:
Mike Frysingere50b6a72020-02-19 01:45:48 -05002322 cmd += ['--silent', '--show-error']
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -07002323 if os.path.exists(tmpPath):
2324 size = os.stat(tmpPath).st_size
2325 if size >= 1024:
2326 cmd += ['--continue-at', '%d' % (size,)]
2327 else:
Renaud Paquay010fed72016-11-11 14:25:29 -08002328 platform_utils.remove(tmpPath)
Xin Li3698ab72019-06-25 16:09:43 -07002329 with GetUrlCookieFile(srcUrl, quiet) as (cookiefile, proxy):
Dave Borowitz137d0132015-01-02 11:12:54 -08002330 if cookiefile:
Mike Frysingerdc1d0e02020-02-09 16:20:06 -05002331 cmd += ['--cookie', cookiefile]
Xin Li3698ab72019-06-25 16:09:43 -07002332 if proxy:
2333 cmd += ['--proxy', proxy]
2334 elif 'http_proxy' in os.environ and 'darwin' == sys.platform:
2335 cmd += ['--proxy', os.environ['http_proxy']]
2336 if srcUrl.startswith('persistent-https'):
2337 srcUrl = 'http' + srcUrl[len('persistent-https'):]
2338 elif srcUrl.startswith('persistent-http'):
2339 srcUrl = 'http' + srcUrl[len('persistent-http'):]
Dave Borowitz137d0132015-01-02 11:12:54 -08002340 cmd += [srcUrl]
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -07002341
Dave Borowitz137d0132015-01-02 11:12:54 -08002342 if IsTrace():
2343 Trace('%s', ' '.join(cmd))
Mike Frysingere50b6a72020-02-19 01:45:48 -05002344 if verbose:
2345 print('%s: Downloading bundle: %s' % (self.name, srcUrl))
2346 stdout = None if verbose else subprocess.PIPE
2347 stderr = None if verbose else subprocess.STDOUT
Dave Borowitz137d0132015-01-02 11:12:54 -08002348 try:
Mike Frysingere50b6a72020-02-19 01:45:48 -05002349 proc = subprocess.Popen(cmd, stdout=stdout, stderr=stderr)
Dave Borowitz137d0132015-01-02 11:12:54 -08002350 except OSError:
2351 return False
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -07002352
Mike Frysingere50b6a72020-02-19 01:45:48 -05002353 (output, _) = proc.communicate()
2354 curlret = proc.returncode
Matt Gumbel2dc810c2012-08-30 09:39:36 -07002355
Dave Borowitz137d0132015-01-02 11:12:54 -08002356 if curlret == 22:
2357 # From curl man page:
2358 # 22: HTTP page not retrieved. The requested url was not found or
2359 # returned another error with the HTTP error code being 400 or above.
2360 # This return code only appears if -f, --fail is used.
Mike Frysinger4847e052020-02-22 00:07:35 -05002361 if verbose:
George Engelbrechtb6871892020-04-02 13:53:01 -06002362 print('%s: Unable to retrieve clone.bundle; ignoring.' % self.name)
2363 if output:
George Engelbrecht2fe84e12020-04-15 11:28:00 -06002364 print('Curl output:\n%s' % output)
Dave Borowitz137d0132015-01-02 11:12:54 -08002365 return False
Mike Frysingere50b6a72020-02-19 01:45:48 -05002366 elif curlret and not verbose and output:
2367 print('%s' % output, file=sys.stderr)
Matt Gumbel2dc810c2012-08-30 09:39:36 -07002368
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -07002369 if os.path.exists(tmpPath):
Kris Giesingc8d882a2014-12-23 13:02:32 -08002370 if curlret == 0 and self._IsValidBundle(tmpPath, quiet):
Renaud Paquayad1abcb2016-11-01 11:34:55 -07002371 platform_utils.rename(tmpPath, dstPath)
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -07002372 return True
2373 else:
Renaud Paquay010fed72016-11-11 14:25:29 -08002374 platform_utils.remove(tmpPath)
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -07002375 return False
2376 else:
2377 return False
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -07002378
Kris Giesingc8d882a2014-12-23 13:02:32 -08002379 def _IsValidBundle(self, path, quiet):
Dave Borowitz91f3ba52013-06-03 12:15:23 -07002380 try:
Pierre Tardy2b7daff2019-05-16 10:28:21 +02002381 with open(path, 'rb') as f:
2382 if f.read(16) == b'# v2 git bundle\n':
Dave Borowitz91f3ba52013-06-03 12:15:23 -07002383 return True
2384 else:
Kris Giesingc8d882a2014-12-23 13:02:32 -08002385 if not quiet:
2386 print("Invalid clone.bundle file; ignoring.", file=sys.stderr)
Dave Borowitz91f3ba52013-06-03 12:15:23 -07002387 return False
2388 except OSError:
2389 return False
2390
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002391 def _Checkout(self, rev, quiet=False):
2392 cmd = ['checkout']
2393 if quiet:
2394 cmd.append('-q')
2395 cmd.append(rev)
2396 cmd.append('--')
2397 if GitCommand(self, cmd).Wait() != 0:
2398 if self._allrefs:
2399 raise GitError('%s checkout %s ' % (self.name, rev))
2400
Mike Frysinger915fda12020-03-22 12:15:20 -04002401 def _CherryPick(self, rev, ffonly=False, record_origin=False):
Pierre Tardye5a21222011-03-24 16:28:18 +01002402 cmd = ['cherry-pick']
Mike Frysingerea431762020-03-22 12:14:01 -04002403 if ffonly:
2404 cmd.append('--ff')
Mike Frysinger915fda12020-03-22 12:15:20 -04002405 if record_origin:
2406 cmd.append('-x')
Pierre Tardye5a21222011-03-24 16:28:18 +01002407 cmd.append(rev)
2408 cmd.append('--')
2409 if GitCommand(self, cmd).Wait() != 0:
2410 if self._allrefs:
2411 raise GitError('%s cherry-pick %s ' % (self.name, rev))
2412
Akshay Verma0f2e45a2018-03-24 12:27:05 +05302413 def _LsRemote(self, refs):
2414 cmd = ['ls-remote', self.remote.name, refs]
Akshay Vermacf7c0832018-03-15 21:56:30 +05302415 p = GitCommand(self, cmd, capture_stdout=True)
2416 if p.Wait() == 0:
Mike Frysinger600f4922019-08-03 02:14:28 -04002417 return p.stdout
Akshay Vermacf7c0832018-03-15 21:56:30 +05302418 return None
2419
Anthony King7bdac712014-07-16 12:56:40 +01002420 def _Revert(self, rev):
Erwan Mahea94f1622011-08-19 13:56:09 +02002421 cmd = ['revert']
2422 cmd.append('--no-edit')
2423 cmd.append(rev)
2424 cmd.append('--')
2425 if GitCommand(self, cmd).Wait() != 0:
2426 if self._allrefs:
2427 raise GitError('%s revert %s ' % (self.name, rev))
2428
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002429 def _ResetHard(self, rev, quiet=True):
2430 cmd = ['reset', '--hard']
2431 if quiet:
2432 cmd.append('-q')
2433 cmd.append(rev)
2434 if GitCommand(self, cmd).Wait() != 0:
2435 raise GitError('%s reset --hard %s ' % (self.name, rev))
2436
Martin Kellye4e94d22017-03-21 16:05:12 -07002437 def _SyncSubmodules(self, quiet=True):
2438 cmd = ['submodule', 'update', '--init', '--recursive']
2439 if quiet:
2440 cmd.append('-q')
2441 if GitCommand(self, cmd).Wait() != 0:
2442 raise GitError('%s submodule update --init --recursive %s ' % self.name)
2443
Anthony King7bdac712014-07-16 12:56:40 +01002444 def _Rebase(self, upstream, onto=None):
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07002445 cmd = ['rebase']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002446 if onto is not None:
2447 cmd.extend(['--onto', onto])
2448 cmd.append(upstream)
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07002449 if GitCommand(self, cmd).Wait() != 0:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002450 raise GitError('%s rebase %s ' % (self.name, upstream))
2451
Pierre Tardy3d125942012-05-04 12:18:12 +02002452 def _FastForward(self, head, ffonly=False):
Mike Frysinger2b1345b2020-02-17 01:07:11 -05002453 cmd = ['merge', '--no-stat', head]
Pierre Tardy3d125942012-05-04 12:18:12 +02002454 if ffonly:
2455 cmd.append("--ff-only")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002456 if GitCommand(self, cmd).Wait() != 0:
2457 raise GitError('%s merge %s ' % (self.name, head))
2458
David Pursehousee8ace262020-02-13 12:41:15 +09002459 def _InitGitDir(self, mirror_git=None, force_sync=False, quiet=False):
Kevin Degi384b3c52014-10-16 16:02:58 -06002460 init_git_dir = not os.path.exists(self.gitdir)
2461 init_obj_dir = not os.path.exists(self.objdir)
Kevin Degib1a07b82015-07-27 13:33:43 -06002462 try:
2463 # Initialize the bare repository, which contains all of the objects.
2464 if init_obj_dir:
2465 os.makedirs(self.objdir)
2466 self.bare_objdir.init()
David James8d201162013-10-11 17:03:19 -07002467
Mike Frysinger21b7fbe2020-02-26 23:53:36 -05002468 if self.use_git_worktrees:
Mike Frysinger21b7fbe2020-02-26 23:53:36 -05002469 # Enable per-worktree config file support if possible. This is more a
2470 # nice-to-have feature for users rather than a hard requirement.
Adrien Bioteau65f51ad2020-07-24 14:56:20 +02002471 if git_require((2, 20, 0)):
Mike Frysinger21b7fbe2020-02-26 23:53:36 -05002472 self.EnableRepositoryExtension('worktreeConfig')
Mike Frysinger979d5bd2020-02-09 02:28:34 -05002473
Kevin Degib1a07b82015-07-27 13:33:43 -06002474 # If we have a separate directory to hold refs, initialize it as well.
2475 if self.objdir != self.gitdir:
2476 if init_git_dir:
2477 os.makedirs(self.gitdir)
2478
2479 if init_obj_dir or init_git_dir:
2480 self._ReferenceGitDir(self.objdir, self.gitdir, share_refs=False,
2481 copy_all=True)
Kevin Degiabaa7f32014-11-12 11:27:45 -07002482 try:
2483 self._CheckDirReference(self.objdir, self.gitdir, share_refs=False)
2484 except GitError as e:
Kevin Degiabaa7f32014-11-12 11:27:45 -07002485 if force_sync:
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07002486 print("Retrying clone after deleting %s" %
2487 self.gitdir, file=sys.stderr)
Kevin Degiabaa7f32014-11-12 11:27:45 -07002488 try:
Renaud Paquay227ad2e2016-11-01 14:37:13 -07002489 platform_utils.rmtree(platform_utils.realpath(self.gitdir))
2490 if self.worktree and os.path.exists(platform_utils.realpath
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07002491 (self.worktree)):
Renaud Paquay227ad2e2016-11-01 14:37:13 -07002492 platform_utils.rmtree(platform_utils.realpath(self.worktree))
David Pursehousee8ace262020-02-13 12:41:15 +09002493 return self._InitGitDir(mirror_git=mirror_git, force_sync=False,
2494 quiet=quiet)
David Pursehouse145e35b2020-02-12 15:40:47 +09002495 except Exception:
Kevin Degiabaa7f32014-11-12 11:27:45 -07002496 raise e
2497 raise e
Kevin Degib1a07b82015-07-27 13:33:43 -06002498
Kevin Degi384b3c52014-10-16 16:02:58 -06002499 if init_git_dir:
Kevin Degib1a07b82015-07-27 13:33:43 -06002500 mp = self.manifest.manifestProject
2501 ref_dir = mp.config.GetString('repo.reference') or ''
Kevin Degi384b3c52014-10-16 16:02:58 -06002502
Kevin Degib1a07b82015-07-27 13:33:43 -06002503 if ref_dir or mirror_git:
2504 if not mirror_git:
2505 mirror_git = os.path.join(ref_dir, self.name + '.git')
2506 repo_git = os.path.join(ref_dir, '.repo', 'projects',
2507 self.relpath + '.git')
Mike Frysinger979d5bd2020-02-09 02:28:34 -05002508 worktrees_git = os.path.join(ref_dir, '.repo', 'worktrees',
2509 self.name + '.git')
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -08002510
Kevin Degib1a07b82015-07-27 13:33:43 -06002511 if os.path.exists(mirror_git):
2512 ref_dir = mirror_git
Kevin Degib1a07b82015-07-27 13:33:43 -06002513 elif os.path.exists(repo_git):
2514 ref_dir = repo_git
Mike Frysinger979d5bd2020-02-09 02:28:34 -05002515 elif os.path.exists(worktrees_git):
2516 ref_dir = worktrees_git
Kevin Degib1a07b82015-07-27 13:33:43 -06002517 else:
2518 ref_dir = None
Shawn O. Pearce88443382010-10-08 10:02:09 +02002519
Kevin Degib1a07b82015-07-27 13:33:43 -06002520 if ref_dir:
Samuel Hollandbaa00092018-01-22 10:57:29 -06002521 if not os.path.isabs(ref_dir):
2522 # The alternate directory is relative to the object database.
2523 ref_dir = os.path.relpath(ref_dir,
2524 os.path.join(self.objdir, 'objects'))
Kevin Degib1a07b82015-07-27 13:33:43 -06002525 _lwrite(os.path.join(self.gitdir, 'objects/info/alternates'),
2526 os.path.join(ref_dir, 'objects') + '\n')
Shawn O. Pearce88443382010-10-08 10:02:09 +02002527
David Pursehousee8ace262020-02-13 12:41:15 +09002528 self._UpdateHooks(quiet=quiet)
Kevin Degib1a07b82015-07-27 13:33:43 -06002529
2530 m = self.manifest.manifestProject.config
2531 for key in ['user.name', 'user.email']:
2532 if m.Has(key, include_defaults=False):
2533 self.config.SetString(key, m.GetString(key))
David Pursehouse76a4a9d2016-08-16 12:11:12 +09002534 self.config.SetString('filter.lfs.smudge', 'git-lfs smudge --skip -- %f')
Francois Ferrandc5b0e232019-05-24 09:35:20 +02002535 self.config.SetString('filter.lfs.process', 'git-lfs filter-process --skip')
Mike Frysinger38867fb2021-02-09 23:14:41 -05002536 self.config.SetBoolean('core.bare', True if self.manifest.IsMirror else None)
Kevin Degib1a07b82015-07-27 13:33:43 -06002537 except Exception:
2538 if init_obj_dir and os.path.exists(self.objdir):
Renaud Paquaya65adf72016-11-03 10:37:53 -07002539 platform_utils.rmtree(self.objdir)
Kevin Degib1a07b82015-07-27 13:33:43 -06002540 if init_git_dir and os.path.exists(self.gitdir):
Renaud Paquaya65adf72016-11-03 10:37:53 -07002541 platform_utils.rmtree(self.gitdir)
Kevin Degib1a07b82015-07-27 13:33:43 -06002542 raise
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002543
David Pursehousee8ace262020-02-13 12:41:15 +09002544 def _UpdateHooks(self, quiet=False):
Jimmie Westera0444582012-10-24 13:44:42 +02002545 if os.path.exists(self.gitdir):
David Pursehousee8ace262020-02-13 12:41:15 +09002546 self._InitHooks(quiet=quiet)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002547
David Pursehousee8ace262020-02-13 12:41:15 +09002548 def _InitHooks(self, quiet=False):
Renaud Paquay227ad2e2016-11-01 14:37:13 -07002549 hooks = platform_utils.realpath(self._gitdir_path('hooks'))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08002550 if not os.path.exists(hooks):
2551 os.makedirs(hooks)
Jonathan Nieder93719792015-03-17 11:29:58 -07002552 for stock_hook in _ProjectHooks():
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07002553 name = os.path.basename(stock_hook)
2554
Victor Boivie65e0f352011-04-18 11:23:29 +02002555 if name in ('commit-msg',) and not self.remote.review \
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07002556 and self is not self.manifest.manifestProject:
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07002557 # Don't install a Gerrit Code Review hook if this
2558 # project does not appear to use it for reviews.
2559 #
Victor Boivie65e0f352011-04-18 11:23:29 +02002560 # Since the manifest project is one of those, but also
2561 # managed through gerrit, it's excluded
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07002562 continue
2563
2564 dst = os.path.join(hooks, name)
Renaud Paquay227ad2e2016-11-01 14:37:13 -07002565 if platform_utils.islink(dst):
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07002566 continue
2567 if os.path.exists(dst):
Mike Frysinger7951e142020-02-21 00:04:15 -05002568 # If the files are the same, we'll leave it alone. We create symlinks
2569 # below by default but fallback to hardlinks if the OS blocks them.
2570 # So if we're here, it's probably because we made a hardlink below.
2571 if not filecmp.cmp(stock_hook, dst, shallow=False):
David Pursehousee8ace262020-02-13 12:41:15 +09002572 if not quiet:
2573 _warn("%s: Not replacing locally modified %s hook",
2574 self.relpath, name)
Mike Frysinger7951e142020-02-21 00:04:15 -05002575 continue
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08002576 try:
Renaud Paquayd5cec5e2016-11-01 11:24:03 -07002577 platform_utils.symlink(
2578 os.path.relpath(stock_hook, os.path.dirname(dst)), dst)
Sarah Owensa5be53f2012-09-09 15:37:57 -07002579 except OSError as e:
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07002580 if e.errno == errno.EPERM:
Mike Frysinger7951e142020-02-21 00:04:15 -05002581 try:
2582 os.link(stock_hook, dst)
2583 except OSError:
2584 raise GitError(self._get_symlink_error_message())
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08002585 else:
2586 raise
2587
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002588 def _InitRemote(self):
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07002589 if self.remote.url:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002590 remote = self.GetRemote(self.remote.name)
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07002591 remote.url = self.remote.url
Steve Raed6480452016-08-10 15:00:00 -07002592 remote.pushUrl = self.remote.pushUrl
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07002593 remote.review = self.remote.review
2594 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002595
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08002596 if self.worktree:
2597 remote.ResetFetch(mirror=False)
2598 else:
2599 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002600 remote.Save()
2601
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002602 def _InitMRef(self):
2603 if self.manifest.branch:
Mike Frysinger21b7fbe2020-02-26 23:53:36 -05002604 if self.use_git_worktrees:
Mike Frysinger29626b42021-05-01 09:37:13 -04002605 # Set up the m/ space to point to the worktree-specific ref space.
2606 # We'll update the worktree-specific ref space on each checkout.
2607 ref = R_M + self.manifest.branch
2608 if not self.bare_ref.symref(ref):
2609 self.bare_git.symbolic_ref(
2610 '-m', 'redirecting to worktree scope',
2611 ref, R_WORKTREE_M + self.manifest.branch)
2612
Mike Frysinger21b7fbe2020-02-26 23:53:36 -05002613 # We can't update this ref with git worktrees until it exists.
2614 # We'll wait until the initial checkout to set it.
2615 if not os.path.exists(self.worktree):
2616 return
2617
2618 base = R_WORKTREE_M
2619 active_git = self.work_git
Remy Böhmer1469c282020-12-15 18:49:02 +01002620
2621 self._InitAnyMRef(HEAD, self.bare_git, detach=True)
Mike Frysinger21b7fbe2020-02-26 23:53:36 -05002622 else:
2623 base = R_M
2624 active_git = self.bare_git
2625
2626 self._InitAnyMRef(base + self.manifest.branch, active_git)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002627
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08002628 def _InitMirrorHead(self):
Mike Frysinger21b7fbe2020-02-26 23:53:36 -05002629 self._InitAnyMRef(HEAD, self.bare_git)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07002630
Remy Böhmer1469c282020-12-15 18:49:02 +01002631 def _InitAnyMRef(self, ref, active_git, detach=False):
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07002632 cur = self.bare_ref.symref(ref)
2633
2634 if self.revisionId:
2635 if cur != '' or self.bare_ref.get(ref) != self.revisionId:
2636 msg = 'manifest set to %s' % self.revisionId
2637 dst = self.revisionId + '^0'
Mike Frysinger21b7fbe2020-02-26 23:53:36 -05002638 active_git.UpdateRef(ref, dst, message=msg, detach=True)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07002639 else:
2640 remote = self.GetRemote(self.remote.name)
2641 dst = remote.ToLocal(self.revisionExpr)
2642 if cur != dst:
2643 msg = 'manifest set to %s' % self.revisionExpr
Remy Böhmer1469c282020-12-15 18:49:02 +01002644 if detach:
2645 active_git.UpdateRef(ref, dst, message=msg, detach=True)
2646 else:
2647 active_git.symbolic_ref('-m', msg, ref, dst)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08002648
Kevin Degi384b3c52014-10-16 16:02:58 -06002649 def _CheckDirReference(self, srcdir, destdir, share_refs):
Mike Frysinger979d5bd2020-02-09 02:28:34 -05002650 # Git worktrees don't use symlinks to share at all.
2651 if self.use_git_worktrees:
2652 return
2653
Dan Willemsenbdb866e2016-04-05 17:22:02 -07002654 symlink_files = self.shareable_files[:]
2655 symlink_dirs = self.shareable_dirs[:]
Kevin Degi384b3c52014-10-16 16:02:58 -06002656 if share_refs:
2657 symlink_files += self.working_tree_files
2658 symlink_dirs += self.working_tree_dirs
2659 to_symlink = symlink_files + symlink_dirs
2660 for name in set(to_symlink):
Mike Frysingered4f2112020-02-11 23:06:29 -05002661 # Try to self-heal a bit in simple cases.
2662 dst_path = os.path.join(destdir, name)
2663 src_path = os.path.join(srcdir, name)
2664
2665 if name in self.working_tree_dirs:
2666 # If the dir is missing under .repo/projects/, create it.
2667 if not os.path.exists(src_path):
2668 os.makedirs(src_path)
2669
2670 elif name in self.working_tree_files:
2671 # If it's a file under the checkout .git/ and the .repo/projects/ has
2672 # nothing, move the file under the .repo/projects/ tree.
2673 if not os.path.exists(src_path) and os.path.isfile(dst_path):
2674 platform_utils.rename(dst_path, src_path)
2675
2676 # If the path exists under the .repo/projects/ and there's no symlink
2677 # under the checkout .git/, recreate the symlink.
2678 if name in self.working_tree_dirs or name in self.working_tree_files:
2679 if os.path.exists(src_path) and not os.path.exists(dst_path):
2680 platform_utils.symlink(
2681 os.path.relpath(src_path, os.path.dirname(dst_path)), dst_path)
2682
2683 dst = platform_utils.realpath(dst_path)
Kevin Degi384b3c52014-10-16 16:02:58 -06002684 if os.path.lexists(dst):
Mike Frysingered4f2112020-02-11 23:06:29 -05002685 src = platform_utils.realpath(src_path)
Kevin Degi384b3c52014-10-16 16:02:58 -06002686 # Fail if the links are pointing to the wrong place
2687 if src != dst:
Marc Herbertec287902016-10-27 12:58:26 -07002688 _error('%s is different in %s vs %s', name, destdir, srcdir)
Kevin Degiabaa7f32014-11-12 11:27:45 -07002689 raise GitError('--force-sync not enabled; cannot overwrite a local '
Simon Ruggierf9b76832015-07-31 17:18:34 -04002690 'work tree. If you\'re comfortable with the '
2691 'possibility of losing the work tree\'s git metadata,'
2692 ' use `repo sync --force-sync {0}` to '
2693 'proceed.'.format(self.relpath))
Kevin Degi384b3c52014-10-16 16:02:58 -06002694
David James8d201162013-10-11 17:03:19 -07002695 def _ReferenceGitDir(self, gitdir, dotgit, share_refs, copy_all):
2696 """Update |dotgit| to reference |gitdir|, using symlinks where possible.
2697
2698 Args:
2699 gitdir: The bare git repository. Must already be initialized.
2700 dotgit: The repository you would like to initialize.
2701 share_refs: If true, |dotgit| will store its refs under |gitdir|.
2702 Only one work tree can store refs under a given |gitdir|.
2703 copy_all: If true, copy all remaining files from |gitdir| -> |dotgit|.
2704 This saves you the effort of initializing |dotgit| yourself.
2705 """
Dan Willemsenbdb866e2016-04-05 17:22:02 -07002706 symlink_files = self.shareable_files[:]
2707 symlink_dirs = self.shareable_dirs[:]
David James8d201162013-10-11 17:03:19 -07002708 if share_refs:
Kevin Degi384b3c52014-10-16 16:02:58 -06002709 symlink_files += self.working_tree_files
2710 symlink_dirs += self.working_tree_dirs
David James8d201162013-10-11 17:03:19 -07002711 to_symlink = symlink_files + symlink_dirs
2712
2713 to_copy = []
2714 if copy_all:
Renaud Paquaybed8b622018-09-27 10:46:58 -07002715 to_copy = platform_utils.listdir(gitdir)
David James8d201162013-10-11 17:03:19 -07002716
Renaud Paquay227ad2e2016-11-01 14:37:13 -07002717 dotgit = platform_utils.realpath(dotgit)
David James8d201162013-10-11 17:03:19 -07002718 for name in set(to_copy).union(to_symlink):
2719 try:
Renaud Paquay227ad2e2016-11-01 14:37:13 -07002720 src = platform_utils.realpath(os.path.join(gitdir, name))
Dan Willemsen2a3e1522015-07-30 20:43:33 -07002721 dst = os.path.join(dotgit, name)
David James8d201162013-10-11 17:03:19 -07002722
Kevin Degi384b3c52014-10-16 16:02:58 -06002723 if os.path.lexists(dst):
2724 continue
David James8d201162013-10-11 17:03:19 -07002725
2726 # If the source dir doesn't exist, create an empty dir.
2727 if name in symlink_dirs and not os.path.lexists(src):
2728 os.makedirs(src)
2729
Cheuk Leung8ac0c962015-07-06 21:33:00 +02002730 if name in to_symlink:
Renaud Paquayd5cec5e2016-11-01 11:24:03 -07002731 platform_utils.symlink(
2732 os.path.relpath(src, os.path.dirname(dst)), dst)
Renaud Paquay227ad2e2016-11-01 14:37:13 -07002733 elif copy_all and not platform_utils.islink(dst):
Renaud Paquaybed8b622018-09-27 10:46:58 -07002734 if platform_utils.isdir(src):
Cheuk Leung8ac0c962015-07-06 21:33:00 +02002735 shutil.copytree(src, dst)
2736 elif os.path.isfile(src):
2737 shutil.copy(src, dst)
2738
Conley Owens80b87fe2014-05-09 17:13:44 -07002739 # If the source file doesn't exist, ensure the destination
2740 # file doesn't either.
2741 if name in symlink_files and not os.path.lexists(src):
2742 try:
Renaud Paquay010fed72016-11-11 14:25:29 -08002743 platform_utils.remove(dst)
Conley Owens80b87fe2014-05-09 17:13:44 -07002744 except OSError:
2745 pass
2746
David James8d201162013-10-11 17:03:19 -07002747 except OSError as e:
2748 if e.errno == errno.EPERM:
Renaud Paquay788e9622017-01-27 11:41:12 -08002749 raise DownloadError(self._get_symlink_error_message())
David James8d201162013-10-11 17:03:19 -07002750 else:
2751 raise
2752
Mike Frysinger979d5bd2020-02-09 02:28:34 -05002753 def _InitGitWorktree(self):
2754 """Init the project using git worktrees."""
2755 self.bare_git.worktree('prune')
2756 self.bare_git.worktree('add', '-ff', '--checkout', '--detach', '--lock',
2757 self.worktree, self.GetRevisionId())
2758
2759 # Rewrite the internal state files to use relative paths between the
2760 # checkouts & worktrees.
2761 dotgit = os.path.join(self.worktree, '.git')
2762 with open(dotgit, 'r') as fp:
2763 # Figure out the checkout->worktree path.
2764 setting = fp.read()
2765 assert setting.startswith('gitdir:')
2766 git_worktree_path = setting.split(':', 1)[1].strip()
Mike Frysinger75264782020-02-21 18:55:07 -05002767 # Some platforms (e.g. Windows) won't let us update dotgit in situ because
2768 # of file permissions. Delete it and recreate it from scratch to avoid.
2769 platform_utils.remove(dotgit)
Remy Bohmer44bc9642020-11-20 21:19:10 +01002770 # Use relative path from checkout->worktree & maintain Unix line endings
2771 # on all OS's to match git behavior.
2772 with open(dotgit, 'w', newline='\n') as fp:
Mike Frysinger979d5bd2020-02-09 02:28:34 -05002773 print('gitdir:', os.path.relpath(git_worktree_path, self.worktree),
2774 file=fp)
Remy Bohmer44bc9642020-11-20 21:19:10 +01002775 # Use relative path from worktree->checkout & maintain Unix line endings
2776 # on all OS's to match git behavior.
2777 with open(os.path.join(git_worktree_path, 'gitdir'), 'w', newline='\n') as fp:
Mike Frysinger979d5bd2020-02-09 02:28:34 -05002778 print(os.path.relpath(dotgit, git_worktree_path), file=fp)
2779
Mike Frysinger21b7fbe2020-02-26 23:53:36 -05002780 self._InitMRef()
2781
Martin Kellye4e94d22017-03-21 16:05:12 -07002782 def _InitWorkTree(self, force_sync=False, submodules=False):
Mike Frysingerf4545122019-11-11 04:34:16 -05002783 realdotgit = os.path.join(self.worktree, '.git')
2784 tmpdotgit = realdotgit + '.tmp'
2785 init_dotgit = not os.path.exists(realdotgit)
2786 if init_dotgit:
Mike Frysinger979d5bd2020-02-09 02:28:34 -05002787 if self.use_git_worktrees:
2788 self._InitGitWorktree()
2789 self._CopyAndLinkFiles()
2790 return
2791
Mike Frysingerf4545122019-11-11 04:34:16 -05002792 dotgit = tmpdotgit
2793 platform_utils.rmtree(tmpdotgit, ignore_errors=True)
2794 os.makedirs(tmpdotgit)
2795 self._ReferenceGitDir(self.gitdir, tmpdotgit, share_refs=True,
2796 copy_all=False)
2797 else:
2798 dotgit = realdotgit
2799
Kevin Degib1a07b82015-07-27 13:33:43 -06002800 try:
Mike Frysingerf4545122019-11-11 04:34:16 -05002801 self._CheckDirReference(self.gitdir, dotgit, share_refs=True)
2802 except GitError as e:
2803 if force_sync and not init_dotgit:
2804 try:
2805 platform_utils.rmtree(dotgit)
2806 return self._InitWorkTree(force_sync=False, submodules=submodules)
David Pursehouse145e35b2020-02-12 15:40:47 +09002807 except Exception:
Mike Frysingerf4545122019-11-11 04:34:16 -05002808 raise e
2809 raise e
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002810
Mike Frysingerf4545122019-11-11 04:34:16 -05002811 if init_dotgit:
2812 _lwrite(os.path.join(tmpdotgit, HEAD), '%s\n' % self.GetRevisionId())
Kevin Degi384b3c52014-10-16 16:02:58 -06002813
Mike Frysingerf4545122019-11-11 04:34:16 -05002814 # Now that the .git dir is fully set up, move it to its final home.
2815 platform_utils.rename(tmpdotgit, realdotgit)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002816
Mike Frysingerf4545122019-11-11 04:34:16 -05002817 # Finish checking out the worktree.
2818 cmd = ['read-tree', '--reset', '-u']
2819 cmd.append('-v')
2820 cmd.append(HEAD)
2821 if GitCommand(self, cmd).Wait() != 0:
2822 raise GitError('Cannot initialize work tree for ' + self.name)
Victor Boivie0960b5b2010-11-26 13:42:13 +01002823
Mike Frysingerf4545122019-11-11 04:34:16 -05002824 if submodules:
2825 self._SyncSubmodules(quiet=True)
2826 self._CopyAndLinkFiles()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002827
Renaud Paquay788e9622017-01-27 11:41:12 -08002828 def _get_symlink_error_message(self):
2829 if platform_utils.isWindows():
2830 return ('Unable to create symbolic link. Please re-run the command as '
2831 'Administrator, or see '
2832 'https://github.com/git-for-windows/git/wiki/Symbolic-Links '
2833 'for other options.')
2834 return 'filesystem must support symlinks'
2835
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002836 def _gitdir_path(self, path):
Renaud Paquay227ad2e2016-11-01 14:37:13 -07002837 return platform_utils.realpath(os.path.join(self.gitdir, path))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002838
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07002839 def _revlist(self, *args, **kw):
2840 a = []
2841 a.extend(args)
2842 a.append('--')
2843 return self.work_git.rev_list(*a, **kw)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002844
2845 @property
2846 def _allrefs(self):
Shawn O. Pearced237b692009-04-17 18:49:50 -07002847 return self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002848
Sebastian Schuberth7ecccf62016-03-29 14:11:20 +02002849 def _getLogs(self, rev1, rev2, oneline=False, color=True, pretty_format=None):
Julien Camperguedd654222014-01-09 16:21:37 +01002850 """Get logs between two revisions of this project."""
2851 comp = '..'
2852 if rev1:
2853 revs = [rev1]
2854 if rev2:
2855 revs.extend([comp, rev2])
2856 cmd = ['log', ''.join(revs)]
2857 out = DiffColoring(self.config)
2858 if out.is_on and color:
2859 cmd.append('--color')
Sebastian Schuberth7ecccf62016-03-29 14:11:20 +02002860 if pretty_format is not None:
2861 cmd.append('--pretty=format:%s' % pretty_format)
Julien Camperguedd654222014-01-09 16:21:37 +01002862 if oneline:
2863 cmd.append('--oneline')
2864
2865 try:
2866 log = GitCommand(self, cmd, capture_stdout=True, capture_stderr=True)
2867 if log.Wait() == 0:
2868 return log.stdout
2869 except GitError:
2870 # worktree may not exist if groups changed for example. In that case,
2871 # try in gitdir instead.
2872 if not os.path.exists(self.worktree):
2873 return self.bare_git.log(*cmd[1:])
2874 else:
2875 raise
2876 return None
2877
Sebastian Schuberth7ecccf62016-03-29 14:11:20 +02002878 def getAddedAndRemovedLogs(self, toProject, oneline=False, color=True,
2879 pretty_format=None):
Julien Camperguedd654222014-01-09 16:21:37 +01002880 """Get the list of logs from this revision to given revisionId"""
2881 logs = {}
2882 selfId = self.GetRevisionId(self._allrefs)
2883 toId = toProject.GetRevisionId(toProject._allrefs)
2884
Sebastian Schuberth7ecccf62016-03-29 14:11:20 +02002885 logs['added'] = self._getLogs(selfId, toId, oneline=oneline, color=color,
2886 pretty_format=pretty_format)
2887 logs['removed'] = self._getLogs(toId, selfId, oneline=oneline, color=color,
2888 pretty_format=pretty_format)
Julien Camperguedd654222014-01-09 16:21:37 +01002889 return logs
2890
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002891 class _GitGetByExec(object):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07002892
David James8d201162013-10-11 17:03:19 -07002893 def __init__(self, project, bare, gitdir):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002894 self._project = project
2895 self._bare = bare
David James8d201162013-10-11 17:03:19 -07002896 self._gitdir = gitdir
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002897
Kimiyuki Onaka0501b292020-08-28 10:05:27 +09002898 # __getstate__ and __setstate__ are required for pickling because __getattr__ exists.
2899 def __getstate__(self):
2900 return (self._project, self._bare, self._gitdir)
2901
2902 def __setstate__(self, state):
2903 self._project, self._bare, self._gitdir = state
2904
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002905 def LsOthers(self):
2906 p = GitCommand(self._project,
2907 ['ls-files',
2908 '-z',
2909 '--others',
2910 '--exclude-standard'],
Anthony King7bdac712014-07-16 12:56:40 +01002911 bare=False,
David James8d201162013-10-11 17:03:19 -07002912 gitdir=self._gitdir,
Anthony King7bdac712014-07-16 12:56:40 +01002913 capture_stdout=True,
2914 capture_stderr=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002915 if p.Wait() == 0:
2916 out = p.stdout
2917 if out:
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07002918 # Backslash is not anomalous
David Pursehouse65b0ba52018-06-24 16:21:51 +09002919 return out[:-1].split('\0')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002920 return []
2921
2922 def DiffZ(self, name, *args):
2923 cmd = [name]
2924 cmd.append('-z')
Eli Ribble2d095da2019-05-02 18:21:42 -07002925 cmd.append('--ignore-submodules')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002926 cmd.extend(args)
2927 p = GitCommand(self._project,
2928 cmd,
David James8d201162013-10-11 17:03:19 -07002929 gitdir=self._gitdir,
Anthony King7bdac712014-07-16 12:56:40 +01002930 bare=False,
2931 capture_stdout=True,
2932 capture_stderr=True)
Mike Frysinger84230002021-02-16 17:08:35 -05002933 p.Wait()
2934 r = {}
2935 out = p.stdout
2936 if out:
2937 out = iter(out[:-1].split('\0'))
2938 while out:
2939 try:
2940 info = next(out)
2941 path = next(out)
2942 except StopIteration:
2943 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002944
Mike Frysinger84230002021-02-16 17:08:35 -05002945 class _Info(object):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07002946
Mike Frysinger84230002021-02-16 17:08:35 -05002947 def __init__(self, path, omode, nmode, oid, nid, state):
2948 self.path = path
2949 self.src_path = None
2950 self.old_mode = omode
2951 self.new_mode = nmode
2952 self.old_id = oid
2953 self.new_id = nid
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002954
Mike Frysinger84230002021-02-16 17:08:35 -05002955 if len(state) == 1:
2956 self.status = state
2957 self.level = None
2958 else:
2959 self.status = state[:1]
2960 self.level = state[1:]
2961 while self.level.startswith('0'):
2962 self.level = self.level[1:]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002963
Mike Frysinger84230002021-02-16 17:08:35 -05002964 info = info[1:].split(' ')
2965 info = _Info(path, *info)
2966 if info.status in ('R', 'C'):
2967 info.src_path = info.path
2968 info.path = next(out)
2969 r[info.path] = info
2970 return r
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002971
Mike Frysinger4b0eb5a2020-02-23 23:22:34 -05002972 def GetDotgitPath(self, subpath=None):
2973 """Return the full path to the .git dir.
2974
2975 As a convenience, append |subpath| if provided.
2976 """
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07002977 if self._bare:
Mike Frysinger4b0eb5a2020-02-23 23:22:34 -05002978 dotgit = self._gitdir
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07002979 else:
Mike Frysinger4b0eb5a2020-02-23 23:22:34 -05002980 dotgit = os.path.join(self._project.worktree, '.git')
2981 if os.path.isfile(dotgit):
2982 # Git worktrees use a "gitdir:" syntax to point to the scratch space.
2983 with open(dotgit) as fp:
2984 setting = fp.read()
2985 assert setting.startswith('gitdir:')
2986 gitdir = setting.split(':', 1)[1].strip()
2987 dotgit = os.path.normpath(os.path.join(self._project.worktree, gitdir))
2988
2989 return dotgit if subpath is None else os.path.join(dotgit, subpath)
2990
2991 def GetHead(self):
2992 """Return the ref that HEAD points to."""
2993 path = self.GetDotgitPath(subpath=HEAD)
Conley Owens75ee0572012-11-15 17:33:11 -08002994 try:
Mike Frysinger3164d402019-11-11 05:40:22 -05002995 with open(path) as fd:
2996 line = fd.readline()
Dan Sandler53e902a2014-03-09 13:20:02 -04002997 except IOError as e:
2998 raise NoManifestException(path, str(e))
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -07002999 try:
Chirayu Desai217ea7d2013-03-01 19:14:38 +05303000 line = line.decode()
3001 except AttributeError:
3002 pass
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07003003 if line.startswith('ref: '):
3004 return line[5:-1]
3005 return line[:-1]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003006
3007 def SetHead(self, ref, message=None):
3008 cmdv = []
3009 if message is not None:
3010 cmdv.extend(['-m', message])
3011 cmdv.append(HEAD)
3012 cmdv.append(ref)
3013 self.symbolic_ref(*cmdv)
3014
3015 def DetachHead(self, new, message=None):
3016 cmdv = ['--no-deref']
3017 if message is not None:
3018 cmdv.extend(['-m', message])
3019 cmdv.append(HEAD)
3020 cmdv.append(new)
3021 self.update_ref(*cmdv)
3022
3023 def UpdateRef(self, name, new, old=None,
3024 message=None,
3025 detach=False):
3026 cmdv = []
3027 if message is not None:
3028 cmdv.extend(['-m', message])
3029 if detach:
3030 cmdv.append('--no-deref')
3031 cmdv.append(name)
3032 cmdv.append(new)
3033 if old is not None:
3034 cmdv.append(old)
3035 self.update_ref(*cmdv)
3036
3037 def DeleteRef(self, name, old=None):
3038 if not old:
3039 old = self.rev_parse(name)
3040 self.update_ref('-d', name, old)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07003041 self._project.bare_ref.deleted(name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003042
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07003043 def rev_list(self, *args, **kw):
3044 if 'format' in kw:
3045 cmdv = ['log', '--pretty=format:%s' % kw['format']]
3046 else:
3047 cmdv = ['rev-list']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003048 cmdv.extend(args)
3049 p = GitCommand(self._project,
3050 cmdv,
Anthony King7bdac712014-07-16 12:56:40 +01003051 bare=self._bare,
David James8d201162013-10-11 17:03:19 -07003052 gitdir=self._gitdir,
Anthony King7bdac712014-07-16 12:56:40 +01003053 capture_stdout=True,
3054 capture_stderr=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003055 if p.Wait() != 0:
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003056 raise GitError('%s rev-list %s: %s' %
3057 (self._project.name, str(args), p.stderr))
Mike Frysinger81f5c592019-07-04 18:13:31 -04003058 return p.stdout.splitlines()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003059
3060 def __getattr__(self, name):
Doug Anderson37282b42011-03-04 11:54:18 -08003061 """Allow arbitrary git commands using pythonic syntax.
3062
3063 This allows you to do things like:
3064 git_obj.rev_parse('HEAD')
3065
3066 Since we don't have a 'rev_parse' method defined, the __getattr__ will
3067 run. We'll replace the '_' with a '-' and try to run a git command.
Dave Borowitz091f8932012-10-23 17:01:04 -07003068 Any other positional arguments will be passed to the git command, and the
3069 following keyword arguments are supported:
3070 config: An optional dict of git config options to be passed with '-c'.
Doug Anderson37282b42011-03-04 11:54:18 -08003071
3072 Args:
3073 name: The name of the git command to call. Any '_' characters will
3074 be replaced with '-'.
3075
3076 Returns:
3077 A callable object that will try to call git with the named command.
3078 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003079 name = name.replace('_', '-')
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003080
Dave Borowitz091f8932012-10-23 17:01:04 -07003081 def runner(*args, **kwargs):
3082 cmdv = []
3083 config = kwargs.pop('config', None)
3084 for k in kwargs:
3085 raise TypeError('%s() got an unexpected keyword argument %r'
3086 % (name, k))
3087 if config is not None:
Chirayu Desai217ea7d2013-03-01 19:14:38 +05303088 for k, v in config.items():
Dave Borowitz091f8932012-10-23 17:01:04 -07003089 cmdv.append('-c')
3090 cmdv.append('%s=%s' % (k, v))
3091 cmdv.append(name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003092 cmdv.extend(args)
3093 p = GitCommand(self._project,
3094 cmdv,
Anthony King7bdac712014-07-16 12:56:40 +01003095 bare=self._bare,
David James8d201162013-10-11 17:03:19 -07003096 gitdir=self._gitdir,
Anthony King7bdac712014-07-16 12:56:40 +01003097 capture_stdout=True,
3098 capture_stderr=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003099 if p.Wait() != 0:
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003100 raise GitError('%s %s: %s' %
3101 (self._project.name, name, p.stderr))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003102 r = p.stdout
3103 if r.endswith('\n') and r.index('\n') == len(r) - 1:
3104 return r[:-1]
3105 return r
3106 return runner
3107
3108
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003109class _PriorSyncFailedError(Exception):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003110
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003111 def __str__(self):
3112 return 'prior sync failed; rebase still in progress'
3113
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003114
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003115class _DirtyError(Exception):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003116
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003117 def __str__(self):
3118 return 'contains uncommitted changes'
3119
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003120
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003121class _InfoMessage(object):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003122
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003123 def __init__(self, project, text):
3124 self.project = project
3125 self.text = text
3126
3127 def Print(self, syncbuf):
3128 syncbuf.out.info('%s/: %s', self.project.relpath, self.text)
3129 syncbuf.out.nl()
3130
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003131
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003132class _Failure(object):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003133
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003134 def __init__(self, project, why):
3135 self.project = project
3136 self.why = why
3137
3138 def Print(self, syncbuf):
3139 syncbuf.out.fail('error: %s/: %s',
3140 self.project.relpath,
3141 str(self.why))
3142 syncbuf.out.nl()
3143
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003144
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003145class _Later(object):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003146
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003147 def __init__(self, project, action):
3148 self.project = project
3149 self.action = action
3150
3151 def Run(self, syncbuf):
3152 out = syncbuf.out
3153 out.project('project %s/', self.project.relpath)
3154 out.nl()
3155 try:
3156 self.action()
3157 out.nl()
3158 return True
David Pursehouse8a68ff92012-09-24 12:15:13 +09003159 except GitError:
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003160 out.nl()
3161 return False
3162
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003163
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003164class _SyncColoring(Coloring):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003165
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003166 def __init__(self, config):
Mike Frysinger5d9c4972021-02-19 13:34:09 -05003167 super().__init__(config, 'reposync')
Anthony King7bdac712014-07-16 12:56:40 +01003168 self.project = self.printer('header', attr='bold')
3169 self.info = self.printer('info')
3170 self.fail = self.printer('fail', fg='red')
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003171
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003172
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003173class SyncBuffer(object):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003174
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003175 def __init__(self, config, detach_head=False):
3176 self._messages = []
3177 self._failures = []
3178 self._later_queue1 = []
3179 self._later_queue2 = []
3180
3181 self.out = _SyncColoring(config)
3182 self.out.redirect(sys.stderr)
3183
3184 self.detach_head = detach_head
3185 self.clean = True
David Rileye0684ad2017-04-05 00:02:59 -07003186 self.recent_clean = True
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003187
3188 def info(self, project, fmt, *args):
3189 self._messages.append(_InfoMessage(project, fmt % args))
3190
3191 def fail(self, project, err=None):
3192 self._failures.append(_Failure(project, err))
David Rileye0684ad2017-04-05 00:02:59 -07003193 self._MarkUnclean()
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003194
3195 def later1(self, project, what):
3196 self._later_queue1.append(_Later(project, what))
3197
3198 def later2(self, project, what):
3199 self._later_queue2.append(_Later(project, what))
3200
3201 def Finish(self):
3202 self._PrintMessages()
3203 self._RunLater()
3204 self._PrintMessages()
3205 return self.clean
3206
David Rileye0684ad2017-04-05 00:02:59 -07003207 def Recently(self):
3208 recent_clean = self.recent_clean
3209 self.recent_clean = True
3210 return recent_clean
3211
3212 def _MarkUnclean(self):
3213 self.clean = False
3214 self.recent_clean = False
3215
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003216 def _RunLater(self):
3217 for q in ['_later_queue1', '_later_queue2']:
3218 if not self._RunQueue(q):
3219 return
3220
3221 def _RunQueue(self, queue):
3222 for m in getattr(self, queue):
3223 if not m.Run(self):
David Rileye0684ad2017-04-05 00:02:59 -07003224 self._MarkUnclean()
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003225 return False
3226 setattr(self, queue, [])
3227 return True
3228
3229 def _PrintMessages(self):
Mike Frysinger70d861f2019-08-26 15:22:36 -04003230 if self._messages or self._failures:
3231 if os.isatty(2):
3232 self.out.write(progress.CSI_ERASE_LINE)
3233 self.out.write('\r')
3234
Shawn O. Pearce350cde42009-04-16 11:21:18 -07003235 for m in self._messages:
3236 m.Print(self)
3237 for m in self._failures:
3238 m.Print(self)
3239
3240 self._messages = []
3241 self._failures = []
3242
3243
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003244class MetaProject(Project):
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003245
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003246 """A special project housed under .repo.
3247 """
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003248
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003249 def __init__(self, manifest, name, gitdir, worktree):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003250 Project.__init__(self,
Anthony King7bdac712014-07-16 12:56:40 +01003251 manifest=manifest,
3252 name=name,
3253 gitdir=gitdir,
3254 objdir=gitdir,
3255 worktree=worktree,
3256 remote=RemoteSpec('origin'),
3257 relpath='.repo/%s' % name,
3258 revisionExpr='refs/heads/master',
3259 revisionId=None,
3260 groups=None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003261
3262 def PreSync(self):
3263 if self.Exists:
3264 cb = self.CurrentBranch
3265 if cb:
3266 base = self.GetBranch(cb).merge
3267 if base:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07003268 self.revisionExpr = base
3269 self.revisionId = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003270
Martin Kelly224a31a2017-07-10 14:46:25 -07003271 def MetaBranchSwitch(self, submodules=False):
Florian Vallee5d016502012-06-07 17:19:26 +02003272 """ Prepare MetaProject for manifest branch switch
3273 """
3274
3275 # detach and delete manifest branch, allowing a new
3276 # branch to take over
Anthony King7bdac712014-07-16 12:56:40 +01003277 syncbuf = SyncBuffer(self.config, detach_head=True)
Martin Kelly224a31a2017-07-10 14:46:25 -07003278 self.Sync_LocalHalf(syncbuf, submodules=submodules)
Florian Vallee5d016502012-06-07 17:19:26 +02003279 syncbuf.Finish()
3280
3281 return GitCommand(self,
Mark E. Hamilton30b0f4e2016-02-10 10:44:30 -07003282 ['update-ref', '-d', 'refs/heads/default'],
3283 capture_stdout=True,
3284 capture_stderr=True).Wait() == 0
Florian Vallee5d016502012-06-07 17:19:26 +02003285
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003286 @property
Shawn O. Pearcef6906872009-04-18 10:49:00 -07003287 def LastFetch(self):
3288 try:
3289 fh = os.path.join(self.gitdir, 'FETCH_HEAD')
3290 return os.path.getmtime(fh)
3291 except OSError:
3292 return 0
3293
3294 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003295 def HasChanges(self):
3296 """Has the remote received new commits not yet checked out?
3297 """
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07003298 if not self.remote or not self.revisionExpr:
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07003299 return False
3300
David Pursehouse8a68ff92012-09-24 12:15:13 +09003301 all_refs = self.bare_ref.all
3302 revid = self.GetRevisionId(all_refs)
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07003303 head = self.work_git.GetHead()
3304 if head.startswith(R_HEADS):
3305 try:
David Pursehouse8a68ff92012-09-24 12:15:13 +09003306 head = all_refs[head]
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07003307 except KeyError:
3308 head = None
3309
3310 if revid == head:
3311 return False
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07003312 elif self._revlist(not_rev(HEAD), revid):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003313 return True
3314 return False