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