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 | |
Shawn O. Pearce | 438ee1c | 2008-11-03 09:59:36 -0800 | [diff] [blame] | 15 | import errno |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 16 | import filecmp |
| 17 | import os |
| 18 | import re |
| 19 | import shutil |
| 20 | import stat |
| 21 | import sys |
| 22 | import urllib2 |
| 23 | |
| 24 | from color import Coloring |
| 25 | from git_command import GitCommand |
| 26 | from git_config import GitConfig, IsId |
| 27 | from gerrit_upload import UploadBundle |
| 28 | from error import GitError, ImportError, UploadError |
| 29 | from remote import Remote |
| 30 | from codereview import proto_client |
| 31 | |
| 32 | HEAD = 'HEAD' |
| 33 | R_HEADS = 'refs/heads/' |
| 34 | R_TAGS = 'refs/tags/' |
| 35 | R_PUB = 'refs/published/' |
| 36 | R_M = 'refs/remotes/m/' |
| 37 | |
| 38 | def _warn(fmt, *args): |
| 39 | msg = fmt % args |
| 40 | print >>sys.stderr, 'warn: %s' % msg |
| 41 | |
| 42 | def _info(fmt, *args): |
| 43 | msg = fmt % args |
| 44 | print >>sys.stderr, 'info: %s' % msg |
| 45 | |
| 46 | def not_rev(r): |
| 47 | return '^' + r |
| 48 | |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 49 | |
| 50 | hook_list = None |
| 51 | def repo_hooks(): |
| 52 | global hook_list |
| 53 | if hook_list is None: |
| 54 | d = os.path.abspath(os.path.dirname(__file__)) |
| 55 | d = os.path.join(d , 'hooks') |
| 56 | hook_list = map(lambda x: os.path.join(d, x), os.listdir(d)) |
| 57 | return hook_list |
| 58 | |
| 59 | def relpath(dst, src): |
| 60 | src = os.path.dirname(src) |
| 61 | top = os.path.commonprefix([dst, src]) |
| 62 | if top.endswith('/'): |
| 63 | top = top[:-1] |
| 64 | else: |
| 65 | top = os.path.dirname(top) |
| 66 | |
| 67 | tmp = src |
| 68 | rel = '' |
| 69 | while top != tmp: |
| 70 | rel += '../' |
| 71 | tmp = os.path.dirname(tmp) |
| 72 | return rel + dst[len(top) + 1:] |
| 73 | |
| 74 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 75 | class DownloadedChange(object): |
| 76 | _commit_cache = None |
| 77 | |
| 78 | def __init__(self, project, base, change_id, ps_id, commit): |
| 79 | self.project = project |
| 80 | self.base = base |
| 81 | self.change_id = change_id |
| 82 | self.ps_id = ps_id |
| 83 | self.commit = commit |
| 84 | |
| 85 | @property |
| 86 | def commits(self): |
| 87 | if self._commit_cache is None: |
| 88 | self._commit_cache = self.project.bare_git.rev_list( |
| 89 | '--abbrev=8', |
| 90 | '--abbrev-commit', |
| 91 | '--pretty=oneline', |
| 92 | '--reverse', |
| 93 | '--date-order', |
| 94 | not_rev(self.base), |
| 95 | self.commit, |
| 96 | '--') |
| 97 | return self._commit_cache |
| 98 | |
| 99 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 100 | class ReviewableBranch(object): |
| 101 | _commit_cache = None |
| 102 | |
| 103 | def __init__(self, project, branch, base): |
| 104 | self.project = project |
| 105 | self.branch = branch |
| 106 | self.base = base |
| 107 | |
| 108 | @property |
| 109 | def name(self): |
| 110 | return self.branch.name |
| 111 | |
| 112 | @property |
| 113 | def commits(self): |
| 114 | if self._commit_cache is None: |
| 115 | self._commit_cache = self.project.bare_git.rev_list( |
| 116 | '--abbrev=8', |
| 117 | '--abbrev-commit', |
| 118 | '--pretty=oneline', |
| 119 | '--reverse', |
| 120 | '--date-order', |
| 121 | not_rev(self.base), |
| 122 | R_HEADS + self.name, |
| 123 | '--') |
| 124 | return self._commit_cache |
| 125 | |
| 126 | @property |
| 127 | def date(self): |
| 128 | return self.project.bare_git.log( |
| 129 | '--pretty=format:%cd', |
| 130 | '-n', '1', |
| 131 | R_HEADS + self.name, |
| 132 | '--') |
| 133 | |
| 134 | def UploadForReview(self): |
| 135 | self.project.UploadForReview(self.name) |
| 136 | |
| 137 | @property |
| 138 | def tip_url(self): |
| 139 | me = self.project.GetBranch(self.name) |
| 140 | commit = self.project.bare_git.rev_parse(R_HEADS + self.name) |
| 141 | return 'http://%s/r/%s' % (me.remote.review, commit[0:12]) |
| 142 | |
Shawn O. Pearce | 0758d2f | 2008-10-22 13:13:40 -0700 | [diff] [blame] | 143 | @property |
| 144 | def owner_email(self): |
| 145 | return self.project.UserEmail |
| 146 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 147 | |
| 148 | class StatusColoring(Coloring): |
| 149 | def __init__(self, config): |
| 150 | Coloring.__init__(self, config, 'status') |
| 151 | self.project = self.printer('header', attr = 'bold') |
| 152 | self.branch = self.printer('header', attr = 'bold') |
| 153 | self.nobranch = self.printer('nobranch', fg = 'red') |
| 154 | |
| 155 | self.added = self.printer('added', fg = 'green') |
| 156 | self.changed = self.printer('changed', fg = 'red') |
| 157 | self.untracked = self.printer('untracked', fg = 'red') |
| 158 | |
| 159 | |
| 160 | class DiffColoring(Coloring): |
| 161 | def __init__(self, config): |
| 162 | Coloring.__init__(self, config, 'diff') |
| 163 | self.project = self.printer('header', attr = 'bold') |
| 164 | |
| 165 | |
| 166 | class _CopyFile: |
| 167 | def __init__(self, src, dest): |
| 168 | self.src = src |
| 169 | self.dest = dest |
| 170 | |
| 171 | def _Copy(self): |
| 172 | src = self.src |
| 173 | dest = self.dest |
| 174 | # copy file if it does not exist or is out of date |
| 175 | if not os.path.exists(dest) or not filecmp.cmp(src, dest): |
| 176 | try: |
| 177 | # remove existing file first, since it might be read-only |
| 178 | if os.path.exists(dest): |
| 179 | os.remove(dest) |
| 180 | shutil.copy(src, dest) |
| 181 | # make the file read-only |
| 182 | mode = os.stat(dest)[stat.ST_MODE] |
| 183 | mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH) |
| 184 | os.chmod(dest, mode) |
| 185 | except IOError: |
| 186 | print >>sys.stderr, \ |
| 187 | 'error: Cannot copy file %s to %s' \ |
| 188 | % (src, dest) |
| 189 | |
| 190 | |
| 191 | class Project(object): |
| 192 | def __init__(self, |
| 193 | manifest, |
| 194 | name, |
| 195 | remote, |
| 196 | gitdir, |
| 197 | worktree, |
| 198 | relpath, |
| 199 | revision): |
| 200 | self.manifest = manifest |
| 201 | self.name = name |
| 202 | self.remote = remote |
| 203 | self.gitdir = gitdir |
| 204 | self.worktree = worktree |
| 205 | self.relpath = relpath |
| 206 | self.revision = revision |
| 207 | self.snapshots = {} |
| 208 | self.extraRemotes = {} |
| 209 | self.copyfiles = [] |
| 210 | self.config = GitConfig.ForRepository( |
| 211 | gitdir = self.gitdir, |
| 212 | defaults = self.manifest.globalConfig) |
| 213 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 214 | if self.worktree: |
| 215 | self.work_git = self._GitGetByExec(self, bare=False) |
| 216 | else: |
| 217 | self.work_git = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 218 | self.bare_git = self._GitGetByExec(self, bare=True) |
| 219 | |
| 220 | @property |
| 221 | def Exists(self): |
| 222 | return os.path.isdir(self.gitdir) |
| 223 | |
| 224 | @property |
| 225 | def CurrentBranch(self): |
| 226 | """Obtain the name of the currently checked out branch. |
| 227 | The branch name omits the 'refs/heads/' prefix. |
| 228 | None is returned if the project is on a detached HEAD. |
| 229 | """ |
| 230 | try: |
| 231 | b = self.work_git.GetHead() |
| 232 | except GitError: |
| 233 | return None |
| 234 | if b.startswith(R_HEADS): |
| 235 | return b[len(R_HEADS):] |
| 236 | return None |
| 237 | |
| 238 | def IsDirty(self, consider_untracked=True): |
| 239 | """Is the working directory modified in some way? |
| 240 | """ |
| 241 | self.work_git.update_index('-q', |
| 242 | '--unmerged', |
| 243 | '--ignore-missing', |
| 244 | '--refresh') |
| 245 | if self.work_git.DiffZ('diff-index','-M','--cached',HEAD): |
| 246 | return True |
| 247 | if self.work_git.DiffZ('diff-files'): |
| 248 | return True |
| 249 | if consider_untracked and self.work_git.LsOthers(): |
| 250 | return True |
| 251 | return False |
| 252 | |
| 253 | _userident_name = None |
| 254 | _userident_email = None |
| 255 | |
| 256 | @property |
| 257 | def UserName(self): |
| 258 | """Obtain the user's personal name. |
| 259 | """ |
| 260 | if self._userident_name is None: |
| 261 | self._LoadUserIdentity() |
| 262 | return self._userident_name |
| 263 | |
| 264 | @property |
| 265 | def UserEmail(self): |
| 266 | """Obtain the user's email address. This is very likely |
| 267 | to be their Gerrit login. |
| 268 | """ |
| 269 | if self._userident_email is None: |
| 270 | self._LoadUserIdentity() |
| 271 | return self._userident_email |
| 272 | |
| 273 | def _LoadUserIdentity(self): |
| 274 | u = self.bare_git.var('GIT_COMMITTER_IDENT') |
| 275 | m = re.compile("^(.*) <([^>]*)> ").match(u) |
| 276 | if m: |
| 277 | self._userident_name = m.group(1) |
| 278 | self._userident_email = m.group(2) |
| 279 | else: |
| 280 | self._userident_name = '' |
| 281 | self._userident_email = '' |
| 282 | |
| 283 | def GetRemote(self, name): |
| 284 | """Get the configuration for a single remote. |
| 285 | """ |
| 286 | return self.config.GetRemote(name) |
| 287 | |
| 288 | def GetBranch(self, name): |
| 289 | """Get the configuration for a single branch. |
| 290 | """ |
| 291 | return self.config.GetBranch(name) |
| 292 | |
| 293 | |
| 294 | ## Status Display ## |
| 295 | |
| 296 | def PrintWorkTreeStatus(self): |
| 297 | """Prints the status of the repository to stdout. |
| 298 | """ |
| 299 | if not os.path.isdir(self.worktree): |
| 300 | print '' |
| 301 | print 'project %s/' % self.relpath |
| 302 | print ' missing (run "repo sync")' |
| 303 | return |
| 304 | |
| 305 | self.work_git.update_index('-q', |
| 306 | '--unmerged', |
| 307 | '--ignore-missing', |
| 308 | '--refresh') |
| 309 | di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD) |
| 310 | df = self.work_git.DiffZ('diff-files') |
| 311 | do = self.work_git.LsOthers() |
| 312 | if not di and not df and not do: |
| 313 | return |
| 314 | |
| 315 | out = StatusColoring(self.config) |
| 316 | out.project('project %-40s', self.relpath + '/') |
| 317 | |
| 318 | branch = self.CurrentBranch |
| 319 | if branch is None: |
| 320 | out.nobranch('(*** NO BRANCH ***)') |
| 321 | else: |
| 322 | out.branch('branch %s', branch) |
| 323 | out.nl() |
| 324 | |
| 325 | paths = list() |
| 326 | paths.extend(di.keys()) |
| 327 | paths.extend(df.keys()) |
| 328 | paths.extend(do) |
| 329 | |
| 330 | paths = list(set(paths)) |
| 331 | paths.sort() |
| 332 | |
| 333 | for p in paths: |
| 334 | try: i = di[p] |
| 335 | except KeyError: i = None |
| 336 | |
| 337 | try: f = df[p] |
| 338 | except KeyError: f = None |
| 339 | |
| 340 | if i: i_status = i.status.upper() |
| 341 | else: i_status = '-' |
| 342 | |
| 343 | if f: f_status = f.status.lower() |
| 344 | else: f_status = '-' |
| 345 | |
| 346 | if i and i.src_path: |
| 347 | line = ' %s%s\t%s => (%s%%)' % (i_status, f_status, |
| 348 | i.src_path, p, i.level) |
| 349 | else: |
| 350 | line = ' %s%s\t%s' % (i_status, f_status, p) |
| 351 | |
| 352 | if i and not f: |
| 353 | out.added('%s', line) |
| 354 | elif (i and f) or (not i and f): |
| 355 | out.changed('%s', line) |
| 356 | elif not i and not f: |
| 357 | out.untracked('%s', line) |
| 358 | else: |
| 359 | out.write('%s', line) |
| 360 | out.nl() |
| 361 | |
| 362 | def PrintWorkTreeDiff(self): |
| 363 | """Prints the status of the repository to stdout. |
| 364 | """ |
| 365 | out = DiffColoring(self.config) |
| 366 | cmd = ['diff'] |
| 367 | if out.is_on: |
| 368 | cmd.append('--color') |
| 369 | cmd.append(HEAD) |
| 370 | cmd.append('--') |
| 371 | p = GitCommand(self, |
| 372 | cmd, |
| 373 | capture_stdout = True, |
| 374 | capture_stderr = True) |
| 375 | has_diff = False |
| 376 | for line in p.process.stdout: |
| 377 | if not has_diff: |
| 378 | out.nl() |
| 379 | out.project('project %s/' % self.relpath) |
| 380 | out.nl() |
| 381 | has_diff = True |
| 382 | print line[:-1] |
| 383 | p.Wait() |
| 384 | |
| 385 | |
| 386 | ## Publish / Upload ## |
| 387 | |
| 388 | def WasPublished(self, branch): |
| 389 | """Was the branch published (uploaded) for code review? |
| 390 | If so, returns the SHA-1 hash of the last published |
| 391 | state for the branch. |
| 392 | """ |
| 393 | try: |
| 394 | return self.bare_git.rev_parse(R_PUB + branch) |
| 395 | except GitError: |
| 396 | return None |
| 397 | |
| 398 | def CleanPublishedCache(self): |
| 399 | """Prunes any stale published refs. |
| 400 | """ |
| 401 | heads = set() |
| 402 | canrm = {} |
| 403 | for name, id in self._allrefs.iteritems(): |
| 404 | if name.startswith(R_HEADS): |
| 405 | heads.add(name) |
| 406 | elif name.startswith(R_PUB): |
| 407 | canrm[name] = id |
| 408 | |
| 409 | for name, id in canrm.iteritems(): |
| 410 | n = name[len(R_PUB):] |
| 411 | if R_HEADS + n not in heads: |
| 412 | self.bare_git.DeleteRef(name, id) |
| 413 | |
| 414 | def GetUploadableBranches(self): |
| 415 | """List any branches which can be uploaded for review. |
| 416 | """ |
| 417 | heads = {} |
| 418 | pubed = {} |
| 419 | |
| 420 | for name, id in self._allrefs.iteritems(): |
| 421 | if name.startswith(R_HEADS): |
| 422 | heads[name[len(R_HEADS):]] = id |
| 423 | elif name.startswith(R_PUB): |
| 424 | pubed[name[len(R_PUB):]] = id |
| 425 | |
| 426 | ready = [] |
| 427 | for branch, id in heads.iteritems(): |
| 428 | if branch in pubed and pubed[branch] == id: |
| 429 | continue |
| 430 | |
| 431 | branch = self.GetBranch(branch) |
| 432 | base = branch.LocalMerge |
| 433 | if branch.LocalMerge: |
| 434 | rb = ReviewableBranch(self, branch, base) |
| 435 | if rb.commits: |
| 436 | ready.append(rb) |
| 437 | return ready |
| 438 | |
| 439 | def UploadForReview(self, branch=None): |
| 440 | """Uploads the named branch for code review. |
| 441 | """ |
| 442 | if branch is None: |
| 443 | branch = self.CurrentBranch |
| 444 | if branch is None: |
| 445 | raise GitError('not currently on a branch') |
| 446 | |
| 447 | branch = self.GetBranch(branch) |
| 448 | if not branch.LocalMerge: |
| 449 | raise GitError('branch %s does not track a remote' % branch.name) |
| 450 | if not branch.remote.review: |
| 451 | raise GitError('remote %s has no review url' % branch.remote.name) |
| 452 | |
| 453 | dest_branch = branch.merge |
| 454 | if not dest_branch.startswith(R_HEADS): |
| 455 | dest_branch = R_HEADS + dest_branch |
| 456 | |
| 457 | base_list = [] |
| 458 | for name, id in self._allrefs.iteritems(): |
| 459 | if branch.remote.WritesTo(name): |
| 460 | base_list.append(not_rev(name)) |
| 461 | if not base_list: |
| 462 | raise GitError('no base refs, cannot upload %s' % branch.name) |
| 463 | |
Shawn O. Pearce | 339ba9f | 2008-11-06 09:52:51 -0800 | [diff] [blame] | 464 | if not branch.remote.projectname: |
| 465 | branch.remote.projectname = self.name |
| 466 | branch.remote.Save() |
| 467 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 468 | print >>sys.stderr, '' |
| 469 | _info("Uploading %s to %s:", branch.name, self.name) |
| 470 | try: |
| 471 | UploadBundle(project = self, |
| 472 | server = branch.remote.review, |
| 473 | email = self.UserEmail, |
Shawn O. Pearce | 339ba9f | 2008-11-06 09:52:51 -0800 | [diff] [blame] | 474 | dest_project = branch.remote.projectname, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 475 | dest_branch = dest_branch, |
| 476 | src_branch = R_HEADS + branch.name, |
| 477 | bases = base_list) |
| 478 | except proto_client.ClientLoginError: |
| 479 | raise UploadError('Login failure') |
| 480 | except urllib2.HTTPError, e: |
| 481 | raise UploadError('HTTP error %d' % e.code) |
| 482 | |
| 483 | msg = "posted to %s for %s" % (branch.remote.review, dest_branch) |
| 484 | self.bare_git.UpdateRef(R_PUB + branch.name, |
| 485 | R_HEADS + branch.name, |
| 486 | message = msg) |
| 487 | |
| 488 | |
| 489 | ## Sync ## |
| 490 | |
| 491 | def Sync_NetworkHalf(self): |
| 492 | """Perform only the network IO portion of the sync process. |
| 493 | Local working directory/branch state is not affected. |
| 494 | """ |
| 495 | if not self.Exists: |
| 496 | print >>sys.stderr |
| 497 | print >>sys.stderr, 'Initializing project %s ...' % self.name |
| 498 | self._InitGitDir() |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 499 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 500 | self._InitRemote() |
| 501 | for r in self.extraRemotes.values(): |
| 502 | if not self._RemoteFetch(r.name): |
| 503 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 504 | if not self._RemoteFetch(): |
| 505 | return False |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 506 | |
| 507 | if self.worktree: |
| 508 | self._RepairAndroidImportErrors() |
| 509 | self._InitMRef() |
| 510 | else: |
| 511 | self._InitMirrorHead() |
| 512 | try: |
| 513 | os.remove(os.path.join(self.gitdir, 'FETCH_HEAD')) |
| 514 | except OSError: |
| 515 | pass |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 516 | return True |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 517 | |
| 518 | def PostRepoUpgrade(self): |
| 519 | self._InitHooks() |
| 520 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 521 | def _CopyFiles(self): |
| 522 | for file in self.copyfiles: |
| 523 | file._Copy() |
| 524 | |
Shawn O. Pearce | 329c31d | 2008-10-24 09:17:25 -0700 | [diff] [blame] | 525 | def _RepairAndroidImportErrors(self): |
| 526 | if self.name in ['platform/external/iptables', |
| 527 | 'platform/external/libpcap', |
| 528 | 'platform/external/tcpdump', |
| 529 | 'platform/external/webkit', |
| 530 | 'platform/system/wlan/ti']: |
| 531 | # I hate myself for doing this... |
| 532 | # |
| 533 | # In the initial Android 1.0 release these projects were |
| 534 | # shipped, some users got them, and then the history had |
| 535 | # to be rewritten to correct problems with their imports. |
| 536 | # The 'android-1.0' tag may still be pointing at the old |
| 537 | # history, so we need to drop the tag and fetch it again. |
| 538 | # |
| 539 | try: |
| 540 | remote = self.GetRemote(self.remote.name) |
| 541 | relname = remote.ToLocal(R_HEADS + 'release-1.0') |
| 542 | tagname = R_TAGS + 'android-1.0' |
| 543 | if self._revlist(not_rev(relname), tagname): |
| 544 | cmd = ['fetch', remote.name, '+%s:%s' % (tagname, tagname)] |
| 545 | GitCommand(self, cmd, bare = True).Wait() |
| 546 | except GitError: |
| 547 | pass |
| 548 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 549 | def Sync_LocalHalf(self): |
| 550 | """Perform only the local IO portion of the sync process. |
| 551 | Network access is not required. |
| 552 | |
| 553 | Return: |
| 554 | True: the sync was successful |
| 555 | False: the sync requires user input |
| 556 | """ |
| 557 | self._InitWorkTree() |
| 558 | self.CleanPublishedCache() |
| 559 | |
| 560 | rem = self.GetRemote(self.remote.name) |
| 561 | rev = rem.ToLocal(self.revision) |
| 562 | branch = self.CurrentBranch |
| 563 | |
| 564 | if branch is None: |
| 565 | # Currently on a detached HEAD. The user is assumed to |
| 566 | # not have any local modifications worth worrying about. |
| 567 | # |
| 568 | lost = self._revlist(not_rev(rev), HEAD) |
| 569 | if lost: |
| 570 | _info("[%s] Discarding %d commits", self.name, len(lost)) |
| 571 | try: |
| 572 | self._Checkout(rev, quiet=True) |
| 573 | except GitError: |
| 574 | return False |
| 575 | self._CopyFiles() |
| 576 | return True |
| 577 | |
| 578 | branch = self.GetBranch(branch) |
| 579 | merge = branch.LocalMerge |
| 580 | |
| 581 | if not merge: |
| 582 | # The current branch has no tracking configuration. |
| 583 | # Jump off it to a deatched HEAD. |
| 584 | # |
| 585 | _info("[%s] Leaving %s" |
| 586 | " (does not track any upstream)", |
| 587 | self.name, |
| 588 | branch.name) |
| 589 | try: |
| 590 | self._Checkout(rev, quiet=True) |
| 591 | except GitError: |
| 592 | return False |
| 593 | self._CopyFiles() |
| 594 | return True |
| 595 | |
| 596 | upstream_gain = self._revlist(not_rev(HEAD), rev) |
| 597 | pub = self.WasPublished(branch.name) |
| 598 | if pub: |
| 599 | not_merged = self._revlist(not_rev(rev), pub) |
| 600 | if not_merged: |
| 601 | if upstream_gain: |
| 602 | # The user has published this branch and some of those |
| 603 | # commits are not yet merged upstream. We do not want |
| 604 | # to rewrite the published commits so we punt. |
| 605 | # |
| 606 | _info("[%s] Branch %s is published," |
| 607 | " but is now %d commits behind.", |
| 608 | self.name, branch.name, len(upstream_gain)) |
| 609 | _info("[%s] Consider merging or rebasing the" |
| 610 | " unpublished commits.", self.name) |
| 611 | return True |
Shawn O. Pearce | 23d7781 | 2008-10-30 11:06:57 -0700 | [diff] [blame] | 612 | elif upstream_gain: |
Shawn O. Pearce | a54c527 | 2008-10-30 11:03:00 -0700 | [diff] [blame] | 613 | # We can fast-forward safely. |
| 614 | # |
| 615 | try: |
| 616 | self._FastForward(rev) |
| 617 | except GitError: |
| 618 | return False |
| 619 | self._CopyFiles() |
| 620 | return True |
Shawn O. Pearce | 23d7781 | 2008-10-30 11:06:57 -0700 | [diff] [blame] | 621 | else: |
| 622 | # Trivially no changes in the upstream. |
| 623 | # |
| 624 | return True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 625 | |
| 626 | if merge == rev: |
| 627 | try: |
| 628 | old_merge = self.bare_git.rev_parse('%s@{1}' % merge) |
| 629 | except GitError: |
| 630 | old_merge = merge |
Shawn O. Pearce | 0734600 | 2008-10-21 07:09:27 -0700 | [diff] [blame] | 631 | if old_merge == '0000000000000000000000000000000000000000' \ |
| 632 | or old_merge == '': |
| 633 | old_merge = merge |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 634 | else: |
| 635 | # The upstream switched on us. Time to cross our fingers |
| 636 | # and pray that the old upstream also wasn't in the habit |
| 637 | # of rebasing itself. |
| 638 | # |
| 639 | _info("[%s] Manifest switched from %s to %s", |
| 640 | self.name, merge, rev) |
| 641 | old_merge = merge |
| 642 | |
| 643 | if rev == old_merge: |
| 644 | upstream_lost = [] |
| 645 | else: |
| 646 | upstream_lost = self._revlist(not_rev(rev), old_merge) |
| 647 | |
| 648 | if not upstream_lost and not upstream_gain: |
| 649 | # Trivially no changes caused by the upstream. |
| 650 | # |
| 651 | return True |
| 652 | |
| 653 | if self.IsDirty(consider_untracked=False): |
| 654 | _warn('[%s] commit (or discard) uncommitted changes' |
| 655 | ' before sync', self.name) |
| 656 | return False |
| 657 | |
| 658 | if upstream_lost: |
| 659 | # Upstream rebased. Not everything in HEAD |
| 660 | # may have been caused by the user. |
| 661 | # |
| 662 | _info("[%s] Discarding %d commits removed from upstream", |
| 663 | self.name, len(upstream_lost)) |
| 664 | |
| 665 | branch.remote = rem |
| 666 | branch.merge = self.revision |
| 667 | branch.Save() |
| 668 | |
| 669 | my_changes = self._revlist(not_rev(old_merge), HEAD) |
| 670 | if my_changes: |
| 671 | try: |
| 672 | self._Rebase(upstream = old_merge, onto = rev) |
| 673 | except GitError: |
| 674 | return False |
| 675 | elif upstream_lost: |
| 676 | try: |
| 677 | self._ResetHard(rev) |
| 678 | except GitError: |
| 679 | return False |
| 680 | else: |
| 681 | try: |
| 682 | self._FastForward(rev) |
| 683 | except GitError: |
| 684 | return False |
| 685 | |
| 686 | self._CopyFiles() |
| 687 | return True |
| 688 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 689 | def AddCopyFile(self, src, dest): |
| 690 | # dest should already be an absolute path, but src is project relative |
| 691 | # make src an absolute path |
| 692 | src = os.path.join(self.worktree, src) |
| 693 | self.copyfiles.append(_CopyFile(src, dest)) |
| 694 | |
Shawn O. Pearce | 632768b | 2008-10-23 11:58:52 -0700 | [diff] [blame] | 695 | def DownloadPatchSet(self, change_id, patch_id): |
| 696 | """Download a single patch set of a single change to FETCH_HEAD. |
| 697 | """ |
| 698 | remote = self.GetRemote(self.remote.name) |
| 699 | |
| 700 | cmd = ['fetch', remote.name] |
| 701 | cmd.append('refs/changes/%2.2d/%d/%d' \ |
| 702 | % (change_id % 100, change_id, patch_id)) |
| 703 | cmd.extend(map(lambda x: str(x), remote.fetch)) |
| 704 | if GitCommand(self, cmd, bare=True).Wait() != 0: |
| 705 | return None |
| 706 | return DownloadedChange(self, |
| 707 | remote.ToLocal(self.revision), |
| 708 | change_id, |
| 709 | patch_id, |
| 710 | self.bare_git.rev_parse('FETCH_HEAD')) |
| 711 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 712 | |
| 713 | ## Branch Management ## |
| 714 | |
| 715 | def StartBranch(self, name): |
| 716 | """Create a new branch off the manifest's revision. |
| 717 | """ |
| 718 | branch = self.GetBranch(name) |
| 719 | branch.remote = self.GetRemote(self.remote.name) |
| 720 | branch.merge = self.revision |
| 721 | |
| 722 | rev = branch.LocalMerge |
| 723 | cmd = ['checkout', '-b', branch.name, rev] |
| 724 | if GitCommand(self, cmd).Wait() == 0: |
| 725 | branch.Save() |
| 726 | else: |
| 727 | raise GitError('%s checkout %s ' % (self.name, rev)) |
| 728 | |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 729 | def AbandonBranch(self, name): |
| 730 | """Destroy a local topic branch. |
| 731 | """ |
| 732 | try: |
| 733 | tip_rev = self.bare_git.rev_parse(R_HEADS + name) |
| 734 | except GitError: |
| 735 | return |
| 736 | |
| 737 | if self.CurrentBranch == name: |
| 738 | self._Checkout( |
| 739 | self.GetRemote(self.remote.name).ToLocal(self.revision), |
| 740 | quiet=True) |
| 741 | |
| 742 | cmd = ['branch', '-D', name] |
| 743 | GitCommand(self, cmd, capture_stdout=True).Wait() |
| 744 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 745 | def PruneHeads(self): |
| 746 | """Prune any topic branches already merged into upstream. |
| 747 | """ |
| 748 | cb = self.CurrentBranch |
| 749 | kill = [] |
| 750 | for name in self._allrefs.keys(): |
| 751 | if name.startswith(R_HEADS): |
| 752 | name = name[len(R_HEADS):] |
| 753 | if cb is None or name != cb: |
| 754 | kill.append(name) |
| 755 | |
| 756 | rev = self.GetRemote(self.remote.name).ToLocal(self.revision) |
| 757 | if cb is not None \ |
| 758 | and not self._revlist(HEAD + '...' + rev) \ |
| 759 | and not self.IsDirty(consider_untracked = False): |
| 760 | self.work_git.DetachHead(HEAD) |
| 761 | kill.append(cb) |
| 762 | |
| 763 | deleted = set() |
| 764 | if kill: |
| 765 | try: |
| 766 | old = self.bare_git.GetHead() |
| 767 | except GitError: |
| 768 | old = 'refs/heads/please_never_use_this_as_a_branch_name' |
| 769 | |
| 770 | rm_re = re.compile(r"^Deleted branch (.*)\.$") |
| 771 | try: |
| 772 | self.bare_git.DetachHead(rev) |
| 773 | |
| 774 | b = ['branch', '-d'] |
| 775 | b.extend(kill) |
| 776 | b = GitCommand(self, b, bare=True, |
| 777 | capture_stdout=True, |
| 778 | capture_stderr=True) |
| 779 | b.Wait() |
| 780 | finally: |
| 781 | self.bare_git.SetHead(old) |
| 782 | |
| 783 | for line in b.stdout.split("\n"): |
| 784 | m = rm_re.match(line) |
| 785 | if m: |
| 786 | deleted.add(m.group(1)) |
| 787 | |
| 788 | if deleted: |
| 789 | self.CleanPublishedCache() |
| 790 | |
| 791 | if cb and cb not in kill: |
| 792 | kill.append(cb) |
| 793 | kill.sort() |
| 794 | |
| 795 | kept = [] |
| 796 | for branch in kill: |
| 797 | if branch not in deleted: |
| 798 | branch = self.GetBranch(branch) |
| 799 | base = branch.LocalMerge |
| 800 | if not base: |
| 801 | base = rev |
| 802 | kept.append(ReviewableBranch(self, branch, base)) |
| 803 | return kept |
| 804 | |
| 805 | |
| 806 | ## Direct Git Commands ## |
| 807 | |
| 808 | def _RemoteFetch(self, name=None): |
| 809 | if not name: |
| 810 | name = self.remote.name |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 811 | cmd = ['fetch'] |
| 812 | if not self.worktree: |
| 813 | cmd.append('--update-head-ok') |
| 814 | cmd.append(name) |
| 815 | return GitCommand(self, cmd, bare = True).Wait() == 0 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 816 | |
| 817 | def _Checkout(self, rev, quiet=False): |
| 818 | cmd = ['checkout'] |
| 819 | if quiet: |
| 820 | cmd.append('-q') |
| 821 | cmd.append(rev) |
| 822 | cmd.append('--') |
| 823 | if GitCommand(self, cmd).Wait() != 0: |
| 824 | if self._allrefs: |
| 825 | raise GitError('%s checkout %s ' % (self.name, rev)) |
| 826 | |
| 827 | def _ResetHard(self, rev, quiet=True): |
| 828 | cmd = ['reset', '--hard'] |
| 829 | if quiet: |
| 830 | cmd.append('-q') |
| 831 | cmd.append(rev) |
| 832 | if GitCommand(self, cmd).Wait() != 0: |
| 833 | raise GitError('%s reset --hard %s ' % (self.name, rev)) |
| 834 | |
| 835 | def _Rebase(self, upstream, onto = None): |
| 836 | cmd = ['rebase', '-i'] |
| 837 | if onto is not None: |
| 838 | cmd.extend(['--onto', onto]) |
| 839 | cmd.append(upstream) |
| 840 | if GitCommand(self, cmd, disable_editor=True).Wait() != 0: |
| 841 | raise GitError('%s rebase %s ' % (self.name, upstream)) |
| 842 | |
| 843 | def _FastForward(self, head): |
| 844 | cmd = ['merge', head] |
| 845 | if GitCommand(self, cmd).Wait() != 0: |
| 846 | raise GitError('%s merge %s ' % (self.name, head)) |
| 847 | |
| 848 | def _InitGitDir(self): |
| 849 | if not os.path.exists(self.gitdir): |
| 850 | os.makedirs(self.gitdir) |
| 851 | self.bare_git.init() |
| 852 | self.config.SetString('core.bare', None) |
| 853 | |
| 854 | hooks = self._gitdir_path('hooks') |
Shawn O. Pearce | de64681 | 2008-10-29 14:38:12 -0700 | [diff] [blame] | 855 | try: |
| 856 | to_rm = os.listdir(hooks) |
| 857 | except OSError: |
| 858 | to_rm = [] |
| 859 | for old_hook in to_rm: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 860 | os.remove(os.path.join(hooks, old_hook)) |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 861 | self._InitHooks() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 862 | |
| 863 | m = self.manifest.manifestProject.config |
| 864 | for key in ['user.name', 'user.email']: |
| 865 | if m.Has(key, include_defaults = False): |
| 866 | self.config.SetString(key, m.GetString(key)) |
| 867 | |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 868 | def _InitHooks(self): |
| 869 | hooks = self._gitdir_path('hooks') |
| 870 | if not os.path.exists(hooks): |
| 871 | os.makedirs(hooks) |
| 872 | for stock_hook in repo_hooks(): |
| 873 | dst = os.path.join(hooks, os.path.basename(stock_hook)) |
| 874 | try: |
| 875 | os.symlink(relpath(stock_hook, dst), dst) |
| 876 | except OSError, e: |
| 877 | if e.errno == errno.EEXIST: |
| 878 | pass |
| 879 | elif e.errno == errno.EPERM: |
| 880 | raise GitError('filesystem must support symlinks') |
| 881 | else: |
| 882 | raise |
| 883 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 884 | def _InitRemote(self): |
| 885 | if self.remote.fetchUrl: |
| 886 | remote = self.GetRemote(self.remote.name) |
| 887 | |
| 888 | url = self.remote.fetchUrl |
| 889 | while url.endswith('/'): |
| 890 | url = url[:-1] |
| 891 | url += '/%s.git' % self.name |
| 892 | remote.url = url |
| 893 | remote.review = self.remote.reviewUrl |
Shawn O. Pearce | 339ba9f | 2008-11-06 09:52:51 -0800 | [diff] [blame] | 894 | if remote.projectname is None: |
| 895 | remote.projectname = self.name |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 896 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 897 | if self.worktree: |
| 898 | remote.ResetFetch(mirror=False) |
| 899 | else: |
| 900 | remote.ResetFetch(mirror=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 901 | remote.Save() |
| 902 | |
| 903 | for r in self.extraRemotes.values(): |
| 904 | remote = self.GetRemote(r.name) |
| 905 | remote.url = r.fetchUrl |
| 906 | remote.review = r.reviewUrl |
Shawn O. Pearce | ae6e094 | 2008-11-06 10:25:35 -0800 | [diff] [blame^] | 907 | if r.projectName: |
| 908 | remote.projectname = r.projectName |
| 909 | elif remote.projectname is None: |
Shawn O. Pearce | 339ba9f | 2008-11-06 09:52:51 -0800 | [diff] [blame] | 910 | remote.projectname = self.name |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 911 | remote.ResetFetch() |
| 912 | remote.Save() |
| 913 | |
| 914 | def _InitMRef(self): |
| 915 | if self.manifest.branch: |
| 916 | msg = 'manifest set to %s' % self.revision |
| 917 | ref = R_M + self.manifest.branch |
| 918 | |
| 919 | if IsId(self.revision): |
| 920 | dst = self.revision + '^0', |
| 921 | self.bare_git.UpdateRef(ref, dst, message = msg, detach = True) |
| 922 | else: |
| 923 | remote = self.GetRemote(self.remote.name) |
| 924 | dst = remote.ToLocal(self.revision) |
| 925 | self.bare_git.symbolic_ref('-m', msg, ref, dst) |
| 926 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 927 | def _InitMirrorHead(self): |
| 928 | dst = self.GetRemote(self.remote.name).ToLocal(self.revision) |
| 929 | msg = 'manifest set to %s' % self.revision |
| 930 | self.bare_git.SetHead(dst, message=msg) |
| 931 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 932 | def _InitWorkTree(self): |
| 933 | dotgit = os.path.join(self.worktree, '.git') |
| 934 | if not os.path.exists(dotgit): |
| 935 | os.makedirs(dotgit) |
| 936 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 937 | for name in ['config', |
| 938 | 'description', |
| 939 | 'hooks', |
| 940 | 'info', |
| 941 | 'logs', |
| 942 | 'objects', |
| 943 | 'packed-refs', |
| 944 | 'refs', |
| 945 | 'rr-cache', |
| 946 | 'svn']: |
Shawn O. Pearce | 438ee1c | 2008-11-03 09:59:36 -0800 | [diff] [blame] | 947 | try: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 948 | src = os.path.join(self.gitdir, name) |
| 949 | dst = os.path.join(dotgit, name) |
| 950 | os.symlink(relpath(src, dst), dst) |
Shawn O. Pearce | 438ee1c | 2008-11-03 09:59:36 -0800 | [diff] [blame] | 951 | except OSError, e: |
| 952 | if e.errno == errno.EPERM: |
| 953 | raise GitError('filesystem must support symlinks') |
| 954 | else: |
| 955 | raise |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 956 | |
| 957 | rev = self.GetRemote(self.remote.name).ToLocal(self.revision) |
| 958 | rev = self.bare_git.rev_parse('%s^0' % rev) |
| 959 | |
| 960 | f = open(os.path.join(dotgit, HEAD), 'wb') |
| 961 | f.write("%s\n" % rev) |
| 962 | f.close() |
| 963 | |
| 964 | cmd = ['read-tree', '--reset', '-u'] |
| 965 | cmd.append('-v') |
| 966 | cmd.append('HEAD') |
| 967 | if GitCommand(self, cmd).Wait() != 0: |
| 968 | raise GitError("cannot initialize work tree") |
| 969 | |
| 970 | def _gitdir_path(self, path): |
| 971 | return os.path.join(self.gitdir, path) |
| 972 | |
| 973 | def _revlist(self, *args): |
| 974 | cmd = [] |
| 975 | cmd.extend(args) |
| 976 | cmd.append('--') |
| 977 | return self.work_git.rev_list(*args) |
| 978 | |
| 979 | @property |
| 980 | def _allrefs(self): |
| 981 | return self.bare_git.ListRefs() |
| 982 | |
| 983 | class _GitGetByExec(object): |
| 984 | def __init__(self, project, bare): |
| 985 | self._project = project |
| 986 | self._bare = bare |
| 987 | |
| 988 | def ListRefs(self, *args): |
| 989 | cmdv = ['for-each-ref', '--format=%(objectname) %(refname)'] |
| 990 | cmdv.extend(args) |
| 991 | p = GitCommand(self._project, |
| 992 | cmdv, |
| 993 | bare = self._bare, |
| 994 | capture_stdout = True, |
| 995 | capture_stderr = True) |
| 996 | r = {} |
| 997 | for line in p.process.stdout: |
| 998 | id, name = line[:-1].split(' ', 2) |
| 999 | r[name] = id |
| 1000 | if p.Wait() != 0: |
| 1001 | raise GitError('%s for-each-ref %s: %s' % ( |
| 1002 | self._project.name, |
| 1003 | str(args), |
| 1004 | p.stderr)) |
| 1005 | return r |
| 1006 | |
| 1007 | def LsOthers(self): |
| 1008 | p = GitCommand(self._project, |
| 1009 | ['ls-files', |
| 1010 | '-z', |
| 1011 | '--others', |
| 1012 | '--exclude-standard'], |
| 1013 | bare = False, |
| 1014 | capture_stdout = True, |
| 1015 | capture_stderr = True) |
| 1016 | if p.Wait() == 0: |
| 1017 | out = p.stdout |
| 1018 | if out: |
| 1019 | return out[:-1].split("\0") |
| 1020 | return [] |
| 1021 | |
| 1022 | def DiffZ(self, name, *args): |
| 1023 | cmd = [name] |
| 1024 | cmd.append('-z') |
| 1025 | cmd.extend(args) |
| 1026 | p = GitCommand(self._project, |
| 1027 | cmd, |
| 1028 | bare = False, |
| 1029 | capture_stdout = True, |
| 1030 | capture_stderr = True) |
| 1031 | try: |
| 1032 | out = p.process.stdout.read() |
| 1033 | r = {} |
| 1034 | if out: |
| 1035 | out = iter(out[:-1].split('\0')) |
| 1036 | while out: |
Shawn O. Pearce | 02dbb6d | 2008-10-21 13:59:08 -0700 | [diff] [blame] | 1037 | try: |
| 1038 | info = out.next() |
| 1039 | path = out.next() |
| 1040 | except StopIteration: |
| 1041 | break |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1042 | |
| 1043 | class _Info(object): |
| 1044 | def __init__(self, path, omode, nmode, oid, nid, state): |
| 1045 | self.path = path |
| 1046 | self.src_path = None |
| 1047 | self.old_mode = omode |
| 1048 | self.new_mode = nmode |
| 1049 | self.old_id = oid |
| 1050 | self.new_id = nid |
| 1051 | |
| 1052 | if len(state) == 1: |
| 1053 | self.status = state |
| 1054 | self.level = None |
| 1055 | else: |
| 1056 | self.status = state[:1] |
| 1057 | self.level = state[1:] |
| 1058 | while self.level.startswith('0'): |
| 1059 | self.level = self.level[1:] |
| 1060 | |
| 1061 | info = info[1:].split(' ') |
| 1062 | info =_Info(path, *info) |
| 1063 | if info.status in ('R', 'C'): |
| 1064 | info.src_path = info.path |
| 1065 | info.path = out.next() |
| 1066 | r[info.path] = info |
| 1067 | return r |
| 1068 | finally: |
| 1069 | p.Wait() |
| 1070 | |
| 1071 | def GetHead(self): |
| 1072 | return self.symbolic_ref(HEAD) |
| 1073 | |
| 1074 | def SetHead(self, ref, message=None): |
| 1075 | cmdv = [] |
| 1076 | if message is not None: |
| 1077 | cmdv.extend(['-m', message]) |
| 1078 | cmdv.append(HEAD) |
| 1079 | cmdv.append(ref) |
| 1080 | self.symbolic_ref(*cmdv) |
| 1081 | |
| 1082 | def DetachHead(self, new, message=None): |
| 1083 | cmdv = ['--no-deref'] |
| 1084 | if message is not None: |
| 1085 | cmdv.extend(['-m', message]) |
| 1086 | cmdv.append(HEAD) |
| 1087 | cmdv.append(new) |
| 1088 | self.update_ref(*cmdv) |
| 1089 | |
| 1090 | def UpdateRef(self, name, new, old=None, |
| 1091 | message=None, |
| 1092 | detach=False): |
| 1093 | cmdv = [] |
| 1094 | if message is not None: |
| 1095 | cmdv.extend(['-m', message]) |
| 1096 | if detach: |
| 1097 | cmdv.append('--no-deref') |
| 1098 | cmdv.append(name) |
| 1099 | cmdv.append(new) |
| 1100 | if old is not None: |
| 1101 | cmdv.append(old) |
| 1102 | self.update_ref(*cmdv) |
| 1103 | |
| 1104 | def DeleteRef(self, name, old=None): |
| 1105 | if not old: |
| 1106 | old = self.rev_parse(name) |
| 1107 | self.update_ref('-d', name, old) |
| 1108 | |
| 1109 | def rev_list(self, *args): |
| 1110 | cmdv = ['rev-list'] |
| 1111 | cmdv.extend(args) |
| 1112 | p = GitCommand(self._project, |
| 1113 | cmdv, |
| 1114 | bare = self._bare, |
| 1115 | capture_stdout = True, |
| 1116 | capture_stderr = True) |
| 1117 | r = [] |
| 1118 | for line in p.process.stdout: |
| 1119 | r.append(line[:-1]) |
| 1120 | if p.Wait() != 0: |
| 1121 | raise GitError('%s rev-list %s: %s' % ( |
| 1122 | self._project.name, |
| 1123 | str(args), |
| 1124 | p.stderr)) |
| 1125 | return r |
| 1126 | |
| 1127 | def __getattr__(self, name): |
| 1128 | name = name.replace('_', '-') |
| 1129 | def runner(*args): |
| 1130 | cmdv = [name] |
| 1131 | cmdv.extend(args) |
| 1132 | p = GitCommand(self._project, |
| 1133 | cmdv, |
| 1134 | bare = self._bare, |
| 1135 | capture_stdout = True, |
| 1136 | capture_stderr = True) |
| 1137 | if p.Wait() != 0: |
| 1138 | raise GitError('%s %s: %s' % ( |
| 1139 | self._project.name, |
| 1140 | name, |
| 1141 | p.stderr)) |
| 1142 | r = p.stdout |
| 1143 | if r.endswith('\n') and r.index('\n') == len(r) - 1: |
| 1144 | return r[:-1] |
| 1145 | return r |
| 1146 | return runner |
| 1147 | |
| 1148 | |
| 1149 | class MetaProject(Project): |
| 1150 | """A special project housed under .repo. |
| 1151 | """ |
| 1152 | def __init__(self, manifest, name, gitdir, worktree): |
| 1153 | repodir = manifest.repodir |
| 1154 | Project.__init__(self, |
| 1155 | manifest = manifest, |
| 1156 | name = name, |
| 1157 | gitdir = gitdir, |
| 1158 | worktree = worktree, |
| 1159 | remote = Remote('origin'), |
| 1160 | relpath = '.repo/%s' % name, |
| 1161 | revision = 'refs/heads/master') |
| 1162 | |
| 1163 | def PreSync(self): |
| 1164 | if self.Exists: |
| 1165 | cb = self.CurrentBranch |
| 1166 | if cb: |
| 1167 | base = self.GetBranch(cb).merge |
| 1168 | if base: |
| 1169 | self.revision = base |
| 1170 | |
| 1171 | @property |
| 1172 | def HasChanges(self): |
| 1173 | """Has the remote received new commits not yet checked out? |
| 1174 | """ |
| 1175 | rev = self.GetRemote(self.remote.name).ToLocal(self.revision) |
| 1176 | if self._revlist(not_rev(HEAD), rev): |
| 1177 | return True |
| 1178 | return False |