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