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