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