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