The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # 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. Pearce | 438ee1c | 2008-11-03 09:59:36 -0800 | [diff] [blame] | 15 | import errno |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 16 | import filecmp |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 17 | import glob |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | import os |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 19 | import random |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | import re |
| 21 | import shutil |
| 22 | import stat |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 23 | import subprocess |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | import sys |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 25 | import tarfile |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 26 | import tempfile |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 27 | import time |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 28 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | from color import Coloring |
Dave Borowitz | b42b474 | 2012-10-31 12:27:27 -0700 | [diff] [blame] | 30 | from git_command import GitCommand, git_require |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 31 | from git_config import GitConfig, IsId, GetSchemeFromUrl, GetUrlCookieFile, \ |
| 32 | ID_RE |
Remy Bohmer | 16c1328 | 2020-09-10 10:38:04 +0200 | [diff] [blame] | 33 | from error import GitError, UploadError, DownloadError |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 34 | from error import ManifestInvalidRevisionError, ManifestInvalidPathError |
Conley Owens | 75ee057 | 2012-11-15 17:33:11 -0800 | [diff] [blame] | 35 | from error import NoManifestException |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 36 | import platform_utils |
Mike Frysinger | 70d861f | 2019-08-26 15:22:36 -0400 | [diff] [blame] | 37 | import progress |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 38 | from repo_trace import IsTrace, Trace |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 39 | |
Mike Frysinger | 21b7fbe | 2020-02-26 23:53:36 -0500 | [diff] [blame] | 40 | from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M, R_WORKTREE_M |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 41 | |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 42 | from pyversion import is_python3 |
Mike Frysinger | 40252c2 | 2016-08-15 21:23:44 -0400 | [diff] [blame] | 43 | if is_python3(): |
| 44 | import urllib.parse |
| 45 | else: |
| 46 | import imp |
| 47 | import urlparse |
| 48 | urllib = imp.new_module('urllib') |
| 49 | urllib.parse = urlparse |
David Pursehouse | a46bf7d | 2020-02-15 12:45:53 +0900 | [diff] [blame] | 50 | input = raw_input # noqa: F821 |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 51 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 52 | |
George Engelbrecht | 9bc283e | 2020-04-02 12:36:09 -0600 | [diff] [blame] | 53 | # Maximum sleep time allowed during retries. |
| 54 | MAXIMUM_RETRY_SLEEP_SEC = 3600.0 |
| 55 | # +-10% random jitter is added to each Fetches retry sleep duration. |
| 56 | RETRY_JITTER_PERCENT = 0.1 |
| 57 | |
| 58 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 59 | def _lwrite(path, content): |
| 60 | lock = '%s.lock' % path |
| 61 | |
Remy Bohmer | 169b021 | 2020-11-21 10:57:52 +0100 | [diff] [blame] | 62 | # Maintain Unix line endings on all OS's to match git behavior. |
| 63 | with open(lock, 'w', newline='\n') as fd: |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 64 | fd.write(content) |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 65 | |
| 66 | try: |
Renaud Paquay | ad1abcb | 2016-11-01 11:34:55 -0700 | [diff] [blame] | 67 | platform_utils.rename(lock, path) |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 68 | except OSError: |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 69 | platform_utils.remove(lock) |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 70 | raise |
| 71 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 72 | |
Shawn O. Pearce | 4824478 | 2009-04-16 08:25:57 -0700 | [diff] [blame] | 73 | def _error(fmt, *args): |
| 74 | msg = fmt % args |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 75 | print('error: %s' % msg, file=sys.stderr) |
Shawn O. Pearce | 4824478 | 2009-04-16 08:25:57 -0700 | [diff] [blame] | 76 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 77 | |
David Pursehouse | f33929d | 2015-08-24 14:39:14 +0900 | [diff] [blame] | 78 | def _warn(fmt, *args): |
| 79 | msg = fmt % args |
| 80 | print('warn: %s' % msg, file=sys.stderr) |
| 81 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 82 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 83 | def not_rev(r): |
| 84 | return '^' + r |
| 85 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 86 | |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 87 | def sq(r): |
| 88 | return "'" + r.replace("'", "'\''") + "'" |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 89 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 90 | |
Jonathan Nieder | 9371979 | 2015-03-17 11:29:58 -0700 | [diff] [blame] | 91 | _project_hook_list = None |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 92 | |
| 93 | |
Jonathan Nieder | 9371979 | 2015-03-17 11:29:58 -0700 | [diff] [blame] | 94 | def _ProjectHooks(): |
| 95 | """List the hooks present in the 'hooks' directory. |
| 96 | |
| 97 | These hooks are project hooks and are copied to the '.git/hooks' directory |
| 98 | of all subprojects. |
| 99 | |
| 100 | This function caches the list of hooks (based on the contents of the |
| 101 | 'repo/hooks' directory) on the first call. |
| 102 | |
| 103 | Returns: |
| 104 | A list of absolute paths to all of the files in the hooks directory. |
| 105 | """ |
| 106 | global _project_hook_list |
| 107 | if _project_hook_list is None: |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 108 | d = platform_utils.realpath(os.path.abspath(os.path.dirname(__file__))) |
Jonathan Nieder | 9371979 | 2015-03-17 11:29:58 -0700 | [diff] [blame] | 109 | d = os.path.join(d, 'hooks') |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 110 | _project_hook_list = [os.path.join(d, x) for x in platform_utils.listdir(d)] |
Jonathan Nieder | 9371979 | 2015-03-17 11:29:58 -0700 | [diff] [blame] | 111 | return _project_hook_list |
| 112 | |
| 113 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 114 | class DownloadedChange(object): |
| 115 | _commit_cache = None |
| 116 | |
| 117 | def __init__(self, project, base, change_id, ps_id, commit): |
| 118 | self.project = project |
| 119 | self.base = base |
| 120 | self.change_id = change_id |
| 121 | self.ps_id = ps_id |
| 122 | self.commit = commit |
| 123 | |
| 124 | @property |
| 125 | def commits(self): |
| 126 | if self._commit_cache is None: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 127 | self._commit_cache = self.project.bare_git.rev_list('--abbrev=8', |
| 128 | '--abbrev-commit', |
| 129 | '--pretty=oneline', |
| 130 | '--reverse', |
| 131 | '--date-order', |
| 132 | not_rev(self.base), |
| 133 | self.commit, |
| 134 | '--') |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 135 | return self._commit_cache |
| 136 | |
| 137 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | class ReviewableBranch(object): |
| 139 | _commit_cache = None |
Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 140 | _base_exists = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 141 | |
| 142 | def __init__(self, project, branch, base): |
| 143 | self.project = project |
| 144 | self.branch = branch |
| 145 | self.base = base |
| 146 | |
| 147 | @property |
| 148 | def name(self): |
| 149 | return self.branch.name |
| 150 | |
| 151 | @property |
| 152 | def commits(self): |
| 153 | if self._commit_cache is None: |
Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 154 | args = ('--abbrev=8', '--abbrev-commit', '--pretty=oneline', '--reverse', |
| 155 | '--date-order', not_rev(self.base), R_HEADS + self.name, '--') |
| 156 | try: |
| 157 | self._commit_cache = self.project.bare_git.rev_list(*args) |
| 158 | except GitError: |
| 159 | # We weren't able to probe the commits for this branch. Was it tracking |
| 160 | # a branch that no longer exists? If so, return no commits. Otherwise, |
| 161 | # rethrow the error as we don't know what's going on. |
| 162 | if self.base_exists: |
| 163 | raise |
| 164 | |
| 165 | self._commit_cache = [] |
| 166 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 167 | return self._commit_cache |
| 168 | |
| 169 | @property |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 170 | def unabbrev_commits(self): |
| 171 | r = dict() |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 172 | for commit in self.project.bare_git.rev_list(not_rev(self.base), |
| 173 | R_HEADS + self.name, |
| 174 | '--'): |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 175 | r[commit[0:8]] = commit |
| 176 | return r |
| 177 | |
| 178 | @property |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 179 | def date(self): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 180 | return self.project.bare_git.log('--pretty=format:%cd', |
| 181 | '-n', '1', |
| 182 | R_HEADS + self.name, |
| 183 | '--') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 184 | |
Mike Frysinger | 6da1775 | 2019-09-11 18:43:17 -0400 | [diff] [blame] | 185 | @property |
| 186 | def base_exists(self): |
| 187 | """Whether the branch we're tracking exists. |
| 188 | |
| 189 | Normally it should, but sometimes branches we track can get deleted. |
| 190 | """ |
| 191 | if self._base_exists is None: |
| 192 | try: |
| 193 | self.project.bare_git.rev_parse('--verify', not_rev(self.base)) |
| 194 | # If we're still here, the base branch exists. |
| 195 | self._base_exists = True |
| 196 | except GitError: |
| 197 | # If we failed to verify, the base branch doesn't exist. |
| 198 | self._base_exists = False |
| 199 | |
| 200 | return self._base_exists |
| 201 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 202 | def UploadForReview(self, people, |
Mike Frysinger | 819cc81 | 2020-02-19 02:27:22 -0500 | [diff] [blame] | 203 | dryrun=False, |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 204 | auto_topic=False, |
Mike Frysinger | 84685ba | 2020-02-19 02:22:22 -0500 | [diff] [blame] | 205 | hashtags=(), |
Mike Frysinger | fc1b18a | 2020-02-24 15:38:07 -0500 | [diff] [blame] | 206 | labels=(), |
Changcheng Xiao | 87984c6 | 2017-08-02 16:55:03 +0200 | [diff] [blame] | 207 | private=False, |
Vadim Bendebury | bd8f658 | 2018-10-31 13:48:01 -0700 | [diff] [blame] | 208 | notify=None, |
Changcheng Xiao | 87984c6 | 2017-08-02 16:55:03 +0200 | [diff] [blame] | 209 | wip=False, |
Łukasz Gardoń | bed59ce | 2017-08-08 10:18:11 +0200 | [diff] [blame] | 210 | dest_branch=None, |
Masaya Suzuki | 305a2d0 | 2017-11-13 10:48:34 -0800 | [diff] [blame] | 211 | validate_certs=True, |
| 212 | push_options=None): |
Mike Frysinger | 819cc81 | 2020-02-19 02:27:22 -0500 | [diff] [blame] | 213 | self.project.UploadForReview(branch=self.name, |
| 214 | people=people, |
| 215 | dryrun=dryrun, |
Brian Harring | 435370c | 2012-07-28 15:37:04 -0700 | [diff] [blame] | 216 | auto_topic=auto_topic, |
Mike Frysinger | 84685ba | 2020-02-19 02:22:22 -0500 | [diff] [blame] | 217 | hashtags=hashtags, |
Mike Frysinger | fc1b18a | 2020-02-24 15:38:07 -0500 | [diff] [blame] | 218 | labels=labels, |
Changcheng Xiao | 87984c6 | 2017-08-02 16:55:03 +0200 | [diff] [blame] | 219 | private=private, |
Vadim Bendebury | bd8f658 | 2018-10-31 13:48:01 -0700 | [diff] [blame] | 220 | notify=notify, |
Changcheng Xiao | 87984c6 | 2017-08-02 16:55:03 +0200 | [diff] [blame] | 221 | wip=wip, |
Łukasz Gardoń | bed59ce | 2017-08-08 10:18:11 +0200 | [diff] [blame] | 222 | dest_branch=dest_branch, |
Masaya Suzuki | 305a2d0 | 2017-11-13 10:48:34 -0800 | [diff] [blame] | 223 | validate_certs=validate_certs, |
| 224 | push_options=push_options) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 225 | |
Ficus Kirkpatrick | bc7ef67 | 2009-05-04 12:45:11 -0700 | [diff] [blame] | 226 | def GetPublishedRefs(self): |
| 227 | refs = {} |
| 228 | output = self.project.bare_git.ls_remote( |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 229 | self.branch.remote.SshReviewUrl(self.project.UserEmail), |
| 230 | 'refs/changes/*') |
Ficus Kirkpatrick | bc7ef67 | 2009-05-04 12:45:11 -0700 | [diff] [blame] | 231 | for line in output.split('\n'): |
| 232 | try: |
| 233 | (sha, ref) = line.split() |
| 234 | refs[sha] = ref |
| 235 | except ValueError: |
| 236 | pass |
| 237 | |
| 238 | return refs |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 239 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 240 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 241 | class StatusColoring(Coloring): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 242 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 243 | def __init__(self, config): |
| 244 | Coloring.__init__(self, config, 'status') |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 245 | self.project = self.printer('header', attr='bold') |
| 246 | self.branch = self.printer('header', attr='bold') |
| 247 | self.nobranch = self.printer('nobranch', fg='red') |
| 248 | self.important = self.printer('important', fg='red') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 249 | |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 250 | self.added = self.printer('added', fg='green') |
| 251 | self.changed = self.printer('changed', fg='red') |
| 252 | self.untracked = self.printer('untracked', fg='red') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 253 | |
| 254 | |
| 255 | class DiffColoring(Coloring): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 256 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 257 | def __init__(self, config): |
| 258 | Coloring.__init__(self, config, 'diff') |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 259 | self.project = self.printer('header', attr='bold') |
Mike Frysinger | 0a9265e | 2019-09-30 23:59:27 -0400 | [diff] [blame] | 260 | self.fail = self.printer('fail', fg='red') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 261 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 262 | |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 263 | class _Annotation(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 264 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 265 | def __init__(self, name, value, keep): |
| 266 | self.name = name |
| 267 | self.value = value |
| 268 | self.keep = keep |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 269 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 270 | |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 271 | def _SafeExpandPath(base, subpath, skipfinal=False): |
| 272 | """Make sure |subpath| is completely safe under |base|. |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 273 | |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 274 | We make sure no intermediate symlinks are traversed, and that the final path |
| 275 | is not a special file (e.g. not a socket or fifo). |
| 276 | |
| 277 | NB: We rely on a number of paths already being filtered out while parsing the |
| 278 | manifest. See the validation logic in manifest_xml.py for more details. |
| 279 | """ |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 280 | # Split up the path by its components. We can't use os.path.sep exclusively |
| 281 | # as some platforms (like Windows) will convert / to \ and that bypasses all |
| 282 | # our constructed logic here. Especially since manifest authors only use |
| 283 | # / in their paths. |
| 284 | resep = re.compile(r'[/%s]' % re.escape(os.path.sep)) |
| 285 | components = resep.split(subpath) |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 286 | if skipfinal: |
| 287 | # Whether the caller handles the final component itself. |
| 288 | finalpart = components.pop() |
| 289 | |
| 290 | path = base |
| 291 | for part in components: |
| 292 | if part in {'.', '..'}: |
| 293 | raise ManifestInvalidPathError( |
| 294 | '%s: "%s" not allowed in paths' % (subpath, part)) |
| 295 | |
| 296 | path = os.path.join(path, part) |
| 297 | if platform_utils.islink(path): |
| 298 | raise ManifestInvalidPathError( |
| 299 | '%s: traversing symlinks not allow' % (path,)) |
| 300 | |
| 301 | if os.path.exists(path): |
| 302 | if not os.path.isfile(path) and not platform_utils.isdir(path): |
| 303 | raise ManifestInvalidPathError( |
| 304 | '%s: only regular files & directories allowed' % (path,)) |
| 305 | |
| 306 | if skipfinal: |
| 307 | path = os.path.join(path, finalpart) |
| 308 | |
| 309 | return path |
| 310 | |
| 311 | |
| 312 | class _CopyFile(object): |
| 313 | """Container for <copyfile> manifest element.""" |
| 314 | |
| 315 | def __init__(self, git_worktree, src, topdir, dest): |
| 316 | """Register a <copyfile> request. |
| 317 | |
| 318 | Args: |
| 319 | git_worktree: Absolute path to the git project checkout. |
| 320 | src: Relative path under |git_worktree| of file to read. |
| 321 | topdir: Absolute path to the top of the repo client checkout. |
| 322 | dest: Relative path under |topdir| of file to write. |
| 323 | """ |
| 324 | self.git_worktree = git_worktree |
| 325 | self.topdir = topdir |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 326 | self.src = src |
| 327 | self.dest = dest |
| 328 | |
| 329 | def _Copy(self): |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 330 | src = _SafeExpandPath(self.git_worktree, self.src) |
| 331 | dest = _SafeExpandPath(self.topdir, self.dest) |
| 332 | |
| 333 | if platform_utils.isdir(src): |
| 334 | raise ManifestInvalidPathError( |
| 335 | '%s: copying from directory not supported' % (self.src,)) |
| 336 | if platform_utils.isdir(dest): |
| 337 | raise ManifestInvalidPathError( |
| 338 | '%s: copying to directory not allowed' % (self.dest,)) |
| 339 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 340 | # copy file if it does not exist or is out of date |
| 341 | if not os.path.exists(dest) or not filecmp.cmp(src, dest): |
| 342 | try: |
| 343 | # remove existing file first, since it might be read-only |
| 344 | if os.path.exists(dest): |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 345 | platform_utils.remove(dest) |
Matthew Buckett | 2daf667 | 2009-07-11 09:43:47 -0400 | [diff] [blame] | 346 | else: |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 347 | dest_dir = os.path.dirname(dest) |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 348 | if not platform_utils.isdir(dest_dir): |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 349 | os.makedirs(dest_dir) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 350 | shutil.copy(src, dest) |
| 351 | # make the file read-only |
| 352 | mode = os.stat(dest)[stat.ST_MODE] |
| 353 | mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH) |
| 354 | os.chmod(dest, mode) |
| 355 | except IOError: |
Shawn O. Pearce | 4824478 | 2009-04-16 08:25:57 -0700 | [diff] [blame] | 356 | _error('Cannot copy file %s to %s', src, dest) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 357 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 358 | |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 359 | class _LinkFile(object): |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 360 | """Container for <linkfile> manifest element.""" |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 361 | |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 362 | def __init__(self, git_worktree, src, topdir, dest): |
| 363 | """Register a <linkfile> request. |
| 364 | |
| 365 | Args: |
| 366 | git_worktree: Absolute path to the git project checkout. |
| 367 | src: Target of symlink relative to path under |git_worktree|. |
| 368 | topdir: Absolute path to the top of the repo client checkout. |
| 369 | dest: Relative path under |topdir| of symlink to create. |
| 370 | """ |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 371 | self.git_worktree = git_worktree |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 372 | self.topdir = topdir |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 373 | self.src = src |
| 374 | self.dest = dest |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 375 | |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 376 | def __linkIt(self, relSrc, absDest): |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 377 | # link file if it does not exist or is out of date |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 378 | if not platform_utils.islink(absDest) or (platform_utils.readlink(absDest) != relSrc): |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 379 | try: |
| 380 | # remove existing file first, since it might be read-only |
Dan Willemsen | e1e0bd1 | 2015-11-18 16:49:38 -0800 | [diff] [blame] | 381 | if os.path.lexists(absDest): |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 382 | platform_utils.remove(absDest) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 383 | else: |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 384 | dest_dir = os.path.dirname(absDest) |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 385 | if not platform_utils.isdir(dest_dir): |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 386 | os.makedirs(dest_dir) |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 387 | platform_utils.symlink(relSrc, absDest) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 388 | except IOError: |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 389 | _error('Cannot link file %s to %s', relSrc, absDest) |
| 390 | |
| 391 | def _Link(self): |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 392 | """Link the self.src & self.dest paths. |
| 393 | |
| 394 | Handles wild cards on the src linking all of the files in the source in to |
| 395 | the destination directory. |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 396 | """ |
Mike Frysinger | 07392ed | 2020-02-10 21:35:48 -0500 | [diff] [blame] | 397 | # Some people use src="." to create stable links to projects. Lets allow |
| 398 | # that but reject all other uses of "." to keep things simple. |
| 399 | if self.src == '.': |
| 400 | src = self.git_worktree |
| 401 | else: |
| 402 | src = _SafeExpandPath(self.git_worktree, self.src) |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 403 | |
Angel Petkov | dbfbcb1 | 2020-05-02 23:16:20 +0300 | [diff] [blame] | 404 | if not glob.has_magic(src): |
| 405 | # Entity does not contain a wild card so just a simple one to one link operation. |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 406 | dest = _SafeExpandPath(self.topdir, self.dest, skipfinal=True) |
| 407 | # dest & src are absolute paths at this point. Make sure the target of |
| 408 | # the symlink is relative in the context of the repo client checkout. |
| 409 | relpath = os.path.relpath(src, os.path.dirname(dest)) |
| 410 | self.__linkIt(relpath, dest) |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 411 | else: |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 412 | dest = _SafeExpandPath(self.topdir, self.dest) |
Angel Petkov | dbfbcb1 | 2020-05-02 23:16:20 +0300 | [diff] [blame] | 413 | # Entity contains a wild card. |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 414 | if os.path.exists(dest) and not platform_utils.isdir(dest): |
| 415 | _error('Link error: src with wildcard, %s must be a directory', dest) |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 416 | else: |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 417 | for absSrcFile in glob.glob(src): |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 418 | # Create a releative path from source dir to destination dir |
| 419 | absSrcDir = os.path.dirname(absSrcFile) |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 420 | relSrcDir = os.path.relpath(absSrcDir, dest) |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 421 | |
| 422 | # Get the source file name |
| 423 | srcFile = os.path.basename(absSrcFile) |
| 424 | |
| 425 | # Now form the final full paths to srcFile. They will be |
| 426 | # absolute for the desintaiton and relative for the srouce. |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 427 | absDest = os.path.join(dest, srcFile) |
Wink Saville | 4c426ef | 2015-06-03 08:05:17 -0700 | [diff] [blame] | 428 | relSrc = os.path.join(relSrcDir, srcFile) |
| 429 | self.__linkIt(relSrc, absDest) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 430 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 431 | |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 432 | class RemoteSpec(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 433 | |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 434 | def __init__(self, |
| 435 | name, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 436 | url=None, |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 437 | pushUrl=None, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 438 | review=None, |
Dan Willemsen | 96c2d65 | 2016-04-06 16:03:54 -0700 | [diff] [blame] | 439 | revision=None, |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 440 | orig_name=None, |
| 441 | fetchUrl=None): |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 442 | self.name = name |
| 443 | self.url = url |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 444 | self.pushUrl = pushUrl |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 445 | self.review = review |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 446 | self.revision = revision |
Dan Willemsen | 96c2d65 | 2016-04-06 16:03:54 -0700 | [diff] [blame] | 447 | self.orig_name = orig_name |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 448 | self.fetchUrl = fetchUrl |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 449 | |
| 450 | class Project(object): |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 451 | # These objects can be shared between several working trees. |
| 452 | shareable_files = ['description', 'info'] |
| 453 | shareable_dirs = ['hooks', 'objects', 'rr-cache', 'svn'] |
| 454 | # These objects can only be used by a single working tree. |
| 455 | working_tree_files = ['config', 'packed-refs', 'shallow'] |
| 456 | working_tree_dirs = ['logs', 'refs'] |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 457 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 458 | def __init__(self, |
| 459 | manifest, |
| 460 | name, |
| 461 | remote, |
| 462 | gitdir, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 463 | objdir, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 464 | worktree, |
| 465 | relpath, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 466 | revisionExpr, |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 467 | revisionId, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 468 | rebase=True, |
| 469 | groups=None, |
| 470 | sync_c=False, |
| 471 | sync_s=False, |
YOUNG HO CHA | a32c92c | 2018-02-14 16:57:31 +0900 | [diff] [blame] | 472 | sync_tags=True, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 473 | clone_depth=None, |
| 474 | upstream=None, |
| 475 | parent=None, |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 476 | use_git_worktrees=False, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 477 | is_derived=False, |
David Pursehouse | b155354 | 2014-09-04 21:28:09 +0900 | [diff] [blame] | 478 | dest_branch=None, |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 479 | optimized_fetch=False, |
George Engelbrecht | 9bc283e | 2020-04-02 12:36:09 -0600 | [diff] [blame] | 480 | retry_fetches=0, |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 481 | old_revision=None): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 482 | """Init a Project object. |
| 483 | |
| 484 | Args: |
| 485 | manifest: The XmlManifest object. |
| 486 | name: The `name` attribute of manifest.xml's project element. |
| 487 | remote: RemoteSpec object specifying its remote's properties. |
| 488 | gitdir: Absolute path of git directory. |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 489 | objdir: Absolute path of directory to store git objects. |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 490 | worktree: Absolute path of git working tree. |
| 491 | relpath: Relative path of git working tree to repo's top directory. |
| 492 | revisionExpr: The `revision` attribute of manifest.xml's project element. |
| 493 | revisionId: git commit id for checking out. |
| 494 | rebase: The `rebase` attribute of manifest.xml's project element. |
| 495 | groups: The `groups` attribute of manifest.xml's project element. |
| 496 | sync_c: The `sync-c` attribute of manifest.xml's project element. |
| 497 | sync_s: The `sync-s` attribute of manifest.xml's project element. |
YOUNG HO CHA | a32c92c | 2018-02-14 16:57:31 +0900 | [diff] [blame] | 498 | sync_tags: The `sync-tags` attribute of manifest.xml's project element. |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 499 | upstream: The `upstream` attribute of manifest.xml's project element. |
| 500 | parent: The parent Project object. |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 501 | use_git_worktrees: Whether to use `git worktree` for this project. |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 502 | is_derived: False if the project was explicitly defined in the manifest; |
| 503 | True if the project is a discovered submodule. |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 504 | dest_branch: The branch to which to push changes for review by default. |
David Pursehouse | b155354 | 2014-09-04 21:28:09 +0900 | [diff] [blame] | 505 | optimized_fetch: If True, when a project is set to a sha1 revision, only |
| 506 | fetch from the remote if the sha1 is not present locally. |
George Engelbrecht | 9bc283e | 2020-04-02 12:36:09 -0600 | [diff] [blame] | 507 | retry_fetches: Retry remote fetches n times upon receiving transient error |
| 508 | with exponential backoff and jitter. |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 509 | old_revision: saved git commit id for open GITC projects. |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 510 | """ |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 511 | self.client = self.manifest = manifest |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 512 | self.name = name |
| 513 | self.remote = remote |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 514 | self.gitdir = gitdir.replace('\\', '/') |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 515 | self.objdir = objdir.replace('\\', '/') |
Shawn O. Pearce | 0ce6ca9 | 2011-01-10 13:26:01 -0800 | [diff] [blame] | 516 | if worktree: |
Renaud Paquay | fef9f21 | 2016-11-01 18:28:01 -0700 | [diff] [blame] | 517 | self.worktree = os.path.normpath(worktree).replace('\\', '/') |
Shawn O. Pearce | 0ce6ca9 | 2011-01-10 13:26:01 -0800 | [diff] [blame] | 518 | else: |
| 519 | self.worktree = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 520 | self.relpath = relpath |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 521 | self.revisionExpr = revisionExpr |
| 522 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 523 | if revisionId is None \ |
| 524 | and revisionExpr \ |
| 525 | and IsId(revisionExpr): |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 526 | self.revisionId = revisionExpr |
| 527 | else: |
| 528 | self.revisionId = revisionId |
| 529 | |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 530 | self.rebase = rebase |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 531 | self.groups = groups |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 532 | self.sync_c = sync_c |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 533 | self.sync_s = sync_s |
YOUNG HO CHA | a32c92c | 2018-02-14 16:57:31 +0900 | [diff] [blame] | 534 | self.sync_tags = sync_tags |
David Pursehouse | ede7f12 | 2012-11-27 22:25:30 +0900 | [diff] [blame] | 535 | self.clone_depth = clone_depth |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 536 | self.upstream = upstream |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 537 | self.parent = parent |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 538 | # NB: Do not use this setting in __init__ to change behavior so that the |
| 539 | # manifest.git checkout can inspect & change it after instantiating. See |
| 540 | # the XmlManifest init code for more info. |
| 541 | self.use_git_worktrees = use_git_worktrees |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 542 | self.is_derived = is_derived |
David Pursehouse | b155354 | 2014-09-04 21:28:09 +0900 | [diff] [blame] | 543 | self.optimized_fetch = optimized_fetch |
George Engelbrecht | 9bc283e | 2020-04-02 12:36:09 -0600 | [diff] [blame] | 544 | self.retry_fetches = max(0, retry_fetches) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 545 | self.subprojects = [] |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 546 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 547 | self.snapshots = {} |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 548 | self.copyfiles = [] |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 549 | self.linkfiles = [] |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 550 | self.annotations = [] |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 551 | self.config = GitConfig.ForRepository(gitdir=self.gitdir, |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 552 | defaults=self.client.globalConfig) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 553 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 554 | if self.worktree: |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 555 | self.work_git = self._GitGetByExec(self, bare=False, gitdir=gitdir) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 556 | else: |
| 557 | self.work_git = None |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 558 | self.bare_git = self._GitGetByExec(self, bare=True, gitdir=gitdir) |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 559 | self.bare_ref = GitRefs(gitdir) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 560 | self.bare_objdir = self._GitGetByExec(self, bare=True, gitdir=objdir) |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 561 | self.dest_branch = dest_branch |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 562 | self.old_revision = old_revision |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 563 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 564 | # This will be filled in if a project is later identified to be the |
| 565 | # project containing repo hooks. |
| 566 | self.enabled_repo_hooks = [] |
| 567 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 568 | @property |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 569 | def Derived(self): |
| 570 | return self.is_derived |
| 571 | |
| 572 | @property |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 573 | def Exists(self): |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 574 | return platform_utils.isdir(self.gitdir) and platform_utils.isdir(self.objdir) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 575 | |
| 576 | @property |
| 577 | def CurrentBranch(self): |
| 578 | """Obtain the name of the currently checked out branch. |
Mike Frysinger | c8290ad | 2019-10-01 01:07:11 -0400 | [diff] [blame] | 579 | |
| 580 | The branch name omits the 'refs/heads/' prefix. |
| 581 | None is returned if the project is on a detached HEAD, or if the work_git is |
| 582 | otheriwse inaccessible (e.g. an incomplete sync). |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 583 | """ |
Mike Frysinger | c8290ad | 2019-10-01 01:07:11 -0400 | [diff] [blame] | 584 | try: |
| 585 | b = self.work_git.GetHead() |
| 586 | except NoManifestException: |
| 587 | # If the local checkout is in a bad state, don't barf. Let the callers |
| 588 | # process this like the head is unreadable. |
| 589 | return None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 590 | if b.startswith(R_HEADS): |
| 591 | return b[len(R_HEADS):] |
| 592 | return None |
| 593 | |
Shawn O. Pearce | 3d2cdd0 | 2009-04-18 15:26:10 -0700 | [diff] [blame] | 594 | def IsRebaseInProgress(self): |
Mike Frysinger | 4b0eb5a | 2020-02-23 23:22:34 -0500 | [diff] [blame] | 595 | return (os.path.exists(self.work_git.GetDotgitPath('rebase-apply')) or |
| 596 | os.path.exists(self.work_git.GetDotgitPath('rebase-merge')) or |
| 597 | os.path.exists(os.path.join(self.worktree, '.dotest'))) |
Julius Gustavsson | 0cb1b3f | 2010-06-17 17:55:02 +0200 | [diff] [blame] | 598 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 599 | def IsDirty(self, consider_untracked=True): |
| 600 | """Is the working directory modified in some way? |
| 601 | """ |
| 602 | self.work_git.update_index('-q', |
| 603 | '--unmerged', |
| 604 | '--ignore-missing', |
| 605 | '--refresh') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 606 | if self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 607 | return True |
| 608 | if self.work_git.DiffZ('diff-files'): |
| 609 | return True |
| 610 | if consider_untracked and self.work_git.LsOthers(): |
| 611 | return True |
| 612 | return False |
| 613 | |
| 614 | _userident_name = None |
| 615 | _userident_email = None |
| 616 | |
| 617 | @property |
| 618 | def UserName(self): |
| 619 | """Obtain the user's personal name. |
| 620 | """ |
| 621 | if self._userident_name is None: |
| 622 | self._LoadUserIdentity() |
| 623 | return self._userident_name |
| 624 | |
| 625 | @property |
| 626 | def UserEmail(self): |
| 627 | """Obtain the user's email address. This is very likely |
| 628 | to be their Gerrit login. |
| 629 | """ |
| 630 | if self._userident_email is None: |
| 631 | self._LoadUserIdentity() |
| 632 | return self._userident_email |
| 633 | |
| 634 | def _LoadUserIdentity(self): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 635 | u = self.bare_git.var('GIT_COMMITTER_IDENT') |
| 636 | m = re.compile("^(.*) <([^>]*)> ").match(u) |
| 637 | if m: |
| 638 | self._userident_name = m.group(1) |
| 639 | self._userident_email = m.group(2) |
| 640 | else: |
| 641 | self._userident_name = '' |
| 642 | self._userident_email = '' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 643 | |
| 644 | def GetRemote(self, name): |
| 645 | """Get the configuration for a single remote. |
| 646 | """ |
| 647 | return self.config.GetRemote(name) |
| 648 | |
| 649 | def GetBranch(self, name): |
| 650 | """Get the configuration for a single branch. |
| 651 | """ |
| 652 | return self.config.GetBranch(name) |
| 653 | |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 654 | def GetBranches(self): |
| 655 | """Get all existing local branches. |
| 656 | """ |
| 657 | current = self.CurrentBranch |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 658 | all_refs = self._allrefs |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 659 | heads = {} |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 660 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 661 | for name, ref_id in all_refs.items(): |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 662 | if name.startswith(R_HEADS): |
| 663 | name = name[len(R_HEADS):] |
| 664 | b = self.GetBranch(name) |
| 665 | b.current = name == current |
| 666 | b.published = None |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 667 | b.revision = ref_id |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 668 | heads[name] = b |
| 669 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 670 | for name, ref_id in all_refs.items(): |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 671 | if name.startswith(R_PUB): |
| 672 | name = name[len(R_PUB):] |
| 673 | b = heads.get(name) |
| 674 | if b: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 675 | b.published = ref_id |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 676 | |
| 677 | return heads |
| 678 | |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 679 | def MatchesGroups(self, manifest_groups): |
| 680 | """Returns true if the manifest groups specified at init should cause |
| 681 | this project to be synced. |
| 682 | Prefixing a manifest group with "-" inverts the meaning of a group. |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 683 | All projects are implicitly labelled with "all". |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 684 | |
| 685 | labels are resolved in order. In the example case of |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 686 | project_groups: "all,group1,group2" |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 687 | manifest_groups: "-group1,group2" |
| 688 | the project will be matched. |
David Holmer | 0a1c6a1 | 2012-11-14 19:19:00 -0500 | [diff] [blame] | 689 | |
| 690 | The special manifest group "default" will match any project that |
| 691 | does not have the special project group "notdefault" |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 692 | """ |
David Holmer | 0a1c6a1 | 2012-11-14 19:19:00 -0500 | [diff] [blame] | 693 | expanded_manifest_groups = manifest_groups or ['default'] |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 694 | expanded_project_groups = ['all'] + (self.groups or []) |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 695 | if 'notdefault' not in expanded_project_groups: |
David Holmer | 0a1c6a1 | 2012-11-14 19:19:00 -0500 | [diff] [blame] | 696 | expanded_project_groups += ['default'] |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 697 | |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 698 | matched = False |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 699 | for group in expanded_manifest_groups: |
| 700 | if group.startswith('-') and group[1:] in expanded_project_groups: |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 701 | matched = False |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 702 | elif group in expanded_project_groups: |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 703 | matched = True |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 704 | |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 705 | return matched |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 706 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 707 | # Status Display ## |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 708 | def UncommitedFiles(self, get_all=True): |
| 709 | """Returns a list of strings, uncommitted files in the git tree. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 710 | |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 711 | Args: |
| 712 | get_all: a boolean, if True - get information about all different |
| 713 | uncommitted files. If False - return as soon as any kind of |
| 714 | uncommitted files is detected. |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 715 | """ |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 716 | details = [] |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 717 | self.work_git.update_index('-q', |
| 718 | '--unmerged', |
| 719 | '--ignore-missing', |
| 720 | '--refresh') |
| 721 | if self.IsRebaseInProgress(): |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 722 | details.append("rebase in progress") |
| 723 | if not get_all: |
| 724 | return details |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 725 | |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 726 | changes = self.work_git.DiffZ('diff-index', '--cached', HEAD).keys() |
| 727 | if changes: |
| 728 | details.extend(changes) |
| 729 | if not get_all: |
| 730 | return details |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 731 | |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 732 | changes = self.work_git.DiffZ('diff-files').keys() |
| 733 | if changes: |
| 734 | details.extend(changes) |
| 735 | if not get_all: |
| 736 | return details |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 737 | |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 738 | changes = self.work_git.LsOthers() |
| 739 | if changes: |
| 740 | details.extend(changes) |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 741 | |
Vadim Bendebury | 14e134d | 2014-10-05 15:40:30 -0700 | [diff] [blame] | 742 | return details |
| 743 | |
| 744 | def HasChanges(self): |
| 745 | """Returns true if there are uncommitted changes. |
| 746 | """ |
| 747 | if self.UncommitedFiles(get_all=False): |
| 748 | return True |
| 749 | else: |
| 750 | return False |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 751 | |
Andrew Wheeler | 4d5bb68 | 2012-02-27 13:52:22 -0600 | [diff] [blame] | 752 | def PrintWorkTreeStatus(self, output_redir=None, quiet=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 753 | """Prints the status of the repository to stdout. |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 754 | |
| 755 | Args: |
Rostislav Krasny | 0bcc2d2 | 2020-01-25 14:49:14 +0200 | [diff] [blame] | 756 | output_redir: If specified, redirect the output to this object. |
Andrew Wheeler | 4d5bb68 | 2012-02-27 13:52:22 -0600 | [diff] [blame] | 757 | quiet: If True then only print the project name. Do not print |
| 758 | the modified files, branch name, etc. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 759 | """ |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 760 | if not platform_utils.isdir(self.worktree): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 761 | if output_redir is None: |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 762 | output_redir = sys.stdout |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 763 | print(file=output_redir) |
| 764 | print('project %s/' % self.relpath, file=output_redir) |
| 765 | print(' missing (run "repo sync")', file=output_redir) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 766 | return |
| 767 | |
| 768 | self.work_git.update_index('-q', |
| 769 | '--unmerged', |
| 770 | '--ignore-missing', |
| 771 | '--refresh') |
Shawn O. Pearce | 3d2cdd0 | 2009-04-18 15:26:10 -0700 | [diff] [blame] | 772 | rb = self.IsRebaseInProgress() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 773 | di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD) |
| 774 | df = self.work_git.DiffZ('diff-files') |
| 775 | do = self.work_git.LsOthers() |
Ali Utku Selen | 76abcc1 | 2012-01-25 10:51:12 +0100 | [diff] [blame] | 776 | if not rb and not di and not df and not do and not self.CurrentBranch: |
Shawn O. Pearce | 161f445 | 2009-04-10 17:41:44 -0700 | [diff] [blame] | 777 | return 'CLEAN' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 778 | |
| 779 | out = StatusColoring(self.config) |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 780 | if output_redir is not None: |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 781 | out.redirect(output_redir) |
Jakub Vrana | 0402cd8 | 2014-09-09 15:39:15 -0700 | [diff] [blame] | 782 | out.project('project %-40s', self.relpath + '/ ') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 783 | |
Andrew Wheeler | 4d5bb68 | 2012-02-27 13:52:22 -0600 | [diff] [blame] | 784 | if quiet: |
| 785 | out.nl() |
| 786 | return 'DIRTY' |
| 787 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 788 | branch = self.CurrentBranch |
| 789 | if branch is None: |
| 790 | out.nobranch('(*** NO BRANCH ***)') |
| 791 | else: |
| 792 | out.branch('branch %s', branch) |
| 793 | out.nl() |
| 794 | |
Shawn O. Pearce | 3d2cdd0 | 2009-04-18 15:26:10 -0700 | [diff] [blame] | 795 | if rb: |
| 796 | out.important('prior sync failed; rebase still in progress') |
| 797 | out.nl() |
| 798 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 799 | paths = list() |
| 800 | paths.extend(di.keys()) |
| 801 | paths.extend(df.keys()) |
| 802 | paths.extend(do) |
| 803 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 804 | for p in sorted(set(paths)): |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 805 | try: |
| 806 | i = di[p] |
| 807 | except KeyError: |
| 808 | i = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 809 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 810 | try: |
| 811 | f = df[p] |
| 812 | except KeyError: |
| 813 | f = None |
Julius Gustavsson | 0cb1b3f | 2010-06-17 17:55:02 +0200 | [diff] [blame] | 814 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 815 | if i: |
| 816 | i_status = i.status.upper() |
| 817 | else: |
| 818 | i_status = '-' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 819 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 820 | if f: |
| 821 | f_status = f.status.lower() |
| 822 | else: |
| 823 | f_status = '-' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 824 | |
| 825 | if i and i.src_path: |
Shawn O. Pearce | fe08675 | 2009-03-03 13:49:48 -0800 | [diff] [blame] | 826 | line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status, |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 827 | i.src_path, p, i.level) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 828 | else: |
| 829 | line = ' %s%s\t%s' % (i_status, f_status, p) |
| 830 | |
| 831 | if i and not f: |
| 832 | out.added('%s', line) |
| 833 | elif (i and f) or (not i and f): |
| 834 | out.changed('%s', line) |
| 835 | elif not i and not f: |
| 836 | out.untracked('%s', line) |
| 837 | else: |
| 838 | out.write('%s', line) |
| 839 | out.nl() |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 840 | |
Shawn O. Pearce | 161f445 | 2009-04-10 17:41:44 -0700 | [diff] [blame] | 841 | return 'DIRTY' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 842 | |
pelya | d67872d | 2012-03-28 14:49:58 +0300 | [diff] [blame] | 843 | def PrintWorkTreeDiff(self, absolute_paths=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 844 | """Prints the status of the repository to stdout. |
| 845 | """ |
| 846 | out = DiffColoring(self.config) |
| 847 | cmd = ['diff'] |
| 848 | if out.is_on: |
| 849 | cmd.append('--color') |
| 850 | cmd.append(HEAD) |
pelya | d67872d | 2012-03-28 14:49:58 +0300 | [diff] [blame] | 851 | if absolute_paths: |
| 852 | cmd.append('--src-prefix=a/%s/' % self.relpath) |
| 853 | cmd.append('--dst-prefix=b/%s/' % self.relpath) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 854 | cmd.append('--') |
Mike Frysinger | 0a9265e | 2019-09-30 23:59:27 -0400 | [diff] [blame] | 855 | try: |
| 856 | p = GitCommand(self, |
| 857 | cmd, |
| 858 | capture_stdout=True, |
| 859 | capture_stderr=True) |
| 860 | except GitError as e: |
| 861 | out.nl() |
| 862 | out.project('project %s/' % self.relpath) |
| 863 | out.nl() |
| 864 | out.fail('%s', str(e)) |
| 865 | out.nl() |
| 866 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 867 | has_diff = False |
| 868 | for line in p.process.stdout: |
Mike Frysinger | 600f492 | 2019-08-03 02:14:28 -0400 | [diff] [blame] | 869 | if not hasattr(line, 'encode'): |
| 870 | line = line.decode() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 871 | if not has_diff: |
| 872 | out.nl() |
| 873 | out.project('project %s/' % self.relpath) |
| 874 | out.nl() |
| 875 | has_diff = True |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 876 | print(line[:-1]) |
Mike Frysinger | 0a9265e | 2019-09-30 23:59:27 -0400 | [diff] [blame] | 877 | return p.Wait() == 0 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 878 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 879 | # Publish / Upload ## |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 880 | def WasPublished(self, branch, all_refs=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 881 | """Was the branch published (uploaded) for code review? |
| 882 | If so, returns the SHA-1 hash of the last published |
| 883 | state for the branch. |
| 884 | """ |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 885 | key = R_PUB + branch |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 886 | if all_refs is None: |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 887 | try: |
| 888 | return self.bare_git.rev_parse(key) |
| 889 | except GitError: |
| 890 | return None |
| 891 | else: |
| 892 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 893 | return all_refs[key] |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 894 | except KeyError: |
| 895 | return None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 896 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 897 | def CleanPublishedCache(self, all_refs=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 898 | """Prunes any stale published refs. |
| 899 | """ |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 900 | if all_refs is None: |
| 901 | all_refs = self._allrefs |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 902 | heads = set() |
| 903 | canrm = {} |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 904 | for name, ref_id in all_refs.items(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 905 | if name.startswith(R_HEADS): |
| 906 | heads.add(name) |
| 907 | elif name.startswith(R_PUB): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 908 | canrm[name] = ref_id |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 909 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 910 | for name, ref_id in canrm.items(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 911 | n = name[len(R_PUB):] |
| 912 | if R_HEADS + n not in heads: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 913 | self.bare_git.DeleteRef(name, ref_id) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 914 | |
Mandeep Singh Baines | d6c93a2 | 2011-05-26 10:34:11 -0700 | [diff] [blame] | 915 | def GetUploadableBranches(self, selected_branch=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 916 | """List any branches which can be uploaded for review. |
| 917 | """ |
| 918 | heads = {} |
| 919 | pubed = {} |
| 920 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 921 | for name, ref_id in self._allrefs.items(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 922 | if name.startswith(R_HEADS): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 923 | heads[name[len(R_HEADS):]] = ref_id |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 924 | elif name.startswith(R_PUB): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 925 | pubed[name[len(R_PUB):]] = ref_id |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 926 | |
| 927 | ready = [] |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 928 | for branch, ref_id in heads.items(): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 929 | if branch in pubed and pubed[branch] == ref_id: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 930 | continue |
Mandeep Singh Baines | d6c93a2 | 2011-05-26 10:34:11 -0700 | [diff] [blame] | 931 | if selected_branch and branch != selected_branch: |
| 932 | continue |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 933 | |
Shawn O. Pearce | 35f2596 | 2008-11-11 17:03:13 -0800 | [diff] [blame] | 934 | rb = self.GetUploadableBranch(branch) |
| 935 | if rb: |
| 936 | ready.append(rb) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 937 | return ready |
| 938 | |
Shawn O. Pearce | 35f2596 | 2008-11-11 17:03:13 -0800 | [diff] [blame] | 939 | def GetUploadableBranch(self, branch_name): |
| 940 | """Get a single uploadable branch, or None. |
| 941 | """ |
| 942 | branch = self.GetBranch(branch_name) |
| 943 | base = branch.LocalMerge |
| 944 | if branch.LocalMerge: |
| 945 | rb = ReviewableBranch(self, branch, base) |
| 946 | if rb.commits: |
| 947 | return rb |
| 948 | return None |
| 949 | |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 950 | def UploadForReview(self, branch=None, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 951 | people=([], []), |
Mike Frysinger | 819cc81 | 2020-02-19 02:27:22 -0500 | [diff] [blame] | 952 | dryrun=False, |
Brian Harring | 435370c | 2012-07-28 15:37:04 -0700 | [diff] [blame] | 953 | auto_topic=False, |
Mike Frysinger | 84685ba | 2020-02-19 02:22:22 -0500 | [diff] [blame] | 954 | hashtags=(), |
Mike Frysinger | fc1b18a | 2020-02-24 15:38:07 -0500 | [diff] [blame] | 955 | labels=(), |
Changcheng Xiao | 87984c6 | 2017-08-02 16:55:03 +0200 | [diff] [blame] | 956 | private=False, |
Vadim Bendebury | bd8f658 | 2018-10-31 13:48:01 -0700 | [diff] [blame] | 957 | notify=None, |
Changcheng Xiao | 87984c6 | 2017-08-02 16:55:03 +0200 | [diff] [blame] | 958 | wip=False, |
Łukasz Gardoń | bed59ce | 2017-08-08 10:18:11 +0200 | [diff] [blame] | 959 | dest_branch=None, |
Masaya Suzuki | 305a2d0 | 2017-11-13 10:48:34 -0800 | [diff] [blame] | 960 | validate_certs=True, |
| 961 | push_options=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 962 | """Uploads the named branch for code review. |
| 963 | """ |
| 964 | if branch is None: |
| 965 | branch = self.CurrentBranch |
| 966 | if branch is None: |
| 967 | raise GitError('not currently on a branch') |
| 968 | |
| 969 | branch = self.GetBranch(branch) |
| 970 | if not branch.LocalMerge: |
| 971 | raise GitError('branch %s does not track a remote' % branch.name) |
| 972 | if not branch.remote.review: |
| 973 | raise GitError('remote %s has no review url' % branch.remote.name) |
| 974 | |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 975 | if dest_branch is None: |
| 976 | dest_branch = self.dest_branch |
| 977 | if dest_branch is None: |
| 978 | dest_branch = branch.merge |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 979 | if not dest_branch.startswith(R_HEADS): |
| 980 | dest_branch = R_HEADS + dest_branch |
| 981 | |
Shawn O. Pearce | 339ba9f | 2008-11-06 09:52:51 -0800 | [diff] [blame] | 982 | if not branch.remote.projectname: |
| 983 | branch.remote.projectname = self.name |
| 984 | branch.remote.Save() |
| 985 | |
Łukasz Gardoń | bed59ce | 2017-08-08 10:18:11 +0200 | [diff] [blame] | 986 | url = branch.remote.ReviewUrl(self.UserEmail, validate_certs) |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 987 | if url is None: |
| 988 | raise UploadError('review not configured') |
| 989 | cmd = ['push'] |
Mike Frysinger | 819cc81 | 2020-02-19 02:27:22 -0500 | [diff] [blame] | 990 | if dryrun: |
| 991 | cmd.append('-n') |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 992 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 993 | if url.startswith('ssh://'): |
Jonathan Nieder | 713c587 | 2018-11-05 13:21:52 -0800 | [diff] [blame] | 994 | cmd.append('--receive-pack=gerrit receive-pack') |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 995 | |
Masaya Suzuki | 305a2d0 | 2017-11-13 10:48:34 -0800 | [diff] [blame] | 996 | for push_option in (push_options or []): |
| 997 | cmd.append('-o') |
| 998 | cmd.append(push_option) |
| 999 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 1000 | cmd.append(url) |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 1001 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 1002 | if dest_branch.startswith(R_HEADS): |
| 1003 | dest_branch = dest_branch[len(R_HEADS):] |
Brian Harring | 435370c | 2012-07-28 15:37:04 -0700 | [diff] [blame] | 1004 | |
Mike Frysinger | b0fbc7f | 2020-02-25 02:58:04 -0500 | [diff] [blame] | 1005 | ref_spec = '%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch) |
David Pursehouse | f25a370 | 2018-11-14 19:01:22 -0800 | [diff] [blame] | 1006 | opts = [] |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 1007 | if auto_topic: |
David Pursehouse | f25a370 | 2018-11-14 19:01:22 -0800 | [diff] [blame] | 1008 | opts += ['topic=' + branch.name] |
Mike Frysinger | 84685ba | 2020-02-19 02:22:22 -0500 | [diff] [blame] | 1009 | opts += ['t=%s' % p for p in hashtags] |
Mike Frysinger | fc1b18a | 2020-02-24 15:38:07 -0500 | [diff] [blame] | 1010 | opts += ['l=%s' % p for p in labels] |
Changcheng Xiao | 87984c6 | 2017-08-02 16:55:03 +0200 | [diff] [blame] | 1011 | |
David Pursehouse | f25a370 | 2018-11-14 19:01:22 -0800 | [diff] [blame] | 1012 | opts += ['r=%s' % p for p in people[0]] |
Jonathan Nieder | 713c587 | 2018-11-05 13:21:52 -0800 | [diff] [blame] | 1013 | opts += ['cc=%s' % p for p in people[1]] |
Vadim Bendebury | bd8f658 | 2018-10-31 13:48:01 -0700 | [diff] [blame] | 1014 | if notify: |
| 1015 | opts += ['notify=' + notify] |
Jonathan Nieder | 713c587 | 2018-11-05 13:21:52 -0800 | [diff] [blame] | 1016 | if private: |
| 1017 | opts += ['private'] |
| 1018 | if wip: |
| 1019 | opts += ['wip'] |
| 1020 | if opts: |
| 1021 | ref_spec = ref_spec + '%' + ','.join(opts) |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 1022 | cmd.append(ref_spec) |
| 1023 | |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1024 | if GitCommand(self, cmd, bare=True).Wait() != 0: |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 1025 | raise UploadError('Upload failed') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1026 | |
Mike Frysinger | d7f8683 | 2020-11-19 19:18:46 -0500 | [diff] [blame] | 1027 | if not dryrun: |
| 1028 | msg = "posted to %s for %s" % (branch.remote.review, dest_branch) |
| 1029 | self.bare_git.UpdateRef(R_PUB + branch.name, |
| 1030 | R_HEADS + branch.name, |
| 1031 | message=msg) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1032 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1033 | # Sync ## |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1034 | def _ExtractArchive(self, tarpath, path=None): |
| 1035 | """Extract the given tar on its current location |
| 1036 | |
| 1037 | Args: |
| 1038 | - tarpath: The path to the actual tar file |
| 1039 | |
| 1040 | """ |
| 1041 | try: |
| 1042 | with tarfile.open(tarpath, 'r') as tar: |
| 1043 | tar.extractall(path=path) |
| 1044 | return True |
| 1045 | except (IOError, tarfile.TarError) as e: |
David Pursehouse | f33929d | 2015-08-24 14:39:14 +0900 | [diff] [blame] | 1046 | _error("Cannot extract archive %s: %s", tarpath, str(e)) |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1047 | return False |
| 1048 | |
Shawn O. Pearce | e02ac0a | 2012-03-14 15:36:59 -0700 | [diff] [blame] | 1049 | def Sync_NetworkHalf(self, |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1050 | quiet=False, |
Mike Frysinger | 521d01b | 2020-02-17 01:51:49 -0500 | [diff] [blame] | 1051 | verbose=False, |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1052 | is_new=None, |
| 1053 | current_branch_only=False, |
| 1054 | force_sync=False, |
| 1055 | clone_bundle=True, |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 1056 | tags=True, |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1057 | archive=False, |
| 1058 | optimized_fetch=False, |
George Engelbrecht | 9bc283e | 2020-04-02 12:36:09 -0600 | [diff] [blame] | 1059 | retry_fetches=0, |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 1060 | prune=False, |
Xin Li | 745be2e | 2019-06-03 11:24:30 -0700 | [diff] [blame] | 1061 | submodules=False, |
| 1062 | clone_filter=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1063 | """Perform only the network IO portion of the sync process. |
| 1064 | Local working directory/branch state is not affected. |
| 1065 | """ |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1066 | if archive and not isinstance(self, MetaProject): |
| 1067 | if self.remote.url.startswith(('http://', 'https://')): |
David Pursehouse | f33929d | 2015-08-24 14:39:14 +0900 | [diff] [blame] | 1068 | _error("%s: Cannot fetch archives from http/https remotes.", self.name) |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1069 | return False |
| 1070 | |
| 1071 | name = self.relpath.replace('\\', '/') |
| 1072 | name = name.replace('/', '_') |
| 1073 | tarpath = '%s.tar' % name |
| 1074 | topdir = self.manifest.topdir |
| 1075 | |
| 1076 | try: |
| 1077 | self._FetchArchive(tarpath, cwd=topdir) |
| 1078 | except GitError as e: |
David Pursehouse | f33929d | 2015-08-24 14:39:14 +0900 | [diff] [blame] | 1079 | _error('%s', e) |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1080 | return False |
| 1081 | |
| 1082 | # From now on, we only need absolute tarpath |
| 1083 | tarpath = os.path.join(topdir, tarpath) |
| 1084 | |
| 1085 | if not self._ExtractArchive(tarpath, path=topdir): |
| 1086 | return False |
| 1087 | try: |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 1088 | platform_utils.remove(tarpath) |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1089 | except OSError as e: |
David Pursehouse | f33929d | 2015-08-24 14:39:14 +0900 | [diff] [blame] | 1090 | _warn("Cannot remove archive %s: %s", tarpath, str(e)) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1091 | self._CopyAndLinkFiles() |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1092 | return True |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 1093 | if is_new is None: |
| 1094 | is_new = not self.Exists |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1095 | if is_new: |
David Pursehouse | e8ace26 | 2020-02-13 12:41:15 +0900 | [diff] [blame] | 1096 | self._InitGitDir(force_sync=force_sync, quiet=quiet) |
Jimmie Wester | a044458 | 2012-10-24 13:44:42 +0200 | [diff] [blame] | 1097 | else: |
David Pursehouse | e8ace26 | 2020-02-13 12:41:15 +0900 | [diff] [blame] | 1098 | self._UpdateHooks(quiet=quiet) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1099 | self._InitRemote() |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1100 | |
| 1101 | if is_new: |
| 1102 | alt = os.path.join(self.gitdir, 'objects/info/alternates') |
| 1103 | try: |
Mike Frysinger | 3164d40 | 2019-11-11 05:40:22 -0500 | [diff] [blame] | 1104 | with open(alt) as fd: |
Samuel Holland | baa0009 | 2018-01-22 10:57:29 -0600 | [diff] [blame] | 1105 | # This works for both absolute and relative alternate directories. |
| 1106 | alt_dir = os.path.join(self.objdir, 'objects', fd.readline().rstrip()) |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1107 | except IOError: |
| 1108 | alt_dir = None |
| 1109 | else: |
| 1110 | alt_dir = None |
| 1111 | |
Mike Frysinger | e50b6a7 | 2020-02-19 01:45:48 -0500 | [diff] [blame] | 1112 | if (clone_bundle |
| 1113 | and alt_dir is None |
| 1114 | and self._ApplyCloneBundle(initial=is_new, quiet=quiet, verbose=verbose)): |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1115 | is_new = False |
| 1116 | |
Shawn O. Pearce | 6ba6ba0 | 2012-05-24 09:46:50 -0700 | [diff] [blame] | 1117 | if not current_branch_only: |
| 1118 | if self.sync_c: |
| 1119 | current_branch_only = True |
| 1120 | elif not self.manifest._loaded: |
| 1121 | # Manifest cannot check defaults until it syncs. |
| 1122 | current_branch_only = False |
| 1123 | elif self.manifest.default.sync_c: |
| 1124 | current_branch_only = True |
| 1125 | |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 1126 | if not self.sync_tags: |
| 1127 | tags = False |
YOUNG HO CHA | a32c92c | 2018-02-14 16:57:31 +0900 | [diff] [blame] | 1128 | |
Aymen Bouaziz | 6c59446 | 2016-10-25 18:03:51 +0200 | [diff] [blame] | 1129 | if self.clone_depth: |
| 1130 | depth = self.clone_depth |
| 1131 | else: |
| 1132 | depth = self.manifest.manifestProject.config.GetString('repo.depth') |
| 1133 | |
Mike Frysinger | 521d01b | 2020-02-17 01:51:49 -0500 | [diff] [blame] | 1134 | # See if we can skip the network fetch entirely. |
| 1135 | if not (optimized_fetch and |
| 1136 | (ID_RE.match(self.revisionExpr) and |
| 1137 | self._CheckForImmutableRevision())): |
| 1138 | if not self._RemoteFetch( |
David Pursehouse | 3cceda5 | 2020-02-18 14:11:39 +0900 | [diff] [blame] | 1139 | initial=is_new, quiet=quiet, verbose=verbose, alt_dir=alt_dir, |
| 1140 | current_branch_only=current_branch_only, |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 1141 | tags=tags, prune=prune, depth=depth, |
David Pursehouse | 3cceda5 | 2020-02-18 14:11:39 +0900 | [diff] [blame] | 1142 | submodules=submodules, force_sync=force_sync, |
George Engelbrecht | 9bc283e | 2020-04-02 12:36:09 -0600 | [diff] [blame] | 1143 | clone_filter=clone_filter, retry_fetches=retry_fetches): |
Mike Frysinger | 521d01b | 2020-02-17 01:51:49 -0500 | [diff] [blame] | 1144 | return False |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1145 | |
Nikolai Merinov | 09f0abb | 2018-10-19 15:07:05 +0500 | [diff] [blame] | 1146 | mp = self.manifest.manifestProject |
| 1147 | dissociate = mp.config.GetBoolean('repo.dissociate') |
| 1148 | if dissociate: |
| 1149 | alternates_file = os.path.join(self.gitdir, 'objects/info/alternates') |
| 1150 | if os.path.exists(alternates_file): |
| 1151 | cmd = ['repack', '-a', '-d'] |
| 1152 | if GitCommand(self, cmd, bare=True).Wait() != 0: |
| 1153 | return False |
| 1154 | platform_utils.remove(alternates_file) |
| 1155 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1156 | if self.worktree: |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1157 | self._InitMRef() |
| 1158 | else: |
| 1159 | self._InitMirrorHead() |
| 1160 | try: |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 1161 | platform_utils.remove(os.path.join(self.gitdir, 'FETCH_HEAD')) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1162 | except OSError: |
| 1163 | pass |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1164 | return True |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 1165 | |
| 1166 | def PostRepoUpgrade(self): |
| 1167 | self._InitHooks() |
| 1168 | |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1169 | def _CopyAndLinkFiles(self): |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 1170 | if self.client.isGitcClient: |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1171 | return |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1172 | for copyfile in self.copyfiles: |
| 1173 | copyfile._Copy() |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1174 | for linkfile in self.linkfiles: |
| 1175 | linkfile._Link() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1176 | |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 1177 | def GetCommitRevisionId(self): |
| 1178 | """Get revisionId of a commit. |
| 1179 | |
| 1180 | Use this method instead of GetRevisionId to get the id of the commit rather |
| 1181 | than the id of the current git object (for example, a tag) |
| 1182 | |
| 1183 | """ |
| 1184 | if not self.revisionExpr.startswith(R_TAGS): |
| 1185 | return self.GetRevisionId(self._allrefs) |
| 1186 | |
| 1187 | try: |
| 1188 | return self.bare_git.rev_list(self.revisionExpr, '-1')[0] |
| 1189 | except GitError: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1190 | raise ManifestInvalidRevisionError('revision %s in %s not found' % |
| 1191 | (self.revisionExpr, self.name)) |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 1192 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1193 | def GetRevisionId(self, all_refs=None): |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1194 | if self.revisionId: |
| 1195 | return self.revisionId |
| 1196 | |
| 1197 | rem = self.GetRemote(self.remote.name) |
| 1198 | rev = rem.ToLocal(self.revisionExpr) |
| 1199 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1200 | if all_refs is not None and rev in all_refs: |
| 1201 | return all_refs[rev] |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1202 | |
| 1203 | try: |
| 1204 | return self.bare_git.rev_parse('--verify', '%s^0' % rev) |
| 1205 | except GitError: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1206 | raise ManifestInvalidRevisionError('revision %s in %s not found' % |
| 1207 | (self.revisionExpr, self.name)) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1208 | |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 1209 | def Sync_LocalHalf(self, syncbuf, force_sync=False, submodules=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1210 | """Perform only the local IO portion of the sync process. |
| 1211 | Network access is not required. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1212 | """ |
Mike Frysinger | b610b85 | 2019-11-11 05:10:03 -0500 | [diff] [blame] | 1213 | if not os.path.exists(self.gitdir): |
| 1214 | syncbuf.fail(self, |
| 1215 | 'Cannot checkout %s due to missing network sync; Run ' |
| 1216 | '`repo sync -n %s` first.' % |
| 1217 | (self.name, self.name)) |
| 1218 | return |
| 1219 | |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 1220 | self._InitWorkTree(force_sync=force_sync, submodules=submodules) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1221 | all_refs = self.bare_ref.all |
| 1222 | self.CleanPublishedCache(all_refs) |
| 1223 | revid = self.GetRevisionId(all_refs) |
Skyler Kaufman | 835cd68 | 2011-03-08 12:14:41 -0800 | [diff] [blame] | 1224 | |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 1225 | def _doff(): |
| 1226 | self._FastForward(revid) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1227 | self._CopyAndLinkFiles() |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 1228 | |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 1229 | def _dosubmodules(): |
| 1230 | self._SyncSubmodules(quiet=True) |
| 1231 | |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1232 | head = self.work_git.GetHead() |
| 1233 | if head.startswith(R_HEADS): |
| 1234 | branch = head[len(R_HEADS):] |
| 1235 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1236 | head = all_refs[head] |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1237 | except KeyError: |
| 1238 | head = None |
| 1239 | else: |
| 1240 | branch = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1241 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1242 | if branch is None or syncbuf.detach_head: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1243 | # Currently on a detached HEAD. The user is assumed to |
| 1244 | # not have any local modifications worth worrying about. |
| 1245 | # |
Shawn O. Pearce | 3d2cdd0 | 2009-04-18 15:26:10 -0700 | [diff] [blame] | 1246 | if self.IsRebaseInProgress(): |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1247 | syncbuf.fail(self, _PriorSyncFailedError()) |
| 1248 | return |
| 1249 | |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1250 | if head == revid: |
| 1251 | # No changes; don't do anything further. |
Florian Vallee | 7cf1b36 | 2012-06-07 17:11:42 +0200 | [diff] [blame] | 1252 | # Except if the head needs to be detached |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1253 | # |
Florian Vallee | 7cf1b36 | 2012-06-07 17:11:42 +0200 | [diff] [blame] | 1254 | if not syncbuf.detach_head: |
Dan Willemsen | 029eaf3 | 2015-09-03 12:52:28 -0700 | [diff] [blame] | 1255 | # The copy/linkfile config may have changed. |
| 1256 | self._CopyAndLinkFiles() |
Florian Vallee | 7cf1b36 | 2012-06-07 17:11:42 +0200 | [diff] [blame] | 1257 | return |
| 1258 | else: |
| 1259 | lost = self._revlist(not_rev(revid), HEAD) |
| 1260 | if lost: |
| 1261 | syncbuf.info(self, "discarding %d commits", len(lost)) |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1262 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1263 | try: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1264 | self._Checkout(revid, quiet=True) |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 1265 | if submodules: |
| 1266 | self._SyncSubmodules(quiet=True) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 1267 | except GitError as e: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1268 | syncbuf.fail(self, e) |
| 1269 | return |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1270 | self._CopyAndLinkFiles() |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1271 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1272 | |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1273 | if head == revid: |
| 1274 | # No changes; don't do anything further. |
| 1275 | # |
Dan Willemsen | 029eaf3 | 2015-09-03 12:52:28 -0700 | [diff] [blame] | 1276 | # The copy/linkfile config may have changed. |
| 1277 | self._CopyAndLinkFiles() |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1278 | return |
| 1279 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1280 | branch = self.GetBranch(branch) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1281 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1282 | if not branch.LocalMerge: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1283 | # The current branch has no tracking configuration. |
Anatol Pomazau | 2a32f6a | 2011-08-30 10:52:33 -0700 | [diff] [blame] | 1284 | # Jump off it to a detached HEAD. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1285 | # |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1286 | syncbuf.info(self, |
| 1287 | "leaving %s; does not track upstream", |
| 1288 | branch.name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1289 | try: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1290 | self._Checkout(revid, quiet=True) |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 1291 | if submodules: |
| 1292 | self._SyncSubmodules(quiet=True) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 1293 | except GitError as e: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1294 | syncbuf.fail(self, e) |
| 1295 | return |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1296 | self._CopyAndLinkFiles() |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1297 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1298 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1299 | upstream_gain = self._revlist(not_rev(HEAD), revid) |
Mike Frysinger | 2ba5a1e | 2019-09-23 17:42:23 -0400 | [diff] [blame] | 1300 | |
| 1301 | # See if we can perform a fast forward merge. This can happen if our |
| 1302 | # branch isn't in the exact same state as we last published. |
| 1303 | try: |
| 1304 | self.work_git.merge_base('--is-ancestor', HEAD, revid) |
| 1305 | # Skip the published logic. |
| 1306 | pub = False |
| 1307 | except GitError: |
| 1308 | pub = self.WasPublished(branch.name, all_refs) |
| 1309 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1310 | if pub: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1311 | not_merged = self._revlist(not_rev(revid), pub) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1312 | if not_merged: |
| 1313 | if upstream_gain: |
| 1314 | # The user has published this branch and some of those |
| 1315 | # commits are not yet merged upstream. We do not want |
| 1316 | # to rewrite the published commits so we punt. |
| 1317 | # |
Daniel Sandler | 4c50dee | 2010-03-02 15:38:03 -0500 | [diff] [blame] | 1318 | syncbuf.fail(self, |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1319 | "branch %s is published (but not merged) and is now " |
| 1320 | "%d commits behind" % (branch.name, len(upstream_gain))) |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1321 | return |
Shawn O. Pearce | 05f66b6 | 2009-04-21 08:26:32 -0700 | [diff] [blame] | 1322 | elif pub == head: |
| 1323 | # All published commits are merged, and thus we are a |
| 1324 | # strict subset. We can fast-forward safely. |
Shawn O. Pearce | a54c527 | 2008-10-30 11:03:00 -0700 | [diff] [blame] | 1325 | # |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1326 | syncbuf.later1(self, _doff) |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 1327 | if submodules: |
| 1328 | syncbuf.later1(self, _dosubmodules) |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1329 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1330 | |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1331 | # Examine the local commits not in the remote. Find the |
| 1332 | # last one attributed to this user, if any. |
| 1333 | # |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1334 | local_changes = self._revlist(not_rev(revid), HEAD, format='%H %ce') |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1335 | last_mine = None |
| 1336 | cnt_mine = 0 |
| 1337 | for commit in local_changes: |
Mike Frysinger | 600f492 | 2019-08-03 02:14:28 -0400 | [diff] [blame] | 1338 | commit_id, committer_email = commit.split(' ', 1) |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1339 | if committer_email == self.UserEmail: |
| 1340 | last_mine = commit_id |
| 1341 | cnt_mine += 1 |
| 1342 | |
Shawn O. Pearce | da88ff4 | 2009-06-03 11:09:12 -0700 | [diff] [blame] | 1343 | if not upstream_gain and cnt_mine == len(local_changes): |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1344 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1345 | |
| 1346 | if self.IsDirty(consider_untracked=False): |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1347 | syncbuf.fail(self, _DirtyError()) |
| 1348 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1349 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1350 | # If the upstream switched on us, warn the user. |
| 1351 | # |
| 1352 | if branch.merge != self.revisionExpr: |
| 1353 | if branch.merge and self.revisionExpr: |
| 1354 | syncbuf.info(self, |
| 1355 | 'manifest switched %s...%s', |
| 1356 | branch.merge, |
| 1357 | self.revisionExpr) |
| 1358 | elif branch.merge: |
| 1359 | syncbuf.info(self, |
| 1360 | 'manifest no longer tracks %s', |
| 1361 | branch.merge) |
| 1362 | |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1363 | if cnt_mine < len(local_changes): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1364 | # Upstream rebased. Not everything in HEAD |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1365 | # was created by this user. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1366 | # |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1367 | syncbuf.info(self, |
| 1368 | "discarding %d commits removed from upstream", |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1369 | len(local_changes) - cnt_mine) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1370 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1371 | branch.remote = self.GetRemote(self.remote.name) |
Anatol Pomazau | cd7c5de | 2012-03-20 13:45:00 -0700 | [diff] [blame] | 1372 | if not ID_RE.match(self.revisionExpr): |
| 1373 | # in case of manifest sync the revisionExpr might be a SHA1 |
| 1374 | branch.merge = self.revisionExpr |
Conley Owens | 04f2f0e | 2014-10-01 17:22:46 -0700 | [diff] [blame] | 1375 | if not branch.merge.startswith('refs/'): |
| 1376 | branch.merge = R_HEADS + branch.merge |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1377 | branch.Save() |
| 1378 | |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 1379 | if cnt_mine > 0 and self.rebase: |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 1380 | def _docopyandlink(): |
| 1381 | self._CopyAndLinkFiles() |
| 1382 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1383 | def _dorebase(): |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1384 | self._Rebase(upstream='%s^1' % last_mine, onto=revid) |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1385 | syncbuf.later2(self, _dorebase) |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 1386 | if submodules: |
| 1387 | syncbuf.later2(self, _dosubmodules) |
| 1388 | syncbuf.later2(self, _docopyandlink) |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1389 | elif local_changes: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1390 | try: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1391 | self._ResetHard(revid) |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 1392 | if submodules: |
| 1393 | self._SyncSubmodules(quiet=True) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1394 | self._CopyAndLinkFiles() |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 1395 | except GitError as e: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1396 | syncbuf.fail(self, e) |
| 1397 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1398 | else: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1399 | syncbuf.later1(self, _doff) |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 1400 | if submodules: |
| 1401 | syncbuf.later1(self, _dosubmodules) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1402 | |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 1403 | def AddCopyFile(self, src, dest, topdir): |
| 1404 | """Mark |src| for copying to |dest| (relative to |topdir|). |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1405 | |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 1406 | No filesystem changes occur here. Actual copying happens later on. |
| 1407 | |
| 1408 | Paths should have basic validation run on them before being queued. |
| 1409 | Further checking will be handled when the actual copy happens. |
| 1410 | """ |
| 1411 | self.copyfiles.append(_CopyFile(self.worktree, src, topdir, dest)) |
| 1412 | |
| 1413 | def AddLinkFile(self, src, dest, topdir): |
| 1414 | """Mark |dest| to create a symlink (relative to |topdir|) pointing to |src|. |
| 1415 | |
| 1416 | No filesystem changes occur here. Actual linking happens later on. |
| 1417 | |
| 1418 | Paths should have basic validation run on them before being queued. |
| 1419 | Further checking will be handled when the actual link happens. |
| 1420 | """ |
| 1421 | self.linkfiles.append(_LinkFile(self.worktree, src, topdir, dest)) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1422 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 1423 | def AddAnnotation(self, name, value, keep): |
| 1424 | self.annotations.append(_Annotation(name, value, keep)) |
| 1425 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 1426 | def DownloadPatchSet(self, change_id, patch_id): |
| 1427 | """Download a single patch set of a single change to FETCH_HEAD. |
| 1428 | """ |
| 1429 | remote = self.GetRemote(self.remote.name) |
| 1430 | |
| 1431 | cmd = ['fetch', remote.name] |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1432 | cmd.append('refs/changes/%2.2d/%d/%d' |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 1433 | % (change_id % 100, change_id, patch_id)) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 1434 | if GitCommand(self, cmd, bare=True).Wait() != 0: |
| 1435 | return None |
| 1436 | return DownloadedChange(self, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1437 | self.GetRevisionId(), |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 1438 | change_id, |
| 1439 | patch_id, |
| 1440 | self.bare_git.rev_parse('FETCH_HEAD')) |
| 1441 | |
Mike Frysinger | c0d1866 | 2020-02-19 19:19:18 -0500 | [diff] [blame] | 1442 | def DeleteWorktree(self, quiet=False, force=False): |
| 1443 | """Delete the source checkout and any other housekeeping tasks. |
| 1444 | |
| 1445 | This currently leaves behind the internal .repo/ cache state. This helps |
| 1446 | when switching branches or manifest changes get reverted as we don't have |
| 1447 | to redownload all the git objects. But we should do some GC at some point. |
| 1448 | |
| 1449 | Args: |
| 1450 | quiet: Whether to hide normal messages. |
| 1451 | force: Always delete tree even if dirty. |
| 1452 | |
| 1453 | Returns: |
| 1454 | True if the worktree was completely cleaned out. |
| 1455 | """ |
| 1456 | if self.IsDirty(): |
| 1457 | if force: |
| 1458 | print('warning: %s: Removing dirty project: uncommitted changes lost.' % |
| 1459 | (self.relpath,), file=sys.stderr) |
| 1460 | else: |
| 1461 | print('error: %s: Cannot remove project: uncommitted changes are ' |
| 1462 | 'present.\n' % (self.relpath,), file=sys.stderr) |
| 1463 | return False |
| 1464 | |
| 1465 | if not quiet: |
| 1466 | print('%s: Deleting obsolete checkout.' % (self.relpath,)) |
| 1467 | |
| 1468 | # Unlock and delink from the main worktree. We don't use git's worktree |
| 1469 | # remove because it will recursively delete projects -- we handle that |
| 1470 | # ourselves below. https://crbug.com/git/48 |
| 1471 | if self.use_git_worktrees: |
| 1472 | needle = platform_utils.realpath(self.gitdir) |
| 1473 | # Find the git worktree commondir under .repo/worktrees/. |
| 1474 | output = self.bare_git.worktree('list', '--porcelain').splitlines()[0] |
| 1475 | assert output.startswith('worktree '), output |
| 1476 | commondir = output[9:] |
| 1477 | # Walk each of the git worktrees to see where they point. |
| 1478 | configs = os.path.join(commondir, 'worktrees') |
| 1479 | for name in os.listdir(configs): |
| 1480 | gitdir = os.path.join(configs, name, 'gitdir') |
| 1481 | with open(gitdir) as fp: |
| 1482 | relpath = fp.read().strip() |
| 1483 | # Resolve the checkout path and see if it matches this project. |
| 1484 | fullpath = platform_utils.realpath(os.path.join(configs, name, relpath)) |
| 1485 | if fullpath == needle: |
| 1486 | platform_utils.rmtree(os.path.join(configs, name)) |
| 1487 | |
| 1488 | # Delete the .git directory first, so we're less likely to have a partially |
| 1489 | # working git repository around. There shouldn't be any git projects here, |
| 1490 | # so rmtree works. |
| 1491 | |
| 1492 | # Try to remove plain files first in case of git worktrees. If this fails |
| 1493 | # for any reason, we'll fall back to rmtree, and that'll display errors if |
| 1494 | # it can't remove things either. |
| 1495 | try: |
| 1496 | platform_utils.remove(self.gitdir) |
| 1497 | except OSError: |
| 1498 | pass |
| 1499 | try: |
| 1500 | platform_utils.rmtree(self.gitdir) |
| 1501 | except OSError as e: |
| 1502 | if e.errno != errno.ENOENT: |
| 1503 | print('error: %s: %s' % (self.gitdir, e), file=sys.stderr) |
| 1504 | print('error: %s: Failed to delete obsolete checkout; remove manually, ' |
| 1505 | 'then run `repo sync -l`.' % (self.relpath,), file=sys.stderr) |
| 1506 | return False |
| 1507 | |
| 1508 | # Delete everything under the worktree, except for directories that contain |
| 1509 | # another git project. |
| 1510 | dirs_to_remove = [] |
| 1511 | failed = False |
| 1512 | for root, dirs, files in platform_utils.walk(self.worktree): |
| 1513 | for f in files: |
| 1514 | path = os.path.join(root, f) |
| 1515 | try: |
| 1516 | platform_utils.remove(path) |
| 1517 | except OSError as e: |
| 1518 | if e.errno != errno.ENOENT: |
| 1519 | print('error: %s: Failed to remove: %s' % (path, e), file=sys.stderr) |
| 1520 | failed = True |
| 1521 | dirs[:] = [d for d in dirs |
| 1522 | if not os.path.lexists(os.path.join(root, d, '.git'))] |
| 1523 | dirs_to_remove += [os.path.join(root, d) for d in dirs |
| 1524 | if os.path.join(root, d) not in dirs_to_remove] |
| 1525 | for d in reversed(dirs_to_remove): |
| 1526 | if platform_utils.islink(d): |
| 1527 | try: |
| 1528 | platform_utils.remove(d) |
| 1529 | except OSError as e: |
| 1530 | if e.errno != errno.ENOENT: |
| 1531 | print('error: %s: Failed to remove: %s' % (d, e), file=sys.stderr) |
| 1532 | failed = True |
| 1533 | elif not platform_utils.listdir(d): |
| 1534 | try: |
| 1535 | platform_utils.rmdir(d) |
| 1536 | except OSError as e: |
| 1537 | if e.errno != errno.ENOENT: |
| 1538 | print('error: %s: Failed to remove: %s' % (d, e), file=sys.stderr) |
| 1539 | failed = True |
| 1540 | if failed: |
| 1541 | print('error: %s: Failed to delete obsolete checkout.' % (self.relpath,), |
| 1542 | file=sys.stderr) |
| 1543 | print(' Remove manually, then run `repo sync -l`.', file=sys.stderr) |
| 1544 | return False |
| 1545 | |
| 1546 | # Try deleting parent dirs if they are empty. |
| 1547 | path = self.worktree |
| 1548 | while path != self.manifest.topdir: |
| 1549 | try: |
| 1550 | platform_utils.rmdir(path) |
| 1551 | except OSError as e: |
| 1552 | if e.errno != errno.ENOENT: |
| 1553 | break |
| 1554 | path = os.path.dirname(path) |
| 1555 | |
| 1556 | return True |
| 1557 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1558 | # Branch Management ## |
Theodore Dubois | 60fdc5c | 2019-07-30 12:14:25 -0700 | [diff] [blame] | 1559 | def StartBranch(self, name, branch_merge='', revision=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1560 | """Create a new branch off the manifest's revision. |
| 1561 | """ |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1562 | if not branch_merge: |
| 1563 | branch_merge = self.revisionExpr |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1564 | head = self.work_git.GetHead() |
| 1565 | if head == (R_HEADS + name): |
| 1566 | return True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1567 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1568 | all_refs = self.bare_ref.all |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1569 | if R_HEADS + name in all_refs: |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1570 | return GitCommand(self, |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1571 | ['checkout', name, '--'], |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1572 | capture_stdout=True, |
| 1573 | capture_stderr=True).Wait() == 0 |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 1574 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1575 | branch = self.GetBranch(name) |
| 1576 | branch.remote = self.GetRemote(self.remote.name) |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1577 | branch.merge = branch_merge |
| 1578 | if not branch.merge.startswith('refs/') and not ID_RE.match(branch_merge): |
| 1579 | branch.merge = R_HEADS + branch_merge |
Theodore Dubois | 60fdc5c | 2019-07-30 12:14:25 -0700 | [diff] [blame] | 1580 | |
| 1581 | if revision is None: |
| 1582 | revid = self.GetRevisionId(all_refs) |
| 1583 | else: |
| 1584 | revid = self.work_git.rev_parse(revision) |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 1585 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1586 | if head.startswith(R_HEADS): |
| 1587 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1588 | head = all_refs[head] |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1589 | except KeyError: |
| 1590 | head = None |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1591 | if revid and head and revid == head: |
Mike Frysinger | 746e7f6 | 2020-02-19 15:49:02 -0500 | [diff] [blame] | 1592 | ref = R_HEADS + name |
| 1593 | self.work_git.update_ref(ref, revid) |
| 1594 | self.work_git.symbolic_ref(HEAD, ref) |
| 1595 | branch.Save() |
| 1596 | return True |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1597 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1598 | if GitCommand(self, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1599 | ['checkout', '-b', branch.name, revid], |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1600 | capture_stdout=True, |
| 1601 | capture_stderr=True).Wait() == 0: |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1602 | branch.Save() |
| 1603 | return True |
| 1604 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1605 | |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1606 | def CheckoutBranch(self, name): |
| 1607 | """Checkout a local topic branch. |
Doug Anderson | 3ba5f95 | 2011-04-07 12:51:04 -0700 | [diff] [blame] | 1608 | |
| 1609 | Args: |
| 1610 | name: The name of the branch to checkout. |
| 1611 | |
| 1612 | Returns: |
| 1613 | True if the checkout succeeded; False if it didn't; None if the branch |
| 1614 | didn't exist. |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1615 | """ |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1616 | rev = R_HEADS + name |
| 1617 | head = self.work_git.GetHead() |
| 1618 | if head == rev: |
| 1619 | # Already on the branch |
| 1620 | # |
| 1621 | return True |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1622 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1623 | all_refs = self.bare_ref.all |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1624 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1625 | revid = all_refs[rev] |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1626 | except KeyError: |
| 1627 | # Branch does not exist in this project |
| 1628 | # |
Doug Anderson | 3ba5f95 | 2011-04-07 12:51:04 -0700 | [diff] [blame] | 1629 | return None |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1630 | |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1631 | if head.startswith(R_HEADS): |
| 1632 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1633 | head = all_refs[head] |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1634 | except KeyError: |
| 1635 | head = None |
| 1636 | |
| 1637 | if head == revid: |
| 1638 | # Same revision; just update HEAD to point to the new |
| 1639 | # target branch, but otherwise take no other action. |
| 1640 | # |
Mike Frysinger | 9f91c43 | 2020-02-23 23:24:40 -0500 | [diff] [blame] | 1641 | _lwrite(self.work_git.GetDotgitPath(subpath=HEAD), |
| 1642 | 'ref: %s%s\n' % (R_HEADS, name)) |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1643 | return True |
| 1644 | |
| 1645 | return GitCommand(self, |
| 1646 | ['checkout', name, '--'], |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1647 | capture_stdout=True, |
| 1648 | capture_stderr=True).Wait() == 0 |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1649 | |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1650 | def AbandonBranch(self, name): |
| 1651 | """Destroy a local topic branch. |
Doug Anderson | dafb1d6 | 2011-04-07 11:46:59 -0700 | [diff] [blame] | 1652 | |
| 1653 | Args: |
| 1654 | name: The name of the branch to abandon. |
| 1655 | |
| 1656 | Returns: |
| 1657 | True if the abandon succeeded; False if it didn't; None if the branch |
| 1658 | didn't exist. |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1659 | """ |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 1660 | rev = R_HEADS + name |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1661 | all_refs = self.bare_ref.all |
| 1662 | if rev not in all_refs: |
Doug Anderson | dafb1d6 | 2011-04-07 11:46:59 -0700 | [diff] [blame] | 1663 | # Doesn't exist |
| 1664 | return None |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1665 | |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 1666 | head = self.work_git.GetHead() |
| 1667 | if head == rev: |
| 1668 | # We can't destroy the branch while we are sitting |
| 1669 | # on it. Switch to a detached HEAD. |
| 1670 | # |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1671 | head = all_refs[head] |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1672 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1673 | revid = self.GetRevisionId(all_refs) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1674 | if head == revid: |
Mike Frysinger | 9f91c43 | 2020-02-23 23:24:40 -0500 | [diff] [blame] | 1675 | _lwrite(self.work_git.GetDotgitPath(subpath=HEAD), '%s\n' % revid) |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 1676 | else: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1677 | self._Checkout(revid, quiet=True) |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 1678 | |
| 1679 | return GitCommand(self, |
| 1680 | ['branch', '-D', name], |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1681 | capture_stdout=True, |
| 1682 | capture_stderr=True).Wait() == 0 |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1683 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1684 | def PruneHeads(self): |
| 1685 | """Prune any topic branches already merged into upstream. |
| 1686 | """ |
| 1687 | cb = self.CurrentBranch |
| 1688 | kill = [] |
Shawn O. Pearce | 3778f9d | 2009-03-02 12:30:50 -0800 | [diff] [blame] | 1689 | left = self._allrefs |
| 1690 | for name in left.keys(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1691 | if name.startswith(R_HEADS): |
| 1692 | name = name[len(R_HEADS):] |
| 1693 | if cb is None or name != cb: |
| 1694 | kill.append(name) |
| 1695 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1696 | rev = self.GetRevisionId(left) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1697 | if cb is not None \ |
| 1698 | and not self._revlist(HEAD + '...' + rev) \ |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1699 | and not self.IsDirty(consider_untracked=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1700 | self.work_git.DetachHead(HEAD) |
| 1701 | kill.append(cb) |
| 1702 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1703 | if kill: |
Shawn O. Pearce | 5b23f24 | 2009-04-17 18:43:33 -0700 | [diff] [blame] | 1704 | old = self.bare_git.GetHead() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1705 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1706 | try: |
| 1707 | self.bare_git.DetachHead(rev) |
| 1708 | |
| 1709 | b = ['branch', '-d'] |
| 1710 | b.extend(kill) |
| 1711 | b = GitCommand(self, b, bare=True, |
| 1712 | capture_stdout=True, |
| 1713 | capture_stderr=True) |
| 1714 | b.Wait() |
| 1715 | finally: |
Dan Willemsen | 1a799d1 | 2015-12-15 13:40:05 -0800 | [diff] [blame] | 1716 | if ID_RE.match(old): |
| 1717 | self.bare_git.DetachHead(old) |
| 1718 | else: |
| 1719 | self.bare_git.SetHead(old) |
Shawn O. Pearce | 3778f9d | 2009-03-02 12:30:50 -0800 | [diff] [blame] | 1720 | left = self._allrefs |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1721 | |
Shawn O. Pearce | 3778f9d | 2009-03-02 12:30:50 -0800 | [diff] [blame] | 1722 | for branch in kill: |
| 1723 | if (R_HEADS + branch) not in left: |
| 1724 | self.CleanPublishedCache() |
| 1725 | break |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1726 | |
| 1727 | if cb and cb not in kill: |
| 1728 | kill.append(cb) |
Shawn O. Pearce | 7c6c64d | 2009-03-02 12:38:13 -0800 | [diff] [blame] | 1729 | kill.sort() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1730 | |
| 1731 | kept = [] |
| 1732 | for branch in kill: |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1733 | if R_HEADS + branch in left: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1734 | branch = self.GetBranch(branch) |
| 1735 | base = branch.LocalMerge |
| 1736 | if not base: |
| 1737 | base = rev |
| 1738 | kept.append(ReviewableBranch(self, branch, base)) |
| 1739 | return kept |
| 1740 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1741 | # Submodule Management ## |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1742 | def GetRegisteredSubprojects(self): |
| 1743 | result = [] |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1744 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1745 | def rec(subprojects): |
| 1746 | if not subprojects: |
| 1747 | return |
| 1748 | result.extend(subprojects) |
| 1749 | for p in subprojects: |
| 1750 | rec(p.subprojects) |
| 1751 | rec(self.subprojects) |
| 1752 | return result |
| 1753 | |
| 1754 | def _GetSubmodules(self): |
| 1755 | # Unfortunately we cannot call `git submodule status --recursive` here |
| 1756 | # because the working tree might not exist yet, and it cannot be used |
| 1757 | # without a working tree in its current implementation. |
| 1758 | |
| 1759 | def get_submodules(gitdir, rev): |
| 1760 | # Parse .gitmodules for submodule sub_paths and sub_urls |
| 1761 | sub_paths, sub_urls = parse_gitmodules(gitdir, rev) |
| 1762 | if not sub_paths: |
| 1763 | return [] |
| 1764 | # Run `git ls-tree` to read SHAs of submodule object, which happen to be |
| 1765 | # revision of submodule repository |
| 1766 | sub_revs = git_ls_tree(gitdir, rev, sub_paths) |
| 1767 | submodules = [] |
| 1768 | for sub_path, sub_url in zip(sub_paths, sub_urls): |
| 1769 | try: |
| 1770 | sub_rev = sub_revs[sub_path] |
| 1771 | except KeyError: |
| 1772 | # Ignore non-exist submodules |
| 1773 | continue |
| 1774 | submodules.append((sub_rev, sub_path, sub_url)) |
| 1775 | return submodules |
| 1776 | |
Sebastian Schuberth | 41a2683 | 2019-03-11 13:53:38 +0100 | [diff] [blame] | 1777 | re_path = re.compile(r'^submodule\.(.+)\.path=(.*)$') |
| 1778 | re_url = re.compile(r'^submodule\.(.+)\.url=(.*)$') |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1779 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1780 | def parse_gitmodules(gitdir, rev): |
| 1781 | cmd = ['cat-file', 'blob', '%s:.gitmodules' % rev] |
| 1782 | try: |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1783 | p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True, |
| 1784 | bare=True, gitdir=gitdir) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1785 | except GitError: |
| 1786 | return [], [] |
| 1787 | if p.Wait() != 0: |
| 1788 | return [], [] |
| 1789 | |
| 1790 | gitmodules_lines = [] |
| 1791 | fd, temp_gitmodules_path = tempfile.mkstemp() |
| 1792 | try: |
Mike Frysinger | 163d42e | 2020-02-11 03:35:24 -0500 | [diff] [blame] | 1793 | os.write(fd, p.stdout.encode('utf-8')) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1794 | os.close(fd) |
| 1795 | cmd = ['config', '--file', temp_gitmodules_path, '--list'] |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1796 | p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True, |
| 1797 | bare=True, gitdir=gitdir) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1798 | if p.Wait() != 0: |
| 1799 | return [], [] |
| 1800 | gitmodules_lines = p.stdout.split('\n') |
| 1801 | except GitError: |
| 1802 | return [], [] |
| 1803 | finally: |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 1804 | platform_utils.remove(temp_gitmodules_path) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1805 | |
| 1806 | names = set() |
| 1807 | paths = {} |
| 1808 | urls = {} |
| 1809 | for line in gitmodules_lines: |
| 1810 | if not line: |
| 1811 | continue |
| 1812 | m = re_path.match(line) |
| 1813 | if m: |
| 1814 | names.add(m.group(1)) |
| 1815 | paths[m.group(1)] = m.group(2) |
| 1816 | continue |
| 1817 | m = re_url.match(line) |
| 1818 | if m: |
| 1819 | names.add(m.group(1)) |
| 1820 | urls[m.group(1)] = m.group(2) |
| 1821 | continue |
| 1822 | names = sorted(names) |
| 1823 | return ([paths.get(name, '') for name in names], |
| 1824 | [urls.get(name, '') for name in names]) |
| 1825 | |
| 1826 | def git_ls_tree(gitdir, rev, paths): |
| 1827 | cmd = ['ls-tree', rev, '--'] |
| 1828 | cmd.extend(paths) |
| 1829 | try: |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1830 | p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True, |
| 1831 | bare=True, gitdir=gitdir) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1832 | except GitError: |
| 1833 | return [] |
| 1834 | if p.Wait() != 0: |
| 1835 | return [] |
| 1836 | objects = {} |
| 1837 | for line in p.stdout.split('\n'): |
| 1838 | if not line.strip(): |
| 1839 | continue |
| 1840 | object_rev, object_path = line.split()[2:4] |
| 1841 | objects[object_path] = object_rev |
| 1842 | return objects |
| 1843 | |
| 1844 | try: |
| 1845 | rev = self.GetRevisionId() |
| 1846 | except GitError: |
| 1847 | return [] |
| 1848 | return get_submodules(self.gitdir, rev) |
| 1849 | |
| 1850 | def GetDerivedSubprojects(self): |
| 1851 | result = [] |
| 1852 | if not self.Exists: |
| 1853 | # If git repo does not exist yet, querying its submodules will |
| 1854 | # mess up its states; so return here. |
| 1855 | return result |
| 1856 | for rev, path, url in self._GetSubmodules(): |
| 1857 | name = self.manifest.GetSubprojectName(self, path) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1858 | relpath, worktree, gitdir, objdir = \ |
| 1859 | self.manifest.GetSubprojectPaths(self, name, path) |
| 1860 | project = self.manifest.paths.get(relpath) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1861 | if project: |
| 1862 | result.extend(project.GetDerivedSubprojects()) |
| 1863 | continue |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1864 | |
Shouheng Zhang | 02c0ee6 | 2017-12-08 09:54:58 +0800 | [diff] [blame] | 1865 | if url.startswith('..'): |
| 1866 | url = urllib.parse.urljoin("%s/" % self.remote.url, url) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1867 | remote = RemoteSpec(self.remote.name, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1868 | url=url, |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 1869 | pushUrl=self.remote.pushUrl, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1870 | review=self.remote.review, |
| 1871 | revision=self.remote.revision) |
| 1872 | subproject = Project(manifest=self.manifest, |
| 1873 | name=name, |
| 1874 | remote=remote, |
| 1875 | gitdir=gitdir, |
| 1876 | objdir=objdir, |
| 1877 | worktree=worktree, |
| 1878 | relpath=relpath, |
Aymen Bouaziz | 2598ed0 | 2016-06-24 14:34:08 +0200 | [diff] [blame] | 1879 | revisionExpr=rev, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1880 | revisionId=rev, |
| 1881 | rebase=self.rebase, |
| 1882 | groups=self.groups, |
| 1883 | sync_c=self.sync_c, |
| 1884 | sync_s=self.sync_s, |
YOUNG HO CHA | a32c92c | 2018-02-14 16:57:31 +0900 | [diff] [blame] | 1885 | sync_tags=self.sync_tags, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 1886 | parent=self, |
| 1887 | is_derived=True) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1888 | result.append(subproject) |
| 1889 | result.extend(subproject.GetDerivedSubprojects()) |
| 1890 | return result |
| 1891 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1892 | # Direct Git Commands ## |
Mike Frysinger | f81c72e | 2020-02-19 15:50:00 -0500 | [diff] [blame] | 1893 | def EnableRepositoryExtension(self, key, value='true', version=1): |
| 1894 | """Enable git repository extension |key| with |value|. |
| 1895 | |
| 1896 | Args: |
| 1897 | key: The extension to enabled. Omit the "extensions." prefix. |
| 1898 | value: The value to use for the extension. |
| 1899 | version: The minimum git repository version needed. |
| 1900 | """ |
| 1901 | # Make sure the git repo version is new enough already. |
| 1902 | found_version = self.config.GetInt('core.repositoryFormatVersion') |
| 1903 | if found_version is None: |
| 1904 | found_version = 0 |
| 1905 | if found_version < version: |
| 1906 | self.config.SetString('core.repositoryFormatVersion', str(version)) |
| 1907 | |
| 1908 | # Enable the extension! |
| 1909 | self.config.SetString('extensions.%s' % (key,), value) |
| 1910 | |
Mike Frysinger | 50a81de | 2020-09-06 15:51:21 -0400 | [diff] [blame] | 1911 | def ResolveRemoteHead(self, name=None): |
| 1912 | """Find out what the default branch (HEAD) points to. |
| 1913 | |
| 1914 | Normally this points to refs/heads/master, but projects are moving to main. |
| 1915 | Support whatever the server uses rather than hardcoding "master" ourselves. |
| 1916 | """ |
| 1917 | if name is None: |
| 1918 | name = self.remote.name |
| 1919 | |
| 1920 | # The output will look like (NB: tabs are separators): |
| 1921 | # ref: refs/heads/master HEAD |
| 1922 | # 5f6803b100bb3cd0f534e96e88c91373e8ed1c44 HEAD |
| 1923 | output = self.bare_git.ls_remote('-q', '--symref', '--exit-code', name, 'HEAD') |
| 1924 | |
| 1925 | for line in output.splitlines(): |
| 1926 | lhs, rhs = line.split('\t', 1) |
| 1927 | if rhs == 'HEAD' and lhs.startswith('ref:'): |
| 1928 | return lhs[4:].strip() |
| 1929 | |
| 1930 | return None |
| 1931 | |
Zac Livingston | e433226 | 2017-06-16 08:56:09 -0600 | [diff] [blame] | 1932 | def _CheckForImmutableRevision(self): |
Chris AtLee | 2fb6466 | 2014-01-16 21:32:33 -0500 | [diff] [blame] | 1933 | try: |
| 1934 | # if revision (sha or tag) is not present then following function |
| 1935 | # throws an error. |
| 1936 | self.bare_git.rev_parse('--verify', '%s^0' % self.revisionExpr) |
| 1937 | return True |
| 1938 | except GitError: |
| 1939 | # There is no such persistent revision. We have to fetch it. |
| 1940 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1941 | |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1942 | def _FetchArchive(self, tarpath, cwd=None): |
| 1943 | cmd = ['archive', '-v', '-o', tarpath] |
| 1944 | cmd.append('--remote=%s' % self.remote.url) |
| 1945 | cmd.append('--prefix=%s/' % self.relpath) |
| 1946 | cmd.append(self.revisionExpr) |
| 1947 | |
| 1948 | command = GitCommand(self, cmd, cwd=cwd, |
| 1949 | capture_stdout=True, |
| 1950 | capture_stderr=True) |
| 1951 | |
| 1952 | if command.Wait() != 0: |
| 1953 | raise GitError('git archive %s: %s' % (self.name, command.stderr)) |
| 1954 | |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1955 | def _RemoteFetch(self, name=None, |
| 1956 | current_branch_only=False, |
Shawn O. Pearce | 16614f8 | 2010-10-29 12:05:43 -0700 | [diff] [blame] | 1957 | initial=False, |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1958 | quiet=False, |
Mike Frysinger | 521d01b | 2020-02-17 01:51:49 -0500 | [diff] [blame] | 1959 | verbose=False, |
Mitchel Humpherys | 597868b | 2012-10-29 10:18:34 -0700 | [diff] [blame] | 1960 | alt_dir=None, |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 1961 | tags=True, |
Aymen Bouaziz | 6c59446 | 2016-10-25 18:03:51 +0200 | [diff] [blame] | 1962 | prune=False, |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 1963 | depth=None, |
Mike Frysinger | e57f114 | 2019-03-18 21:27:54 -0400 | [diff] [blame] | 1964 | submodules=False, |
Xin Li | 745be2e | 2019-06-03 11:24:30 -0700 | [diff] [blame] | 1965 | force_sync=False, |
George Engelbrecht | 9bc283e | 2020-04-02 12:36:09 -0600 | [diff] [blame] | 1966 | clone_filter=None, |
| 1967 | retry_fetches=2, |
| 1968 | retry_sleep_initial_sec=4.0, |
| 1969 | retry_exp_factor=2.0): |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1970 | is_sha1 = False |
| 1971 | tag_name = None |
David Pursehouse | 9bc422f | 2014-04-15 10:28:56 +0900 | [diff] [blame] | 1972 | # The depth should not be used when fetching to a mirror because |
| 1973 | # it will result in a shallow repository that cannot be cloned or |
| 1974 | # fetched from. |
Aymen Bouaziz | 6c59446 | 2016-10-25 18:03:51 +0200 | [diff] [blame] | 1975 | # The repo project should also never be synced with partial depth. |
| 1976 | if self.manifest.IsMirror or self.relpath == '.repo/repo': |
| 1977 | depth = None |
David Pursehouse | 9bc422f | 2014-04-15 10:28:56 +0900 | [diff] [blame] | 1978 | |
Shawn Pearce | 69e04d8 | 2014-01-29 12:48:54 -0800 | [diff] [blame] | 1979 | if depth: |
| 1980 | current_branch_only = True |
| 1981 | |
Nasser Grainawi | 909d58b | 2014-09-19 12:13:04 -0600 | [diff] [blame] | 1982 | if ID_RE.match(self.revisionExpr) is not None: |
| 1983 | is_sha1 = True |
| 1984 | |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1985 | if current_branch_only: |
Nasser Grainawi | 909d58b | 2014-09-19 12:13:04 -0600 | [diff] [blame] | 1986 | if self.revisionExpr.startswith(R_TAGS): |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1987 | # this is a tag and its sha1 value should never change |
| 1988 | tag_name = self.revisionExpr[len(R_TAGS):] |
| 1989 | |
| 1990 | if is_sha1 or tag_name is not None: |
Zac Livingston | e433226 | 2017-06-16 08:56:09 -0600 | [diff] [blame] | 1991 | if self._CheckForImmutableRevision(): |
Mike Frysinger | 521d01b | 2020-02-17 01:51:49 -0500 | [diff] [blame] | 1992 | if verbose: |
Tim Schumacher | 1f1596b | 2019-04-15 11:17:08 +0200 | [diff] [blame] | 1993 | print('Skipped fetching project %s (already have persistent ref)' |
| 1994 | % self.name) |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1995 | return True |
Bertrand SIMONNET | 3000cda | 2014-11-25 16:19:29 -0800 | [diff] [blame] | 1996 | if is_sha1 and not depth: |
| 1997 | # When syncing a specific commit and --depth is not set: |
| 1998 | # * if upstream is explicitly specified and is not a sha1, fetch only |
| 1999 | # upstream as users expect only upstream to be fetch. |
| 2000 | # Note: The commit might not be in upstream in which case the sync |
| 2001 | # will fail. |
| 2002 | # * otherwise, fetch all branches to make sure we end up with the |
| 2003 | # specific commit. |
Aymen Bouaziz | 037040f | 2016-06-28 12:27:23 +0200 | [diff] [blame] | 2004 | if self.upstream: |
| 2005 | current_branch_only = not ID_RE.match(self.upstream) |
| 2006 | else: |
| 2007 | current_branch_only = False |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 2008 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2009 | if not name: |
| 2010 | name = self.remote.name |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 2011 | |
| 2012 | ssh_proxy = False |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 2013 | remote = self.GetRemote(name) |
| 2014 | if remote.PreConnectFetch(): |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 2015 | ssh_proxy = True |
| 2016 | |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2017 | if initial: |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2018 | if alt_dir and 'objects' == os.path.basename(alt_dir): |
| 2019 | ref_dir = os.path.dirname(alt_dir) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2020 | packed_refs = os.path.join(self.gitdir, 'packed-refs') |
| 2021 | remote = self.GetRemote(name) |
| 2022 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2023 | all_refs = self.bare_ref.all |
| 2024 | ids = set(all_refs.values()) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2025 | tmp = set() |
| 2026 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 2027 | for r, ref_id in GitRefs(ref_dir).all.items(): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2028 | if r not in all_refs: |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2029 | if r.startswith(R_TAGS) or remote.WritesTo(r): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2030 | all_refs[r] = ref_id |
| 2031 | ids.add(ref_id) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2032 | continue |
| 2033 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2034 | if ref_id in ids: |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2035 | continue |
| 2036 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2037 | r = 'refs/_alt/%s' % ref_id |
| 2038 | all_refs[r] = ref_id |
| 2039 | ids.add(ref_id) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2040 | tmp.add(r) |
| 2041 | |
heping | 3d7bbc9 | 2017-04-12 19:51:47 +0800 | [diff] [blame] | 2042 | tmp_packed_lines = [] |
| 2043 | old_packed_lines = [] |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2044 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 2045 | for r in sorted(all_refs): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2046 | line = '%s %s\n' % (all_refs[r], r) |
heping | 3d7bbc9 | 2017-04-12 19:51:47 +0800 | [diff] [blame] | 2047 | tmp_packed_lines.append(line) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2048 | if r not in tmp: |
heping | 3d7bbc9 | 2017-04-12 19:51:47 +0800 | [diff] [blame] | 2049 | old_packed_lines.append(line) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2050 | |
heping | 3d7bbc9 | 2017-04-12 19:51:47 +0800 | [diff] [blame] | 2051 | tmp_packed = ''.join(tmp_packed_lines) |
| 2052 | old_packed = ''.join(old_packed_lines) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2053 | _lwrite(packed_refs, tmp_packed) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2054 | else: |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2055 | alt_dir = None |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2056 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 2057 | cmd = ['fetch'] |
Doug Anderson | 30d4529 | 2011-05-04 15:01:04 -0700 | [diff] [blame] | 2058 | |
Xin Li | 745be2e | 2019-06-03 11:24:30 -0700 | [diff] [blame] | 2059 | if clone_filter: |
| 2060 | git_require((2, 19, 0), fail=True, msg='partial clones') |
| 2061 | cmd.append('--filter=%s' % clone_filter) |
Mike Frysinger | f81c72e | 2020-02-19 15:50:00 -0500 | [diff] [blame] | 2062 | self.EnableRepositoryExtension('partialclone', self.remote.name) |
Xin Li | 745be2e | 2019-06-03 11:24:30 -0700 | [diff] [blame] | 2063 | |
Conley Owens | f97e838 | 2015-01-21 11:12:46 -0800 | [diff] [blame] | 2064 | if depth: |
Doug Anderson | 30d4529 | 2011-05-04 15:01:04 -0700 | [diff] [blame] | 2065 | cmd.append('--depth=%s' % depth) |
Dan Willemsen | eeab686 | 2015-08-03 13:11:53 -0700 | [diff] [blame] | 2066 | else: |
| 2067 | # If this repo has shallow objects, then we don't know which refs have |
| 2068 | # shallow objects or not. Tell git to unshallow all fetched refs. Don't |
| 2069 | # do this with projects that don't have shallow objects, since it is less |
| 2070 | # efficient. |
| 2071 | if os.path.exists(os.path.join(self.gitdir, 'shallow')): |
| 2072 | cmd.append('--depth=2147483647') |
Doug Anderson | 30d4529 | 2011-05-04 15:01:04 -0700 | [diff] [blame] | 2073 | |
Mike Frysinger | 4847e05 | 2020-02-22 00:07:35 -0500 | [diff] [blame] | 2074 | if not verbose: |
Shawn O. Pearce | 16614f8 | 2010-10-29 12:05:43 -0700 | [diff] [blame] | 2075 | cmd.append('--quiet') |
Mike Frysinger | 4847e05 | 2020-02-22 00:07:35 -0500 | [diff] [blame] | 2076 | if not quiet and sys.stdout.isatty(): |
| 2077 | cmd.append('--progress') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 2078 | if not self.worktree: |
| 2079 | cmd.append('--update-head-ok') |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2080 | cmd.append(name) |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 2081 | |
Mike Frysinger | e57f114 | 2019-03-18 21:27:54 -0400 | [diff] [blame] | 2082 | if force_sync: |
| 2083 | cmd.append('--force') |
| 2084 | |
David Pursehouse | 74cfd27 | 2015-10-14 10:50:15 +0900 | [diff] [blame] | 2085 | if prune: |
| 2086 | cmd.append('--prune') |
| 2087 | |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 2088 | if submodules: |
| 2089 | cmd.append('--recurse-submodules=on-demand') |
| 2090 | |
Kuang-che Wu | 6856f98 | 2019-11-25 12:37:55 +0800 | [diff] [blame] | 2091 | spec = [] |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 2092 | if not current_branch_only: |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 2093 | # Fetch whole repo |
Conley Owens | 80b87fe | 2014-05-09 17:13:44 -0700 | [diff] [blame] | 2094 | spec.append(str((u'+refs/heads/*:') + remote.ToLocal('refs/heads/*'))) |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 2095 | elif tag_name is not None: |
Conley Owens | 80b87fe | 2014-05-09 17:13:44 -0700 | [diff] [blame] | 2096 | spec.append('tag') |
| 2097 | spec.append(tag_name) |
Nasser Grainawi | 04e52d6 | 2014-09-30 13:34:52 -0600 | [diff] [blame] | 2098 | |
Chirayu Desai | f7b64e3 | 2020-02-04 17:50:57 +0530 | [diff] [blame] | 2099 | if self.manifest.IsMirror and not current_branch_only: |
| 2100 | branch = None |
| 2101 | else: |
| 2102 | branch = self.revisionExpr |
Kuang-che Wu | 6856f98 | 2019-11-25 12:37:55 +0800 | [diff] [blame] | 2103 | if (not self.manifest.IsMirror and is_sha1 and depth |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 2104 | and git_require((1, 8, 3))): |
Kuang-che Wu | 6856f98 | 2019-11-25 12:37:55 +0800 | [diff] [blame] | 2105 | # Shallow checkout of a specific commit, fetch from that commit and not |
| 2106 | # the heads only as the commit might be deeper in the history. |
| 2107 | spec.append(branch) |
| 2108 | else: |
| 2109 | if is_sha1: |
| 2110 | branch = self.upstream |
| 2111 | if branch is not None and branch.strip(): |
| 2112 | if not branch.startswith('refs/'): |
| 2113 | branch = R_HEADS + branch |
| 2114 | spec.append(str((u'+%s:' % branch) + remote.ToLocal(branch))) |
| 2115 | |
| 2116 | # If mirroring repo and we cannot deduce the tag or branch to fetch, fetch |
| 2117 | # whole repo. |
| 2118 | if self.manifest.IsMirror and not spec: |
| 2119 | spec.append(str((u'+refs/heads/*:') + remote.ToLocal('refs/heads/*'))) |
| 2120 | |
| 2121 | # If using depth then we should not get all the tags since they may |
| 2122 | # be outside of the depth. |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 2123 | if not tags or depth: |
Kuang-che Wu | 6856f98 | 2019-11-25 12:37:55 +0800 | [diff] [blame] | 2124 | cmd.append('--no-tags') |
| 2125 | else: |
| 2126 | cmd.append('--tags') |
| 2127 | spec.append(str((u'+refs/tags/*:') + remote.ToLocal('refs/tags/*'))) |
| 2128 | |
Conley Owens | 80b87fe | 2014-05-09 17:13:44 -0700 | [diff] [blame] | 2129 | cmd.extend(spec) |
| 2130 | |
George Engelbrecht | 9bc283e | 2020-04-02 12:36:09 -0600 | [diff] [blame] | 2131 | # At least one retry minimum due to git remote prune. |
| 2132 | retry_fetches = max(retry_fetches, 2) |
| 2133 | retry_cur_sleep = retry_sleep_initial_sec |
| 2134 | ok = prune_tried = False |
| 2135 | for try_n in range(retry_fetches): |
Mike Frysinger | 31990f0 | 2020-02-17 01:35:18 -0500 | [diff] [blame] | 2136 | gitcmd = GitCommand(self, cmd, bare=True, ssh_proxy=ssh_proxy, |
Mike Frysinger | 4847e05 | 2020-02-22 00:07:35 -0500 | [diff] [blame] | 2137 | merge_output=True, capture_stdout=quiet) |
John L. Villalovos | 126e298 | 2015-01-29 21:58:12 -0800 | [diff] [blame] | 2138 | ret = gitcmd.Wait() |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 2139 | if ret == 0: |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2140 | ok = True |
| 2141 | break |
George Engelbrecht | 9bc283e | 2020-04-02 12:36:09 -0600 | [diff] [blame] | 2142 | |
| 2143 | # Retry later due to HTTP 429 Too Many Requests. |
| 2144 | elif ('error:' in gitcmd.stderr and |
| 2145 | 'HTTP 429' in gitcmd.stderr): |
| 2146 | if not quiet: |
| 2147 | print('429 received, sleeping: %s sec' % retry_cur_sleep, |
| 2148 | file=sys.stderr) |
| 2149 | time.sleep(retry_cur_sleep) |
| 2150 | retry_cur_sleep = min(retry_exp_factor * retry_cur_sleep, |
| 2151 | MAXIMUM_RETRY_SLEEP_SEC) |
| 2152 | retry_cur_sleep *= (1 - random.uniform(-RETRY_JITTER_PERCENT, |
| 2153 | RETRY_JITTER_PERCENT)) |
| 2154 | continue |
| 2155 | |
| 2156 | # If this is not last attempt, try 'git remote prune'. |
| 2157 | elif (try_n < retry_fetches - 1 and |
| 2158 | 'error:' in gitcmd.stderr and |
| 2159 | 'git remote prune' in gitcmd.stderr and |
| 2160 | not prune_tried): |
| 2161 | prune_tried = True |
John L. Villalovos | 126e298 | 2015-01-29 21:58:12 -0800 | [diff] [blame] | 2162 | prunecmd = GitCommand(self, ['remote', 'prune', name], bare=True, |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 2163 | ssh_proxy=ssh_proxy) |
John L. Villalovos | e30f46b | 2015-02-25 14:27:02 -0800 | [diff] [blame] | 2164 | ret = prunecmd.Wait() |
John L. Villalovos | e30f46b | 2015-02-25 14:27:02 -0800 | [diff] [blame] | 2165 | if ret: |
John L. Villalovos | 126e298 | 2015-01-29 21:58:12 -0800 | [diff] [blame] | 2166 | break |
| 2167 | continue |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 2168 | elif current_branch_only and is_sha1 and ret == 128: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2169 | # Exit code 128 means "couldn't find the ref you asked for"; if we're |
| 2170 | # in sha1 mode, we just tried sync'ing from the upstream field; it |
| 2171 | # doesn't exist, thus abort the optimization attempt and do a full sync. |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 2172 | break |
Colin Cross | c4b301f | 2015-05-13 00:10:02 -0700 | [diff] [blame] | 2173 | elif ret < 0: |
| 2174 | # Git died with a signal, exit immediately |
| 2175 | break |
Mike Frysinger | 31990f0 | 2020-02-17 01:35:18 -0500 | [diff] [blame] | 2176 | if not verbose: |
| 2177 | print('%s:\n%s' % (self.name, gitcmd.stdout), file=sys.stderr) |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2178 | time.sleep(random.randint(30, 45)) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2179 | |
| 2180 | if initial: |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2181 | if alt_dir: |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2182 | if old_packed != '': |
| 2183 | _lwrite(packed_refs, old_packed) |
| 2184 | else: |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 2185 | platform_utils.remove(packed_refs) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2186 | self.bare_git.pack_refs('--all', '--prune') |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 2187 | |
Aymen Bouaziz | 6c59446 | 2016-10-25 18:03:51 +0200 | [diff] [blame] | 2188 | if is_sha1 and current_branch_only: |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 2189 | # We just synced the upstream given branch; verify we |
| 2190 | # got what we wanted, else trigger a second run of all |
| 2191 | # refs. |
Zac Livingston | e433226 | 2017-06-16 08:56:09 -0600 | [diff] [blame] | 2192 | if not self._CheckForImmutableRevision(): |
Mike Frysinger | 521d01b | 2020-02-17 01:51:49 -0500 | [diff] [blame] | 2193 | # Sync the current branch only with depth set to None. |
| 2194 | # We always pass depth=None down to avoid infinite recursion. |
| 2195 | return self._RemoteFetch( |
| 2196 | name=name, quiet=quiet, verbose=verbose, |
| 2197 | current_branch_only=current_branch_only and depth, |
| 2198 | initial=False, alt_dir=alt_dir, |
| 2199 | depth=None, clone_filter=clone_filter) |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 2200 | |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2201 | return ok |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2202 | |
Mike Frysinger | e50b6a7 | 2020-02-19 01:45:48 -0500 | [diff] [blame] | 2203 | def _ApplyCloneBundle(self, initial=False, quiet=False, verbose=False): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2204 | if initial and \ |
| 2205 | (self.manifest.manifestProject.config.GetString('repo.depth') or |
| 2206 | self.clone_depth): |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2207 | return False |
| 2208 | |
| 2209 | remote = self.GetRemote(self.remote.name) |
| 2210 | bundle_url = remote.url + '/clone.bundle' |
| 2211 | bundle_url = GitConfig.ForUser().UrlInsteadOf(bundle_url) |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2212 | if GetSchemeFromUrl(bundle_url) not in ('http', 'https', |
| 2213 | 'persistent-http', |
| 2214 | 'persistent-https'): |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2215 | return False |
| 2216 | |
| 2217 | bundle_dst = os.path.join(self.gitdir, 'clone.bundle') |
| 2218 | bundle_tmp = os.path.join(self.gitdir, 'clone.bundle.tmp') |
| 2219 | |
| 2220 | exist_dst = os.path.exists(bundle_dst) |
| 2221 | exist_tmp = os.path.exists(bundle_tmp) |
| 2222 | |
| 2223 | if not initial and not exist_dst and not exist_tmp: |
| 2224 | return False |
| 2225 | |
| 2226 | if not exist_dst: |
Mike Frysinger | e50b6a7 | 2020-02-19 01:45:48 -0500 | [diff] [blame] | 2227 | exist_dst = self._FetchBundle(bundle_url, bundle_tmp, bundle_dst, quiet, |
| 2228 | verbose) |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2229 | if not exist_dst: |
| 2230 | return False |
| 2231 | |
| 2232 | cmd = ['fetch'] |
Mike Frysinger | 4847e05 | 2020-02-22 00:07:35 -0500 | [diff] [blame] | 2233 | if not verbose: |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2234 | cmd.append('--quiet') |
Mike Frysinger | 4847e05 | 2020-02-22 00:07:35 -0500 | [diff] [blame] | 2235 | if not quiet and sys.stdout.isatty(): |
| 2236 | cmd.append('--progress') |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2237 | if not self.worktree: |
| 2238 | cmd.append('--update-head-ok') |
| 2239 | cmd.append(bundle_dst) |
| 2240 | for f in remote.fetch: |
| 2241 | cmd.append(str(f)) |
Xin Li | 6e53844 | 2018-12-10 11:33:16 -0800 | [diff] [blame] | 2242 | cmd.append('+refs/tags/*:refs/tags/*') |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 2243 | |
| 2244 | ok = GitCommand(self, cmd, bare=True).Wait() == 0 |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 2245 | if os.path.exists(bundle_dst): |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 2246 | platform_utils.remove(bundle_dst) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 2247 | if os.path.exists(bundle_tmp): |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 2248 | platform_utils.remove(bundle_tmp) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2249 | return ok |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2250 | |
Mike Frysinger | e50b6a7 | 2020-02-19 01:45:48 -0500 | [diff] [blame] | 2251 | def _FetchBundle(self, srcUrl, tmpPath, dstPath, quiet, verbose): |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 2252 | if os.path.exists(dstPath): |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 2253 | platform_utils.remove(dstPath) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 2254 | |
Matt Gumbel | 2dc810c | 2012-08-30 09:39:36 -0700 | [diff] [blame] | 2255 | cmd = ['curl', '--fail', '--output', tmpPath, '--netrc', '--location'] |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 2256 | if quiet: |
Mike Frysinger | e50b6a7 | 2020-02-19 01:45:48 -0500 | [diff] [blame] | 2257 | cmd += ['--silent', '--show-error'] |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 2258 | if os.path.exists(tmpPath): |
| 2259 | size = os.stat(tmpPath).st_size |
| 2260 | if size >= 1024: |
| 2261 | cmd += ['--continue-at', '%d' % (size,)] |
| 2262 | else: |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 2263 | platform_utils.remove(tmpPath) |
Xin Li | 3698ab7 | 2019-06-25 16:09:43 -0700 | [diff] [blame] | 2264 | with GetUrlCookieFile(srcUrl, quiet) as (cookiefile, proxy): |
Dave Borowitz | 137d013 | 2015-01-02 11:12:54 -0800 | [diff] [blame] | 2265 | if cookiefile: |
Mike Frysinger | dc1d0e0 | 2020-02-09 16:20:06 -0500 | [diff] [blame] | 2266 | cmd += ['--cookie', cookiefile] |
Xin Li | 3698ab7 | 2019-06-25 16:09:43 -0700 | [diff] [blame] | 2267 | if proxy: |
| 2268 | cmd += ['--proxy', proxy] |
| 2269 | elif 'http_proxy' in os.environ and 'darwin' == sys.platform: |
| 2270 | cmd += ['--proxy', os.environ['http_proxy']] |
| 2271 | if srcUrl.startswith('persistent-https'): |
| 2272 | srcUrl = 'http' + srcUrl[len('persistent-https'):] |
| 2273 | elif srcUrl.startswith('persistent-http'): |
| 2274 | srcUrl = 'http' + srcUrl[len('persistent-http'):] |
Dave Borowitz | 137d013 | 2015-01-02 11:12:54 -0800 | [diff] [blame] | 2275 | cmd += [srcUrl] |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 2276 | |
Dave Borowitz | 137d013 | 2015-01-02 11:12:54 -0800 | [diff] [blame] | 2277 | if IsTrace(): |
| 2278 | Trace('%s', ' '.join(cmd)) |
Mike Frysinger | e50b6a7 | 2020-02-19 01:45:48 -0500 | [diff] [blame] | 2279 | if verbose: |
| 2280 | print('%s: Downloading bundle: %s' % (self.name, srcUrl)) |
| 2281 | stdout = None if verbose else subprocess.PIPE |
| 2282 | stderr = None if verbose else subprocess.STDOUT |
Dave Borowitz | 137d013 | 2015-01-02 11:12:54 -0800 | [diff] [blame] | 2283 | try: |
Mike Frysinger | e50b6a7 | 2020-02-19 01:45:48 -0500 | [diff] [blame] | 2284 | proc = subprocess.Popen(cmd, stdout=stdout, stderr=stderr) |
Dave Borowitz | 137d013 | 2015-01-02 11:12:54 -0800 | [diff] [blame] | 2285 | except OSError: |
| 2286 | return False |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 2287 | |
Mike Frysinger | e50b6a7 | 2020-02-19 01:45:48 -0500 | [diff] [blame] | 2288 | (output, _) = proc.communicate() |
| 2289 | curlret = proc.returncode |
Matt Gumbel | 2dc810c | 2012-08-30 09:39:36 -0700 | [diff] [blame] | 2290 | |
Dave Borowitz | 137d013 | 2015-01-02 11:12:54 -0800 | [diff] [blame] | 2291 | if curlret == 22: |
| 2292 | # From curl man page: |
| 2293 | # 22: HTTP page not retrieved. The requested url was not found or |
| 2294 | # returned another error with the HTTP error code being 400 or above. |
| 2295 | # This return code only appears if -f, --fail is used. |
Mike Frysinger | 4847e05 | 2020-02-22 00:07:35 -0500 | [diff] [blame] | 2296 | if verbose: |
George Engelbrecht | b687189 | 2020-04-02 13:53:01 -0600 | [diff] [blame] | 2297 | print('%s: Unable to retrieve clone.bundle; ignoring.' % self.name) |
| 2298 | if output: |
George Engelbrecht | 2fe84e1 | 2020-04-15 11:28:00 -0600 | [diff] [blame] | 2299 | print('Curl output:\n%s' % output) |
Dave Borowitz | 137d013 | 2015-01-02 11:12:54 -0800 | [diff] [blame] | 2300 | return False |
Mike Frysinger | e50b6a7 | 2020-02-19 01:45:48 -0500 | [diff] [blame] | 2301 | elif curlret and not verbose and output: |
| 2302 | print('%s' % output, file=sys.stderr) |
Matt Gumbel | 2dc810c | 2012-08-30 09:39:36 -0700 | [diff] [blame] | 2303 | |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 2304 | if os.path.exists(tmpPath): |
Kris Giesing | c8d882a | 2014-12-23 13:02:32 -0800 | [diff] [blame] | 2305 | if curlret == 0 and self._IsValidBundle(tmpPath, quiet): |
Renaud Paquay | ad1abcb | 2016-11-01 11:34:55 -0700 | [diff] [blame] | 2306 | platform_utils.rename(tmpPath, dstPath) |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 2307 | return True |
| 2308 | else: |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 2309 | platform_utils.remove(tmpPath) |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 2310 | return False |
| 2311 | else: |
| 2312 | return False |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 2313 | |
Kris Giesing | c8d882a | 2014-12-23 13:02:32 -0800 | [diff] [blame] | 2314 | def _IsValidBundle(self, path, quiet): |
Dave Borowitz | 91f3ba5 | 2013-06-03 12:15:23 -0700 | [diff] [blame] | 2315 | try: |
Pierre Tardy | 2b7daff | 2019-05-16 10:28:21 +0200 | [diff] [blame] | 2316 | with open(path, 'rb') as f: |
| 2317 | if f.read(16) == b'# v2 git bundle\n': |
Dave Borowitz | 91f3ba5 | 2013-06-03 12:15:23 -0700 | [diff] [blame] | 2318 | return True |
| 2319 | else: |
Kris Giesing | c8d882a | 2014-12-23 13:02:32 -0800 | [diff] [blame] | 2320 | if not quiet: |
| 2321 | print("Invalid clone.bundle file; ignoring.", file=sys.stderr) |
Dave Borowitz | 91f3ba5 | 2013-06-03 12:15:23 -0700 | [diff] [blame] | 2322 | return False |
| 2323 | except OSError: |
| 2324 | return False |
| 2325 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2326 | def _Checkout(self, rev, quiet=False): |
| 2327 | cmd = ['checkout'] |
| 2328 | if quiet: |
| 2329 | cmd.append('-q') |
| 2330 | cmd.append(rev) |
| 2331 | cmd.append('--') |
| 2332 | if GitCommand(self, cmd).Wait() != 0: |
| 2333 | if self._allrefs: |
| 2334 | raise GitError('%s checkout %s ' % (self.name, rev)) |
| 2335 | |
Mike Frysinger | 915fda1 | 2020-03-22 12:15:20 -0400 | [diff] [blame] | 2336 | def _CherryPick(self, rev, ffonly=False, record_origin=False): |
Pierre Tardy | e5a2122 | 2011-03-24 16:28:18 +0100 | [diff] [blame] | 2337 | cmd = ['cherry-pick'] |
Mike Frysinger | ea43176 | 2020-03-22 12:14:01 -0400 | [diff] [blame] | 2338 | if ffonly: |
| 2339 | cmd.append('--ff') |
Mike Frysinger | 915fda1 | 2020-03-22 12:15:20 -0400 | [diff] [blame] | 2340 | if record_origin: |
| 2341 | cmd.append('-x') |
Pierre Tardy | e5a2122 | 2011-03-24 16:28:18 +0100 | [diff] [blame] | 2342 | cmd.append(rev) |
| 2343 | cmd.append('--') |
| 2344 | if GitCommand(self, cmd).Wait() != 0: |
| 2345 | if self._allrefs: |
| 2346 | raise GitError('%s cherry-pick %s ' % (self.name, rev)) |
| 2347 | |
Akshay Verma | 0f2e45a | 2018-03-24 12:27:05 +0530 | [diff] [blame] | 2348 | def _LsRemote(self, refs): |
| 2349 | cmd = ['ls-remote', self.remote.name, refs] |
Akshay Verma | cf7c083 | 2018-03-15 21:56:30 +0530 | [diff] [blame] | 2350 | p = GitCommand(self, cmd, capture_stdout=True) |
| 2351 | if p.Wait() == 0: |
Mike Frysinger | 600f492 | 2019-08-03 02:14:28 -0400 | [diff] [blame] | 2352 | return p.stdout |
Akshay Verma | cf7c083 | 2018-03-15 21:56:30 +0530 | [diff] [blame] | 2353 | return None |
| 2354 | |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2355 | def _Revert(self, rev): |
Erwan Mahe | a94f162 | 2011-08-19 13:56:09 +0200 | [diff] [blame] | 2356 | cmd = ['revert'] |
| 2357 | cmd.append('--no-edit') |
| 2358 | cmd.append(rev) |
| 2359 | cmd.append('--') |
| 2360 | if GitCommand(self, cmd).Wait() != 0: |
| 2361 | if self._allrefs: |
| 2362 | raise GitError('%s revert %s ' % (self.name, rev)) |
| 2363 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2364 | def _ResetHard(self, rev, quiet=True): |
| 2365 | cmd = ['reset', '--hard'] |
| 2366 | if quiet: |
| 2367 | cmd.append('-q') |
| 2368 | cmd.append(rev) |
| 2369 | if GitCommand(self, cmd).Wait() != 0: |
| 2370 | raise GitError('%s reset --hard %s ' % (self.name, rev)) |
| 2371 | |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 2372 | def _SyncSubmodules(self, quiet=True): |
| 2373 | cmd = ['submodule', 'update', '--init', '--recursive'] |
| 2374 | if quiet: |
| 2375 | cmd.append('-q') |
| 2376 | if GitCommand(self, cmd).Wait() != 0: |
| 2377 | raise GitError('%s submodule update --init --recursive %s ' % self.name) |
| 2378 | |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2379 | def _Rebase(self, upstream, onto=None): |
Shawn O. Pearce | 19a83d8 | 2009-04-16 08:14:26 -0700 | [diff] [blame] | 2380 | cmd = ['rebase'] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2381 | if onto is not None: |
| 2382 | cmd.extend(['--onto', onto]) |
| 2383 | cmd.append(upstream) |
Shawn O. Pearce | 19a83d8 | 2009-04-16 08:14:26 -0700 | [diff] [blame] | 2384 | if GitCommand(self, cmd).Wait() != 0: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2385 | raise GitError('%s rebase %s ' % (self.name, upstream)) |
| 2386 | |
Pierre Tardy | 3d12594 | 2012-05-04 12:18:12 +0200 | [diff] [blame] | 2387 | def _FastForward(self, head, ffonly=False): |
Mike Frysinger | 2b1345b | 2020-02-17 01:07:11 -0500 | [diff] [blame] | 2388 | cmd = ['merge', '--no-stat', head] |
Pierre Tardy | 3d12594 | 2012-05-04 12:18:12 +0200 | [diff] [blame] | 2389 | if ffonly: |
| 2390 | cmd.append("--ff-only") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2391 | if GitCommand(self, cmd).Wait() != 0: |
| 2392 | raise GitError('%s merge %s ' % (self.name, head)) |
| 2393 | |
David Pursehouse | e8ace26 | 2020-02-13 12:41:15 +0900 | [diff] [blame] | 2394 | def _InitGitDir(self, mirror_git=None, force_sync=False, quiet=False): |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2395 | init_git_dir = not os.path.exists(self.gitdir) |
| 2396 | init_obj_dir = not os.path.exists(self.objdir) |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2397 | try: |
| 2398 | # Initialize the bare repository, which contains all of the objects. |
| 2399 | if init_obj_dir: |
| 2400 | os.makedirs(self.objdir) |
| 2401 | self.bare_objdir.init() |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2402 | |
Mike Frysinger | 21b7fbe | 2020-02-26 23:53:36 -0500 | [diff] [blame] | 2403 | if self.use_git_worktrees: |
| 2404 | # Set up the m/ space to point to the worktree-specific ref space. |
| 2405 | # We'll update the worktree-specific ref space on each checkout. |
| 2406 | if self.manifest.branch: |
| 2407 | self.bare_git.symbolic_ref( |
| 2408 | '-m', 'redirecting to worktree scope', |
| 2409 | R_M + self.manifest.branch, |
| 2410 | R_WORKTREE_M + self.manifest.branch) |
| 2411 | |
| 2412 | # Enable per-worktree config file support if possible. This is more a |
| 2413 | # nice-to-have feature for users rather than a hard requirement. |
Adrien Bioteau | 65f51ad | 2020-07-24 14:56:20 +0200 | [diff] [blame] | 2414 | if git_require((2, 20, 0)): |
Mike Frysinger | 21b7fbe | 2020-02-26 23:53:36 -0500 | [diff] [blame] | 2415 | self.EnableRepositoryExtension('worktreeConfig') |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 2416 | |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2417 | # If we have a separate directory to hold refs, initialize it as well. |
| 2418 | if self.objdir != self.gitdir: |
| 2419 | if init_git_dir: |
| 2420 | os.makedirs(self.gitdir) |
| 2421 | |
| 2422 | if init_obj_dir or init_git_dir: |
| 2423 | self._ReferenceGitDir(self.objdir, self.gitdir, share_refs=False, |
| 2424 | copy_all=True) |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 2425 | try: |
| 2426 | self._CheckDirReference(self.objdir, self.gitdir, share_refs=False) |
| 2427 | except GitError as e: |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 2428 | if force_sync: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2429 | print("Retrying clone after deleting %s" % |
| 2430 | self.gitdir, file=sys.stderr) |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 2431 | try: |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 2432 | platform_utils.rmtree(platform_utils.realpath(self.gitdir)) |
| 2433 | if self.worktree and os.path.exists(platform_utils.realpath |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2434 | (self.worktree)): |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 2435 | platform_utils.rmtree(platform_utils.realpath(self.worktree)) |
David Pursehouse | e8ace26 | 2020-02-13 12:41:15 +0900 | [diff] [blame] | 2436 | return self._InitGitDir(mirror_git=mirror_git, force_sync=False, |
| 2437 | quiet=quiet) |
David Pursehouse | 145e35b | 2020-02-12 15:40:47 +0900 | [diff] [blame] | 2438 | except Exception: |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 2439 | raise e |
| 2440 | raise e |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2441 | |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2442 | if init_git_dir: |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2443 | mp = self.manifest.manifestProject |
| 2444 | ref_dir = mp.config.GetString('repo.reference') or '' |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2445 | |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2446 | if ref_dir or mirror_git: |
| 2447 | if not mirror_git: |
| 2448 | mirror_git = os.path.join(ref_dir, self.name + '.git') |
| 2449 | repo_git = os.path.join(ref_dir, '.repo', 'projects', |
| 2450 | self.relpath + '.git') |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 2451 | worktrees_git = os.path.join(ref_dir, '.repo', 'worktrees', |
| 2452 | self.name + '.git') |
Shawn O. Pearce | 2816d4f | 2009-03-03 17:53:18 -0800 | [diff] [blame] | 2453 | |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2454 | if os.path.exists(mirror_git): |
| 2455 | ref_dir = mirror_git |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2456 | elif os.path.exists(repo_git): |
| 2457 | ref_dir = repo_git |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 2458 | elif os.path.exists(worktrees_git): |
| 2459 | ref_dir = worktrees_git |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2460 | else: |
| 2461 | ref_dir = None |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2462 | |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2463 | if ref_dir: |
Samuel Holland | baa0009 | 2018-01-22 10:57:29 -0600 | [diff] [blame] | 2464 | if not os.path.isabs(ref_dir): |
| 2465 | # The alternate directory is relative to the object database. |
| 2466 | ref_dir = os.path.relpath(ref_dir, |
| 2467 | os.path.join(self.objdir, 'objects')) |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2468 | _lwrite(os.path.join(self.gitdir, 'objects/info/alternates'), |
| 2469 | os.path.join(ref_dir, 'objects') + '\n') |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2470 | |
David Pursehouse | e8ace26 | 2020-02-13 12:41:15 +0900 | [diff] [blame] | 2471 | self._UpdateHooks(quiet=quiet) |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2472 | |
| 2473 | m = self.manifest.manifestProject.config |
| 2474 | for key in ['user.name', 'user.email']: |
| 2475 | if m.Has(key, include_defaults=False): |
| 2476 | self.config.SetString(key, m.GetString(key)) |
David Pursehouse | 76a4a9d | 2016-08-16 12:11:12 +0900 | [diff] [blame] | 2477 | self.config.SetString('filter.lfs.smudge', 'git-lfs smudge --skip -- %f') |
Francois Ferrand | c5b0e23 | 2019-05-24 09:35:20 +0200 | [diff] [blame] | 2478 | self.config.SetString('filter.lfs.process', 'git-lfs filter-process --skip') |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2479 | if self.manifest.IsMirror: |
| 2480 | self.config.SetString('core.bare', 'true') |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2481 | else: |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2482 | self.config.SetString('core.bare', None) |
| 2483 | except Exception: |
| 2484 | if init_obj_dir and os.path.exists(self.objdir): |
Renaud Paquay | a65adf7 | 2016-11-03 10:37:53 -0700 | [diff] [blame] | 2485 | platform_utils.rmtree(self.objdir) |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2486 | if init_git_dir and os.path.exists(self.gitdir): |
Renaud Paquay | a65adf7 | 2016-11-03 10:37:53 -0700 | [diff] [blame] | 2487 | platform_utils.rmtree(self.gitdir) |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2488 | raise |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2489 | |
David Pursehouse | e8ace26 | 2020-02-13 12:41:15 +0900 | [diff] [blame] | 2490 | def _UpdateHooks(self, quiet=False): |
Jimmie Wester | a044458 | 2012-10-24 13:44:42 +0200 | [diff] [blame] | 2491 | if os.path.exists(self.gitdir): |
David Pursehouse | e8ace26 | 2020-02-13 12:41:15 +0900 | [diff] [blame] | 2492 | self._InitHooks(quiet=quiet) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2493 | |
David Pursehouse | e8ace26 | 2020-02-13 12:41:15 +0900 | [diff] [blame] | 2494 | def _InitHooks(self, quiet=False): |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 2495 | hooks = platform_utils.realpath(self._gitdir_path('hooks')) |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 2496 | if not os.path.exists(hooks): |
| 2497 | os.makedirs(hooks) |
Jonathan Nieder | 9371979 | 2015-03-17 11:29:58 -0700 | [diff] [blame] | 2498 | for stock_hook in _ProjectHooks(): |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 2499 | name = os.path.basename(stock_hook) |
| 2500 | |
Victor Boivie | 65e0f35 | 2011-04-18 11:23:29 +0200 | [diff] [blame] | 2501 | if name in ('commit-msg',) and not self.remote.review \ |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2502 | and self is not self.manifest.manifestProject: |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 2503 | # Don't install a Gerrit Code Review hook if this |
| 2504 | # project does not appear to use it for reviews. |
| 2505 | # |
Victor Boivie | 65e0f35 | 2011-04-18 11:23:29 +0200 | [diff] [blame] | 2506 | # Since the manifest project is one of those, but also |
| 2507 | # managed through gerrit, it's excluded |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 2508 | continue |
| 2509 | |
| 2510 | dst = os.path.join(hooks, name) |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 2511 | if platform_utils.islink(dst): |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 2512 | continue |
| 2513 | if os.path.exists(dst): |
Mike Frysinger | 7951e14 | 2020-02-21 00:04:15 -0500 | [diff] [blame] | 2514 | # If the files are the same, we'll leave it alone. We create symlinks |
| 2515 | # below by default but fallback to hardlinks if the OS blocks them. |
| 2516 | # So if we're here, it's probably because we made a hardlink below. |
| 2517 | if not filecmp.cmp(stock_hook, dst, shallow=False): |
David Pursehouse | e8ace26 | 2020-02-13 12:41:15 +0900 | [diff] [blame] | 2518 | if not quiet: |
| 2519 | _warn("%s: Not replacing locally modified %s hook", |
| 2520 | self.relpath, name) |
Mike Frysinger | 7951e14 | 2020-02-21 00:04:15 -0500 | [diff] [blame] | 2521 | continue |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 2522 | try: |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 2523 | platform_utils.symlink( |
| 2524 | os.path.relpath(stock_hook, os.path.dirname(dst)), dst) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 2525 | except OSError as e: |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 2526 | if e.errno == errno.EPERM: |
Mike Frysinger | 7951e14 | 2020-02-21 00:04:15 -0500 | [diff] [blame] | 2527 | try: |
| 2528 | os.link(stock_hook, dst) |
| 2529 | except OSError: |
| 2530 | raise GitError(self._get_symlink_error_message()) |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 2531 | else: |
| 2532 | raise |
| 2533 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2534 | def _InitRemote(self): |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 2535 | if self.remote.url: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2536 | remote = self.GetRemote(self.remote.name) |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 2537 | remote.url = self.remote.url |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 2538 | remote.pushUrl = self.remote.pushUrl |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 2539 | remote.review = self.remote.review |
| 2540 | remote.projectname = self.name |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2541 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 2542 | if self.worktree: |
| 2543 | remote.ResetFetch(mirror=False) |
| 2544 | else: |
| 2545 | remote.ResetFetch(mirror=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2546 | remote.Save() |
| 2547 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2548 | def _InitMRef(self): |
| 2549 | if self.manifest.branch: |
Mike Frysinger | 21b7fbe | 2020-02-26 23:53:36 -0500 | [diff] [blame] | 2550 | if self.use_git_worktrees: |
| 2551 | # We can't update this ref with git worktrees until it exists. |
| 2552 | # We'll wait until the initial checkout to set it. |
| 2553 | if not os.path.exists(self.worktree): |
| 2554 | return |
| 2555 | |
| 2556 | base = R_WORKTREE_M |
| 2557 | active_git = self.work_git |
Remy Böhmer | 1469c28 | 2020-12-15 18:49:02 +0100 | [diff] [blame] | 2558 | |
| 2559 | self._InitAnyMRef(HEAD, self.bare_git, detach=True) |
Mike Frysinger | 21b7fbe | 2020-02-26 23:53:36 -0500 | [diff] [blame] | 2560 | else: |
| 2561 | base = R_M |
| 2562 | active_git = self.bare_git |
| 2563 | |
| 2564 | self._InitAnyMRef(base + self.manifest.branch, active_git) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2565 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 2566 | def _InitMirrorHead(self): |
Mike Frysinger | 21b7fbe | 2020-02-26 23:53:36 -0500 | [diff] [blame] | 2567 | self._InitAnyMRef(HEAD, self.bare_git) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 2568 | |
Remy Böhmer | 1469c28 | 2020-12-15 18:49:02 +0100 | [diff] [blame] | 2569 | def _InitAnyMRef(self, ref, active_git, detach=False): |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 2570 | cur = self.bare_ref.symref(ref) |
| 2571 | |
| 2572 | if self.revisionId: |
| 2573 | if cur != '' or self.bare_ref.get(ref) != self.revisionId: |
| 2574 | msg = 'manifest set to %s' % self.revisionId |
| 2575 | dst = self.revisionId + '^0' |
Mike Frysinger | 21b7fbe | 2020-02-26 23:53:36 -0500 | [diff] [blame] | 2576 | active_git.UpdateRef(ref, dst, message=msg, detach=True) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 2577 | else: |
| 2578 | remote = self.GetRemote(self.remote.name) |
| 2579 | dst = remote.ToLocal(self.revisionExpr) |
| 2580 | if cur != dst: |
| 2581 | msg = 'manifest set to %s' % self.revisionExpr |
Remy Böhmer | 1469c28 | 2020-12-15 18:49:02 +0100 | [diff] [blame] | 2582 | if detach: |
| 2583 | active_git.UpdateRef(ref, dst, message=msg, detach=True) |
| 2584 | else: |
| 2585 | active_git.symbolic_ref('-m', msg, ref, dst) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 2586 | |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2587 | def _CheckDirReference(self, srcdir, destdir, share_refs): |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 2588 | # Git worktrees don't use symlinks to share at all. |
| 2589 | if self.use_git_worktrees: |
| 2590 | return |
| 2591 | |
Dan Willemsen | bdb866e | 2016-04-05 17:22:02 -0700 | [diff] [blame] | 2592 | symlink_files = self.shareable_files[:] |
| 2593 | symlink_dirs = self.shareable_dirs[:] |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2594 | if share_refs: |
| 2595 | symlink_files += self.working_tree_files |
| 2596 | symlink_dirs += self.working_tree_dirs |
| 2597 | to_symlink = symlink_files + symlink_dirs |
| 2598 | for name in set(to_symlink): |
Mike Frysinger | ed4f211 | 2020-02-11 23:06:29 -0500 | [diff] [blame] | 2599 | # Try to self-heal a bit in simple cases. |
| 2600 | dst_path = os.path.join(destdir, name) |
| 2601 | src_path = os.path.join(srcdir, name) |
| 2602 | |
| 2603 | if name in self.working_tree_dirs: |
| 2604 | # If the dir is missing under .repo/projects/, create it. |
| 2605 | if not os.path.exists(src_path): |
| 2606 | os.makedirs(src_path) |
| 2607 | |
| 2608 | elif name in self.working_tree_files: |
| 2609 | # If it's a file under the checkout .git/ and the .repo/projects/ has |
| 2610 | # nothing, move the file under the .repo/projects/ tree. |
| 2611 | if not os.path.exists(src_path) and os.path.isfile(dst_path): |
| 2612 | platform_utils.rename(dst_path, src_path) |
| 2613 | |
| 2614 | # If the path exists under the .repo/projects/ and there's no symlink |
| 2615 | # under the checkout .git/, recreate the symlink. |
| 2616 | if name in self.working_tree_dirs or name in self.working_tree_files: |
| 2617 | if os.path.exists(src_path) and not os.path.exists(dst_path): |
| 2618 | platform_utils.symlink( |
| 2619 | os.path.relpath(src_path, os.path.dirname(dst_path)), dst_path) |
| 2620 | |
| 2621 | dst = platform_utils.realpath(dst_path) |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2622 | if os.path.lexists(dst): |
Mike Frysinger | ed4f211 | 2020-02-11 23:06:29 -0500 | [diff] [blame] | 2623 | src = platform_utils.realpath(src_path) |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2624 | # Fail if the links are pointing to the wrong place |
| 2625 | if src != dst: |
Marc Herbert | ec28790 | 2016-10-27 12:58:26 -0700 | [diff] [blame] | 2626 | _error('%s is different in %s vs %s', name, destdir, srcdir) |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 2627 | raise GitError('--force-sync not enabled; cannot overwrite a local ' |
Simon Ruggier | f9b7683 | 2015-07-31 17:18:34 -0400 | [diff] [blame] | 2628 | 'work tree. If you\'re comfortable with the ' |
| 2629 | 'possibility of losing the work tree\'s git metadata,' |
| 2630 | ' use `repo sync --force-sync {0}` to ' |
| 2631 | 'proceed.'.format(self.relpath)) |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2632 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2633 | def _ReferenceGitDir(self, gitdir, dotgit, share_refs, copy_all): |
| 2634 | """Update |dotgit| to reference |gitdir|, using symlinks where possible. |
| 2635 | |
| 2636 | Args: |
| 2637 | gitdir: The bare git repository. Must already be initialized. |
| 2638 | dotgit: The repository you would like to initialize. |
| 2639 | share_refs: If true, |dotgit| will store its refs under |gitdir|. |
| 2640 | Only one work tree can store refs under a given |gitdir|. |
| 2641 | copy_all: If true, copy all remaining files from |gitdir| -> |dotgit|. |
| 2642 | This saves you the effort of initializing |dotgit| yourself. |
| 2643 | """ |
Dan Willemsen | bdb866e | 2016-04-05 17:22:02 -0700 | [diff] [blame] | 2644 | symlink_files = self.shareable_files[:] |
| 2645 | symlink_dirs = self.shareable_dirs[:] |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2646 | if share_refs: |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2647 | symlink_files += self.working_tree_files |
| 2648 | symlink_dirs += self.working_tree_dirs |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2649 | to_symlink = symlink_files + symlink_dirs |
| 2650 | |
| 2651 | to_copy = [] |
| 2652 | if copy_all: |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 2653 | to_copy = platform_utils.listdir(gitdir) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2654 | |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 2655 | dotgit = platform_utils.realpath(dotgit) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2656 | for name in set(to_copy).union(to_symlink): |
| 2657 | try: |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 2658 | src = platform_utils.realpath(os.path.join(gitdir, name)) |
Dan Willemsen | 2a3e152 | 2015-07-30 20:43:33 -0700 | [diff] [blame] | 2659 | dst = os.path.join(dotgit, name) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2660 | |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2661 | if os.path.lexists(dst): |
| 2662 | continue |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2663 | |
| 2664 | # If the source dir doesn't exist, create an empty dir. |
| 2665 | if name in symlink_dirs and not os.path.lexists(src): |
| 2666 | os.makedirs(src) |
| 2667 | |
Cheuk Leung | 8ac0c96 | 2015-07-06 21:33:00 +0200 | [diff] [blame] | 2668 | if name in to_symlink: |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 2669 | platform_utils.symlink( |
| 2670 | os.path.relpath(src, os.path.dirname(dst)), dst) |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 2671 | elif copy_all and not platform_utils.islink(dst): |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 2672 | if platform_utils.isdir(src): |
Cheuk Leung | 8ac0c96 | 2015-07-06 21:33:00 +0200 | [diff] [blame] | 2673 | shutil.copytree(src, dst) |
| 2674 | elif os.path.isfile(src): |
| 2675 | shutil.copy(src, dst) |
| 2676 | |
Conley Owens | 80b87fe | 2014-05-09 17:13:44 -0700 | [diff] [blame] | 2677 | # If the source file doesn't exist, ensure the destination |
| 2678 | # file doesn't either. |
| 2679 | if name in symlink_files and not os.path.lexists(src): |
| 2680 | try: |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 2681 | platform_utils.remove(dst) |
Conley Owens | 80b87fe | 2014-05-09 17:13:44 -0700 | [diff] [blame] | 2682 | except OSError: |
| 2683 | pass |
| 2684 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2685 | except OSError as e: |
| 2686 | if e.errno == errno.EPERM: |
Renaud Paquay | 788e962 | 2017-01-27 11:41:12 -0800 | [diff] [blame] | 2687 | raise DownloadError(self._get_symlink_error_message()) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2688 | else: |
| 2689 | raise |
| 2690 | |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 2691 | def _InitGitWorktree(self): |
| 2692 | """Init the project using git worktrees.""" |
| 2693 | self.bare_git.worktree('prune') |
| 2694 | self.bare_git.worktree('add', '-ff', '--checkout', '--detach', '--lock', |
| 2695 | self.worktree, self.GetRevisionId()) |
| 2696 | |
| 2697 | # Rewrite the internal state files to use relative paths between the |
| 2698 | # checkouts & worktrees. |
| 2699 | dotgit = os.path.join(self.worktree, '.git') |
| 2700 | with open(dotgit, 'r') as fp: |
| 2701 | # Figure out the checkout->worktree path. |
| 2702 | setting = fp.read() |
| 2703 | assert setting.startswith('gitdir:') |
| 2704 | git_worktree_path = setting.split(':', 1)[1].strip() |
Mike Frysinger | 7526478 | 2020-02-21 18:55:07 -0500 | [diff] [blame] | 2705 | # Some platforms (e.g. Windows) won't let us update dotgit in situ because |
| 2706 | # of file permissions. Delete it and recreate it from scratch to avoid. |
| 2707 | platform_utils.remove(dotgit) |
Remy Bohmer | 44bc964 | 2020-11-20 21:19:10 +0100 | [diff] [blame] | 2708 | # Use relative path from checkout->worktree & maintain Unix line endings |
| 2709 | # on all OS's to match git behavior. |
| 2710 | with open(dotgit, 'w', newline='\n') as fp: |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 2711 | print('gitdir:', os.path.relpath(git_worktree_path, self.worktree), |
| 2712 | file=fp) |
Remy Bohmer | 44bc964 | 2020-11-20 21:19:10 +0100 | [diff] [blame] | 2713 | # Use relative path from worktree->checkout & maintain Unix line endings |
| 2714 | # on all OS's to match git behavior. |
| 2715 | with open(os.path.join(git_worktree_path, 'gitdir'), 'w', newline='\n') as fp: |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 2716 | print(os.path.relpath(dotgit, git_worktree_path), file=fp) |
| 2717 | |
Mike Frysinger | 21b7fbe | 2020-02-26 23:53:36 -0500 | [diff] [blame] | 2718 | self._InitMRef() |
| 2719 | |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 2720 | def _InitWorkTree(self, force_sync=False, submodules=False): |
Mike Frysinger | f454512 | 2019-11-11 04:34:16 -0500 | [diff] [blame] | 2721 | realdotgit = os.path.join(self.worktree, '.git') |
| 2722 | tmpdotgit = realdotgit + '.tmp' |
| 2723 | init_dotgit = not os.path.exists(realdotgit) |
| 2724 | if init_dotgit: |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 2725 | if self.use_git_worktrees: |
| 2726 | self._InitGitWorktree() |
| 2727 | self._CopyAndLinkFiles() |
| 2728 | return |
| 2729 | |
Mike Frysinger | f454512 | 2019-11-11 04:34:16 -0500 | [diff] [blame] | 2730 | dotgit = tmpdotgit |
| 2731 | platform_utils.rmtree(tmpdotgit, ignore_errors=True) |
| 2732 | os.makedirs(tmpdotgit) |
| 2733 | self._ReferenceGitDir(self.gitdir, tmpdotgit, share_refs=True, |
| 2734 | copy_all=False) |
| 2735 | else: |
| 2736 | dotgit = realdotgit |
| 2737 | |
Kevin Degi | b1a07b8 | 2015-07-27 13:33:43 -0600 | [diff] [blame] | 2738 | try: |
Mike Frysinger | f454512 | 2019-11-11 04:34:16 -0500 | [diff] [blame] | 2739 | self._CheckDirReference(self.gitdir, dotgit, share_refs=True) |
| 2740 | except GitError as e: |
| 2741 | if force_sync and not init_dotgit: |
| 2742 | try: |
| 2743 | platform_utils.rmtree(dotgit) |
| 2744 | return self._InitWorkTree(force_sync=False, submodules=submodules) |
David Pursehouse | 145e35b | 2020-02-12 15:40:47 +0900 | [diff] [blame] | 2745 | except Exception: |
Mike Frysinger | f454512 | 2019-11-11 04:34:16 -0500 | [diff] [blame] | 2746 | raise e |
| 2747 | raise e |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2748 | |
Mike Frysinger | f454512 | 2019-11-11 04:34:16 -0500 | [diff] [blame] | 2749 | if init_dotgit: |
| 2750 | _lwrite(os.path.join(tmpdotgit, HEAD), '%s\n' % self.GetRevisionId()) |
Kevin Degi | 384b3c5 | 2014-10-16 16:02:58 -0600 | [diff] [blame] | 2751 | |
Mike Frysinger | f454512 | 2019-11-11 04:34:16 -0500 | [diff] [blame] | 2752 | # Now that the .git dir is fully set up, move it to its final home. |
| 2753 | platform_utils.rename(tmpdotgit, realdotgit) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2754 | |
Mike Frysinger | f454512 | 2019-11-11 04:34:16 -0500 | [diff] [blame] | 2755 | # Finish checking out the worktree. |
| 2756 | cmd = ['read-tree', '--reset', '-u'] |
| 2757 | cmd.append('-v') |
| 2758 | cmd.append(HEAD) |
| 2759 | if GitCommand(self, cmd).Wait() != 0: |
| 2760 | raise GitError('Cannot initialize work tree for ' + self.name) |
Victor Boivie | 0960b5b | 2010-11-26 13:42:13 +0100 | [diff] [blame] | 2761 | |
Mike Frysinger | f454512 | 2019-11-11 04:34:16 -0500 | [diff] [blame] | 2762 | if submodules: |
| 2763 | self._SyncSubmodules(quiet=True) |
| 2764 | self._CopyAndLinkFiles() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2765 | |
Renaud Paquay | 788e962 | 2017-01-27 11:41:12 -0800 | [diff] [blame] | 2766 | def _get_symlink_error_message(self): |
| 2767 | if platform_utils.isWindows(): |
| 2768 | return ('Unable to create symbolic link. Please re-run the command as ' |
| 2769 | 'Administrator, or see ' |
| 2770 | 'https://github.com/git-for-windows/git/wiki/Symbolic-Links ' |
| 2771 | 'for other options.') |
| 2772 | return 'filesystem must support symlinks' |
| 2773 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2774 | def _gitdir_path(self, path): |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 2775 | return platform_utils.realpath(os.path.join(self.gitdir, path)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2776 | |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 2777 | def _revlist(self, *args, **kw): |
| 2778 | a = [] |
| 2779 | a.extend(args) |
| 2780 | a.append('--') |
| 2781 | return self.work_git.rev_list(*a, **kw) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2782 | |
| 2783 | @property |
| 2784 | def _allrefs(self): |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 2785 | return self.bare_ref.all |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2786 | |
Sebastian Schuberth | 7ecccf6 | 2016-03-29 14:11:20 +0200 | [diff] [blame] | 2787 | def _getLogs(self, rev1, rev2, oneline=False, color=True, pretty_format=None): |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 2788 | """Get logs between two revisions of this project.""" |
| 2789 | comp = '..' |
| 2790 | if rev1: |
| 2791 | revs = [rev1] |
| 2792 | if rev2: |
| 2793 | revs.extend([comp, rev2]) |
| 2794 | cmd = ['log', ''.join(revs)] |
| 2795 | out = DiffColoring(self.config) |
| 2796 | if out.is_on and color: |
| 2797 | cmd.append('--color') |
Sebastian Schuberth | 7ecccf6 | 2016-03-29 14:11:20 +0200 | [diff] [blame] | 2798 | if pretty_format is not None: |
| 2799 | cmd.append('--pretty=format:%s' % pretty_format) |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 2800 | if oneline: |
| 2801 | cmd.append('--oneline') |
| 2802 | |
| 2803 | try: |
| 2804 | log = GitCommand(self, cmd, capture_stdout=True, capture_stderr=True) |
| 2805 | if log.Wait() == 0: |
| 2806 | return log.stdout |
| 2807 | except GitError: |
| 2808 | # worktree may not exist if groups changed for example. In that case, |
| 2809 | # try in gitdir instead. |
| 2810 | if not os.path.exists(self.worktree): |
| 2811 | return self.bare_git.log(*cmd[1:]) |
| 2812 | else: |
| 2813 | raise |
| 2814 | return None |
| 2815 | |
Sebastian Schuberth | 7ecccf6 | 2016-03-29 14:11:20 +0200 | [diff] [blame] | 2816 | def getAddedAndRemovedLogs(self, toProject, oneline=False, color=True, |
| 2817 | pretty_format=None): |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 2818 | """Get the list of logs from this revision to given revisionId""" |
| 2819 | logs = {} |
| 2820 | selfId = self.GetRevisionId(self._allrefs) |
| 2821 | toId = toProject.GetRevisionId(toProject._allrefs) |
| 2822 | |
Sebastian Schuberth | 7ecccf6 | 2016-03-29 14:11:20 +0200 | [diff] [blame] | 2823 | logs['added'] = self._getLogs(selfId, toId, oneline=oneline, color=color, |
| 2824 | pretty_format=pretty_format) |
| 2825 | logs['removed'] = self._getLogs(toId, selfId, oneline=oneline, color=color, |
| 2826 | pretty_format=pretty_format) |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 2827 | return logs |
| 2828 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2829 | class _GitGetByExec(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2830 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2831 | def __init__(self, project, bare, gitdir): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2832 | self._project = project |
| 2833 | self._bare = bare |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2834 | self._gitdir = gitdir |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2835 | |
Kimiyuki Onaka | 0501b29 | 2020-08-28 10:05:27 +0900 | [diff] [blame] | 2836 | # __getstate__ and __setstate__ are required for pickling because __getattr__ exists. |
| 2837 | def __getstate__(self): |
| 2838 | return (self._project, self._bare, self._gitdir) |
| 2839 | |
| 2840 | def __setstate__(self, state): |
| 2841 | self._project, self._bare, self._gitdir = state |
| 2842 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2843 | def LsOthers(self): |
| 2844 | p = GitCommand(self._project, |
| 2845 | ['ls-files', |
| 2846 | '-z', |
| 2847 | '--others', |
| 2848 | '--exclude-standard'], |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2849 | bare=False, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2850 | gitdir=self._gitdir, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2851 | capture_stdout=True, |
| 2852 | capture_stderr=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2853 | if p.Wait() == 0: |
| 2854 | out = p.stdout |
| 2855 | if out: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2856 | # Backslash is not anomalous |
David Pursehouse | 65b0ba5 | 2018-06-24 16:21:51 +0900 | [diff] [blame] | 2857 | return out[:-1].split('\0') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2858 | return [] |
| 2859 | |
| 2860 | def DiffZ(self, name, *args): |
| 2861 | cmd = [name] |
| 2862 | cmd.append('-z') |
Eli Ribble | 2d095da | 2019-05-02 18:21:42 -0700 | [diff] [blame] | 2863 | cmd.append('--ignore-submodules') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2864 | cmd.extend(args) |
| 2865 | p = GitCommand(self._project, |
| 2866 | cmd, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2867 | gitdir=self._gitdir, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2868 | bare=False, |
| 2869 | capture_stdout=True, |
| 2870 | capture_stderr=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2871 | try: |
| 2872 | out = p.process.stdout.read() |
Mike Frysinger | 600f492 | 2019-08-03 02:14:28 -0400 | [diff] [blame] | 2873 | if not hasattr(out, 'encode'): |
| 2874 | out = out.decode() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2875 | r = {} |
| 2876 | if out: |
David Pursehouse | 65b0ba5 | 2018-06-24 16:21:51 +0900 | [diff] [blame] | 2877 | out = iter(out[:-1].split('\0')) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2878 | while out: |
Shawn O. Pearce | 02dbb6d | 2008-10-21 13:59:08 -0700 | [diff] [blame] | 2879 | try: |
Anthony King | 2cd1f04 | 2014-05-05 21:24:05 +0100 | [diff] [blame] | 2880 | info = next(out) |
| 2881 | path = next(out) |
Shawn O. Pearce | 02dbb6d | 2008-10-21 13:59:08 -0700 | [diff] [blame] | 2882 | except StopIteration: |
| 2883 | break |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2884 | |
| 2885 | class _Info(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2886 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2887 | def __init__(self, path, omode, nmode, oid, nid, state): |
| 2888 | self.path = path |
| 2889 | self.src_path = None |
| 2890 | self.old_mode = omode |
| 2891 | self.new_mode = nmode |
| 2892 | self.old_id = oid |
| 2893 | self.new_id = nid |
| 2894 | |
| 2895 | if len(state) == 1: |
| 2896 | self.status = state |
| 2897 | self.level = None |
| 2898 | else: |
| 2899 | self.status = state[:1] |
| 2900 | self.level = state[1:] |
| 2901 | while self.level.startswith('0'): |
| 2902 | self.level = self.level[1:] |
| 2903 | |
| 2904 | info = info[1:].split(' ') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 2905 | info = _Info(path, *info) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2906 | if info.status in ('R', 'C'): |
| 2907 | info.src_path = info.path |
Anthony King | 2cd1f04 | 2014-05-05 21:24:05 +0100 | [diff] [blame] | 2908 | info.path = next(out) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2909 | r[info.path] = info |
| 2910 | return r |
| 2911 | finally: |
| 2912 | p.Wait() |
| 2913 | |
Mike Frysinger | 4b0eb5a | 2020-02-23 23:22:34 -0500 | [diff] [blame] | 2914 | def GetDotgitPath(self, subpath=None): |
| 2915 | """Return the full path to the .git dir. |
| 2916 | |
| 2917 | As a convenience, append |subpath| if provided. |
| 2918 | """ |
Shawn O. Pearce | 5b23f24 | 2009-04-17 18:43:33 -0700 | [diff] [blame] | 2919 | if self._bare: |
Mike Frysinger | 4b0eb5a | 2020-02-23 23:22:34 -0500 | [diff] [blame] | 2920 | dotgit = self._gitdir |
Shawn O. Pearce | 5b23f24 | 2009-04-17 18:43:33 -0700 | [diff] [blame] | 2921 | else: |
Mike Frysinger | 4b0eb5a | 2020-02-23 23:22:34 -0500 | [diff] [blame] | 2922 | dotgit = os.path.join(self._project.worktree, '.git') |
| 2923 | if os.path.isfile(dotgit): |
| 2924 | # Git worktrees use a "gitdir:" syntax to point to the scratch space. |
| 2925 | with open(dotgit) as fp: |
| 2926 | setting = fp.read() |
| 2927 | assert setting.startswith('gitdir:') |
| 2928 | gitdir = setting.split(':', 1)[1].strip() |
| 2929 | dotgit = os.path.normpath(os.path.join(self._project.worktree, gitdir)) |
| 2930 | |
| 2931 | return dotgit if subpath is None else os.path.join(dotgit, subpath) |
| 2932 | |
| 2933 | def GetHead(self): |
| 2934 | """Return the ref that HEAD points to.""" |
| 2935 | path = self.GetDotgitPath(subpath=HEAD) |
Conley Owens | 75ee057 | 2012-11-15 17:33:11 -0800 | [diff] [blame] | 2936 | try: |
Mike Frysinger | 3164d40 | 2019-11-11 05:40:22 -0500 | [diff] [blame] | 2937 | with open(path) as fd: |
| 2938 | line = fd.readline() |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 2939 | except IOError as e: |
| 2940 | raise NoManifestException(path, str(e)) |
Shawn O. Pearce | 76ca9f8 | 2009-04-18 14:48:03 -0700 | [diff] [blame] | 2941 | try: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 2942 | line = line.decode() |
| 2943 | except AttributeError: |
| 2944 | pass |
Shawn O. Pearce | 5b23f24 | 2009-04-17 18:43:33 -0700 | [diff] [blame] | 2945 | if line.startswith('ref: '): |
| 2946 | return line[5:-1] |
| 2947 | return line[:-1] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2948 | |
| 2949 | def SetHead(self, ref, message=None): |
| 2950 | cmdv = [] |
| 2951 | if message is not None: |
| 2952 | cmdv.extend(['-m', message]) |
| 2953 | cmdv.append(HEAD) |
| 2954 | cmdv.append(ref) |
| 2955 | self.symbolic_ref(*cmdv) |
| 2956 | |
| 2957 | def DetachHead(self, new, message=None): |
| 2958 | cmdv = ['--no-deref'] |
| 2959 | if message is not None: |
| 2960 | cmdv.extend(['-m', message]) |
| 2961 | cmdv.append(HEAD) |
| 2962 | cmdv.append(new) |
| 2963 | self.update_ref(*cmdv) |
| 2964 | |
| 2965 | def UpdateRef(self, name, new, old=None, |
| 2966 | message=None, |
| 2967 | detach=False): |
| 2968 | cmdv = [] |
| 2969 | if message is not None: |
| 2970 | cmdv.extend(['-m', message]) |
| 2971 | if detach: |
| 2972 | cmdv.append('--no-deref') |
| 2973 | cmdv.append(name) |
| 2974 | cmdv.append(new) |
| 2975 | if old is not None: |
| 2976 | cmdv.append(old) |
| 2977 | self.update_ref(*cmdv) |
| 2978 | |
| 2979 | def DeleteRef(self, name, old=None): |
| 2980 | if not old: |
| 2981 | old = self.rev_parse(name) |
| 2982 | self.update_ref('-d', name, old) |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 2983 | self._project.bare_ref.deleted(name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2984 | |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 2985 | def rev_list(self, *args, **kw): |
| 2986 | if 'format' in kw: |
| 2987 | cmdv = ['log', '--pretty=format:%s' % kw['format']] |
| 2988 | else: |
| 2989 | cmdv = ['rev-list'] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2990 | cmdv.extend(args) |
| 2991 | p = GitCommand(self._project, |
| 2992 | cmdv, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2993 | bare=self._bare, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2994 | gitdir=self._gitdir, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 2995 | capture_stdout=True, |
| 2996 | capture_stderr=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2997 | if p.Wait() != 0: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 2998 | raise GitError('%s rev-list %s: %s' % |
| 2999 | (self._project.name, str(args), p.stderr)) |
Mike Frysinger | 81f5c59 | 2019-07-04 18:13:31 -0400 | [diff] [blame] | 3000 | return p.stdout.splitlines() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3001 | |
| 3002 | def __getattr__(self, name): |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 3003 | """Allow arbitrary git commands using pythonic syntax. |
| 3004 | |
| 3005 | This allows you to do things like: |
| 3006 | git_obj.rev_parse('HEAD') |
| 3007 | |
| 3008 | Since we don't have a 'rev_parse' method defined, the __getattr__ will |
| 3009 | run. We'll replace the '_' with a '-' and try to run a git command. |
Dave Borowitz | 091f893 | 2012-10-23 17:01:04 -0700 | [diff] [blame] | 3010 | Any other positional arguments will be passed to the git command, and the |
| 3011 | following keyword arguments are supported: |
| 3012 | config: An optional dict of git config options to be passed with '-c'. |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 3013 | |
| 3014 | Args: |
| 3015 | name: The name of the git command to call. Any '_' characters will |
| 3016 | be replaced with '-'. |
| 3017 | |
| 3018 | Returns: |
| 3019 | A callable object that will try to call git with the named command. |
| 3020 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3021 | name = name.replace('_', '-') |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3022 | |
Dave Borowitz | 091f893 | 2012-10-23 17:01:04 -0700 | [diff] [blame] | 3023 | def runner(*args, **kwargs): |
| 3024 | cmdv = [] |
| 3025 | config = kwargs.pop('config', None) |
| 3026 | for k in kwargs: |
| 3027 | raise TypeError('%s() got an unexpected keyword argument %r' |
| 3028 | % (name, k)) |
| 3029 | if config is not None: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 3030 | for k, v in config.items(): |
Dave Borowitz | 091f893 | 2012-10-23 17:01:04 -0700 | [diff] [blame] | 3031 | cmdv.append('-c') |
| 3032 | cmdv.append('%s=%s' % (k, v)) |
| 3033 | cmdv.append(name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3034 | cmdv.extend(args) |
| 3035 | p = GitCommand(self._project, |
| 3036 | cmdv, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 3037 | bare=self._bare, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 3038 | gitdir=self._gitdir, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 3039 | capture_stdout=True, |
| 3040 | capture_stderr=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3041 | if p.Wait() != 0: |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3042 | raise GitError('%s %s: %s' % |
| 3043 | (self._project.name, name, p.stderr)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3044 | r = p.stdout |
| 3045 | if r.endswith('\n') and r.index('\n') == len(r) - 1: |
| 3046 | return r[:-1] |
| 3047 | return r |
| 3048 | return runner |
| 3049 | |
| 3050 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3051 | class _PriorSyncFailedError(Exception): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3052 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3053 | def __str__(self): |
| 3054 | return 'prior sync failed; rebase still in progress' |
| 3055 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3056 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3057 | class _DirtyError(Exception): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3058 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3059 | def __str__(self): |
| 3060 | return 'contains uncommitted changes' |
| 3061 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3062 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3063 | class _InfoMessage(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3064 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3065 | def __init__(self, project, text): |
| 3066 | self.project = project |
| 3067 | self.text = text |
| 3068 | |
| 3069 | def Print(self, syncbuf): |
| 3070 | syncbuf.out.info('%s/: %s', self.project.relpath, self.text) |
| 3071 | syncbuf.out.nl() |
| 3072 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3073 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3074 | class _Failure(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3075 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3076 | def __init__(self, project, why): |
| 3077 | self.project = project |
| 3078 | self.why = why |
| 3079 | |
| 3080 | def Print(self, syncbuf): |
| 3081 | syncbuf.out.fail('error: %s/: %s', |
| 3082 | self.project.relpath, |
| 3083 | str(self.why)) |
| 3084 | syncbuf.out.nl() |
| 3085 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3086 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3087 | class _Later(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3088 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3089 | def __init__(self, project, action): |
| 3090 | self.project = project |
| 3091 | self.action = action |
| 3092 | |
| 3093 | def Run(self, syncbuf): |
| 3094 | out = syncbuf.out |
| 3095 | out.project('project %s/', self.project.relpath) |
| 3096 | out.nl() |
| 3097 | try: |
| 3098 | self.action() |
| 3099 | out.nl() |
| 3100 | return True |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 3101 | except GitError: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3102 | out.nl() |
| 3103 | return False |
| 3104 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3105 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3106 | class _SyncColoring(Coloring): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3107 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3108 | def __init__(self, config): |
| 3109 | Coloring.__init__(self, config, 'reposync') |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 3110 | self.project = self.printer('header', attr='bold') |
| 3111 | self.info = self.printer('info') |
| 3112 | self.fail = self.printer('fail', fg='red') |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3113 | |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3114 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3115 | class SyncBuffer(object): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3116 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3117 | def __init__(self, config, detach_head=False): |
| 3118 | self._messages = [] |
| 3119 | self._failures = [] |
| 3120 | self._later_queue1 = [] |
| 3121 | self._later_queue2 = [] |
| 3122 | |
| 3123 | self.out = _SyncColoring(config) |
| 3124 | self.out.redirect(sys.stderr) |
| 3125 | |
| 3126 | self.detach_head = detach_head |
| 3127 | self.clean = True |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 3128 | self.recent_clean = True |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3129 | |
| 3130 | def info(self, project, fmt, *args): |
| 3131 | self._messages.append(_InfoMessage(project, fmt % args)) |
| 3132 | |
| 3133 | def fail(self, project, err=None): |
| 3134 | self._failures.append(_Failure(project, err)) |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 3135 | self._MarkUnclean() |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3136 | |
| 3137 | def later1(self, project, what): |
| 3138 | self._later_queue1.append(_Later(project, what)) |
| 3139 | |
| 3140 | def later2(self, project, what): |
| 3141 | self._later_queue2.append(_Later(project, what)) |
| 3142 | |
| 3143 | def Finish(self): |
| 3144 | self._PrintMessages() |
| 3145 | self._RunLater() |
| 3146 | self._PrintMessages() |
| 3147 | return self.clean |
| 3148 | |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 3149 | def Recently(self): |
| 3150 | recent_clean = self.recent_clean |
| 3151 | self.recent_clean = True |
| 3152 | return recent_clean |
| 3153 | |
| 3154 | def _MarkUnclean(self): |
| 3155 | self.clean = False |
| 3156 | self.recent_clean = False |
| 3157 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3158 | def _RunLater(self): |
| 3159 | for q in ['_later_queue1', '_later_queue2']: |
| 3160 | if not self._RunQueue(q): |
| 3161 | return |
| 3162 | |
| 3163 | def _RunQueue(self, queue): |
| 3164 | for m in getattr(self, queue): |
| 3165 | if not m.Run(self): |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 3166 | self._MarkUnclean() |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3167 | return False |
| 3168 | setattr(self, queue, []) |
| 3169 | return True |
| 3170 | |
| 3171 | def _PrintMessages(self): |
Mike Frysinger | 70d861f | 2019-08-26 15:22:36 -0400 | [diff] [blame] | 3172 | if self._messages or self._failures: |
| 3173 | if os.isatty(2): |
| 3174 | self.out.write(progress.CSI_ERASE_LINE) |
| 3175 | self.out.write('\r') |
| 3176 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 3177 | for m in self._messages: |
| 3178 | m.Print(self) |
| 3179 | for m in self._failures: |
| 3180 | m.Print(self) |
| 3181 | |
| 3182 | self._messages = [] |
| 3183 | self._failures = [] |
| 3184 | |
| 3185 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3186 | class MetaProject(Project): |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3187 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3188 | """A special project housed under .repo. |
| 3189 | """ |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3190 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3191 | def __init__(self, manifest, name, gitdir, worktree): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3192 | Project.__init__(self, |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 3193 | manifest=manifest, |
| 3194 | name=name, |
| 3195 | gitdir=gitdir, |
| 3196 | objdir=gitdir, |
| 3197 | worktree=worktree, |
| 3198 | remote=RemoteSpec('origin'), |
| 3199 | relpath='.repo/%s' % name, |
| 3200 | revisionExpr='refs/heads/master', |
| 3201 | revisionId=None, |
| 3202 | groups=None) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3203 | |
| 3204 | def PreSync(self): |
| 3205 | if self.Exists: |
| 3206 | cb = self.CurrentBranch |
| 3207 | if cb: |
| 3208 | base = self.GetBranch(cb).merge |
| 3209 | if base: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 3210 | self.revisionExpr = base |
| 3211 | self.revisionId = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3212 | |
Martin Kelly | 224a31a | 2017-07-10 14:46:25 -0700 | [diff] [blame] | 3213 | def MetaBranchSwitch(self, submodules=False): |
Florian Vallee | 5d01650 | 2012-06-07 17:19:26 +0200 | [diff] [blame] | 3214 | """ Prepare MetaProject for manifest branch switch |
| 3215 | """ |
| 3216 | |
| 3217 | # detach and delete manifest branch, allowing a new |
| 3218 | # branch to take over |
Anthony King | 7bdac71 | 2014-07-16 12:56:40 +0100 | [diff] [blame] | 3219 | syncbuf = SyncBuffer(self.config, detach_head=True) |
Martin Kelly | 224a31a | 2017-07-10 14:46:25 -0700 | [diff] [blame] | 3220 | self.Sync_LocalHalf(syncbuf, submodules=submodules) |
Florian Vallee | 5d01650 | 2012-06-07 17:19:26 +0200 | [diff] [blame] | 3221 | syncbuf.Finish() |
| 3222 | |
| 3223 | return GitCommand(self, |
Mark E. Hamilton | 30b0f4e | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 3224 | ['update-ref', '-d', 'refs/heads/default'], |
| 3225 | capture_stdout=True, |
| 3226 | capture_stderr=True).Wait() == 0 |
Florian Vallee | 5d01650 | 2012-06-07 17:19:26 +0200 | [diff] [blame] | 3227 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3228 | @property |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 3229 | def LastFetch(self): |
| 3230 | try: |
| 3231 | fh = os.path.join(self.gitdir, 'FETCH_HEAD') |
| 3232 | return os.path.getmtime(fh) |
| 3233 | except OSError: |
| 3234 | return 0 |
| 3235 | |
| 3236 | @property |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3237 | def HasChanges(self): |
| 3238 | """Has the remote received new commits not yet checked out? |
| 3239 | """ |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 3240 | if not self.remote or not self.revisionExpr: |
Shawn O. Pearce | 336f7bd | 2009-04-18 10:39:28 -0700 | [diff] [blame] | 3241 | return False |
| 3242 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 3243 | all_refs = self.bare_ref.all |
| 3244 | revid = self.GetRevisionId(all_refs) |
Shawn O. Pearce | 336f7bd | 2009-04-18 10:39:28 -0700 | [diff] [blame] | 3245 | head = self.work_git.GetHead() |
| 3246 | if head.startswith(R_HEADS): |
| 3247 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 3248 | head = all_refs[head] |
Shawn O. Pearce | 336f7bd | 2009-04-18 10:39:28 -0700 | [diff] [blame] | 3249 | except KeyError: |
| 3250 | head = None |
| 3251 | |
| 3252 | if revid == head: |
| 3253 | return False |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 3254 | elif self._revlist(not_rev(HEAD), revid): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3255 | return True |
| 3256 | return False |