blob: 09a768fb4c62d37cbb9847032afc4b7b882d43f6 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001# 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. Pearce438ee1c2008-11-03 09:59:36 -080015import errno
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070016import filecmp
17import os
18import re
19import shutil
20import stat
21import sys
22import urllib2
23
24from color import Coloring
25from git_command import GitCommand
26from git_config import GitConfig, IsId
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027from error import GitError, ImportError, UploadError
Shawn O. Pearce559b8462009-03-02 12:56:08 -080028from error import ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029from remote import Remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070030
Shawn O. Pearced237b692009-04-17 18:49:50 -070031from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
Shawn O. Pearce48244782009-04-16 08:25:57 -070033def _error(fmt, *args):
34 msg = fmt % args
35 print >>sys.stderr, 'error: %s' % msg
36
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070037def not_rev(r):
38 return '^' + r
39
Shawn O. Pearceb54a3922009-01-05 16:18:58 -080040def sq(r):
41 return "'" + r.replace("'", "'\''") + "'"
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080042
43hook_list = None
44def repo_hooks():
45 global hook_list
46 if hook_list is None:
47 d = os.path.abspath(os.path.dirname(__file__))
48 d = os.path.join(d , 'hooks')
49 hook_list = map(lambda x: os.path.join(d, x), os.listdir(d))
50 return hook_list
51
52def relpath(dst, src):
53 src = os.path.dirname(src)
54 top = os.path.commonprefix([dst, src])
55 if top.endswith('/'):
56 top = top[:-1]
57 else:
58 top = os.path.dirname(top)
59
60 tmp = src
61 rel = ''
62 while top != tmp:
63 rel += '../'
64 tmp = os.path.dirname(tmp)
65 return rel + dst[len(top) + 1:]
66
67
Shawn O. Pearce632768b2008-10-23 11:58:52 -070068class DownloadedChange(object):
69 _commit_cache = None
70
71 def __init__(self, project, base, change_id, ps_id, commit):
72 self.project = project
73 self.base = base
74 self.change_id = change_id
75 self.ps_id = ps_id
76 self.commit = commit
77
78 @property
79 def commits(self):
80 if self._commit_cache is None:
81 self._commit_cache = self.project.bare_git.rev_list(
82 '--abbrev=8',
83 '--abbrev-commit',
84 '--pretty=oneline',
85 '--reverse',
86 '--date-order',
87 not_rev(self.base),
88 self.commit,
89 '--')
90 return self._commit_cache
91
92
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070093class ReviewableBranch(object):
94 _commit_cache = None
95
96 def __init__(self, project, branch, base):
97 self.project = project
98 self.branch = branch
99 self.base = base
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800100 self.replace_changes = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700101
102 @property
103 def name(self):
104 return self.branch.name
105
106 @property
107 def commits(self):
108 if self._commit_cache is None:
109 self._commit_cache = self.project.bare_git.rev_list(
110 '--abbrev=8',
111 '--abbrev-commit',
112 '--pretty=oneline',
113 '--reverse',
114 '--date-order',
115 not_rev(self.base),
116 R_HEADS + self.name,
117 '--')
118 return self._commit_cache
119
120 @property
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800121 def unabbrev_commits(self):
122 r = dict()
123 for commit in self.project.bare_git.rev_list(
124 not_rev(self.base),
125 R_HEADS + self.name,
126 '--'):
127 r[commit[0:8]] = commit
128 return r
129
130 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700131 def date(self):
132 return self.project.bare_git.log(
133 '--pretty=format:%cd',
134 '-n', '1',
135 R_HEADS + self.name,
136 '--')
137
Joe Onorato2896a792008-11-17 16:56:36 -0500138 def UploadForReview(self, people):
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800139 self.project.UploadForReview(self.name,
Joe Onorato2896a792008-11-17 16:56:36 -0500140 self.replace_changes,
141 people)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700142
143 @property
144 def tip_url(self):
145 me = self.project.GetBranch(self.name)
146 commit = self.project.bare_git.rev_parse(R_HEADS + self.name)
147 return 'http://%s/r/%s' % (me.remote.review, commit[0:12])
148
Shawn O. Pearce0758d2f2008-10-22 13:13:40 -0700149 @property
150 def owner_email(self):
151 return self.project.UserEmail
152
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700153
154class StatusColoring(Coloring):
155 def __init__(self, config):
156 Coloring.__init__(self, config, 'status')
157 self.project = self.printer('header', attr = 'bold')
158 self.branch = self.printer('header', attr = 'bold')
159 self.nobranch = self.printer('nobranch', fg = 'red')
160
161 self.added = self.printer('added', fg = 'green')
162 self.changed = self.printer('changed', fg = 'red')
163 self.untracked = self.printer('untracked', fg = 'red')
164
165
166class DiffColoring(Coloring):
167 def __init__(self, config):
168 Coloring.__init__(self, config, 'diff')
169 self.project = self.printer('header', attr = 'bold')
170
171
172class _CopyFile:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800173 def __init__(self, src, dest, abssrc, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700174 self.src = src
175 self.dest = dest
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800176 self.abs_src = abssrc
177 self.abs_dest = absdest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700178
179 def _Copy(self):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800180 src = self.abs_src
181 dest = self.abs_dest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700182 # copy file if it does not exist or is out of date
183 if not os.path.exists(dest) or not filecmp.cmp(src, dest):
184 try:
185 # remove existing file first, since it might be read-only
186 if os.path.exists(dest):
187 os.remove(dest)
188 shutil.copy(src, dest)
189 # make the file read-only
190 mode = os.stat(dest)[stat.ST_MODE]
191 mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
192 os.chmod(dest, mode)
193 except IOError:
Shawn O. Pearce48244782009-04-16 08:25:57 -0700194 _error('Cannot copy file %s to %s', src, dest)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700195
196
197class Project(object):
198 def __init__(self,
199 manifest,
200 name,
201 remote,
202 gitdir,
203 worktree,
204 relpath,
205 revision):
206 self.manifest = manifest
207 self.name = name
208 self.remote = remote
209 self.gitdir = gitdir
210 self.worktree = worktree
211 self.relpath = relpath
212 self.revision = revision
213 self.snapshots = {}
214 self.extraRemotes = {}
215 self.copyfiles = []
216 self.config = GitConfig.ForRepository(
217 gitdir = self.gitdir,
218 defaults = self.manifest.globalConfig)
219
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800220 if self.worktree:
221 self.work_git = self._GitGetByExec(self, bare=False)
222 else:
223 self.work_git = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700224 self.bare_git = self._GitGetByExec(self, bare=True)
Shawn O. Pearced237b692009-04-17 18:49:50 -0700225 self.bare_ref = GitRefs(gitdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700226
227 @property
228 def Exists(self):
229 return os.path.isdir(self.gitdir)
230
231 @property
232 def CurrentBranch(self):
233 """Obtain the name of the currently checked out branch.
234 The branch name omits the 'refs/heads/' prefix.
235 None is returned if the project is on a detached HEAD.
236 """
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700237 b = self.work_git.GetHead()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700238 if b.startswith(R_HEADS):
239 return b[len(R_HEADS):]
240 return None
241
242 def IsDirty(self, consider_untracked=True):
243 """Is the working directory modified in some way?
244 """
245 self.work_git.update_index('-q',
246 '--unmerged',
247 '--ignore-missing',
248 '--refresh')
249 if self.work_git.DiffZ('diff-index','-M','--cached',HEAD):
250 return True
251 if self.work_git.DiffZ('diff-files'):
252 return True
253 if consider_untracked and self.work_git.LsOthers():
254 return True
255 return False
256
257 _userident_name = None
258 _userident_email = None
259
260 @property
261 def UserName(self):
262 """Obtain the user's personal name.
263 """
264 if self._userident_name is None:
265 self._LoadUserIdentity()
266 return self._userident_name
267
268 @property
269 def UserEmail(self):
270 """Obtain the user's email address. This is very likely
271 to be their Gerrit login.
272 """
273 if self._userident_email is None:
274 self._LoadUserIdentity()
275 return self._userident_email
276
277 def _LoadUserIdentity(self):
278 u = self.bare_git.var('GIT_COMMITTER_IDENT')
279 m = re.compile("^(.*) <([^>]*)> ").match(u)
280 if m:
281 self._userident_name = m.group(1)
282 self._userident_email = m.group(2)
283 else:
284 self._userident_name = ''
285 self._userident_email = ''
286
287 def GetRemote(self, name):
288 """Get the configuration for a single remote.
289 """
290 return self.config.GetRemote(name)
291
292 def GetBranch(self, name):
293 """Get the configuration for a single branch.
294 """
295 return self.config.GetBranch(name)
296
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700297 def GetBranches(self):
298 """Get all existing local branches.
299 """
300 current = self.CurrentBranch
Shawn O. Pearced237b692009-04-17 18:49:50 -0700301 all = self._allrefs
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700302 heads = {}
303 pubd = {}
304
305 for name, id in all.iteritems():
306 if name.startswith(R_HEADS):
307 name = name[len(R_HEADS):]
308 b = self.GetBranch(name)
309 b.current = name == current
310 b.published = None
311 b.revision = id
312 heads[name] = b
313
314 for name, id in all.iteritems():
315 if name.startswith(R_PUB):
316 name = name[len(R_PUB):]
317 b = heads.get(name)
318 if b:
319 b.published = id
320
321 return heads
322
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700323
324## Status Display ##
325
326 def PrintWorkTreeStatus(self):
327 """Prints the status of the repository to stdout.
328 """
329 if not os.path.isdir(self.worktree):
330 print ''
331 print 'project %s/' % self.relpath
332 print ' missing (run "repo sync")'
333 return
334
335 self.work_git.update_index('-q',
336 '--unmerged',
337 '--ignore-missing',
338 '--refresh')
339 di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD)
340 df = self.work_git.DiffZ('diff-files')
341 do = self.work_git.LsOthers()
342 if not di and not df and not do:
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700343 return 'CLEAN'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700344
345 out = StatusColoring(self.config)
346 out.project('project %-40s', self.relpath + '/')
347
348 branch = self.CurrentBranch
349 if branch is None:
350 out.nobranch('(*** NO BRANCH ***)')
351 else:
352 out.branch('branch %s', branch)
353 out.nl()
354
355 paths = list()
356 paths.extend(di.keys())
357 paths.extend(df.keys())
358 paths.extend(do)
359
360 paths = list(set(paths))
361 paths.sort()
362
363 for p in paths:
364 try: i = di[p]
365 except KeyError: i = None
366
367 try: f = df[p]
368 except KeyError: f = None
369
370 if i: i_status = i.status.upper()
371 else: i_status = '-'
372
373 if f: f_status = f.status.lower()
374 else: f_status = '-'
375
376 if i and i.src_path:
Shawn O. Pearcefe086752009-03-03 13:49:48 -0800377 line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700378 i.src_path, p, i.level)
379 else:
380 line = ' %s%s\t%s' % (i_status, f_status, p)
381
382 if i and not f:
383 out.added('%s', line)
384 elif (i and f) or (not i and f):
385 out.changed('%s', line)
386 elif not i and not f:
387 out.untracked('%s', line)
388 else:
389 out.write('%s', line)
390 out.nl()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700391 return 'DIRTY'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700392
393 def PrintWorkTreeDiff(self):
394 """Prints the status of the repository to stdout.
395 """
396 out = DiffColoring(self.config)
397 cmd = ['diff']
398 if out.is_on:
399 cmd.append('--color')
400 cmd.append(HEAD)
401 cmd.append('--')
402 p = GitCommand(self,
403 cmd,
404 capture_stdout = True,
405 capture_stderr = True)
406 has_diff = False
407 for line in p.process.stdout:
408 if not has_diff:
409 out.nl()
410 out.project('project %s/' % self.relpath)
411 out.nl()
412 has_diff = True
413 print line[:-1]
414 p.Wait()
415
416
417## Publish / Upload ##
418
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700419 def WasPublished(self, branch, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700420 """Was the branch published (uploaded) for code review?
421 If so, returns the SHA-1 hash of the last published
422 state for the branch.
423 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700424 key = R_PUB + branch
425 if all is None:
426 try:
427 return self.bare_git.rev_parse(key)
428 except GitError:
429 return None
430 else:
431 try:
432 return all[key]
433 except KeyError:
434 return None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700435
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700436 def CleanPublishedCache(self, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700437 """Prunes any stale published refs.
438 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700439 if all is None:
440 all = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700441 heads = set()
442 canrm = {}
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700443 for name, id in all.iteritems():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700444 if name.startswith(R_HEADS):
445 heads.add(name)
446 elif name.startswith(R_PUB):
447 canrm[name] = id
448
449 for name, id in canrm.iteritems():
450 n = name[len(R_PUB):]
451 if R_HEADS + n not in heads:
452 self.bare_git.DeleteRef(name, id)
453
454 def GetUploadableBranches(self):
455 """List any branches which can be uploaded for review.
456 """
457 heads = {}
458 pubed = {}
459
460 for name, id in self._allrefs.iteritems():
461 if name.startswith(R_HEADS):
462 heads[name[len(R_HEADS):]] = id
463 elif name.startswith(R_PUB):
464 pubed[name[len(R_PUB):]] = id
465
466 ready = []
467 for branch, id in heads.iteritems():
468 if branch in pubed and pubed[branch] == id:
469 continue
470
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800471 rb = self.GetUploadableBranch(branch)
472 if rb:
473 ready.append(rb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700474 return ready
475
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800476 def GetUploadableBranch(self, branch_name):
477 """Get a single uploadable branch, or None.
478 """
479 branch = self.GetBranch(branch_name)
480 base = branch.LocalMerge
481 if branch.LocalMerge:
482 rb = ReviewableBranch(self, branch, base)
483 if rb.commits:
484 return rb
485 return None
486
Joe Onorato2896a792008-11-17 16:56:36 -0500487 def UploadForReview(self, branch=None, replace_changes=None, people=([],[])):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700488 """Uploads the named branch for code review.
489 """
490 if branch is None:
491 branch = self.CurrentBranch
492 if branch is None:
493 raise GitError('not currently on a branch')
494
495 branch = self.GetBranch(branch)
496 if not branch.LocalMerge:
497 raise GitError('branch %s does not track a remote' % branch.name)
498 if not branch.remote.review:
499 raise GitError('remote %s has no review url' % branch.remote.name)
500
501 dest_branch = branch.merge
502 if not dest_branch.startswith(R_HEADS):
503 dest_branch = R_HEADS + dest_branch
504
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800505 if not branch.remote.projectname:
506 branch.remote.projectname = self.name
507 branch.remote.Save()
508
Shawn O. Pearce370e3fa2009-01-26 10:55:39 -0800509 if branch.remote.ReviewProtocol == 'ssh':
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800510 if dest_branch.startswith(R_HEADS):
511 dest_branch = dest_branch[len(R_HEADS):]
512
513 rp = ['gerrit receive-pack']
514 for e in people[0]:
515 rp.append('--reviewer=%s' % sq(e))
516 for e in people[1]:
517 rp.append('--cc=%s' % sq(e))
518
519 cmd = ['push']
520 cmd.append('--receive-pack=%s' % " ".join(rp))
521 cmd.append(branch.remote.SshReviewUrl(self.UserEmail))
522 cmd.append('%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch))
523 if replace_changes:
524 for change_id,commit_id in replace_changes.iteritems():
525 cmd.append('%s:refs/changes/%s/new' % (commit_id, change_id))
526 if GitCommand(self, cmd, bare = True).Wait() != 0:
527 raise UploadError('Upload failed')
528
529 else:
530 raise UploadError('Unsupported protocol %s' \
531 % branch.remote.review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700532
533 msg = "posted to %s for %s" % (branch.remote.review, dest_branch)
534 self.bare_git.UpdateRef(R_PUB + branch.name,
535 R_HEADS + branch.name,
536 message = msg)
537
538
539## Sync ##
540
541 def Sync_NetworkHalf(self):
542 """Perform only the network IO portion of the sync process.
543 Local working directory/branch state is not affected.
544 """
545 if not self.Exists:
546 print >>sys.stderr
547 print >>sys.stderr, 'Initializing project %s ...' % self.name
548 self._InitGitDir()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800549
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700550 self._InitRemote()
551 for r in self.extraRemotes.values():
552 if not self._RemoteFetch(r.name):
553 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700554 if not self._RemoteFetch():
555 return False
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800556
557 if self.worktree:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800558 self._InitMRef()
559 else:
560 self._InitMirrorHead()
561 try:
562 os.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
563 except OSError:
564 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700565 return True
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800566
567 def PostRepoUpgrade(self):
568 self._InitHooks()
569
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700570 def _CopyFiles(self):
571 for file in self.copyfiles:
572 file._Copy()
573
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700574 def Sync_LocalHalf(self, syncbuf):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700575 """Perform only the local IO portion of the sync process.
576 Network access is not required.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700577 """
578 self._InitWorkTree()
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700579 all = self.bare_ref.all
580 self.CleanPublishedCache(all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700581
582 rem = self.GetRemote(self.remote.name)
583 rev = rem.ToLocal(self.revision)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700584 if rev in all:
585 revid = all[rev]
586 elif IsId(rev):
587 revid = rev
588 else:
589 try:
590 revid = self.bare_git.rev_parse('--verify', '%s^0' % rev)
591 except GitError:
592 raise ManifestInvalidRevisionError(
593 'revision %s in %s not found' % (self.revision, self.name))
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800594
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700595 head = self.work_git.GetHead()
596 if head.startswith(R_HEADS):
597 branch = head[len(R_HEADS):]
598 try:
599 head = all[head]
600 except KeyError:
601 head = None
602 else:
603 branch = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700604
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700605 if branch is None or syncbuf.detach_head:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700606 # Currently on a detached HEAD. The user is assumed to
607 # not have any local modifications worth worrying about.
608 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700609 if os.path.exists(os.path.join(self.worktree, '.dotest')) \
610 or os.path.exists(os.path.join(self.worktree, '.git', 'rebase-apply')):
611 syncbuf.fail(self, _PriorSyncFailedError())
612 return
613
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700614 if head == revid:
615 # No changes; don't do anything further.
616 #
617 return
618
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700619 lost = self._revlist(not_rev(rev), HEAD)
620 if lost:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700621 syncbuf.info(self, "discarding %d commits", len(lost))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700622 try:
623 self._Checkout(rev, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700624 except GitError, e:
625 syncbuf.fail(self, e)
626 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700627 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700628 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700629
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700630 if head == revid:
631 # No changes; don't do anything further.
632 #
633 return
634
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700635 branch = self.GetBranch(branch)
636 merge = branch.LocalMerge
637
638 if not merge:
639 # The current branch has no tracking configuration.
640 # Jump off it to a deatched HEAD.
641 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700642 syncbuf.info(self,
643 "leaving %s; does not track upstream",
644 branch.name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700645 try:
646 self._Checkout(rev, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700647 except GitError, e:
648 syncbuf.fail(self, e)
649 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700650 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700651 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700652
653 upstream_gain = self._revlist(not_rev(HEAD), rev)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700654 pub = self.WasPublished(branch.name, all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700655 if pub:
656 not_merged = self._revlist(not_rev(rev), pub)
657 if not_merged:
658 if upstream_gain:
659 # The user has published this branch and some of those
660 # commits are not yet merged upstream. We do not want
661 # to rewrite the published commits so we punt.
662 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700663 syncbuf.info(self,
664 "branch %s is published but is now %d commits behind",
665 branch.name,
666 len(upstream_gain))
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700667 return
Shawn O. Pearce23d77812008-10-30 11:06:57 -0700668 elif upstream_gain:
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700669 # We can fast-forward safely.
670 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700671 def _doff():
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700672 self._FastForward(rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700673 self._CopyFiles()
674 syncbuf.later1(self, _doff)
675 return
Shawn O. Pearce23d77812008-10-30 11:06:57 -0700676 else:
677 # Trivially no changes in the upstream.
678 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700679 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700680
681 if merge == rev:
682 try:
683 old_merge = self.bare_git.rev_parse('%s@{1}' % merge)
684 except GitError:
685 old_merge = merge
Shawn O. Pearce07346002008-10-21 07:09:27 -0700686 if old_merge == '0000000000000000000000000000000000000000' \
687 or old_merge == '':
688 old_merge = merge
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700689 else:
690 # The upstream switched on us. Time to cross our fingers
691 # and pray that the old upstream also wasn't in the habit
692 # of rebasing itself.
693 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700694 syncbuf.info(self, "manifest switched %s...%s", merge, rev)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700695 old_merge = merge
696
697 if rev == old_merge:
698 upstream_lost = []
699 else:
700 upstream_lost = self._revlist(not_rev(rev), old_merge)
701
702 if not upstream_lost and not upstream_gain:
703 # Trivially no changes caused by the upstream.
704 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700705 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700706
707 if self.IsDirty(consider_untracked=False):
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700708 syncbuf.fail(self, _DirtyError())
709 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700710
711 if upstream_lost:
712 # Upstream rebased. Not everything in HEAD
713 # may have been caused by the user.
714 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700715 syncbuf.info(self,
716 "discarding %d commits removed from upstream",
717 len(upstream_lost))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700718
719 branch.remote = rem
720 branch.merge = self.revision
721 branch.Save()
722
723 my_changes = self._revlist(not_rev(old_merge), HEAD)
724 if my_changes:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700725 def _dorebase():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700726 self._Rebase(upstream = old_merge, onto = rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700727 self._CopyFiles()
728 syncbuf.later2(self, _dorebase)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700729 elif upstream_lost:
730 try:
731 self._ResetHard(rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700732 self._CopyFiles()
733 except GitError, e:
734 syncbuf.fail(self, e)
735 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700736 else:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700737 def _doff():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700738 self._FastForward(rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700739 self._CopyFiles()
740 syncbuf.later1(self, _doff)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700741
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800742 def AddCopyFile(self, src, dest, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700743 # dest should already be an absolute path, but src is project relative
744 # make src an absolute path
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800745 abssrc = os.path.join(self.worktree, src)
746 self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700747
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700748 def DownloadPatchSet(self, change_id, patch_id):
749 """Download a single patch set of a single change to FETCH_HEAD.
750 """
751 remote = self.GetRemote(self.remote.name)
752
753 cmd = ['fetch', remote.name]
754 cmd.append('refs/changes/%2.2d/%d/%d' \
755 % (change_id % 100, change_id, patch_id))
756 cmd.extend(map(lambda x: str(x), remote.fetch))
757 if GitCommand(self, cmd, bare=True).Wait() != 0:
758 return None
759 return DownloadedChange(self,
760 remote.ToLocal(self.revision),
761 change_id,
762 patch_id,
763 self.bare_git.rev_parse('FETCH_HEAD'))
764
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700765
766## Branch Management ##
767
768 def StartBranch(self, name):
769 """Create a new branch off the manifest's revision.
770 """
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700771 try:
772 self.bare_git.rev_parse(R_HEADS + name)
773 exists = True
774 except GitError:
775 exists = False;
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700776
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700777 if exists:
778 if name == self.CurrentBranch:
779 return True
780 else:
781 cmd = ['checkout', name, '--']
782 return GitCommand(self, cmd).Wait() == 0
783
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700784 else:
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700785 branch = self.GetBranch(name)
786 branch.remote = self.GetRemote(self.remote.name)
787 branch.merge = self.revision
788
789 rev = branch.LocalMerge
790 cmd = ['checkout', '-b', branch.name, rev]
791 if GitCommand(self, cmd).Wait() == 0:
792 branch.Save()
793 return True
794 else:
795 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700796
Wink Saville02d79452009-04-10 13:01:24 -0700797 def CheckoutBranch(self, name):
798 """Checkout a local topic branch.
799 """
800
801 # Be sure the branch exists
802 try:
803 tip_rev = self.bare_git.rev_parse(R_HEADS + name)
804 except GitError:
805 return False;
806
807 # Do the checkout
808 cmd = ['checkout', name, '--']
Shawn O. Pearce2675c3f2009-04-10 16:20:25 -0700809 return GitCommand(self, cmd).Wait() == 0
Wink Saville02d79452009-04-10 13:01:24 -0700810
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800811 def AbandonBranch(self, name):
812 """Destroy a local topic branch.
813 """
814 try:
815 tip_rev = self.bare_git.rev_parse(R_HEADS + name)
816 except GitError:
817 return
818
819 if self.CurrentBranch == name:
820 self._Checkout(
821 self.GetRemote(self.remote.name).ToLocal(self.revision),
822 quiet=True)
823
824 cmd = ['branch', '-D', name]
825 GitCommand(self, cmd, capture_stdout=True).Wait()
826
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700827 def PruneHeads(self):
828 """Prune any topic branches already merged into upstream.
829 """
830 cb = self.CurrentBranch
831 kill = []
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800832 left = self._allrefs
833 for name in left.keys():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700834 if name.startswith(R_HEADS):
835 name = name[len(R_HEADS):]
836 if cb is None or name != cb:
837 kill.append(name)
838
839 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
840 if cb is not None \
841 and not self._revlist(HEAD + '...' + rev) \
842 and not self.IsDirty(consider_untracked = False):
843 self.work_git.DetachHead(HEAD)
844 kill.append(cb)
845
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700846 if kill:
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700847 old = self.bare_git.GetHead()
848 if old is None:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700849 old = 'refs/heads/please_never_use_this_as_a_branch_name'
850
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700851 try:
852 self.bare_git.DetachHead(rev)
853
854 b = ['branch', '-d']
855 b.extend(kill)
856 b = GitCommand(self, b, bare=True,
857 capture_stdout=True,
858 capture_stderr=True)
859 b.Wait()
860 finally:
861 self.bare_git.SetHead(old)
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800862 left = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700863
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800864 for branch in kill:
865 if (R_HEADS + branch) not in left:
866 self.CleanPublishedCache()
867 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700868
869 if cb and cb not in kill:
870 kill.append(cb)
Shawn O. Pearce7c6c64d2009-03-02 12:38:13 -0800871 kill.sort()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700872
873 kept = []
874 for branch in kill:
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800875 if (R_HEADS + branch) in left:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700876 branch = self.GetBranch(branch)
877 base = branch.LocalMerge
878 if not base:
879 base = rev
880 kept.append(ReviewableBranch(self, branch, base))
881 return kept
882
883
884## Direct Git Commands ##
885
886 def _RemoteFetch(self, name=None):
887 if not name:
888 name = self.remote.name
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800889 cmd = ['fetch']
890 if not self.worktree:
891 cmd.append('--update-head-ok')
892 cmd.append(name)
893 return GitCommand(self, cmd, bare = True).Wait() == 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700894
895 def _Checkout(self, rev, quiet=False):
896 cmd = ['checkout']
897 if quiet:
898 cmd.append('-q')
899 cmd.append(rev)
900 cmd.append('--')
901 if GitCommand(self, cmd).Wait() != 0:
902 if self._allrefs:
903 raise GitError('%s checkout %s ' % (self.name, rev))
904
905 def _ResetHard(self, rev, quiet=True):
906 cmd = ['reset', '--hard']
907 if quiet:
908 cmd.append('-q')
909 cmd.append(rev)
910 if GitCommand(self, cmd).Wait() != 0:
911 raise GitError('%s reset --hard %s ' % (self.name, rev))
912
913 def _Rebase(self, upstream, onto = None):
Shawn O. Pearce19a83d82009-04-16 08:14:26 -0700914 cmd = ['rebase']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700915 if onto is not None:
916 cmd.extend(['--onto', onto])
917 cmd.append(upstream)
Shawn O. Pearce19a83d82009-04-16 08:14:26 -0700918 if GitCommand(self, cmd).Wait() != 0:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700919 raise GitError('%s rebase %s ' % (self.name, upstream))
920
921 def _FastForward(self, head):
922 cmd = ['merge', head]
923 if GitCommand(self, cmd).Wait() != 0:
924 raise GitError('%s merge %s ' % (self.name, head))
925
926 def _InitGitDir(self):
927 if not os.path.exists(self.gitdir):
928 os.makedirs(self.gitdir)
929 self.bare_git.init()
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -0800930
931 if self.manifest.IsMirror:
932 self.config.SetString('core.bare', 'true')
933 else:
934 self.config.SetString('core.bare', None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700935
936 hooks = self._gitdir_path('hooks')
Shawn O. Pearcede646812008-10-29 14:38:12 -0700937 try:
938 to_rm = os.listdir(hooks)
939 except OSError:
940 to_rm = []
941 for old_hook in to_rm:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700942 os.remove(os.path.join(hooks, old_hook))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800943 self._InitHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700944
945 m = self.manifest.manifestProject.config
946 for key in ['user.name', 'user.email']:
947 if m.Has(key, include_defaults = False):
948 self.config.SetString(key, m.GetString(key))
949
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800950 def _InitHooks(self):
951 hooks = self._gitdir_path('hooks')
952 if not os.path.exists(hooks):
953 os.makedirs(hooks)
954 for stock_hook in repo_hooks():
955 dst = os.path.join(hooks, os.path.basename(stock_hook))
956 try:
957 os.symlink(relpath(stock_hook, dst), dst)
958 except OSError, e:
959 if e.errno == errno.EEXIST:
960 pass
961 elif e.errno == errno.EPERM:
962 raise GitError('filesystem must support symlinks')
963 else:
964 raise
965
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700966 def _InitRemote(self):
967 if self.remote.fetchUrl:
968 remote = self.GetRemote(self.remote.name)
969
970 url = self.remote.fetchUrl
971 while url.endswith('/'):
972 url = url[:-1]
973 url += '/%s.git' % self.name
974 remote.url = url
975 remote.review = self.remote.reviewUrl
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800976 if remote.projectname is None:
977 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700978
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800979 if self.worktree:
980 remote.ResetFetch(mirror=False)
981 else:
982 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700983 remote.Save()
984
985 for r in self.extraRemotes.values():
986 remote = self.GetRemote(r.name)
987 remote.url = r.fetchUrl
988 remote.review = r.reviewUrl
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800989 if r.projectName:
990 remote.projectname = r.projectName
991 elif remote.projectname is None:
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800992 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700993 remote.ResetFetch()
994 remote.Save()
995
996 def _InitMRef(self):
997 if self.manifest.branch:
998 msg = 'manifest set to %s' % self.revision
999 ref = R_M + self.manifest.branch
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -07001000 cur = self.bare_ref.symref(ref)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001001
1002 if IsId(self.revision):
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -07001003 if cur != '' or self.bare_ref.get(ref) != self.revision:
1004 dst = self.revision + '^0'
1005 self.bare_git.UpdateRef(ref, dst, message = msg, detach = True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001006 else:
1007 remote = self.GetRemote(self.remote.name)
1008 dst = remote.ToLocal(self.revision)
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -07001009 if cur != dst:
1010 self.bare_git.symbolic_ref('-m', msg, ref, dst)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001011
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001012 def _InitMirrorHead(self):
1013 dst = self.GetRemote(self.remote.name).ToLocal(self.revision)
1014 msg = 'manifest set to %s' % self.revision
1015 self.bare_git.SetHead(dst, message=msg)
1016
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001017 def _InitWorkTree(self):
1018 dotgit = os.path.join(self.worktree, '.git')
1019 if not os.path.exists(dotgit):
1020 os.makedirs(dotgit)
1021
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001022 for name in ['config',
1023 'description',
1024 'hooks',
1025 'info',
1026 'logs',
1027 'objects',
1028 'packed-refs',
1029 'refs',
1030 'rr-cache',
1031 'svn']:
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001032 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001033 src = os.path.join(self.gitdir, name)
1034 dst = os.path.join(dotgit, name)
1035 os.symlink(relpath(src, dst), dst)
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001036 except OSError, e:
1037 if e.errno == errno.EPERM:
1038 raise GitError('filesystem must support symlinks')
1039 else:
1040 raise
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001041
1042 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
1043 rev = self.bare_git.rev_parse('%s^0' % rev)
1044
1045 f = open(os.path.join(dotgit, HEAD), 'wb')
1046 f.write("%s\n" % rev)
1047 f.close()
1048
1049 cmd = ['read-tree', '--reset', '-u']
1050 cmd.append('-v')
1051 cmd.append('HEAD')
1052 if GitCommand(self, cmd).Wait() != 0:
1053 raise GitError("cannot initialize work tree")
1054
1055 def _gitdir_path(self, path):
1056 return os.path.join(self.gitdir, path)
1057
1058 def _revlist(self, *args):
1059 cmd = []
1060 cmd.extend(args)
1061 cmd.append('--')
1062 return self.work_git.rev_list(*args)
1063
1064 @property
1065 def _allrefs(self):
Shawn O. Pearced237b692009-04-17 18:49:50 -07001066 return self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001067
1068 class _GitGetByExec(object):
1069 def __init__(self, project, bare):
1070 self._project = project
1071 self._bare = bare
1072
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001073 def LsOthers(self):
1074 p = GitCommand(self._project,
1075 ['ls-files',
1076 '-z',
1077 '--others',
1078 '--exclude-standard'],
1079 bare = False,
1080 capture_stdout = True,
1081 capture_stderr = True)
1082 if p.Wait() == 0:
1083 out = p.stdout
1084 if out:
1085 return out[:-1].split("\0")
1086 return []
1087
1088 def DiffZ(self, name, *args):
1089 cmd = [name]
1090 cmd.append('-z')
1091 cmd.extend(args)
1092 p = GitCommand(self._project,
1093 cmd,
1094 bare = False,
1095 capture_stdout = True,
1096 capture_stderr = True)
1097 try:
1098 out = p.process.stdout.read()
1099 r = {}
1100 if out:
1101 out = iter(out[:-1].split('\0'))
1102 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07001103 try:
1104 info = out.next()
1105 path = out.next()
1106 except StopIteration:
1107 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001108
1109 class _Info(object):
1110 def __init__(self, path, omode, nmode, oid, nid, state):
1111 self.path = path
1112 self.src_path = None
1113 self.old_mode = omode
1114 self.new_mode = nmode
1115 self.old_id = oid
1116 self.new_id = nid
1117
1118 if len(state) == 1:
1119 self.status = state
1120 self.level = None
1121 else:
1122 self.status = state[:1]
1123 self.level = state[1:]
1124 while self.level.startswith('0'):
1125 self.level = self.level[1:]
1126
1127 info = info[1:].split(' ')
1128 info =_Info(path, *info)
1129 if info.status in ('R', 'C'):
1130 info.src_path = info.path
1131 info.path = out.next()
1132 r[info.path] = info
1133 return r
1134 finally:
1135 p.Wait()
1136
1137 def GetHead(self):
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001138 if self._bare:
1139 path = os.path.join(self._project.gitdir, HEAD)
1140 else:
1141 path = os.path.join(self._project.worktree, '.git', HEAD)
1142 line = open(path, 'r').read()
1143 if line.startswith('ref: '):
1144 return line[5:-1]
1145 return line[:-1]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001146
1147 def SetHead(self, ref, message=None):
1148 cmdv = []
1149 if message is not None:
1150 cmdv.extend(['-m', message])
1151 cmdv.append(HEAD)
1152 cmdv.append(ref)
1153 self.symbolic_ref(*cmdv)
1154
1155 def DetachHead(self, new, message=None):
1156 cmdv = ['--no-deref']
1157 if message is not None:
1158 cmdv.extend(['-m', message])
1159 cmdv.append(HEAD)
1160 cmdv.append(new)
1161 self.update_ref(*cmdv)
1162
1163 def UpdateRef(self, name, new, old=None,
1164 message=None,
1165 detach=False):
1166 cmdv = []
1167 if message is not None:
1168 cmdv.extend(['-m', message])
1169 if detach:
1170 cmdv.append('--no-deref')
1171 cmdv.append(name)
1172 cmdv.append(new)
1173 if old is not None:
1174 cmdv.append(old)
1175 self.update_ref(*cmdv)
1176
1177 def DeleteRef(self, name, old=None):
1178 if not old:
1179 old = self.rev_parse(name)
1180 self.update_ref('-d', name, old)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001181 self._project.bare_ref.deleted(name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001182
1183 def rev_list(self, *args):
1184 cmdv = ['rev-list']
1185 cmdv.extend(args)
1186 p = GitCommand(self._project,
1187 cmdv,
1188 bare = self._bare,
1189 capture_stdout = True,
1190 capture_stderr = True)
1191 r = []
1192 for line in p.process.stdout:
1193 r.append(line[:-1])
1194 if p.Wait() != 0:
1195 raise GitError('%s rev-list %s: %s' % (
1196 self._project.name,
1197 str(args),
1198 p.stderr))
1199 return r
1200
1201 def __getattr__(self, name):
1202 name = name.replace('_', '-')
1203 def runner(*args):
1204 cmdv = [name]
1205 cmdv.extend(args)
1206 p = GitCommand(self._project,
1207 cmdv,
1208 bare = self._bare,
1209 capture_stdout = True,
1210 capture_stderr = True)
1211 if p.Wait() != 0:
1212 raise GitError('%s %s: %s' % (
1213 self._project.name,
1214 name,
1215 p.stderr))
1216 r = p.stdout
1217 if r.endswith('\n') and r.index('\n') == len(r) - 1:
1218 return r[:-1]
1219 return r
1220 return runner
1221
1222
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001223class _PriorSyncFailedError(Exception):
1224 def __str__(self):
1225 return 'prior sync failed; rebase still in progress'
1226
1227class _DirtyError(Exception):
1228 def __str__(self):
1229 return 'contains uncommitted changes'
1230
1231class _InfoMessage(object):
1232 def __init__(self, project, text):
1233 self.project = project
1234 self.text = text
1235
1236 def Print(self, syncbuf):
1237 syncbuf.out.info('%s/: %s', self.project.relpath, self.text)
1238 syncbuf.out.nl()
1239
1240class _Failure(object):
1241 def __init__(self, project, why):
1242 self.project = project
1243 self.why = why
1244
1245 def Print(self, syncbuf):
1246 syncbuf.out.fail('error: %s/: %s',
1247 self.project.relpath,
1248 str(self.why))
1249 syncbuf.out.nl()
1250
1251class _Later(object):
1252 def __init__(self, project, action):
1253 self.project = project
1254 self.action = action
1255
1256 def Run(self, syncbuf):
1257 out = syncbuf.out
1258 out.project('project %s/', self.project.relpath)
1259 out.nl()
1260 try:
1261 self.action()
1262 out.nl()
1263 return True
1264 except GitError, e:
1265 out.nl()
1266 return False
1267
1268class _SyncColoring(Coloring):
1269 def __init__(self, config):
1270 Coloring.__init__(self, config, 'reposync')
1271 self.project = self.printer('header', attr = 'bold')
1272 self.info = self.printer('info')
1273 self.fail = self.printer('fail', fg='red')
1274
1275class SyncBuffer(object):
1276 def __init__(self, config, detach_head=False):
1277 self._messages = []
1278 self._failures = []
1279 self._later_queue1 = []
1280 self._later_queue2 = []
1281
1282 self.out = _SyncColoring(config)
1283 self.out.redirect(sys.stderr)
1284
1285 self.detach_head = detach_head
1286 self.clean = True
1287
1288 def info(self, project, fmt, *args):
1289 self._messages.append(_InfoMessage(project, fmt % args))
1290
1291 def fail(self, project, err=None):
1292 self._failures.append(_Failure(project, err))
1293 self.clean = False
1294
1295 def later1(self, project, what):
1296 self._later_queue1.append(_Later(project, what))
1297
1298 def later2(self, project, what):
1299 self._later_queue2.append(_Later(project, what))
1300
1301 def Finish(self):
1302 self._PrintMessages()
1303 self._RunLater()
1304 self._PrintMessages()
1305 return self.clean
1306
1307 def _RunLater(self):
1308 for q in ['_later_queue1', '_later_queue2']:
1309 if not self._RunQueue(q):
1310 return
1311
1312 def _RunQueue(self, queue):
1313 for m in getattr(self, queue):
1314 if not m.Run(self):
1315 self.clean = False
1316 return False
1317 setattr(self, queue, [])
1318 return True
1319
1320 def _PrintMessages(self):
1321 for m in self._messages:
1322 m.Print(self)
1323 for m in self._failures:
1324 m.Print(self)
1325
1326 self._messages = []
1327 self._failures = []
1328
1329
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001330class MetaProject(Project):
1331 """A special project housed under .repo.
1332 """
1333 def __init__(self, manifest, name, gitdir, worktree):
1334 repodir = manifest.repodir
1335 Project.__init__(self,
1336 manifest = manifest,
1337 name = name,
1338 gitdir = gitdir,
1339 worktree = worktree,
1340 remote = Remote('origin'),
1341 relpath = '.repo/%s' % name,
1342 revision = 'refs/heads/master')
1343
1344 def PreSync(self):
1345 if self.Exists:
1346 cb = self.CurrentBranch
1347 if cb:
1348 base = self.GetBranch(cb).merge
1349 if base:
1350 self.revision = base
1351
1352 @property
1353 def HasChanges(self):
1354 """Has the remote received new commits not yet checked out?
1355 """
1356 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
1357 if self._revlist(not_rev(HEAD), rev):
1358 return True
1359 return False