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