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