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