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