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: |
Jesse Hall | 672cc49 | 2013-11-27 11:17:13 -0800 | [diff] [blame] | 86 | d = os.path.realpath(os.path.abspath(os.path.dirname(__file__))) |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 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, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 491 | objdir, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 492 | worktree, |
| 493 | relpath, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 494 | revisionExpr, |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 495 | revisionId, |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 496 | rebase = True, |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 497 | groups = None, |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 498 | sync_c = False, |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 499 | sync_s = False, |
David Pursehouse | ede7f12 | 2012-11-27 22:25:30 +0900 | [diff] [blame] | 500 | clone_depth = None, |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 501 | upstream = None, |
| 502 | parent = None, |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 503 | is_derived = False, |
| 504 | dest_branch = None): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 505 | """Init a Project object. |
| 506 | |
| 507 | Args: |
| 508 | manifest: The XmlManifest object. |
| 509 | name: The `name` attribute of manifest.xml's project element. |
| 510 | remote: RemoteSpec object specifying its remote's properties. |
| 511 | gitdir: Absolute path of git directory. |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 512 | objdir: Absolute path of directory to store git objects. |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 513 | worktree: Absolute path of git working tree. |
| 514 | relpath: Relative path of git working tree to repo's top directory. |
| 515 | revisionExpr: The `revision` attribute of manifest.xml's project element. |
| 516 | revisionId: git commit id for checking out. |
| 517 | rebase: The `rebase` attribute of manifest.xml's project element. |
| 518 | groups: The `groups` attribute of manifest.xml's project element. |
| 519 | sync_c: The `sync-c` attribute of manifest.xml's project element. |
| 520 | sync_s: The `sync-s` attribute of manifest.xml's project element. |
| 521 | upstream: The `upstream` attribute of manifest.xml's project element. |
| 522 | parent: The parent Project object. |
| 523 | is_derived: False if the project was explicitly defined in the manifest; |
| 524 | True if the project is a discovered submodule. |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 525 | 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] | 526 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 527 | self.manifest = manifest |
| 528 | self.name = name |
| 529 | self.remote = remote |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 530 | self.gitdir = gitdir.replace('\\', '/') |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 531 | self.objdir = objdir.replace('\\', '/') |
Shawn O. Pearce | 0ce6ca9 | 2011-01-10 13:26:01 -0800 | [diff] [blame] | 532 | if worktree: |
| 533 | self.worktree = worktree.replace('\\', '/') |
| 534 | else: |
| 535 | self.worktree = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 536 | self.relpath = relpath |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 537 | self.revisionExpr = revisionExpr |
| 538 | |
| 539 | if revisionId is None \ |
| 540 | and revisionExpr \ |
| 541 | and IsId(revisionExpr): |
| 542 | self.revisionId = revisionExpr |
| 543 | else: |
| 544 | self.revisionId = revisionId |
| 545 | |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 546 | self.rebase = rebase |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 547 | self.groups = groups |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 548 | self.sync_c = sync_c |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 549 | self.sync_s = sync_s |
David Pursehouse | ede7f12 | 2012-11-27 22:25:30 +0900 | [diff] [blame] | 550 | self.clone_depth = clone_depth |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 551 | self.upstream = upstream |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 552 | self.parent = parent |
| 553 | self.is_derived = is_derived |
| 554 | self.subprojects = [] |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 555 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 556 | self.snapshots = {} |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 557 | self.copyfiles = [] |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 558 | self.annotations = [] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 559 | self.config = GitConfig.ForRepository( |
| 560 | gitdir = self.gitdir, |
| 561 | defaults = self.manifest.globalConfig) |
| 562 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 563 | if self.worktree: |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 564 | self.work_git = self._GitGetByExec(self, bare=False, gitdir=gitdir) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 565 | else: |
| 566 | self.work_git = None |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 567 | self.bare_git = self._GitGetByExec(self, bare=True, gitdir=gitdir) |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 568 | self.bare_ref = GitRefs(gitdir) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 569 | self.bare_objdir = self._GitGetByExec(self, bare=True, gitdir=objdir) |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 570 | self.dest_branch = dest_branch |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 571 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 572 | # This will be filled in if a project is later identified to be the |
| 573 | # project containing repo hooks. |
| 574 | self.enabled_repo_hooks = [] |
| 575 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 576 | @property |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 577 | def Derived(self): |
| 578 | return self.is_derived |
| 579 | |
| 580 | @property |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 581 | def Exists(self): |
| 582 | return os.path.isdir(self.gitdir) |
| 583 | |
| 584 | @property |
| 585 | def CurrentBranch(self): |
| 586 | """Obtain the name of the currently checked out branch. |
| 587 | The branch name omits the 'refs/heads/' prefix. |
| 588 | None is returned if the project is on a detached HEAD. |
| 589 | """ |
Shawn O. Pearce | 5b23f24 | 2009-04-17 18:43:33 -0700 | [diff] [blame] | 590 | b = self.work_git.GetHead() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 591 | if b.startswith(R_HEADS): |
| 592 | return b[len(R_HEADS):] |
| 593 | return None |
| 594 | |
Shawn O. Pearce | 3d2cdd0 | 2009-04-18 15:26:10 -0700 | [diff] [blame] | 595 | def IsRebaseInProgress(self): |
| 596 | w = self.worktree |
| 597 | g = os.path.join(w, '.git') |
| 598 | return os.path.exists(os.path.join(g, 'rebase-apply')) \ |
| 599 | or os.path.exists(os.path.join(g, 'rebase-merge')) \ |
| 600 | or os.path.exists(os.path.join(w, '.dotest')) |
Julius Gustavsson | 0cb1b3f | 2010-06-17 17:55:02 +0200 | [diff] [blame] | 601 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 602 | def IsDirty(self, consider_untracked=True): |
| 603 | """Is the working directory modified in some way? |
| 604 | """ |
| 605 | self.work_git.update_index('-q', |
| 606 | '--unmerged', |
| 607 | '--ignore-missing', |
| 608 | '--refresh') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 609 | 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] | 610 | return True |
| 611 | if self.work_git.DiffZ('diff-files'): |
| 612 | return True |
| 613 | if consider_untracked and self.work_git.LsOthers(): |
| 614 | return True |
| 615 | return False |
| 616 | |
| 617 | _userident_name = None |
| 618 | _userident_email = None |
| 619 | |
| 620 | @property |
| 621 | def UserName(self): |
| 622 | """Obtain the user's personal name. |
| 623 | """ |
| 624 | if self._userident_name is None: |
| 625 | self._LoadUserIdentity() |
| 626 | return self._userident_name |
| 627 | |
| 628 | @property |
| 629 | def UserEmail(self): |
| 630 | """Obtain the user's email address. This is very likely |
| 631 | to be their Gerrit login. |
| 632 | """ |
| 633 | if self._userident_email is None: |
| 634 | self._LoadUserIdentity() |
| 635 | return self._userident_email |
| 636 | |
| 637 | def _LoadUserIdentity(self): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 638 | u = self.bare_git.var('GIT_COMMITTER_IDENT') |
| 639 | m = re.compile("^(.*) <([^>]*)> ").match(u) |
| 640 | if m: |
| 641 | self._userident_name = m.group(1) |
| 642 | self._userident_email = m.group(2) |
| 643 | else: |
| 644 | self._userident_name = '' |
| 645 | self._userident_email = '' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 646 | |
| 647 | def GetRemote(self, name): |
| 648 | """Get the configuration for a single remote. |
| 649 | """ |
| 650 | return self.config.GetRemote(name) |
| 651 | |
| 652 | def GetBranch(self, name): |
| 653 | """Get the configuration for a single branch. |
| 654 | """ |
| 655 | return self.config.GetBranch(name) |
| 656 | |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 657 | def GetBranches(self): |
| 658 | """Get all existing local branches. |
| 659 | """ |
| 660 | current = self.CurrentBranch |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 661 | all_refs = self._allrefs |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 662 | heads = {} |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 663 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 664 | for name, ref_id in all_refs.items(): |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 665 | if name.startswith(R_HEADS): |
| 666 | name = name[len(R_HEADS):] |
| 667 | b = self.GetBranch(name) |
| 668 | b.current = name == current |
| 669 | b.published = None |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 670 | b.revision = ref_id |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 671 | heads[name] = b |
| 672 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 673 | for name, ref_id in all_refs.items(): |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 674 | if name.startswith(R_PUB): |
| 675 | name = name[len(R_PUB):] |
| 676 | b = heads.get(name) |
| 677 | if b: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 678 | b.published = ref_id |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 679 | |
| 680 | return heads |
| 681 | |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 682 | def MatchesGroups(self, manifest_groups): |
| 683 | """Returns true if the manifest groups specified at init should cause |
| 684 | this project to be synced. |
| 685 | Prefixing a manifest group with "-" inverts the meaning of a group. |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 686 | All projects are implicitly labelled with "all". |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 687 | |
| 688 | labels are resolved in order. In the example case of |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 689 | project_groups: "all,group1,group2" |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 690 | manifest_groups: "-group1,group2" |
| 691 | the project will be matched. |
David Holmer | 0a1c6a1 | 2012-11-14 19:19:00 -0500 | [diff] [blame] | 692 | |
| 693 | The special manifest group "default" will match any project that |
| 694 | does not have the special project group "notdefault" |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 695 | """ |
David Holmer | 0a1c6a1 | 2012-11-14 19:19:00 -0500 | [diff] [blame] | 696 | expanded_manifest_groups = manifest_groups or ['default'] |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 697 | expanded_project_groups = ['all'] + (self.groups or []) |
David Holmer | 0a1c6a1 | 2012-11-14 19:19:00 -0500 | [diff] [blame] | 698 | if not 'notdefault' in expanded_project_groups: |
| 699 | expanded_project_groups += ['default'] |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 700 | |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 701 | matched = False |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 702 | for group in expanded_manifest_groups: |
| 703 | if group.startswith('-') and group[1:] in expanded_project_groups: |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 704 | matched = False |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 705 | elif group in expanded_project_groups: |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 706 | matched = True |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 707 | |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 708 | return matched |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 709 | |
| 710 | ## Status Display ## |
| 711 | |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 712 | def HasChanges(self): |
| 713 | """Returns true if there are uncommitted changes. |
| 714 | """ |
| 715 | self.work_git.update_index('-q', |
| 716 | '--unmerged', |
| 717 | '--ignore-missing', |
| 718 | '--refresh') |
| 719 | if self.IsRebaseInProgress(): |
| 720 | return True |
| 721 | |
| 722 | if self.work_git.DiffZ('diff-index', '--cached', HEAD): |
| 723 | return True |
| 724 | |
| 725 | if self.work_git.DiffZ('diff-files'): |
| 726 | return True |
| 727 | |
| 728 | if self.work_git.LsOthers(): |
| 729 | return True |
| 730 | |
| 731 | return False |
| 732 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 733 | def PrintWorkTreeStatus(self, output_redir=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 734 | """Prints the status of the repository to stdout. |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 735 | |
| 736 | Args: |
| 737 | output: If specified, redirect the output to this object. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 738 | """ |
| 739 | if not os.path.isdir(self.worktree): |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 740 | if output_redir == None: |
| 741 | output_redir = sys.stdout |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 742 | print(file=output_redir) |
| 743 | print('project %s/' % self.relpath, file=output_redir) |
| 744 | print(' missing (run "repo sync")', file=output_redir) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 745 | return |
| 746 | |
| 747 | self.work_git.update_index('-q', |
| 748 | '--unmerged', |
| 749 | '--ignore-missing', |
| 750 | '--refresh') |
Shawn O. Pearce | 3d2cdd0 | 2009-04-18 15:26:10 -0700 | [diff] [blame] | 751 | rb = self.IsRebaseInProgress() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 752 | di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD) |
| 753 | df = self.work_git.DiffZ('diff-files') |
| 754 | do = self.work_git.LsOthers() |
Ali Utku Selen | 76abcc1 | 2012-01-25 10:51:12 +0100 | [diff] [blame] | 755 | 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] | 756 | return 'CLEAN' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 757 | |
| 758 | out = StatusColoring(self.config) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 759 | if not output_redir == None: |
| 760 | out.redirect(output_redir) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 761 | out.project('project %-40s', self.relpath + '/') |
| 762 | |
| 763 | branch = self.CurrentBranch |
| 764 | if branch is None: |
| 765 | out.nobranch('(*** NO BRANCH ***)') |
| 766 | else: |
| 767 | out.branch('branch %s', branch) |
| 768 | out.nl() |
| 769 | |
Shawn O. Pearce | 3d2cdd0 | 2009-04-18 15:26:10 -0700 | [diff] [blame] | 770 | if rb: |
| 771 | out.important('prior sync failed; rebase still in progress') |
| 772 | out.nl() |
| 773 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 774 | paths = list() |
| 775 | paths.extend(di.keys()) |
| 776 | paths.extend(df.keys()) |
| 777 | paths.extend(do) |
| 778 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 779 | for p in sorted(set(paths)): |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 780 | try: |
| 781 | i = di[p] |
| 782 | except KeyError: |
| 783 | i = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 784 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 785 | try: |
| 786 | f = df[p] |
| 787 | except KeyError: |
| 788 | f = None |
Julius Gustavsson | 0cb1b3f | 2010-06-17 17:55:02 +0200 | [diff] [blame] | 789 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 790 | if i: |
| 791 | i_status = i.status.upper() |
| 792 | else: |
| 793 | i_status = '-' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 794 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 795 | if f: |
| 796 | f_status = f.status.lower() |
| 797 | else: |
| 798 | f_status = '-' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 799 | |
| 800 | if i and i.src_path: |
Shawn O. Pearce | fe08675 | 2009-03-03 13:49:48 -0800 | [diff] [blame] | 801 | 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] | 802 | i.src_path, p, i.level) |
| 803 | else: |
| 804 | line = ' %s%s\t%s' % (i_status, f_status, p) |
| 805 | |
| 806 | if i and not f: |
| 807 | out.added('%s', line) |
| 808 | elif (i and f) or (not i and f): |
| 809 | out.changed('%s', line) |
| 810 | elif not i and not f: |
| 811 | out.untracked('%s', line) |
| 812 | else: |
| 813 | out.write('%s', line) |
| 814 | out.nl() |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 815 | |
Shawn O. Pearce | 161f445 | 2009-04-10 17:41:44 -0700 | [diff] [blame] | 816 | return 'DIRTY' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 817 | |
pelya | d67872d | 2012-03-28 14:49:58 +0300 | [diff] [blame] | 818 | def PrintWorkTreeDiff(self, absolute_paths=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 819 | """Prints the status of the repository to stdout. |
| 820 | """ |
| 821 | out = DiffColoring(self.config) |
| 822 | cmd = ['diff'] |
| 823 | if out.is_on: |
| 824 | cmd.append('--color') |
| 825 | cmd.append(HEAD) |
pelya | d67872d | 2012-03-28 14:49:58 +0300 | [diff] [blame] | 826 | if absolute_paths: |
| 827 | cmd.append('--src-prefix=a/%s/' % self.relpath) |
| 828 | cmd.append('--dst-prefix=b/%s/' % self.relpath) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 829 | cmd.append('--') |
| 830 | p = GitCommand(self, |
| 831 | cmd, |
| 832 | capture_stdout = True, |
| 833 | capture_stderr = True) |
| 834 | has_diff = False |
| 835 | for line in p.process.stdout: |
| 836 | if not has_diff: |
| 837 | out.nl() |
| 838 | out.project('project %s/' % self.relpath) |
| 839 | out.nl() |
| 840 | has_diff = True |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 841 | print(line[:-1]) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 842 | p.Wait() |
| 843 | |
| 844 | |
| 845 | ## Publish / Upload ## |
| 846 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 847 | def WasPublished(self, branch, all_refs=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 848 | """Was the branch published (uploaded) for code review? |
| 849 | If so, returns the SHA-1 hash of the last published |
| 850 | state for the branch. |
| 851 | """ |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 852 | key = R_PUB + branch |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 853 | if all_refs is None: |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 854 | try: |
| 855 | return self.bare_git.rev_parse(key) |
| 856 | except GitError: |
| 857 | return None |
| 858 | else: |
| 859 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 860 | return all_refs[key] |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 861 | except KeyError: |
| 862 | return None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 863 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 864 | def CleanPublishedCache(self, all_refs=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 865 | """Prunes any stale published refs. |
| 866 | """ |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 867 | if all_refs is None: |
| 868 | all_refs = self._allrefs |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 869 | heads = set() |
| 870 | canrm = {} |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 871 | for name, ref_id in all_refs.items(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 872 | if name.startswith(R_HEADS): |
| 873 | heads.add(name) |
| 874 | elif name.startswith(R_PUB): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 875 | canrm[name] = ref_id |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 876 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 877 | for name, ref_id in canrm.items(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 878 | n = name[len(R_PUB):] |
| 879 | if R_HEADS + n not in heads: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 880 | self.bare_git.DeleteRef(name, ref_id) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 881 | |
Mandeep Singh Baines | d6c93a2 | 2011-05-26 10:34:11 -0700 | [diff] [blame] | 882 | def GetUploadableBranches(self, selected_branch=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 883 | """List any branches which can be uploaded for review. |
| 884 | """ |
| 885 | heads = {} |
| 886 | pubed = {} |
| 887 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 888 | for name, ref_id in self._allrefs.items(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 889 | if name.startswith(R_HEADS): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 890 | heads[name[len(R_HEADS):]] = ref_id |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 891 | elif name.startswith(R_PUB): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 892 | pubed[name[len(R_PUB):]] = ref_id |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 893 | |
| 894 | ready = [] |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 895 | for branch, ref_id in heads.items(): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 896 | if branch in pubed and pubed[branch] == ref_id: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 897 | continue |
Mandeep Singh Baines | d6c93a2 | 2011-05-26 10:34:11 -0700 | [diff] [blame] | 898 | if selected_branch and branch != selected_branch: |
| 899 | continue |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 900 | |
Shawn O. Pearce | 35f2596 | 2008-11-11 17:03:13 -0800 | [diff] [blame] | 901 | rb = self.GetUploadableBranch(branch) |
| 902 | if rb: |
| 903 | ready.append(rb) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 904 | return ready |
| 905 | |
Shawn O. Pearce | 35f2596 | 2008-11-11 17:03:13 -0800 | [diff] [blame] | 906 | def GetUploadableBranch(self, branch_name): |
| 907 | """Get a single uploadable branch, or None. |
| 908 | """ |
| 909 | branch = self.GetBranch(branch_name) |
| 910 | base = branch.LocalMerge |
| 911 | if branch.LocalMerge: |
| 912 | rb = ReviewableBranch(self, branch, base) |
| 913 | if rb.commits: |
| 914 | return rb |
| 915 | return None |
| 916 | |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 917 | def UploadForReview(self, branch=None, |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 918 | people=([],[]), |
Brian Harring | 435370c | 2012-07-28 15:37:04 -0700 | [diff] [blame] | 919 | auto_topic=False, |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 920 | draft=False, |
| 921 | dest_branch=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 922 | """Uploads the named branch for code review. |
| 923 | """ |
| 924 | if branch is None: |
| 925 | branch = self.CurrentBranch |
| 926 | if branch is None: |
| 927 | raise GitError('not currently on a branch') |
| 928 | |
| 929 | branch = self.GetBranch(branch) |
| 930 | if not branch.LocalMerge: |
| 931 | raise GitError('branch %s does not track a remote' % branch.name) |
| 932 | if not branch.remote.review: |
| 933 | raise GitError('remote %s has no review url' % branch.remote.name) |
| 934 | |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 935 | if dest_branch is None: |
| 936 | dest_branch = self.dest_branch |
| 937 | if dest_branch is None: |
| 938 | dest_branch = branch.merge |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 939 | if not dest_branch.startswith(R_HEADS): |
| 940 | dest_branch = R_HEADS + dest_branch |
| 941 | |
Shawn O. Pearce | 339ba9f | 2008-11-06 09:52:51 -0800 | [diff] [blame] | 942 | if not branch.remote.projectname: |
| 943 | branch.remote.projectname = self.name |
| 944 | branch.remote.Save() |
| 945 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 946 | url = branch.remote.ReviewUrl(self.UserEmail) |
| 947 | if url is None: |
| 948 | raise UploadError('review not configured') |
| 949 | cmd = ['push'] |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 950 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 951 | if url.startswith('ssh://'): |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 952 | rp = ['gerrit receive-pack'] |
| 953 | for e in people[0]: |
| 954 | rp.append('--reviewer=%s' % sq(e)) |
| 955 | for e in people[1]: |
| 956 | rp.append('--cc=%s' % sq(e)) |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 957 | cmd.append('--receive-pack=%s' % " ".join(rp)) |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 958 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 959 | cmd.append(url) |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 960 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 961 | if dest_branch.startswith(R_HEADS): |
| 962 | dest_branch = dest_branch[len(R_HEADS):] |
Brian Harring | 435370c | 2012-07-28 15:37:04 -0700 | [diff] [blame] | 963 | |
| 964 | upload_type = 'for' |
| 965 | if draft: |
| 966 | upload_type = 'drafts' |
| 967 | |
| 968 | ref_spec = '%s:refs/%s/%s' % (R_HEADS + branch.name, upload_type, |
| 969 | dest_branch) |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 970 | if auto_topic: |
| 971 | ref_spec = ref_spec + '/' + branch.name |
Shawn Pearce | 45d2168 | 2013-02-28 00:35:51 -0800 | [diff] [blame] | 972 | if not url.startswith('ssh://'): |
| 973 | rp = ['r=%s' % p for p in people[0]] + \ |
| 974 | ['cc=%s' % p for p in people[1]] |
| 975 | if rp: |
| 976 | ref_spec = ref_spec + '%' + ','.join(rp) |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 977 | cmd.append(ref_spec) |
| 978 | |
| 979 | if GitCommand(self, cmd, bare = True).Wait() != 0: |
| 980 | raise UploadError('Upload failed') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 981 | |
| 982 | msg = "posted to %s for %s" % (branch.remote.review, dest_branch) |
| 983 | self.bare_git.UpdateRef(R_PUB + branch.name, |
| 984 | R_HEADS + branch.name, |
| 985 | message = msg) |
| 986 | |
| 987 | |
| 988 | ## Sync ## |
| 989 | |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 990 | def _ExtractArchive(self, tarpath, path=None): |
| 991 | """Extract the given tar on its current location |
| 992 | |
| 993 | Args: |
| 994 | - tarpath: The path to the actual tar file |
| 995 | |
| 996 | """ |
| 997 | try: |
| 998 | with tarfile.open(tarpath, 'r') as tar: |
| 999 | tar.extractall(path=path) |
| 1000 | return True |
| 1001 | except (IOError, tarfile.TarError) as e: |
| 1002 | print("error: Cannot extract archive %s: " |
| 1003 | "%s" % (tarpath, str(e)), file=sys.stderr) |
| 1004 | return False |
| 1005 | |
Shawn O. Pearce | e02ac0a | 2012-03-14 15:36:59 -0700 | [diff] [blame] | 1006 | def Sync_NetworkHalf(self, |
| 1007 | quiet=False, |
| 1008 | is_new=None, |
| 1009 | current_branch_only=False, |
Mitchel Humpherys | 597868b | 2012-10-29 10:18:34 -0700 | [diff] [blame] | 1010 | clone_bundle=True, |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1011 | no_tags=False, |
| 1012 | archive=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1013 | """Perform only the network IO portion of the sync process. |
| 1014 | Local working directory/branch state is not affected. |
| 1015 | """ |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1016 | if archive and not isinstance(self, MetaProject): |
| 1017 | if self.remote.url.startswith(('http://', 'https://')): |
| 1018 | print("error: %s: Cannot fetch archives from http/https " |
| 1019 | "remotes." % self.name, file=sys.stderr) |
| 1020 | return False |
| 1021 | |
| 1022 | name = self.relpath.replace('\\', '/') |
| 1023 | name = name.replace('/', '_') |
| 1024 | tarpath = '%s.tar' % name |
| 1025 | topdir = self.manifest.topdir |
| 1026 | |
| 1027 | try: |
| 1028 | self._FetchArchive(tarpath, cwd=topdir) |
| 1029 | except GitError as e: |
| 1030 | print('error: %s' % str(e), file=sys.stderr) |
| 1031 | return False |
| 1032 | |
| 1033 | # From now on, we only need absolute tarpath |
| 1034 | tarpath = os.path.join(topdir, tarpath) |
| 1035 | |
| 1036 | if not self._ExtractArchive(tarpath, path=topdir): |
| 1037 | return False |
| 1038 | try: |
| 1039 | os.remove(tarpath) |
| 1040 | except OSError as e: |
| 1041 | print("warn: Cannot remove archive %s: " |
| 1042 | "%s" % (tarpath, str(e)), file=sys.stderr) |
| 1043 | self._CopyFiles() |
| 1044 | return True |
| 1045 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 1046 | if is_new is None: |
| 1047 | is_new = not self.Exists |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1048 | if is_new: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1049 | self._InitGitDir() |
Jimmie Wester | a044458 | 2012-10-24 13:44:42 +0200 | [diff] [blame] | 1050 | else: |
| 1051 | self._UpdateHooks() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1052 | self._InitRemote() |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1053 | |
| 1054 | if is_new: |
| 1055 | alt = os.path.join(self.gitdir, 'objects/info/alternates') |
| 1056 | try: |
| 1057 | fd = open(alt, 'rb') |
| 1058 | try: |
| 1059 | alt_dir = fd.readline().rstrip() |
| 1060 | finally: |
| 1061 | fd.close() |
| 1062 | except IOError: |
| 1063 | alt_dir = None |
| 1064 | else: |
| 1065 | alt_dir = None |
| 1066 | |
Shawn O. Pearce | e02ac0a | 2012-03-14 15:36:59 -0700 | [diff] [blame] | 1067 | if clone_bundle \ |
| 1068 | and alt_dir is None \ |
| 1069 | and self._ApplyCloneBundle(initial=is_new, quiet=quiet): |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1070 | is_new = False |
| 1071 | |
Shawn O. Pearce | 6ba6ba0 | 2012-05-24 09:46:50 -0700 | [diff] [blame] | 1072 | if not current_branch_only: |
| 1073 | if self.sync_c: |
| 1074 | current_branch_only = True |
| 1075 | elif not self.manifest._loaded: |
| 1076 | # Manifest cannot check defaults until it syncs. |
| 1077 | current_branch_only = False |
| 1078 | elif self.manifest.default.sync_c: |
| 1079 | current_branch_only = True |
| 1080 | |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1081 | 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] | 1082 | current_branch_only=current_branch_only, |
| 1083 | no_tags=no_tags): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1084 | return False |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1085 | |
| 1086 | if self.worktree: |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1087 | self._InitMRef() |
| 1088 | else: |
| 1089 | self._InitMirrorHead() |
| 1090 | try: |
| 1091 | os.remove(os.path.join(self.gitdir, 'FETCH_HEAD')) |
| 1092 | except OSError: |
| 1093 | pass |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1094 | return True |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 1095 | |
| 1096 | def PostRepoUpgrade(self): |
| 1097 | self._InitHooks() |
| 1098 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1099 | def _CopyFiles(self): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1100 | for copyfile in self.copyfiles: |
| 1101 | copyfile._Copy() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1102 | |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame^] | 1103 | def GetCommitRevisionId(self): |
| 1104 | """Get revisionId of a commit. |
| 1105 | |
| 1106 | Use this method instead of GetRevisionId to get the id of the commit rather |
| 1107 | than the id of the current git object (for example, a tag) |
| 1108 | |
| 1109 | """ |
| 1110 | if not self.revisionExpr.startswith(R_TAGS): |
| 1111 | return self.GetRevisionId(self._allrefs) |
| 1112 | |
| 1113 | try: |
| 1114 | return self.bare_git.rev_list(self.revisionExpr, '-1')[0] |
| 1115 | except GitError: |
| 1116 | raise ManifestInvalidRevisionError( |
| 1117 | 'revision %s in %s not found' % (self.revisionExpr, |
| 1118 | self.name)) |
| 1119 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1120 | def GetRevisionId(self, all_refs=None): |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1121 | if self.revisionId: |
| 1122 | return self.revisionId |
| 1123 | |
| 1124 | rem = self.GetRemote(self.remote.name) |
| 1125 | rev = rem.ToLocal(self.revisionExpr) |
| 1126 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1127 | if all_refs is not None and rev in all_refs: |
| 1128 | return all_refs[rev] |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1129 | |
| 1130 | try: |
| 1131 | return self.bare_git.rev_parse('--verify', '%s^0' % rev) |
| 1132 | except GitError: |
| 1133 | raise ManifestInvalidRevisionError( |
| 1134 | 'revision %s in %s not found' % (self.revisionExpr, |
| 1135 | self.name)) |
| 1136 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1137 | def Sync_LocalHalf(self, syncbuf): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1138 | """Perform only the local IO portion of the sync process. |
| 1139 | Network access is not required. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1140 | """ |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1141 | self._InitWorkTree() |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1142 | all_refs = self.bare_ref.all |
| 1143 | self.CleanPublishedCache(all_refs) |
| 1144 | revid = self.GetRevisionId(all_refs) |
Skyler Kaufman | 835cd68 | 2011-03-08 12:14:41 -0800 | [diff] [blame] | 1145 | |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 1146 | def _doff(): |
| 1147 | self._FastForward(revid) |
| 1148 | self._CopyFiles() |
| 1149 | |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1150 | head = self.work_git.GetHead() |
| 1151 | if head.startswith(R_HEADS): |
| 1152 | branch = head[len(R_HEADS):] |
| 1153 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1154 | head = all_refs[head] |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1155 | except KeyError: |
| 1156 | head = None |
| 1157 | else: |
| 1158 | branch = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1159 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1160 | if branch is None or syncbuf.detach_head: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1161 | # Currently on a detached HEAD. The user is assumed to |
| 1162 | # not have any local modifications worth worrying about. |
| 1163 | # |
Shawn O. Pearce | 3d2cdd0 | 2009-04-18 15:26:10 -0700 | [diff] [blame] | 1164 | if self.IsRebaseInProgress(): |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1165 | syncbuf.fail(self, _PriorSyncFailedError()) |
| 1166 | return |
| 1167 | |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1168 | if head == revid: |
| 1169 | # No changes; don't do anything further. |
Florian Vallee | 7cf1b36 | 2012-06-07 17:11:42 +0200 | [diff] [blame] | 1170 | # Except if the head needs to be detached |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1171 | # |
Florian Vallee | 7cf1b36 | 2012-06-07 17:11:42 +0200 | [diff] [blame] | 1172 | if not syncbuf.detach_head: |
| 1173 | return |
| 1174 | else: |
| 1175 | lost = self._revlist(not_rev(revid), HEAD) |
| 1176 | if lost: |
| 1177 | syncbuf.info(self, "discarding %d commits", len(lost)) |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1178 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1179 | try: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1180 | self._Checkout(revid, quiet=True) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 1181 | except GitError as e: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1182 | syncbuf.fail(self, e) |
| 1183 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1184 | self._CopyFiles() |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1185 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1186 | |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 1187 | if head == revid: |
| 1188 | # No changes; don't do anything further. |
| 1189 | # |
| 1190 | return |
| 1191 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1192 | branch = self.GetBranch(branch) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1193 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1194 | if not branch.LocalMerge: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1195 | # The current branch has no tracking configuration. |
Anatol Pomazau | 2a32f6a | 2011-08-30 10:52:33 -0700 | [diff] [blame] | 1196 | # Jump off it to a detached HEAD. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1197 | # |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1198 | syncbuf.info(self, |
| 1199 | "leaving %s; does not track upstream", |
| 1200 | branch.name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1201 | try: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1202 | self._Checkout(revid, quiet=True) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 1203 | except GitError as e: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1204 | syncbuf.fail(self, e) |
| 1205 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1206 | self._CopyFiles() |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1207 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1208 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1209 | upstream_gain = self._revlist(not_rev(HEAD), revid) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1210 | pub = self.WasPublished(branch.name, all_refs) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1211 | if pub: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1212 | not_merged = self._revlist(not_rev(revid), pub) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1213 | if not_merged: |
| 1214 | if upstream_gain: |
| 1215 | # The user has published this branch and some of those |
| 1216 | # commits are not yet merged upstream. We do not want |
| 1217 | # to rewrite the published commits so we punt. |
| 1218 | # |
Daniel Sandler | 4c50dee | 2010-03-02 15:38:03 -0500 | [diff] [blame] | 1219 | syncbuf.fail(self, |
| 1220 | "branch %s is published (but not merged) and is now %d commits behind" |
| 1221 | % (branch.name, len(upstream_gain))) |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1222 | return |
Shawn O. Pearce | 05f66b6 | 2009-04-21 08:26:32 -0700 | [diff] [blame] | 1223 | elif pub == head: |
| 1224 | # All published commits are merged, and thus we are a |
| 1225 | # strict subset. We can fast-forward safely. |
Shawn O. Pearce | a54c527 | 2008-10-30 11:03:00 -0700 | [diff] [blame] | 1226 | # |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1227 | syncbuf.later1(self, _doff) |
| 1228 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1229 | |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1230 | # Examine the local commits not in the remote. Find the |
| 1231 | # last one attributed to this user, if any. |
| 1232 | # |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1233 | local_changes = self._revlist(not_rev(revid), HEAD, format='%H %ce') |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1234 | last_mine = None |
| 1235 | cnt_mine = 0 |
| 1236 | for commit in local_changes: |
Chirayu Desai | 0eb35cb | 2013-11-19 18:46:29 +0530 | [diff] [blame] | 1237 | commit_id, committer_email = commit.decode('utf-8').split(' ', 1) |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1238 | if committer_email == self.UserEmail: |
| 1239 | last_mine = commit_id |
| 1240 | cnt_mine += 1 |
| 1241 | |
Shawn O. Pearce | da88ff4 | 2009-06-03 11:09:12 -0700 | [diff] [blame] | 1242 | if not upstream_gain and cnt_mine == len(local_changes): |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1243 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1244 | |
| 1245 | if self.IsDirty(consider_untracked=False): |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1246 | syncbuf.fail(self, _DirtyError()) |
| 1247 | return |
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 | # If the upstream switched on us, warn the user. |
| 1250 | # |
| 1251 | if branch.merge != self.revisionExpr: |
| 1252 | if branch.merge and self.revisionExpr: |
| 1253 | syncbuf.info(self, |
| 1254 | 'manifest switched %s...%s', |
| 1255 | branch.merge, |
| 1256 | self.revisionExpr) |
| 1257 | elif branch.merge: |
| 1258 | syncbuf.info(self, |
| 1259 | 'manifest no longer tracks %s', |
| 1260 | branch.merge) |
| 1261 | |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1262 | if cnt_mine < len(local_changes): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1263 | # Upstream rebased. Not everything in HEAD |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1264 | # was created by this user. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1265 | # |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1266 | syncbuf.info(self, |
| 1267 | "discarding %d commits removed from upstream", |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1268 | len(local_changes) - cnt_mine) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1269 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1270 | branch.remote = self.GetRemote(self.remote.name) |
Anatol Pomazau | cd7c5de | 2012-03-20 13:45:00 -0700 | [diff] [blame] | 1271 | if not ID_RE.match(self.revisionExpr): |
| 1272 | # in case of manifest sync the revisionExpr might be a SHA1 |
| 1273 | branch.merge = self.revisionExpr |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1274 | branch.Save() |
| 1275 | |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 1276 | if cnt_mine > 0 and self.rebase: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1277 | def _dorebase(): |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1278 | self._Rebase(upstream = '%s^1' % last_mine, onto = revid) |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1279 | self._CopyFiles() |
| 1280 | syncbuf.later2(self, _dorebase) |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 1281 | elif local_changes: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1282 | try: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1283 | self._ResetHard(revid) |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1284 | self._CopyFiles() |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 1285 | except GitError as e: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1286 | syncbuf.fail(self, e) |
| 1287 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1288 | else: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 1289 | syncbuf.later1(self, _doff) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1290 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 1291 | def AddCopyFile(self, src, dest, absdest): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1292 | # dest should already be an absolute path, but src is project relative |
| 1293 | # make src an absolute path |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 1294 | abssrc = os.path.join(self.worktree, src) |
| 1295 | self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1296 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 1297 | def AddAnnotation(self, name, value, keep): |
| 1298 | self.annotations.append(_Annotation(name, value, keep)) |
| 1299 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 1300 | def DownloadPatchSet(self, change_id, patch_id): |
| 1301 | """Download a single patch set of a single change to FETCH_HEAD. |
| 1302 | """ |
| 1303 | remote = self.GetRemote(self.remote.name) |
| 1304 | |
| 1305 | cmd = ['fetch', remote.name] |
| 1306 | cmd.append('refs/changes/%2.2d/%d/%d' \ |
| 1307 | % (change_id % 100, change_id, patch_id)) |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 1308 | if GitCommand(self, cmd, bare=True).Wait() != 0: |
| 1309 | return None |
| 1310 | return DownloadedChange(self, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1311 | self.GetRevisionId(), |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 1312 | change_id, |
| 1313 | patch_id, |
| 1314 | self.bare_git.rev_parse('FETCH_HEAD')) |
| 1315 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1316 | |
| 1317 | ## Branch Management ## |
| 1318 | |
| 1319 | def StartBranch(self, name): |
| 1320 | """Create a new branch off the manifest's revision. |
| 1321 | """ |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1322 | head = self.work_git.GetHead() |
| 1323 | if head == (R_HEADS + name): |
| 1324 | return True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1325 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1326 | all_refs = self.bare_ref.all |
| 1327 | if (R_HEADS + name) in all_refs: |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1328 | return GitCommand(self, |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1329 | ['checkout', name, '--'], |
Shawn O. Pearce | 0f0dfa3 | 2009-04-18 14:53:39 -0700 | [diff] [blame] | 1330 | capture_stdout = True, |
| 1331 | capture_stderr = True).Wait() == 0 |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 1332 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1333 | branch = self.GetBranch(name) |
| 1334 | branch.remote = self.GetRemote(self.remote.name) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1335 | branch.merge = self.revisionExpr |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1336 | revid = self.GetRevisionId(all_refs) |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 1337 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1338 | if head.startswith(R_HEADS): |
| 1339 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1340 | head = all_refs[head] |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1341 | except KeyError: |
| 1342 | head = None |
| 1343 | |
| 1344 | if revid and head and revid == head: |
| 1345 | ref = os.path.join(self.gitdir, R_HEADS + name) |
| 1346 | try: |
| 1347 | os.makedirs(os.path.dirname(ref)) |
| 1348 | except OSError: |
| 1349 | pass |
| 1350 | _lwrite(ref, '%s\n' % revid) |
| 1351 | _lwrite(os.path.join(self.worktree, '.git', HEAD), |
| 1352 | 'ref: %s%s\n' % (R_HEADS, name)) |
| 1353 | branch.Save() |
| 1354 | return True |
| 1355 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1356 | if GitCommand(self, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1357 | ['checkout', '-b', branch.name, revid], |
Shawn O. Pearce | 0f0dfa3 | 2009-04-18 14:53:39 -0700 | [diff] [blame] | 1358 | capture_stdout = True, |
| 1359 | capture_stderr = True).Wait() == 0: |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 1360 | branch.Save() |
| 1361 | return True |
| 1362 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1363 | |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1364 | def CheckoutBranch(self, name): |
| 1365 | """Checkout a local topic branch. |
Doug Anderson | 3ba5f95 | 2011-04-07 12:51:04 -0700 | [diff] [blame] | 1366 | |
| 1367 | Args: |
| 1368 | name: The name of the branch to checkout. |
| 1369 | |
| 1370 | Returns: |
| 1371 | True if the checkout succeeded; False if it didn't; None if the branch |
| 1372 | didn't exist. |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1373 | """ |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1374 | rev = R_HEADS + name |
| 1375 | head = self.work_git.GetHead() |
| 1376 | if head == rev: |
| 1377 | # Already on the branch |
| 1378 | # |
| 1379 | return True |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1380 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1381 | all_refs = self.bare_ref.all |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1382 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1383 | revid = all_refs[rev] |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1384 | except KeyError: |
| 1385 | # Branch does not exist in this project |
| 1386 | # |
Doug Anderson | 3ba5f95 | 2011-04-07 12:51:04 -0700 | [diff] [blame] | 1387 | return None |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1388 | |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1389 | if head.startswith(R_HEADS): |
| 1390 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1391 | head = all_refs[head] |
Shawn O. Pearce | 89e717d | 2009-04-18 15:04:41 -0700 | [diff] [blame] | 1392 | except KeyError: |
| 1393 | head = None |
| 1394 | |
| 1395 | if head == revid: |
| 1396 | # Same revision; just update HEAD to point to the new |
| 1397 | # target branch, but otherwise take no other action. |
| 1398 | # |
| 1399 | _lwrite(os.path.join(self.worktree, '.git', HEAD), |
| 1400 | 'ref: %s%s\n' % (R_HEADS, name)) |
| 1401 | return True |
| 1402 | |
| 1403 | return GitCommand(self, |
| 1404 | ['checkout', name, '--'], |
| 1405 | capture_stdout = True, |
| 1406 | capture_stderr = True).Wait() == 0 |
Wink Saville | 02d7945 | 2009-04-10 13:01:24 -0700 | [diff] [blame] | 1407 | |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1408 | def AbandonBranch(self, name): |
| 1409 | """Destroy a local topic branch. |
Doug Anderson | dafb1d6 | 2011-04-07 11:46:59 -0700 | [diff] [blame] | 1410 | |
| 1411 | Args: |
| 1412 | name: The name of the branch to abandon. |
| 1413 | |
| 1414 | Returns: |
| 1415 | True if the abandon succeeded; False if it didn't; None if the branch |
| 1416 | didn't exist. |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1417 | """ |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 1418 | rev = R_HEADS + name |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1419 | all_refs = self.bare_ref.all |
| 1420 | if rev not in all_refs: |
Doug Anderson | dafb1d6 | 2011-04-07 11:46:59 -0700 | [diff] [blame] | 1421 | # Doesn't exist |
| 1422 | return None |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1423 | |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 1424 | head = self.work_git.GetHead() |
| 1425 | if head == rev: |
| 1426 | # We can't destroy the branch while we are sitting |
| 1427 | # on it. Switch to a detached HEAD. |
| 1428 | # |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1429 | head = all_refs[head] |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1430 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1431 | revid = self.GetRevisionId(all_refs) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1432 | if head == revid: |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 1433 | _lwrite(os.path.join(self.worktree, '.git', HEAD), |
| 1434 | '%s\n' % revid) |
| 1435 | else: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1436 | self._Checkout(revid, quiet=True) |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 1437 | |
| 1438 | return GitCommand(self, |
| 1439 | ['branch', '-D', name], |
| 1440 | capture_stdout = True, |
| 1441 | capture_stderr = True).Wait() == 0 |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1442 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1443 | def PruneHeads(self): |
| 1444 | """Prune any topic branches already merged into upstream. |
| 1445 | """ |
| 1446 | cb = self.CurrentBranch |
| 1447 | kill = [] |
Shawn O. Pearce | 3778f9d | 2009-03-02 12:30:50 -0800 | [diff] [blame] | 1448 | left = self._allrefs |
| 1449 | for name in left.keys(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1450 | if name.startswith(R_HEADS): |
| 1451 | name = name[len(R_HEADS):] |
| 1452 | if cb is None or name != cb: |
| 1453 | kill.append(name) |
| 1454 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1455 | rev = self.GetRevisionId(left) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1456 | if cb is not None \ |
| 1457 | and not self._revlist(HEAD + '...' + rev) \ |
| 1458 | and not self.IsDirty(consider_untracked = False): |
| 1459 | self.work_git.DetachHead(HEAD) |
| 1460 | kill.append(cb) |
| 1461 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1462 | if kill: |
Shawn O. Pearce | 5b23f24 | 2009-04-17 18:43:33 -0700 | [diff] [blame] | 1463 | old = self.bare_git.GetHead() |
| 1464 | if old is None: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1465 | old = 'refs/heads/please_never_use_this_as_a_branch_name' |
| 1466 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1467 | try: |
| 1468 | self.bare_git.DetachHead(rev) |
| 1469 | |
| 1470 | b = ['branch', '-d'] |
| 1471 | b.extend(kill) |
| 1472 | b = GitCommand(self, b, bare=True, |
| 1473 | capture_stdout=True, |
| 1474 | capture_stderr=True) |
| 1475 | b.Wait() |
| 1476 | finally: |
| 1477 | self.bare_git.SetHead(old) |
Shawn O. Pearce | 3778f9d | 2009-03-02 12:30:50 -0800 | [diff] [blame] | 1478 | left = self._allrefs |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1479 | |
Shawn O. Pearce | 3778f9d | 2009-03-02 12:30:50 -0800 | [diff] [blame] | 1480 | for branch in kill: |
| 1481 | if (R_HEADS + branch) not in left: |
| 1482 | self.CleanPublishedCache() |
| 1483 | break |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1484 | |
| 1485 | if cb and cb not in kill: |
| 1486 | kill.append(cb) |
Shawn O. Pearce | 7c6c64d | 2009-03-02 12:38:13 -0800 | [diff] [blame] | 1487 | kill.sort() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1488 | |
| 1489 | kept = [] |
| 1490 | for branch in kill: |
Shawn O. Pearce | 3778f9d | 2009-03-02 12:30:50 -0800 | [diff] [blame] | 1491 | if (R_HEADS + branch) in left: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1492 | branch = self.GetBranch(branch) |
| 1493 | base = branch.LocalMerge |
| 1494 | if not base: |
| 1495 | base = rev |
| 1496 | kept.append(ReviewableBranch(self, branch, base)) |
| 1497 | return kept |
| 1498 | |
| 1499 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1500 | ## Submodule Management ## |
| 1501 | |
| 1502 | def GetRegisteredSubprojects(self): |
| 1503 | result = [] |
| 1504 | def rec(subprojects): |
| 1505 | if not subprojects: |
| 1506 | return |
| 1507 | result.extend(subprojects) |
| 1508 | for p in subprojects: |
| 1509 | rec(p.subprojects) |
| 1510 | rec(self.subprojects) |
| 1511 | return result |
| 1512 | |
| 1513 | def _GetSubmodules(self): |
| 1514 | # Unfortunately we cannot call `git submodule status --recursive` here |
| 1515 | # because the working tree might not exist yet, and it cannot be used |
| 1516 | # without a working tree in its current implementation. |
| 1517 | |
| 1518 | def get_submodules(gitdir, rev): |
| 1519 | # Parse .gitmodules for submodule sub_paths and sub_urls |
| 1520 | sub_paths, sub_urls = parse_gitmodules(gitdir, rev) |
| 1521 | if not sub_paths: |
| 1522 | return [] |
| 1523 | # Run `git ls-tree` to read SHAs of submodule object, which happen to be |
| 1524 | # revision of submodule repository |
| 1525 | sub_revs = git_ls_tree(gitdir, rev, sub_paths) |
| 1526 | submodules = [] |
| 1527 | for sub_path, sub_url in zip(sub_paths, sub_urls): |
| 1528 | try: |
| 1529 | sub_rev = sub_revs[sub_path] |
| 1530 | except KeyError: |
| 1531 | # Ignore non-exist submodules |
| 1532 | continue |
| 1533 | submodules.append((sub_rev, sub_path, sub_url)) |
| 1534 | return submodules |
| 1535 | |
| 1536 | re_path = re.compile(r'^submodule\.([^.]+)\.path=(.*)$') |
| 1537 | re_url = re.compile(r'^submodule\.([^.]+)\.url=(.*)$') |
| 1538 | def parse_gitmodules(gitdir, rev): |
| 1539 | cmd = ['cat-file', 'blob', '%s:.gitmodules' % rev] |
| 1540 | try: |
| 1541 | p = GitCommand(None, cmd, capture_stdout = True, capture_stderr = True, |
| 1542 | bare = True, gitdir = gitdir) |
| 1543 | except GitError: |
| 1544 | return [], [] |
| 1545 | if p.Wait() != 0: |
| 1546 | return [], [] |
| 1547 | |
| 1548 | gitmodules_lines = [] |
| 1549 | fd, temp_gitmodules_path = tempfile.mkstemp() |
| 1550 | try: |
| 1551 | os.write(fd, p.stdout) |
| 1552 | os.close(fd) |
| 1553 | cmd = ['config', '--file', temp_gitmodules_path, '--list'] |
| 1554 | p = GitCommand(None, cmd, capture_stdout = True, capture_stderr = True, |
| 1555 | bare = True, gitdir = gitdir) |
| 1556 | if p.Wait() != 0: |
| 1557 | return [], [] |
| 1558 | gitmodules_lines = p.stdout.split('\n') |
| 1559 | except GitError: |
| 1560 | return [], [] |
| 1561 | finally: |
| 1562 | os.remove(temp_gitmodules_path) |
| 1563 | |
| 1564 | names = set() |
| 1565 | paths = {} |
| 1566 | urls = {} |
| 1567 | for line in gitmodules_lines: |
| 1568 | if not line: |
| 1569 | continue |
| 1570 | m = re_path.match(line) |
| 1571 | if m: |
| 1572 | names.add(m.group(1)) |
| 1573 | paths[m.group(1)] = m.group(2) |
| 1574 | continue |
| 1575 | m = re_url.match(line) |
| 1576 | if m: |
| 1577 | names.add(m.group(1)) |
| 1578 | urls[m.group(1)] = m.group(2) |
| 1579 | continue |
| 1580 | names = sorted(names) |
| 1581 | return ([paths.get(name, '') for name in names], |
| 1582 | [urls.get(name, '') for name in names]) |
| 1583 | |
| 1584 | def git_ls_tree(gitdir, rev, paths): |
| 1585 | cmd = ['ls-tree', rev, '--'] |
| 1586 | cmd.extend(paths) |
| 1587 | try: |
| 1588 | p = GitCommand(None, cmd, capture_stdout = True, capture_stderr = True, |
| 1589 | bare = True, gitdir = gitdir) |
| 1590 | except GitError: |
| 1591 | return [] |
| 1592 | if p.Wait() != 0: |
| 1593 | return [] |
| 1594 | objects = {} |
| 1595 | for line in p.stdout.split('\n'): |
| 1596 | if not line.strip(): |
| 1597 | continue |
| 1598 | object_rev, object_path = line.split()[2:4] |
| 1599 | objects[object_path] = object_rev |
| 1600 | return objects |
| 1601 | |
| 1602 | try: |
| 1603 | rev = self.GetRevisionId() |
| 1604 | except GitError: |
| 1605 | return [] |
| 1606 | return get_submodules(self.gitdir, rev) |
| 1607 | |
| 1608 | def GetDerivedSubprojects(self): |
| 1609 | result = [] |
| 1610 | if not self.Exists: |
| 1611 | # If git repo does not exist yet, querying its submodules will |
| 1612 | # mess up its states; so return here. |
| 1613 | return result |
| 1614 | for rev, path, url in self._GetSubmodules(): |
| 1615 | name = self.manifest.GetSubprojectName(self, path) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1616 | relpath, worktree, gitdir, objdir = \ |
| 1617 | self.manifest.GetSubprojectPaths(self, name, path) |
| 1618 | project = self.manifest.paths.get(relpath) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1619 | if project: |
| 1620 | result.extend(project.GetDerivedSubprojects()) |
| 1621 | continue |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1622 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1623 | remote = RemoteSpec(self.remote.name, |
| 1624 | url = url, |
| 1625 | review = self.remote.review) |
| 1626 | subproject = Project(manifest = self.manifest, |
| 1627 | name = name, |
| 1628 | remote = remote, |
| 1629 | gitdir = gitdir, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1630 | objdir = objdir, |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1631 | worktree = worktree, |
| 1632 | relpath = relpath, |
| 1633 | revisionExpr = self.revisionExpr, |
| 1634 | revisionId = rev, |
| 1635 | rebase = self.rebase, |
| 1636 | groups = self.groups, |
| 1637 | sync_c = self.sync_c, |
| 1638 | sync_s = self.sync_s, |
| 1639 | parent = self, |
| 1640 | is_derived = True) |
| 1641 | result.append(subproject) |
| 1642 | result.extend(subproject.GetDerivedSubprojects()) |
| 1643 | return result |
| 1644 | |
| 1645 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1646 | ## Direct Git Commands ## |
| 1647 | |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 1648 | def _FetchArchive(self, tarpath, cwd=None): |
| 1649 | cmd = ['archive', '-v', '-o', tarpath] |
| 1650 | cmd.append('--remote=%s' % self.remote.url) |
| 1651 | cmd.append('--prefix=%s/' % self.relpath) |
| 1652 | cmd.append(self.revisionExpr) |
| 1653 | |
| 1654 | command = GitCommand(self, cmd, cwd=cwd, |
| 1655 | capture_stdout=True, |
| 1656 | capture_stderr=True) |
| 1657 | |
| 1658 | if command.Wait() != 0: |
| 1659 | raise GitError('git archive %s: %s' % (self.name, command.stderr)) |
| 1660 | |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1661 | def _RemoteFetch(self, name=None, |
| 1662 | current_branch_only=False, |
Shawn O. Pearce | 16614f8 | 2010-10-29 12:05:43 -0700 | [diff] [blame] | 1663 | initial=False, |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1664 | quiet=False, |
Mitchel Humpherys | 597868b | 2012-10-29 10:18:34 -0700 | [diff] [blame] | 1665 | alt_dir=None, |
| 1666 | no_tags=False): |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1667 | |
| 1668 | is_sha1 = False |
| 1669 | tag_name = None |
| 1670 | |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 1671 | def CheckForSha1(): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 1672 | try: |
| 1673 | # if revision (sha or tag) is not present then following function |
| 1674 | # throws an error. |
| 1675 | self.bare_git.rev_parse('--verify', '%s^0' % self.revisionExpr) |
| 1676 | return True |
| 1677 | except GitError: |
| 1678 | # There is no such persistent revision. We have to fetch it. |
| 1679 | return False |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 1680 | |
Shawn Pearce | 69e04d8 | 2014-01-29 12:48:54 -0800 | [diff] [blame] | 1681 | if self.clone_depth: |
| 1682 | depth = self.clone_depth |
| 1683 | else: |
| 1684 | depth = self.manifest.manifestProject.config.GetString('repo.depth') |
| 1685 | if depth: |
| 1686 | current_branch_only = True |
| 1687 | |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1688 | if current_branch_only: |
| 1689 | if ID_RE.match(self.revisionExpr) is not None: |
| 1690 | is_sha1 = True |
| 1691 | elif self.revisionExpr.startswith(R_TAGS): |
| 1692 | # this is a tag and its sha1 value should never change |
| 1693 | tag_name = self.revisionExpr[len(R_TAGS):] |
| 1694 | |
| 1695 | if is_sha1 or tag_name is not None: |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 1696 | if CheckForSha1(): |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1697 | return True |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 1698 | if is_sha1 and (not self.upstream or ID_RE.match(self.upstream)): |
| 1699 | current_branch_only = False |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1700 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1701 | if not name: |
| 1702 | name = self.remote.name |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 1703 | |
| 1704 | ssh_proxy = False |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 1705 | remote = self.GetRemote(name) |
| 1706 | if remote.PreConnectFetch(): |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 1707 | ssh_proxy = True |
| 1708 | |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1709 | if initial: |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1710 | if alt_dir and 'objects' == os.path.basename(alt_dir): |
| 1711 | ref_dir = os.path.dirname(alt_dir) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1712 | packed_refs = os.path.join(self.gitdir, 'packed-refs') |
| 1713 | remote = self.GetRemote(name) |
| 1714 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1715 | all_refs = self.bare_ref.all |
| 1716 | ids = set(all_refs.values()) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1717 | tmp = set() |
| 1718 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1719 | for r, ref_id in GitRefs(ref_dir).all.items(): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1720 | if r not in all_refs: |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1721 | if r.startswith(R_TAGS) or remote.WritesTo(r): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1722 | all_refs[r] = ref_id |
| 1723 | ids.add(ref_id) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1724 | continue |
| 1725 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1726 | if ref_id in ids: |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1727 | continue |
| 1728 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1729 | r = 'refs/_alt/%s' % ref_id |
| 1730 | all_refs[r] = ref_id |
| 1731 | ids.add(ref_id) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1732 | tmp.add(r) |
| 1733 | |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1734 | tmp_packed = '' |
| 1735 | old_packed = '' |
| 1736 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1737 | for r in sorted(all_refs): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1738 | line = '%s %s\n' % (all_refs[r], r) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1739 | tmp_packed += line |
| 1740 | if r not in tmp: |
| 1741 | old_packed += line |
| 1742 | |
| 1743 | _lwrite(packed_refs, tmp_packed) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1744 | else: |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1745 | alt_dir = None |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1746 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1747 | cmd = ['fetch'] |
Doug Anderson | 30d4529 | 2011-05-04 15:01:04 -0700 | [diff] [blame] | 1748 | |
| 1749 | # The --depth option only affects the initial fetch; after that we'll do |
| 1750 | # full fetches of changes. |
Doug Anderson | 30d4529 | 2011-05-04 15:01:04 -0700 | [diff] [blame] | 1751 | if depth and initial: |
| 1752 | cmd.append('--depth=%s' % depth) |
| 1753 | |
Shawn O. Pearce | 16614f8 | 2010-10-29 12:05:43 -0700 | [diff] [blame] | 1754 | if quiet: |
| 1755 | cmd.append('--quiet') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1756 | if not self.worktree: |
| 1757 | cmd.append('--update-head-ok') |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1758 | cmd.append(name) |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1759 | |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 1760 | if not current_branch_only: |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1761 | # Fetch whole repo |
Jimmie Wester | 2f992cb | 2012-12-07 12:49:51 +0100 | [diff] [blame] | 1762 | # If using depth then we should not get all the tags since they may |
| 1763 | # be outside of the depth. |
| 1764 | if no_tags or depth: |
Mitchel Humpherys | 597868b | 2012-10-29 10:18:34 -0700 | [diff] [blame] | 1765 | cmd.append('--no-tags') |
| 1766 | else: |
| 1767 | cmd.append('--tags') |
Conley Owens | 5654805 | 2014-02-11 18:44:58 -0800 | [diff] [blame] | 1768 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1769 | cmd.append(str((u'+refs/heads/*:') + remote.ToLocal('refs/heads/*'))) |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1770 | elif tag_name is not None: |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1771 | cmd.append('tag') |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1772 | cmd.append(tag_name) |
| 1773 | else: |
| 1774 | branch = self.revisionExpr |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 1775 | if is_sha1: |
| 1776 | branch = self.upstream |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 1777 | if branch.startswith(R_HEADS): |
| 1778 | branch = branch[len(R_HEADS):] |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1779 | 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] | 1780 | |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1781 | ok = False |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 1782 | for _i in range(2): |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 1783 | ret = GitCommand(self, cmd, bare=True, ssh_proxy=ssh_proxy).Wait() |
| 1784 | if ret == 0: |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1785 | ok = True |
| 1786 | break |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 1787 | elif current_branch_only and is_sha1 and ret == 128: |
| 1788 | # Exit code 128 means "couldn't find the ref you asked for"; if we're in sha1 |
| 1789 | # mode, we just tried sync'ing from the upstream field; it doesn't exist, thus |
| 1790 | # abort the optimization attempt and do a full sync. |
| 1791 | break |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1792 | time.sleep(random.randint(30, 45)) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1793 | |
| 1794 | if initial: |
Conley Owens | 5654805 | 2014-02-11 18:44:58 -0800 | [diff] [blame] | 1795 | # Ensure that some refs exist. Otherwise, we probably aren't looking |
| 1796 | # at a real git repository and may have a bad url. |
| 1797 | if not self.bare_ref.all: |
| 1798 | ok = False |
| 1799 | |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1800 | if alt_dir: |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1801 | if old_packed != '': |
| 1802 | _lwrite(packed_refs, old_packed) |
| 1803 | else: |
| 1804 | os.remove(packed_refs) |
| 1805 | self.bare_git.pack_refs('--all', '--prune') |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 1806 | |
| 1807 | if is_sha1 and current_branch_only and self.upstream: |
| 1808 | # We just synced the upstream given branch; verify we |
| 1809 | # got what we wanted, else trigger a second run of all |
| 1810 | # refs. |
| 1811 | if not CheckForSha1(): |
| 1812 | return self._RemoteFetch(name=name, current_branch_only=False, |
| 1813 | initial=False, quiet=quiet, alt_dir=alt_dir) |
| 1814 | |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1815 | return ok |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1816 | |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1817 | def _ApplyCloneBundle(self, initial=False, quiet=False): |
David Pursehouse | ede7f12 | 2012-11-27 22:25:30 +0900 | [diff] [blame] | 1818 | 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] | 1819 | return False |
| 1820 | |
| 1821 | remote = self.GetRemote(self.remote.name) |
| 1822 | bundle_url = remote.url + '/clone.bundle' |
| 1823 | bundle_url = GitConfig.ForUser().UrlInsteadOf(bundle_url) |
Dave Borowitz | 74c1f3d | 2013-06-03 15:05:07 -0700 | [diff] [blame] | 1824 | if GetSchemeFromUrl(bundle_url) not in ( |
| 1825 | 'http', 'https', 'persistent-http', 'persistent-https'): |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1826 | return False |
| 1827 | |
| 1828 | bundle_dst = os.path.join(self.gitdir, 'clone.bundle') |
| 1829 | bundle_tmp = os.path.join(self.gitdir, 'clone.bundle.tmp') |
| 1830 | |
| 1831 | exist_dst = os.path.exists(bundle_dst) |
| 1832 | exist_tmp = os.path.exists(bundle_tmp) |
| 1833 | |
| 1834 | if not initial and not exist_dst and not exist_tmp: |
| 1835 | return False |
| 1836 | |
| 1837 | if not exist_dst: |
| 1838 | exist_dst = self._FetchBundle(bundle_url, bundle_tmp, bundle_dst, quiet) |
| 1839 | if not exist_dst: |
| 1840 | return False |
| 1841 | |
| 1842 | cmd = ['fetch'] |
| 1843 | if quiet: |
| 1844 | cmd.append('--quiet') |
| 1845 | if not self.worktree: |
| 1846 | cmd.append('--update-head-ok') |
| 1847 | cmd.append(bundle_dst) |
| 1848 | for f in remote.fetch: |
| 1849 | cmd.append(str(f)) |
| 1850 | cmd.append('refs/tags/*:refs/tags/*') |
| 1851 | |
| 1852 | ok = GitCommand(self, cmd, bare=True).Wait() == 0 |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 1853 | if os.path.exists(bundle_dst): |
| 1854 | os.remove(bundle_dst) |
| 1855 | if os.path.exists(bundle_tmp): |
| 1856 | os.remove(bundle_tmp) |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 1857 | return ok |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1858 | |
Shawn O. Pearce | c325dc3 | 2011-10-03 08:30:24 -0700 | [diff] [blame] | 1859 | def _FetchBundle(self, srcUrl, tmpPath, dstPath, quiet): |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 1860 | if os.path.exists(dstPath): |
| 1861 | os.remove(dstPath) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 1862 | |
Matt Gumbel | 2dc810c | 2012-08-30 09:39:36 -0700 | [diff] [blame] | 1863 | cmd = ['curl', '--fail', '--output', tmpPath, '--netrc', '--location'] |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 1864 | if quiet: |
| 1865 | cmd += ['--silent'] |
| 1866 | if os.path.exists(tmpPath): |
| 1867 | size = os.stat(tmpPath).st_size |
| 1868 | if size >= 1024: |
| 1869 | cmd += ['--continue-at', '%d' % (size,)] |
| 1870 | else: |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 1871 | os.remove(tmpPath) |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 1872 | if 'http_proxy' in os.environ and 'darwin' == sys.platform: |
| 1873 | cmd += ['--proxy', os.environ['http_proxy']] |
Dave Borowitz | 74c1f3d | 2013-06-03 15:05:07 -0700 | [diff] [blame] | 1874 | cookiefile = self._GetBundleCookieFile(srcUrl) |
Torne (Richard Coles) | ed68d0e | 2013-01-11 16:22:54 +0000 | [diff] [blame] | 1875 | if cookiefile: |
| 1876 | cmd += ['--cookie', cookiefile] |
Dave Borowitz | 74c1f3d | 2013-06-03 15:05:07 -0700 | [diff] [blame] | 1877 | if srcUrl.startswith('persistent-'): |
| 1878 | srcUrl = srcUrl[len('persistent-'):] |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 1879 | cmd += [srcUrl] |
| 1880 | |
| 1881 | if IsTrace(): |
| 1882 | Trace('%s', ' '.join(cmd)) |
| 1883 | try: |
| 1884 | proc = subprocess.Popen(cmd) |
| 1885 | except OSError: |
| 1886 | return False |
| 1887 | |
Matt Gumbel | 2dc810c | 2012-08-30 09:39:36 -0700 | [diff] [blame] | 1888 | curlret = proc.wait() |
| 1889 | |
| 1890 | if curlret == 22: |
| 1891 | # From curl man page: |
| 1892 | # 22: HTTP page not retrieved. The requested url was not found or |
| 1893 | # returned another error with the HTTP error code being 400 or above. |
| 1894 | # This return code only appears if -f, --fail is used. |
| 1895 | if not quiet: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 1896 | print("Server does not provide clone.bundle; ignoring.", |
| 1897 | file=sys.stderr) |
Matt Gumbel | 2dc810c | 2012-08-30 09:39:36 -0700 | [diff] [blame] | 1898 | return False |
| 1899 | |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 1900 | if os.path.exists(tmpPath): |
Dave Borowitz | 91f3ba5 | 2013-06-03 12:15:23 -0700 | [diff] [blame] | 1901 | if curlret == 0 and self._IsValidBundle(tmpPath): |
Shawn O. Pearce | 5e7127d | 2012-08-02 14:57:37 -0700 | [diff] [blame] | 1902 | os.rename(tmpPath, dstPath) |
| 1903 | return True |
| 1904 | else: |
| 1905 | os.remove(tmpPath) |
| 1906 | return False |
| 1907 | else: |
| 1908 | return False |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 1909 | |
Dave Borowitz | 91f3ba5 | 2013-06-03 12:15:23 -0700 | [diff] [blame] | 1910 | def _IsValidBundle(self, path): |
| 1911 | try: |
| 1912 | with open(path) as f: |
| 1913 | if f.read(16) == '# v2 git bundle\n': |
| 1914 | return True |
| 1915 | else: |
| 1916 | print("Invalid clone.bundle file; ignoring.", file=sys.stderr) |
| 1917 | return False |
| 1918 | except OSError: |
| 1919 | return False |
| 1920 | |
Dave Borowitz | 74c1f3d | 2013-06-03 15:05:07 -0700 | [diff] [blame] | 1921 | def _GetBundleCookieFile(self, url): |
| 1922 | if url.startswith('persistent-'): |
| 1923 | try: |
| 1924 | p = subprocess.Popen( |
| 1925 | ['git-remote-persistent-https', '-print_config', url], |
| 1926 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 1927 | stderr=subprocess.PIPE) |
Dave Borowitz | 0836a22 | 2013-09-25 17:46:01 -0700 | [diff] [blame] | 1928 | p.stdin.close() # Tell subprocess it's ok to close. |
Dave Borowitz | 74c1f3d | 2013-06-03 15:05:07 -0700 | [diff] [blame] | 1929 | prefix = 'http.cookiefile=' |
Dave Borowitz | 0836a22 | 2013-09-25 17:46:01 -0700 | [diff] [blame] | 1930 | cookiefile = None |
Dave Borowitz | 74c1f3d | 2013-06-03 15:05:07 -0700 | [diff] [blame] | 1931 | for line in p.stdout: |
| 1932 | line = line.strip() |
| 1933 | if line.startswith(prefix): |
Dave Borowitz | 0836a22 | 2013-09-25 17:46:01 -0700 | [diff] [blame] | 1934 | cookiefile = line[len(prefix):] |
| 1935 | break |
Dave Borowitz | 74c1f3d | 2013-06-03 15:05:07 -0700 | [diff] [blame] | 1936 | if p.wait(): |
Conley Owens | cbc0798 | 2013-11-21 10:38:03 -0800 | [diff] [blame] | 1937 | err_msg = p.stderr.read() |
| 1938 | if ' -print_config' in err_msg: |
Dave Borowitz | 74c1f3d | 2013-06-03 15:05:07 -0700 | [diff] [blame] | 1939 | pass # Persistent proxy doesn't support -print_config. |
| 1940 | else: |
Conley Owens | cbc0798 | 2013-11-21 10:38:03 -0800 | [diff] [blame] | 1941 | print(err_msg, file=sys.stderr) |
Dave Borowitz | 0836a22 | 2013-09-25 17:46:01 -0700 | [diff] [blame] | 1942 | if cookiefile: |
| 1943 | return cookiefile |
Dave Borowitz | 74c1f3d | 2013-06-03 15:05:07 -0700 | [diff] [blame] | 1944 | except OSError as e: |
| 1945 | if e.errno == errno.ENOENT: |
| 1946 | pass # No persistent proxy. |
| 1947 | raise |
| 1948 | return GitConfig.ForUser().GetString('http.cookiefile') |
| 1949 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1950 | def _Checkout(self, rev, quiet=False): |
| 1951 | cmd = ['checkout'] |
| 1952 | if quiet: |
| 1953 | cmd.append('-q') |
| 1954 | cmd.append(rev) |
| 1955 | cmd.append('--') |
| 1956 | if GitCommand(self, cmd).Wait() != 0: |
| 1957 | if self._allrefs: |
| 1958 | raise GitError('%s checkout %s ' % (self.name, rev)) |
| 1959 | |
Pierre Tardy | e5a2122 | 2011-03-24 16:28:18 +0100 | [diff] [blame] | 1960 | def _CherryPick(self, rev, quiet=False): |
| 1961 | cmd = ['cherry-pick'] |
| 1962 | cmd.append(rev) |
| 1963 | cmd.append('--') |
| 1964 | if GitCommand(self, cmd).Wait() != 0: |
| 1965 | if self._allrefs: |
| 1966 | raise GitError('%s cherry-pick %s ' % (self.name, rev)) |
| 1967 | |
Erwan Mahe | a94f162 | 2011-08-19 13:56:09 +0200 | [diff] [blame] | 1968 | def _Revert(self, rev, quiet=False): |
| 1969 | cmd = ['revert'] |
| 1970 | cmd.append('--no-edit') |
| 1971 | cmd.append(rev) |
| 1972 | cmd.append('--') |
| 1973 | if GitCommand(self, cmd).Wait() != 0: |
| 1974 | if self._allrefs: |
| 1975 | raise GitError('%s revert %s ' % (self.name, rev)) |
| 1976 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1977 | def _ResetHard(self, rev, quiet=True): |
| 1978 | cmd = ['reset', '--hard'] |
| 1979 | if quiet: |
| 1980 | cmd.append('-q') |
| 1981 | cmd.append(rev) |
| 1982 | if GitCommand(self, cmd).Wait() != 0: |
| 1983 | raise GitError('%s reset --hard %s ' % (self.name, rev)) |
| 1984 | |
| 1985 | def _Rebase(self, upstream, onto = None): |
Shawn O. Pearce | 19a83d8 | 2009-04-16 08:14:26 -0700 | [diff] [blame] | 1986 | cmd = ['rebase'] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1987 | if onto is not None: |
| 1988 | cmd.extend(['--onto', onto]) |
| 1989 | cmd.append(upstream) |
Shawn O. Pearce | 19a83d8 | 2009-04-16 08:14:26 -0700 | [diff] [blame] | 1990 | if GitCommand(self, cmd).Wait() != 0: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1991 | raise GitError('%s rebase %s ' % (self.name, upstream)) |
| 1992 | |
Pierre Tardy | 3d12594 | 2012-05-04 12:18:12 +0200 | [diff] [blame] | 1993 | def _FastForward(self, head, ffonly=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1994 | cmd = ['merge', head] |
Pierre Tardy | 3d12594 | 2012-05-04 12:18:12 +0200 | [diff] [blame] | 1995 | if ffonly: |
| 1996 | cmd.append("--ff-only") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1997 | if GitCommand(self, cmd).Wait() != 0: |
| 1998 | raise GitError('%s merge %s ' % (self.name, head)) |
| 1999 | |
Victor Boivie | 2b30e3a | 2012-10-05 12:37:58 +0200 | [diff] [blame] | 2000 | def _InitGitDir(self, mirror_git=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2001 | if not os.path.exists(self.gitdir): |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2002 | |
| 2003 | # Initialize the bare repository, which contains all of the objects. |
| 2004 | if not os.path.exists(self.objdir): |
| 2005 | os.makedirs(self.objdir) |
| 2006 | self.bare_objdir.init() |
| 2007 | |
| 2008 | # If we have a separate directory to hold refs, initialize it as well. |
| 2009 | if self.objdir != self.gitdir: |
| 2010 | os.makedirs(self.gitdir) |
| 2011 | self._ReferenceGitDir(self.objdir, self.gitdir, share_refs=False, |
| 2012 | copy_all=True) |
Shawn O. Pearce | 2816d4f | 2009-03-03 17:53:18 -0800 | [diff] [blame] | 2013 | |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2014 | mp = self.manifest.manifestProject |
Victor Boivie | 2b30e3a | 2012-10-05 12:37:58 +0200 | [diff] [blame] | 2015 | ref_dir = mp.config.GetString('repo.reference') or '' |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2016 | |
Victor Boivie | 2b30e3a | 2012-10-05 12:37:58 +0200 | [diff] [blame] | 2017 | if ref_dir or mirror_git: |
| 2018 | if not mirror_git: |
| 2019 | mirror_git = os.path.join(ref_dir, self.name + '.git') |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 2020 | repo_git = os.path.join(ref_dir, '.repo', 'projects', |
| 2021 | self.relpath + '.git') |
| 2022 | |
| 2023 | if os.path.exists(mirror_git): |
| 2024 | ref_dir = mirror_git |
| 2025 | |
| 2026 | elif os.path.exists(repo_git): |
| 2027 | ref_dir = repo_git |
| 2028 | |
| 2029 | else: |
| 2030 | ref_dir = None |
| 2031 | |
| 2032 | if ref_dir: |
| 2033 | _lwrite(os.path.join(self.gitdir, 'objects/info/alternates'), |
| 2034 | os.path.join(ref_dir, 'objects') + '\n') |
| 2035 | |
Jimmie Wester | a044458 | 2012-10-24 13:44:42 +0200 | [diff] [blame] | 2036 | self._UpdateHooks() |
| 2037 | |
| 2038 | m = self.manifest.manifestProject.config |
| 2039 | for key in ['user.name', 'user.email']: |
| 2040 | if m.Has(key, include_defaults = False): |
| 2041 | self.config.SetString(key, m.GetString(key)) |
Shawn O. Pearce | 2816d4f | 2009-03-03 17:53:18 -0800 | [diff] [blame] | 2042 | if self.manifest.IsMirror: |
| 2043 | self.config.SetString('core.bare', 'true') |
| 2044 | else: |
| 2045 | self.config.SetString('core.bare', None) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2046 | |
Jimmie Wester | a044458 | 2012-10-24 13:44:42 +0200 | [diff] [blame] | 2047 | def _UpdateHooks(self): |
| 2048 | if os.path.exists(self.gitdir): |
| 2049 | # Always recreate hooks since they can have been changed |
| 2050 | # since the latest update. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2051 | hooks = self._gitdir_path('hooks') |
Shawn O. Pearce | de64681 | 2008-10-29 14:38:12 -0700 | [diff] [blame] | 2052 | try: |
| 2053 | to_rm = os.listdir(hooks) |
| 2054 | except OSError: |
| 2055 | to_rm = [] |
| 2056 | for old_hook in to_rm: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2057 | os.remove(os.path.join(hooks, old_hook)) |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 2058 | self._InitHooks() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2059 | |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 2060 | def _InitHooks(self): |
Jesse Hall | 672cc49 | 2013-11-27 11:17:13 -0800 | [diff] [blame] | 2061 | hooks = os.path.realpath(self._gitdir_path('hooks')) |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 2062 | if not os.path.exists(hooks): |
| 2063 | os.makedirs(hooks) |
Doug Anderson | 8ced864 | 2011-01-10 14:16:30 -0800 | [diff] [blame] | 2064 | for stock_hook in _ProjectHooks(): |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 2065 | name = os.path.basename(stock_hook) |
| 2066 | |
Victor Boivie | 65e0f35 | 2011-04-18 11:23:29 +0200 | [diff] [blame] | 2067 | if name in ('commit-msg',) and not self.remote.review \ |
| 2068 | and not self is self.manifest.manifestProject: |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 2069 | # Don't install a Gerrit Code Review hook if this |
| 2070 | # project does not appear to use it for reviews. |
| 2071 | # |
Victor Boivie | 65e0f35 | 2011-04-18 11:23:29 +0200 | [diff] [blame] | 2072 | # Since the manifest project is one of those, but also |
| 2073 | # managed through gerrit, it's excluded |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 2074 | continue |
| 2075 | |
| 2076 | dst = os.path.join(hooks, name) |
| 2077 | if os.path.islink(dst): |
| 2078 | continue |
| 2079 | if os.path.exists(dst): |
| 2080 | if filecmp.cmp(stock_hook, dst, shallow=False): |
| 2081 | os.remove(dst) |
| 2082 | else: |
| 2083 | _error("%s: Not replacing %s hook", self.relpath, name) |
| 2084 | continue |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 2085 | try: |
Mickaël Salaün | b9477bc | 2012-08-05 13:39:26 +0200 | [diff] [blame] | 2086 | os.symlink(os.path.relpath(stock_hook, os.path.dirname(dst)), dst) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 2087 | except OSError as e: |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 2088 | if e.errno == errno.EPERM: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 2089 | raise GitError('filesystem must support symlinks') |
| 2090 | else: |
| 2091 | raise |
| 2092 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2093 | def _InitRemote(self): |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 2094 | if self.remote.url: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2095 | remote = self.GetRemote(self.remote.name) |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 2096 | remote.url = self.remote.url |
| 2097 | remote.review = self.remote.review |
| 2098 | remote.projectname = self.name |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2099 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 2100 | if self.worktree: |
| 2101 | remote.ResetFetch(mirror=False) |
| 2102 | else: |
| 2103 | remote.ResetFetch(mirror=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2104 | remote.Save() |
| 2105 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2106 | def _InitMRef(self): |
| 2107 | if self.manifest.branch: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 2108 | self._InitAnyMRef(R_M + self.manifest.branch) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2109 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 2110 | def _InitMirrorHead(self): |
Shawn O. Pearce | fe200ee | 2009-06-01 15:28:21 -0700 | [diff] [blame] | 2111 | self._InitAnyMRef(HEAD) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 2112 | |
| 2113 | def _InitAnyMRef(self, ref): |
| 2114 | cur = self.bare_ref.symref(ref) |
| 2115 | |
| 2116 | if self.revisionId: |
| 2117 | if cur != '' or self.bare_ref.get(ref) != self.revisionId: |
| 2118 | msg = 'manifest set to %s' % self.revisionId |
| 2119 | dst = self.revisionId + '^0' |
| 2120 | self.bare_git.UpdateRef(ref, dst, message = msg, detach = True) |
| 2121 | else: |
| 2122 | remote = self.GetRemote(self.remote.name) |
| 2123 | dst = remote.ToLocal(self.revisionExpr) |
| 2124 | if cur != dst: |
| 2125 | msg = 'manifest set to %s' % self.revisionExpr |
| 2126 | self.bare_git.symbolic_ref('-m', msg, ref, dst) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 2127 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2128 | def _ReferenceGitDir(self, gitdir, dotgit, share_refs, copy_all): |
| 2129 | """Update |dotgit| to reference |gitdir|, using symlinks where possible. |
| 2130 | |
| 2131 | Args: |
| 2132 | gitdir: The bare git repository. Must already be initialized. |
| 2133 | dotgit: The repository you would like to initialize. |
| 2134 | share_refs: If true, |dotgit| will store its refs under |gitdir|. |
| 2135 | Only one work tree can store refs under a given |gitdir|. |
| 2136 | copy_all: If true, copy all remaining files from |gitdir| -> |dotgit|. |
| 2137 | This saves you the effort of initializing |dotgit| yourself. |
| 2138 | """ |
| 2139 | # These objects can be shared between several working trees. |
| 2140 | symlink_files = ['description', 'info'] |
| 2141 | symlink_dirs = ['hooks', 'objects', 'rr-cache', 'svn'] |
| 2142 | if share_refs: |
| 2143 | # These objects can only be used by a single working tree. |
| 2144 | symlink_files += ['config', 'packed-refs'] |
| 2145 | symlink_dirs += ['logs', 'refs'] |
| 2146 | to_symlink = symlink_files + symlink_dirs |
| 2147 | |
| 2148 | to_copy = [] |
| 2149 | if copy_all: |
| 2150 | to_copy = os.listdir(gitdir) |
| 2151 | |
| 2152 | for name in set(to_copy).union(to_symlink): |
| 2153 | try: |
| 2154 | src = os.path.realpath(os.path.join(gitdir, name)) |
| 2155 | dst = os.path.realpath(os.path.join(dotgit, name)) |
| 2156 | |
| 2157 | if os.path.lexists(dst) and not os.path.islink(dst): |
| 2158 | raise GitError('cannot overwrite a local work tree') |
| 2159 | |
| 2160 | # If the source dir doesn't exist, create an empty dir. |
| 2161 | if name in symlink_dirs and not os.path.lexists(src): |
| 2162 | os.makedirs(src) |
| 2163 | |
| 2164 | if name in to_symlink: |
| 2165 | os.symlink(os.path.relpath(src, os.path.dirname(dst)), dst) |
| 2166 | elif copy_all and not os.path.islink(dst): |
| 2167 | if os.path.isdir(src): |
| 2168 | shutil.copytree(src, dst) |
| 2169 | elif os.path.isfile(src): |
| 2170 | shutil.copy(src, dst) |
| 2171 | except OSError as e: |
| 2172 | if e.errno == errno.EPERM: |
| 2173 | raise GitError('filesystem must support symlinks') |
| 2174 | else: |
| 2175 | raise |
| 2176 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2177 | def _InitWorkTree(self): |
| 2178 | dotgit = os.path.join(self.worktree, '.git') |
| 2179 | if not os.path.exists(dotgit): |
| 2180 | os.makedirs(dotgit) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2181 | self._ReferenceGitDir(self.gitdir, dotgit, share_refs=True, |
| 2182 | copy_all=False) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2183 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 2184 | _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] | 2185 | |
| 2186 | cmd = ['read-tree', '--reset', '-u'] |
| 2187 | cmd.append('-v') |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 2188 | cmd.append(HEAD) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2189 | if GitCommand(self, cmd).Wait() != 0: |
| 2190 | raise GitError("cannot initialize work tree") |
Victor Boivie | 0960b5b | 2010-11-26 13:42:13 +0100 | [diff] [blame] | 2191 | |
Shawn O. Pearce | 9360966 | 2009-04-21 10:50:33 -0700 | [diff] [blame] | 2192 | self._CopyFiles() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2193 | |
| 2194 | def _gitdir_path(self, path): |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2195 | return os.path.realpath(os.path.join(self.gitdir, path)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2196 | |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 2197 | def _revlist(self, *args, **kw): |
| 2198 | a = [] |
| 2199 | a.extend(args) |
| 2200 | a.append('--') |
| 2201 | return self.work_git.rev_list(*a, **kw) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2202 | |
| 2203 | @property |
| 2204 | def _allrefs(self): |
Shawn O. Pearce | d237b69 | 2009-04-17 18:49:50 -0700 | [diff] [blame] | 2205 | return self.bare_ref.all |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2206 | |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame^] | 2207 | def _getLogs(self, rev1, rev2, oneline=False, color=True): |
| 2208 | """Get logs between two revisions of this project.""" |
| 2209 | comp = '..' |
| 2210 | if rev1: |
| 2211 | revs = [rev1] |
| 2212 | if rev2: |
| 2213 | revs.extend([comp, rev2]) |
| 2214 | cmd = ['log', ''.join(revs)] |
| 2215 | out = DiffColoring(self.config) |
| 2216 | if out.is_on and color: |
| 2217 | cmd.append('--color') |
| 2218 | if oneline: |
| 2219 | cmd.append('--oneline') |
| 2220 | |
| 2221 | try: |
| 2222 | log = GitCommand(self, cmd, capture_stdout=True, capture_stderr=True) |
| 2223 | if log.Wait() == 0: |
| 2224 | return log.stdout |
| 2225 | except GitError: |
| 2226 | # worktree may not exist if groups changed for example. In that case, |
| 2227 | # try in gitdir instead. |
| 2228 | if not os.path.exists(self.worktree): |
| 2229 | return self.bare_git.log(*cmd[1:]) |
| 2230 | else: |
| 2231 | raise |
| 2232 | return None |
| 2233 | |
| 2234 | def getAddedAndRemovedLogs(self, toProject, oneline=False, color=True): |
| 2235 | """Get the list of logs from this revision to given revisionId""" |
| 2236 | logs = {} |
| 2237 | selfId = self.GetRevisionId(self._allrefs) |
| 2238 | toId = toProject.GetRevisionId(toProject._allrefs) |
| 2239 | |
| 2240 | logs['added'] = self._getLogs(selfId, toId, oneline=oneline, color=color) |
| 2241 | logs['removed'] = self._getLogs(toId, selfId, oneline=oneline, color=color) |
| 2242 | return logs |
| 2243 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2244 | class _GitGetByExec(object): |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2245 | def __init__(self, project, bare, gitdir): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2246 | self._project = project |
| 2247 | self._bare = bare |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2248 | self._gitdir = gitdir |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2249 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2250 | def LsOthers(self): |
| 2251 | p = GitCommand(self._project, |
| 2252 | ['ls-files', |
| 2253 | '-z', |
| 2254 | '--others', |
| 2255 | '--exclude-standard'], |
| 2256 | bare = False, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2257 | gitdir=self._gitdir, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2258 | capture_stdout = True, |
| 2259 | capture_stderr = True) |
| 2260 | if p.Wait() == 0: |
| 2261 | out = p.stdout |
| 2262 | if out: |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 2263 | return out[:-1].split('\0') # pylint: disable=W1401 |
| 2264 | # Backslash is not anomalous |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2265 | return [] |
| 2266 | |
| 2267 | def DiffZ(self, name, *args): |
| 2268 | cmd = [name] |
| 2269 | cmd.append('-z') |
| 2270 | cmd.extend(args) |
| 2271 | p = GitCommand(self._project, |
| 2272 | cmd, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2273 | gitdir=self._gitdir, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2274 | bare = False, |
| 2275 | capture_stdout = True, |
| 2276 | capture_stderr = True) |
| 2277 | try: |
| 2278 | out = p.process.stdout.read() |
| 2279 | r = {} |
| 2280 | if out: |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 2281 | out = iter(out[:-1].split('\0')) # pylint: disable=W1401 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2282 | while out: |
Shawn O. Pearce | 02dbb6d | 2008-10-21 13:59:08 -0700 | [diff] [blame] | 2283 | try: |
| 2284 | info = out.next() |
| 2285 | path = out.next() |
| 2286 | except StopIteration: |
| 2287 | break |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2288 | |
| 2289 | class _Info(object): |
| 2290 | def __init__(self, path, omode, nmode, oid, nid, state): |
| 2291 | self.path = path |
| 2292 | self.src_path = None |
| 2293 | self.old_mode = omode |
| 2294 | self.new_mode = nmode |
| 2295 | self.old_id = oid |
| 2296 | self.new_id = nid |
| 2297 | |
| 2298 | if len(state) == 1: |
| 2299 | self.status = state |
| 2300 | self.level = None |
| 2301 | else: |
| 2302 | self.status = state[:1] |
| 2303 | self.level = state[1:] |
| 2304 | while self.level.startswith('0'): |
| 2305 | self.level = self.level[1:] |
| 2306 | |
| 2307 | info = info[1:].split(' ') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 2308 | info = _Info(path, *info) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2309 | if info.status in ('R', 'C'): |
| 2310 | info.src_path = info.path |
| 2311 | info.path = out.next() |
| 2312 | r[info.path] = info |
| 2313 | return r |
| 2314 | finally: |
| 2315 | p.Wait() |
| 2316 | |
| 2317 | def GetHead(self): |
Shawn O. Pearce | 5b23f24 | 2009-04-17 18:43:33 -0700 | [diff] [blame] | 2318 | if self._bare: |
| 2319 | path = os.path.join(self._project.gitdir, HEAD) |
| 2320 | else: |
| 2321 | path = os.path.join(self._project.worktree, '.git', HEAD) |
Conley Owens | 75ee057 | 2012-11-15 17:33:11 -0800 | [diff] [blame] | 2322 | try: |
| 2323 | fd = open(path, 'rb') |
| 2324 | except IOError: |
| 2325 | raise NoManifestException(path) |
Shawn O. Pearce | 76ca9f8 | 2009-04-18 14:48:03 -0700 | [diff] [blame] | 2326 | try: |
| 2327 | line = fd.read() |
| 2328 | finally: |
| 2329 | fd.close() |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 2330 | try: |
| 2331 | line = line.decode() |
| 2332 | except AttributeError: |
| 2333 | pass |
Shawn O. Pearce | 5b23f24 | 2009-04-17 18:43:33 -0700 | [diff] [blame] | 2334 | if line.startswith('ref: '): |
| 2335 | return line[5:-1] |
| 2336 | return line[:-1] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2337 | |
| 2338 | def SetHead(self, ref, message=None): |
| 2339 | cmdv = [] |
| 2340 | if message is not None: |
| 2341 | cmdv.extend(['-m', message]) |
| 2342 | cmdv.append(HEAD) |
| 2343 | cmdv.append(ref) |
| 2344 | self.symbolic_ref(*cmdv) |
| 2345 | |
| 2346 | def DetachHead(self, new, message=None): |
| 2347 | cmdv = ['--no-deref'] |
| 2348 | if message is not None: |
| 2349 | cmdv.extend(['-m', message]) |
| 2350 | cmdv.append(HEAD) |
| 2351 | cmdv.append(new) |
| 2352 | self.update_ref(*cmdv) |
| 2353 | |
| 2354 | def UpdateRef(self, name, new, old=None, |
| 2355 | message=None, |
| 2356 | detach=False): |
| 2357 | cmdv = [] |
| 2358 | if message is not None: |
| 2359 | cmdv.extend(['-m', message]) |
| 2360 | if detach: |
| 2361 | cmdv.append('--no-deref') |
| 2362 | cmdv.append(name) |
| 2363 | cmdv.append(new) |
| 2364 | if old is not None: |
| 2365 | cmdv.append(old) |
| 2366 | self.update_ref(*cmdv) |
| 2367 | |
| 2368 | def DeleteRef(self, name, old=None): |
| 2369 | if not old: |
| 2370 | old = self.rev_parse(name) |
| 2371 | self.update_ref('-d', name, old) |
Shawn O. Pearce | fbcde47 | 2009-04-17 20:58:02 -0700 | [diff] [blame] | 2372 | self._project.bare_ref.deleted(name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2373 | |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 2374 | def rev_list(self, *args, **kw): |
| 2375 | if 'format' in kw: |
| 2376 | cmdv = ['log', '--pretty=format:%s' % kw['format']] |
| 2377 | else: |
| 2378 | cmdv = ['rev-list'] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2379 | cmdv.extend(args) |
| 2380 | p = GitCommand(self._project, |
| 2381 | cmdv, |
| 2382 | bare = self._bare, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2383 | gitdir=self._gitdir, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2384 | capture_stdout = True, |
| 2385 | capture_stderr = True) |
| 2386 | r = [] |
| 2387 | for line in p.process.stdout: |
Shawn O. Pearce | 8ad8a0e | 2009-05-29 18:28:25 -0700 | [diff] [blame] | 2388 | if line[-1] == '\n': |
| 2389 | line = line[:-1] |
| 2390 | r.append(line) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2391 | if p.Wait() != 0: |
| 2392 | raise GitError('%s rev-list %s: %s' % ( |
| 2393 | self._project.name, |
| 2394 | str(args), |
| 2395 | p.stderr)) |
| 2396 | return r |
| 2397 | |
| 2398 | def __getattr__(self, name): |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 2399 | """Allow arbitrary git commands using pythonic syntax. |
| 2400 | |
| 2401 | This allows you to do things like: |
| 2402 | git_obj.rev_parse('HEAD') |
| 2403 | |
| 2404 | Since we don't have a 'rev_parse' method defined, the __getattr__ will |
| 2405 | 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] | 2406 | Any other positional arguments will be passed to the git command, and the |
| 2407 | following keyword arguments are supported: |
| 2408 | 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] | 2409 | |
| 2410 | Args: |
| 2411 | name: The name of the git command to call. Any '_' characters will |
| 2412 | be replaced with '-'. |
| 2413 | |
| 2414 | Returns: |
| 2415 | A callable object that will try to call git with the named command. |
| 2416 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2417 | name = name.replace('_', '-') |
Dave Borowitz | 091f893 | 2012-10-23 17:01:04 -0700 | [diff] [blame] | 2418 | def runner(*args, **kwargs): |
| 2419 | cmdv = [] |
| 2420 | config = kwargs.pop('config', None) |
| 2421 | for k in kwargs: |
| 2422 | raise TypeError('%s() got an unexpected keyword argument %r' |
| 2423 | % (name, k)) |
| 2424 | if config is not None: |
Dave Borowitz | b42b474 | 2012-10-31 12:27:27 -0700 | [diff] [blame] | 2425 | if not git_require((1, 7, 2)): |
| 2426 | raise ValueError('cannot set config on command line for %s()' |
| 2427 | % name) |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 2428 | for k, v in config.items(): |
Dave Borowitz | 091f893 | 2012-10-23 17:01:04 -0700 | [diff] [blame] | 2429 | cmdv.append('-c') |
| 2430 | cmdv.append('%s=%s' % (k, v)) |
| 2431 | cmdv.append(name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2432 | cmdv.extend(args) |
| 2433 | p = GitCommand(self._project, |
| 2434 | cmdv, |
| 2435 | bare = self._bare, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2436 | gitdir=self._gitdir, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2437 | capture_stdout = True, |
| 2438 | capture_stderr = True) |
| 2439 | if p.Wait() != 0: |
| 2440 | raise GitError('%s %s: %s' % ( |
| 2441 | self._project.name, |
| 2442 | name, |
| 2443 | p.stderr)) |
| 2444 | r = p.stdout |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 2445 | try: |
Conley Owens | edd0151 | 2013-09-26 12:59:58 -0700 | [diff] [blame] | 2446 | r = r.decode('utf-8') |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 2447 | except AttributeError: |
| 2448 | pass |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2449 | if r.endswith('\n') and r.index('\n') == len(r) - 1: |
| 2450 | return r[:-1] |
| 2451 | return r |
| 2452 | return runner |
| 2453 | |
| 2454 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2455 | class _PriorSyncFailedError(Exception): |
| 2456 | def __str__(self): |
| 2457 | return 'prior sync failed; rebase still in progress' |
| 2458 | |
| 2459 | class _DirtyError(Exception): |
| 2460 | def __str__(self): |
| 2461 | return 'contains uncommitted changes' |
| 2462 | |
| 2463 | class _InfoMessage(object): |
| 2464 | def __init__(self, project, text): |
| 2465 | self.project = project |
| 2466 | self.text = text |
| 2467 | |
| 2468 | def Print(self, syncbuf): |
| 2469 | syncbuf.out.info('%s/: %s', self.project.relpath, self.text) |
| 2470 | syncbuf.out.nl() |
| 2471 | |
| 2472 | class _Failure(object): |
| 2473 | def __init__(self, project, why): |
| 2474 | self.project = project |
| 2475 | self.why = why |
| 2476 | |
| 2477 | def Print(self, syncbuf): |
| 2478 | syncbuf.out.fail('error: %s/: %s', |
| 2479 | self.project.relpath, |
| 2480 | str(self.why)) |
| 2481 | syncbuf.out.nl() |
| 2482 | |
| 2483 | class _Later(object): |
| 2484 | def __init__(self, project, action): |
| 2485 | self.project = project |
| 2486 | self.action = action |
| 2487 | |
| 2488 | def Run(self, syncbuf): |
| 2489 | out = syncbuf.out |
| 2490 | out.project('project %s/', self.project.relpath) |
| 2491 | out.nl() |
| 2492 | try: |
| 2493 | self.action() |
| 2494 | out.nl() |
| 2495 | return True |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2496 | except GitError: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 2497 | out.nl() |
| 2498 | return False |
| 2499 | |
| 2500 | class _SyncColoring(Coloring): |
| 2501 | def __init__(self, config): |
| 2502 | Coloring.__init__(self, config, 'reposync') |
| 2503 | self.project = self.printer('header', attr = 'bold') |
| 2504 | self.info = self.printer('info') |
| 2505 | self.fail = self.printer('fail', fg='red') |
| 2506 | |
| 2507 | class SyncBuffer(object): |
| 2508 | def __init__(self, config, detach_head=False): |
| 2509 | self._messages = [] |
| 2510 | self._failures = [] |
| 2511 | self._later_queue1 = [] |
| 2512 | self._later_queue2 = [] |
| 2513 | |
| 2514 | self.out = _SyncColoring(config) |
| 2515 | self.out.redirect(sys.stderr) |
| 2516 | |
| 2517 | self.detach_head = detach_head |
| 2518 | self.clean = True |
| 2519 | |
| 2520 | def info(self, project, fmt, *args): |
| 2521 | self._messages.append(_InfoMessage(project, fmt % args)) |
| 2522 | |
| 2523 | def fail(self, project, err=None): |
| 2524 | self._failures.append(_Failure(project, err)) |
| 2525 | self.clean = False |
| 2526 | |
| 2527 | def later1(self, project, what): |
| 2528 | self._later_queue1.append(_Later(project, what)) |
| 2529 | |
| 2530 | def later2(self, project, what): |
| 2531 | self._later_queue2.append(_Later(project, what)) |
| 2532 | |
| 2533 | def Finish(self): |
| 2534 | self._PrintMessages() |
| 2535 | self._RunLater() |
| 2536 | self._PrintMessages() |
| 2537 | return self.clean |
| 2538 | |
| 2539 | def _RunLater(self): |
| 2540 | for q in ['_later_queue1', '_later_queue2']: |
| 2541 | if not self._RunQueue(q): |
| 2542 | return |
| 2543 | |
| 2544 | def _RunQueue(self, queue): |
| 2545 | for m in getattr(self, queue): |
| 2546 | if not m.Run(self): |
| 2547 | self.clean = False |
| 2548 | return False |
| 2549 | setattr(self, queue, []) |
| 2550 | return True |
| 2551 | |
| 2552 | def _PrintMessages(self): |
| 2553 | for m in self._messages: |
| 2554 | m.Print(self) |
| 2555 | for m in self._failures: |
| 2556 | m.Print(self) |
| 2557 | |
| 2558 | self._messages = [] |
| 2559 | self._failures = [] |
| 2560 | |
| 2561 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2562 | class MetaProject(Project): |
| 2563 | """A special project housed under .repo. |
| 2564 | """ |
| 2565 | def __init__(self, manifest, name, gitdir, worktree): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2566 | Project.__init__(self, |
| 2567 | manifest = manifest, |
| 2568 | name = name, |
| 2569 | gitdir = gitdir, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 2570 | objdir = gitdir, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2571 | worktree = worktree, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 2572 | remote = RemoteSpec('origin'), |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2573 | relpath = '.repo/%s' % name, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 2574 | revisionExpr = 'refs/heads/master', |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 2575 | revisionId = None, |
| 2576 | groups = None) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2577 | |
| 2578 | def PreSync(self): |
| 2579 | if self.Exists: |
| 2580 | cb = self.CurrentBranch |
| 2581 | if cb: |
| 2582 | base = self.GetBranch(cb).merge |
| 2583 | if base: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 2584 | self.revisionExpr = base |
| 2585 | self.revisionId = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2586 | |
Florian Vallee | 5d01650 | 2012-06-07 17:19:26 +0200 | [diff] [blame] | 2587 | def MetaBranchSwitch(self, target): |
| 2588 | """ Prepare MetaProject for manifest branch switch |
| 2589 | """ |
| 2590 | |
| 2591 | # detach and delete manifest branch, allowing a new |
| 2592 | # branch to take over |
| 2593 | syncbuf = SyncBuffer(self.config, detach_head = True) |
| 2594 | self.Sync_LocalHalf(syncbuf) |
| 2595 | syncbuf.Finish() |
| 2596 | |
| 2597 | return GitCommand(self, |
Torne (Richard Coles) | e8f75fa | 2012-07-20 15:32:19 +0100 | [diff] [blame] | 2598 | ['update-ref', '-d', 'refs/heads/default'], |
Florian Vallee | 5d01650 | 2012-06-07 17:19:26 +0200 | [diff] [blame] | 2599 | capture_stdout = True, |
| 2600 | capture_stderr = True).Wait() == 0 |
| 2601 | |
| 2602 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2603 | @property |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 2604 | def LastFetch(self): |
| 2605 | try: |
| 2606 | fh = os.path.join(self.gitdir, 'FETCH_HEAD') |
| 2607 | return os.path.getmtime(fh) |
| 2608 | except OSError: |
| 2609 | return 0 |
| 2610 | |
| 2611 | @property |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2612 | def HasChanges(self): |
| 2613 | """Has the remote received new commits not yet checked out? |
| 2614 | """ |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 2615 | if not self.remote or not self.revisionExpr: |
Shawn O. Pearce | 336f7bd | 2009-04-18 10:39:28 -0700 | [diff] [blame] | 2616 | return False |
| 2617 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2618 | all_refs = self.bare_ref.all |
| 2619 | revid = self.GetRevisionId(all_refs) |
Shawn O. Pearce | 336f7bd | 2009-04-18 10:39:28 -0700 | [diff] [blame] | 2620 | head = self.work_git.GetHead() |
| 2621 | if head.startswith(R_HEADS): |
| 2622 | try: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 2623 | head = all_refs[head] |
Shawn O. Pearce | 336f7bd | 2009-04-18 10:39:28 -0700 | [diff] [blame] | 2624 | except KeyError: |
| 2625 | head = None |
| 2626 | |
| 2627 | if revid == head: |
| 2628 | return False |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 2629 | elif self._revlist(not_rev(HEAD), revid): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2630 | return True |
| 2631 | return False |