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