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