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