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