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