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