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