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