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