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