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