blob: eebe96d5105f5cbd5e95e88f5a8dfc2613ca265b [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
28from remote import Remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029
30HEAD = 'HEAD'
31R_HEADS = 'refs/heads/'
32R_TAGS = 'refs/tags/'
33R_PUB = 'refs/published/'
34R_M = 'refs/remotes/m/'
35
36def _warn(fmt, *args):
37 msg = fmt % args
38 print >>sys.stderr, 'warn: %s' % msg
39
40def _info(fmt, *args):
41 msg = fmt % args
42 print >>sys.stderr, 'info: %s' % msg
43
44def not_rev(r):
45 return '^' + r
46
Shawn O. Pearceb54a3922009-01-05 16:18:58 -080047def sq(r):
48 return "'" + r.replace("'", "'\''") + "'"
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080049
50hook_list = None
51def repo_hooks():
52 global hook_list
53 if hook_list is None:
54 d = os.path.abspath(os.path.dirname(__file__))
55 d = os.path.join(d , 'hooks')
56 hook_list = map(lambda x: os.path.join(d, x), os.listdir(d))
57 return hook_list
58
59def relpath(dst, src):
60 src = os.path.dirname(src)
61 top = os.path.commonprefix([dst, src])
62 if top.endswith('/'):
63 top = top[:-1]
64 else:
65 top = os.path.dirname(top)
66
67 tmp = src
68 rel = ''
69 while top != tmp:
70 rel += '../'
71 tmp = os.path.dirname(tmp)
72 return rel + dst[len(top) + 1:]
73
74
Shawn O. Pearce632768b2008-10-23 11:58:52 -070075class DownloadedChange(object):
76 _commit_cache = None
77
78 def __init__(self, project, base, change_id, ps_id, commit):
79 self.project = project
80 self.base = base
81 self.change_id = change_id
82 self.ps_id = ps_id
83 self.commit = commit
84
85 @property
86 def commits(self):
87 if self._commit_cache is None:
88 self._commit_cache = self.project.bare_git.rev_list(
89 '--abbrev=8',
90 '--abbrev-commit',
91 '--pretty=oneline',
92 '--reverse',
93 '--date-order',
94 not_rev(self.base),
95 self.commit,
96 '--')
97 return self._commit_cache
98
99
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700100class ReviewableBranch(object):
101 _commit_cache = None
102
103 def __init__(self, project, branch, base):
104 self.project = project
105 self.branch = branch
106 self.base = base
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800107 self.replace_changes = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700108
109 @property
110 def name(self):
111 return self.branch.name
112
113 @property
114 def commits(self):
115 if self._commit_cache is None:
116 self._commit_cache = self.project.bare_git.rev_list(
117 '--abbrev=8',
118 '--abbrev-commit',
119 '--pretty=oneline',
120 '--reverse',
121 '--date-order',
122 not_rev(self.base),
123 R_HEADS + self.name,
124 '--')
125 return self._commit_cache
126
127 @property
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800128 def unabbrev_commits(self):
129 r = dict()
130 for commit in self.project.bare_git.rev_list(
131 not_rev(self.base),
132 R_HEADS + self.name,
133 '--'):
134 r[commit[0:8]] = commit
135 return r
136
137 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700138 def date(self):
139 return self.project.bare_git.log(
140 '--pretty=format:%cd',
141 '-n', '1',
142 R_HEADS + self.name,
143 '--')
144
Joe Onorato2896a792008-11-17 16:56:36 -0500145 def UploadForReview(self, people):
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800146 self.project.UploadForReview(self.name,
Joe Onorato2896a792008-11-17 16:56:36 -0500147 self.replace_changes,
148 people)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700149
150 @property
151 def tip_url(self):
152 me = self.project.GetBranch(self.name)
153 commit = self.project.bare_git.rev_parse(R_HEADS + self.name)
154 return 'http://%s/r/%s' % (me.remote.review, commit[0:12])
155
Shawn O. Pearce0758d2f2008-10-22 13:13:40 -0700156 @property
157 def owner_email(self):
158 return self.project.UserEmail
159
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700160
161class StatusColoring(Coloring):
162 def __init__(self, config):
163 Coloring.__init__(self, config, 'status')
164 self.project = self.printer('header', attr = 'bold')
165 self.branch = self.printer('header', attr = 'bold')
166 self.nobranch = self.printer('nobranch', fg = 'red')
167
168 self.added = self.printer('added', fg = 'green')
169 self.changed = self.printer('changed', fg = 'red')
170 self.untracked = self.printer('untracked', fg = 'red')
171
172
173class DiffColoring(Coloring):
174 def __init__(self, config):
175 Coloring.__init__(self, config, 'diff')
176 self.project = self.printer('header', attr = 'bold')
177
178
179class _CopyFile:
180 def __init__(self, src, dest):
181 self.src = src
182 self.dest = dest
183
184 def _Copy(self):
185 src = self.src
186 dest = self.dest
187 # copy file if it does not exist or is out of date
188 if not os.path.exists(dest) or not filecmp.cmp(src, dest):
189 try:
190 # remove existing file first, since it might be read-only
191 if os.path.exists(dest):
192 os.remove(dest)
193 shutil.copy(src, dest)
194 # make the file read-only
195 mode = os.stat(dest)[stat.ST_MODE]
196 mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
197 os.chmod(dest, mode)
198 except IOError:
199 print >>sys.stderr, \
200 'error: Cannot copy file %s to %s' \
201 % (src, dest)
202
203
204class Project(object):
205 def __init__(self,
206 manifest,
207 name,
208 remote,
209 gitdir,
210 worktree,
211 relpath,
212 revision):
213 self.manifest = manifest
214 self.name = name
215 self.remote = remote
216 self.gitdir = gitdir
217 self.worktree = worktree
218 self.relpath = relpath
219 self.revision = revision
220 self.snapshots = {}
221 self.extraRemotes = {}
222 self.copyfiles = []
223 self.config = GitConfig.ForRepository(
224 gitdir = self.gitdir,
225 defaults = self.manifest.globalConfig)
226
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800227 if self.worktree:
228 self.work_git = self._GitGetByExec(self, bare=False)
229 else:
230 self.work_git = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700231 self.bare_git = self._GitGetByExec(self, bare=True)
232
233 @property
234 def Exists(self):
235 return os.path.isdir(self.gitdir)
236
237 @property
238 def CurrentBranch(self):
239 """Obtain the name of the currently checked out branch.
240 The branch name omits the 'refs/heads/' prefix.
241 None is returned if the project is on a detached HEAD.
242 """
243 try:
244 b = self.work_git.GetHead()
245 except GitError:
246 return None
247 if b.startswith(R_HEADS):
248 return b[len(R_HEADS):]
249 return None
250
251 def IsDirty(self, consider_untracked=True):
252 """Is the working directory modified in some way?
253 """
254 self.work_git.update_index('-q',
255 '--unmerged',
256 '--ignore-missing',
257 '--refresh')
258 if self.work_git.DiffZ('diff-index','-M','--cached',HEAD):
259 return True
260 if self.work_git.DiffZ('diff-files'):
261 return True
262 if consider_untracked and self.work_git.LsOthers():
263 return True
264 return False
265
266 _userident_name = None
267 _userident_email = None
268
269 @property
270 def UserName(self):
271 """Obtain the user's personal name.
272 """
273 if self._userident_name is None:
274 self._LoadUserIdentity()
275 return self._userident_name
276
277 @property
278 def UserEmail(self):
279 """Obtain the user's email address. This is very likely
280 to be their Gerrit login.
281 """
282 if self._userident_email is None:
283 self._LoadUserIdentity()
284 return self._userident_email
285
286 def _LoadUserIdentity(self):
287 u = self.bare_git.var('GIT_COMMITTER_IDENT')
288 m = re.compile("^(.*) <([^>]*)> ").match(u)
289 if m:
290 self._userident_name = m.group(1)
291 self._userident_email = m.group(2)
292 else:
293 self._userident_name = ''
294 self._userident_email = ''
295
296 def GetRemote(self, name):
297 """Get the configuration for a single remote.
298 """
299 return self.config.GetRemote(name)
300
301 def GetBranch(self, name):
302 """Get the configuration for a single branch.
303 """
304 return self.config.GetBranch(name)
305
306
307## Status Display ##
308
309 def PrintWorkTreeStatus(self):
310 """Prints the status of the repository to stdout.
311 """
312 if not os.path.isdir(self.worktree):
313 print ''
314 print 'project %s/' % self.relpath
315 print ' missing (run "repo sync")'
316 return
317
318 self.work_git.update_index('-q',
319 '--unmerged',
320 '--ignore-missing',
321 '--refresh')
322 di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD)
323 df = self.work_git.DiffZ('diff-files')
324 do = self.work_git.LsOthers()
325 if not di and not df and not do:
326 return
327
328 out = StatusColoring(self.config)
329 out.project('project %-40s', self.relpath + '/')
330
331 branch = self.CurrentBranch
332 if branch is None:
333 out.nobranch('(*** NO BRANCH ***)')
334 else:
335 out.branch('branch %s', branch)
336 out.nl()
337
338 paths = list()
339 paths.extend(di.keys())
340 paths.extend(df.keys())
341 paths.extend(do)
342
343 paths = list(set(paths))
344 paths.sort()
345
346 for p in paths:
347 try: i = di[p]
348 except KeyError: i = None
349
350 try: f = df[p]
351 except KeyError: f = None
352
353 if i: i_status = i.status.upper()
354 else: i_status = '-'
355
356 if f: f_status = f.status.lower()
357 else: f_status = '-'
358
359 if i and i.src_path:
360 line = ' %s%s\t%s => (%s%%)' % (i_status, f_status,
361 i.src_path, p, i.level)
362 else:
363 line = ' %s%s\t%s' % (i_status, f_status, p)
364
365 if i and not f:
366 out.added('%s', line)
367 elif (i and f) or (not i and f):
368 out.changed('%s', line)
369 elif not i and not f:
370 out.untracked('%s', line)
371 else:
372 out.write('%s', line)
373 out.nl()
374
375 def PrintWorkTreeDiff(self):
376 """Prints the status of the repository to stdout.
377 """
378 out = DiffColoring(self.config)
379 cmd = ['diff']
380 if out.is_on:
381 cmd.append('--color')
382 cmd.append(HEAD)
383 cmd.append('--')
384 p = GitCommand(self,
385 cmd,
386 capture_stdout = True,
387 capture_stderr = True)
388 has_diff = False
389 for line in p.process.stdout:
390 if not has_diff:
391 out.nl()
392 out.project('project %s/' % self.relpath)
393 out.nl()
394 has_diff = True
395 print line[:-1]
396 p.Wait()
397
398
399## Publish / Upload ##
400
401 def WasPublished(self, branch):
402 """Was the branch published (uploaded) for code review?
403 If so, returns the SHA-1 hash of the last published
404 state for the branch.
405 """
406 try:
407 return self.bare_git.rev_parse(R_PUB + branch)
408 except GitError:
409 return None
410
411 def CleanPublishedCache(self):
412 """Prunes any stale published refs.
413 """
414 heads = set()
415 canrm = {}
416 for name, id in self._allrefs.iteritems():
417 if name.startswith(R_HEADS):
418 heads.add(name)
419 elif name.startswith(R_PUB):
420 canrm[name] = id
421
422 for name, id in canrm.iteritems():
423 n = name[len(R_PUB):]
424 if R_HEADS + n not in heads:
425 self.bare_git.DeleteRef(name, id)
426
427 def GetUploadableBranches(self):
428 """List any branches which can be uploaded for review.
429 """
430 heads = {}
431 pubed = {}
432
433 for name, id in self._allrefs.iteritems():
434 if name.startswith(R_HEADS):
435 heads[name[len(R_HEADS):]] = id
436 elif name.startswith(R_PUB):
437 pubed[name[len(R_PUB):]] = id
438
439 ready = []
440 for branch, id in heads.iteritems():
441 if branch in pubed and pubed[branch] == id:
442 continue
443
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800444 rb = self.GetUploadableBranch(branch)
445 if rb:
446 ready.append(rb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700447 return ready
448
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800449 def GetUploadableBranch(self, branch_name):
450 """Get a single uploadable branch, or None.
451 """
452 branch = self.GetBranch(branch_name)
453 base = branch.LocalMerge
454 if branch.LocalMerge:
455 rb = ReviewableBranch(self, branch, base)
456 if rb.commits:
457 return rb
458 return None
459
Joe Onorato2896a792008-11-17 16:56:36 -0500460 def UploadForReview(self, branch=None, replace_changes=None, people=([],[])):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700461 """Uploads the named branch for code review.
462 """
463 if branch is None:
464 branch = self.CurrentBranch
465 if branch is None:
466 raise GitError('not currently on a branch')
467
468 branch = self.GetBranch(branch)
469 if not branch.LocalMerge:
470 raise GitError('branch %s does not track a remote' % branch.name)
471 if not branch.remote.review:
472 raise GitError('remote %s has no review url' % branch.remote.name)
473
474 dest_branch = branch.merge
475 if not dest_branch.startswith(R_HEADS):
476 dest_branch = R_HEADS + dest_branch
477
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800478 if not branch.remote.projectname:
479 branch.remote.projectname = self.name
480 branch.remote.Save()
481
Shawn O. Pearce370e3fa2009-01-26 10:55:39 -0800482 if branch.remote.ReviewProtocol == 'ssh':
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800483 if dest_branch.startswith(R_HEADS):
484 dest_branch = dest_branch[len(R_HEADS):]
485
486 rp = ['gerrit receive-pack']
487 for e in people[0]:
488 rp.append('--reviewer=%s' % sq(e))
489 for e in people[1]:
490 rp.append('--cc=%s' % sq(e))
491
492 cmd = ['push']
493 cmd.append('--receive-pack=%s' % " ".join(rp))
494 cmd.append(branch.remote.SshReviewUrl(self.UserEmail))
495 cmd.append('%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch))
496 if replace_changes:
497 for change_id,commit_id in replace_changes.iteritems():
498 cmd.append('%s:refs/changes/%s/new' % (commit_id, change_id))
499 if GitCommand(self, cmd, bare = True).Wait() != 0:
500 raise UploadError('Upload failed')
501
502 else:
503 raise UploadError('Unsupported protocol %s' \
504 % branch.remote.review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700505
506 msg = "posted to %s for %s" % (branch.remote.review, dest_branch)
507 self.bare_git.UpdateRef(R_PUB + branch.name,
508 R_HEADS + branch.name,
509 message = msg)
510
511
512## Sync ##
513
514 def Sync_NetworkHalf(self):
515 """Perform only the network IO portion of the sync process.
516 Local working directory/branch state is not affected.
517 """
518 if not self.Exists:
519 print >>sys.stderr
520 print >>sys.stderr, 'Initializing project %s ...' % self.name
521 self._InitGitDir()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800522
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700523 self._InitRemote()
524 for r in self.extraRemotes.values():
525 if not self._RemoteFetch(r.name):
526 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700527 if not self._RemoteFetch():
528 return False
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800529
530 if self.worktree:
531 self._RepairAndroidImportErrors()
532 self._InitMRef()
533 else:
534 self._InitMirrorHead()
535 try:
536 os.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
537 except OSError:
538 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700539 return True
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800540
541 def PostRepoUpgrade(self):
542 self._InitHooks()
543
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700544 def _CopyFiles(self):
545 for file in self.copyfiles:
546 file._Copy()
547
Shawn O. Pearce329c31d2008-10-24 09:17:25 -0700548 def _RepairAndroidImportErrors(self):
549 if self.name in ['platform/external/iptables',
550 'platform/external/libpcap',
551 'platform/external/tcpdump',
552 'platform/external/webkit',
553 'platform/system/wlan/ti']:
554 # I hate myself for doing this...
555 #
556 # In the initial Android 1.0 release these projects were
557 # shipped, some users got them, and then the history had
558 # to be rewritten to correct problems with their imports.
559 # The 'android-1.0' tag may still be pointing at the old
560 # history, so we need to drop the tag and fetch it again.
561 #
562 try:
563 remote = self.GetRemote(self.remote.name)
564 relname = remote.ToLocal(R_HEADS + 'release-1.0')
565 tagname = R_TAGS + 'android-1.0'
566 if self._revlist(not_rev(relname), tagname):
567 cmd = ['fetch', remote.name, '+%s:%s' % (tagname, tagname)]
568 GitCommand(self, cmd, bare = True).Wait()
569 except GitError:
570 pass
571
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700572 def Sync_LocalHalf(self):
573 """Perform only the local IO portion of the sync process.
574 Network access is not required.
575
576 Return:
577 True: the sync was successful
578 False: the sync requires user input
579 """
580 self._InitWorkTree()
581 self.CleanPublishedCache()
582
583 rem = self.GetRemote(self.remote.name)
584 rev = rem.ToLocal(self.revision)
585 branch = self.CurrentBranch
586
587 if branch is None:
588 # Currently on a detached HEAD. The user is assumed to
589 # not have any local modifications worth worrying about.
590 #
591 lost = self._revlist(not_rev(rev), HEAD)
592 if lost:
593 _info("[%s] Discarding %d commits", self.name, len(lost))
594 try:
595 self._Checkout(rev, quiet=True)
596 except GitError:
597 return False
598 self._CopyFiles()
599 return True
600
601 branch = self.GetBranch(branch)
602 merge = branch.LocalMerge
603
604 if not merge:
605 # The current branch has no tracking configuration.
606 # Jump off it to a deatched HEAD.
607 #
608 _info("[%s] Leaving %s"
609 " (does not track any upstream)",
610 self.name,
611 branch.name)
612 try:
613 self._Checkout(rev, quiet=True)
614 except GitError:
615 return False
616 self._CopyFiles()
617 return True
618
619 upstream_gain = self._revlist(not_rev(HEAD), rev)
620 pub = self.WasPublished(branch.name)
621 if pub:
622 not_merged = self._revlist(not_rev(rev), pub)
623 if not_merged:
624 if upstream_gain:
625 # The user has published this branch and some of those
626 # commits are not yet merged upstream. We do not want
627 # to rewrite the published commits so we punt.
628 #
629 _info("[%s] Branch %s is published,"
630 " but is now %d commits behind.",
631 self.name, branch.name, len(upstream_gain))
632 _info("[%s] Consider merging or rebasing the"
633 " unpublished commits.", self.name)
634 return True
Shawn O. Pearce23d77812008-10-30 11:06:57 -0700635 elif upstream_gain:
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700636 # We can fast-forward safely.
637 #
638 try:
639 self._FastForward(rev)
640 except GitError:
641 return False
642 self._CopyFiles()
643 return True
Shawn O. Pearce23d77812008-10-30 11:06:57 -0700644 else:
645 # Trivially no changes in the upstream.
646 #
647 return True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700648
649 if merge == rev:
650 try:
651 old_merge = self.bare_git.rev_parse('%s@{1}' % merge)
652 except GitError:
653 old_merge = merge
Shawn O. Pearce07346002008-10-21 07:09:27 -0700654 if old_merge == '0000000000000000000000000000000000000000' \
655 or old_merge == '':
656 old_merge = merge
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700657 else:
658 # The upstream switched on us. Time to cross our fingers
659 # and pray that the old upstream also wasn't in the habit
660 # of rebasing itself.
661 #
662 _info("[%s] Manifest switched from %s to %s",
663 self.name, merge, rev)
664 old_merge = merge
665
666 if rev == old_merge:
667 upstream_lost = []
668 else:
669 upstream_lost = self._revlist(not_rev(rev), old_merge)
670
671 if not upstream_lost and not upstream_gain:
672 # Trivially no changes caused by the upstream.
673 #
674 return True
675
676 if self.IsDirty(consider_untracked=False):
677 _warn('[%s] commit (or discard) uncommitted changes'
678 ' before sync', self.name)
679 return False
680
681 if upstream_lost:
682 # Upstream rebased. Not everything in HEAD
683 # may have been caused by the user.
684 #
685 _info("[%s] Discarding %d commits removed from upstream",
686 self.name, len(upstream_lost))
687
688 branch.remote = rem
689 branch.merge = self.revision
690 branch.Save()
691
692 my_changes = self._revlist(not_rev(old_merge), HEAD)
693 if my_changes:
694 try:
695 self._Rebase(upstream = old_merge, onto = rev)
696 except GitError:
697 return False
698 elif upstream_lost:
699 try:
700 self._ResetHard(rev)
701 except GitError:
702 return False
703 else:
704 try:
705 self._FastForward(rev)
706 except GitError:
707 return False
708
709 self._CopyFiles()
710 return True
711
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700712 def AddCopyFile(self, src, dest):
713 # dest should already be an absolute path, but src is project relative
714 # make src an absolute path
715 src = os.path.join(self.worktree, src)
716 self.copyfiles.append(_CopyFile(src, dest))
717
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700718 def DownloadPatchSet(self, change_id, patch_id):
719 """Download a single patch set of a single change to FETCH_HEAD.
720 """
721 remote = self.GetRemote(self.remote.name)
722
723 cmd = ['fetch', remote.name]
724 cmd.append('refs/changes/%2.2d/%d/%d' \
725 % (change_id % 100, change_id, patch_id))
726 cmd.extend(map(lambda x: str(x), remote.fetch))
727 if GitCommand(self, cmd, bare=True).Wait() != 0:
728 return None
729 return DownloadedChange(self,
730 remote.ToLocal(self.revision),
731 change_id,
732 patch_id,
733 self.bare_git.rev_parse('FETCH_HEAD'))
734
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700735
736## Branch Management ##
737
738 def StartBranch(self, name):
739 """Create a new branch off the manifest's revision.
740 """
741 branch = self.GetBranch(name)
742 branch.remote = self.GetRemote(self.remote.name)
743 branch.merge = self.revision
744
745 rev = branch.LocalMerge
746 cmd = ['checkout', '-b', branch.name, rev]
747 if GitCommand(self, cmd).Wait() == 0:
748 branch.Save()
749 else:
750 raise GitError('%s checkout %s ' % (self.name, rev))
751
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800752 def AbandonBranch(self, name):
753 """Destroy a local topic branch.
754 """
755 try:
756 tip_rev = self.bare_git.rev_parse(R_HEADS + name)
757 except GitError:
758 return
759
760 if self.CurrentBranch == name:
761 self._Checkout(
762 self.GetRemote(self.remote.name).ToLocal(self.revision),
763 quiet=True)
764
765 cmd = ['branch', '-D', name]
766 GitCommand(self, cmd, capture_stdout=True).Wait()
767
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700768 def PruneHeads(self):
769 """Prune any topic branches already merged into upstream.
770 """
771 cb = self.CurrentBranch
772 kill = []
773 for name in self._allrefs.keys():
774 if name.startswith(R_HEADS):
775 name = name[len(R_HEADS):]
776 if cb is None or name != cb:
777 kill.append(name)
778
779 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
780 if cb is not None \
781 and not self._revlist(HEAD + '...' + rev) \
782 and not self.IsDirty(consider_untracked = False):
783 self.work_git.DetachHead(HEAD)
784 kill.append(cb)
785
786 deleted = set()
787 if kill:
788 try:
789 old = self.bare_git.GetHead()
790 except GitError:
791 old = 'refs/heads/please_never_use_this_as_a_branch_name'
792
793 rm_re = re.compile(r"^Deleted branch (.*)\.$")
794 try:
795 self.bare_git.DetachHead(rev)
796
797 b = ['branch', '-d']
798 b.extend(kill)
799 b = GitCommand(self, b, bare=True,
800 capture_stdout=True,
801 capture_stderr=True)
802 b.Wait()
803 finally:
804 self.bare_git.SetHead(old)
805
806 for line in b.stdout.split("\n"):
807 m = rm_re.match(line)
808 if m:
809 deleted.add(m.group(1))
810
811 if deleted:
812 self.CleanPublishedCache()
813
814 if cb and cb not in kill:
815 kill.append(cb)
816 kill.sort()
817
818 kept = []
819 for branch in kill:
820 if branch not in deleted:
821 branch = self.GetBranch(branch)
822 base = branch.LocalMerge
823 if not base:
824 base = rev
825 kept.append(ReviewableBranch(self, branch, base))
826 return kept
827
828
829## Direct Git Commands ##
830
831 def _RemoteFetch(self, name=None):
832 if not name:
833 name = self.remote.name
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800834 cmd = ['fetch']
835 if not self.worktree:
836 cmd.append('--update-head-ok')
837 cmd.append(name)
838 return GitCommand(self, cmd, bare = True).Wait() == 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700839
840 def _Checkout(self, rev, quiet=False):
841 cmd = ['checkout']
842 if quiet:
843 cmd.append('-q')
844 cmd.append(rev)
845 cmd.append('--')
846 if GitCommand(self, cmd).Wait() != 0:
847 if self._allrefs:
848 raise GitError('%s checkout %s ' % (self.name, rev))
849
850 def _ResetHard(self, rev, quiet=True):
851 cmd = ['reset', '--hard']
852 if quiet:
853 cmd.append('-q')
854 cmd.append(rev)
855 if GitCommand(self, cmd).Wait() != 0:
856 raise GitError('%s reset --hard %s ' % (self.name, rev))
857
858 def _Rebase(self, upstream, onto = None):
859 cmd = ['rebase', '-i']
860 if onto is not None:
861 cmd.extend(['--onto', onto])
862 cmd.append(upstream)
863 if GitCommand(self, cmd, disable_editor=True).Wait() != 0:
864 raise GitError('%s rebase %s ' % (self.name, upstream))
865
866 def _FastForward(self, head):
867 cmd = ['merge', head]
868 if GitCommand(self, cmd).Wait() != 0:
869 raise GitError('%s merge %s ' % (self.name, head))
870
871 def _InitGitDir(self):
872 if not os.path.exists(self.gitdir):
873 os.makedirs(self.gitdir)
874 self.bare_git.init()
875 self.config.SetString('core.bare', None)
876
877 hooks = self._gitdir_path('hooks')
Shawn O. Pearcede646812008-10-29 14:38:12 -0700878 try:
879 to_rm = os.listdir(hooks)
880 except OSError:
881 to_rm = []
882 for old_hook in to_rm:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700883 os.remove(os.path.join(hooks, old_hook))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800884 self._InitHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700885
886 m = self.manifest.manifestProject.config
887 for key in ['user.name', 'user.email']:
888 if m.Has(key, include_defaults = False):
889 self.config.SetString(key, m.GetString(key))
890
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800891 def _InitHooks(self):
892 hooks = self._gitdir_path('hooks')
893 if not os.path.exists(hooks):
894 os.makedirs(hooks)
895 for stock_hook in repo_hooks():
896 dst = os.path.join(hooks, os.path.basename(stock_hook))
897 try:
898 os.symlink(relpath(stock_hook, dst), dst)
899 except OSError, e:
900 if e.errno == errno.EEXIST:
901 pass
902 elif e.errno == errno.EPERM:
903 raise GitError('filesystem must support symlinks')
904 else:
905 raise
906
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700907 def _InitRemote(self):
908 if self.remote.fetchUrl:
909 remote = self.GetRemote(self.remote.name)
910
911 url = self.remote.fetchUrl
912 while url.endswith('/'):
913 url = url[:-1]
914 url += '/%s.git' % self.name
915 remote.url = url
916 remote.review = self.remote.reviewUrl
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800917 if remote.projectname is None:
918 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700919
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800920 if self.worktree:
921 remote.ResetFetch(mirror=False)
922 else:
923 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700924 remote.Save()
925
926 for r in self.extraRemotes.values():
927 remote = self.GetRemote(r.name)
928 remote.url = r.fetchUrl
929 remote.review = r.reviewUrl
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800930 if r.projectName:
931 remote.projectname = r.projectName
932 elif remote.projectname is None:
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800933 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700934 remote.ResetFetch()
935 remote.Save()
936
937 def _InitMRef(self):
938 if self.manifest.branch:
939 msg = 'manifest set to %s' % self.revision
940 ref = R_M + self.manifest.branch
941
942 if IsId(self.revision):
Marcelo E. Magallon21f73852008-12-31 04:44:37 +0000943 dst = self.revision + '^0'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700944 self.bare_git.UpdateRef(ref, dst, message = msg, detach = True)
945 else:
946 remote = self.GetRemote(self.remote.name)
947 dst = remote.ToLocal(self.revision)
948 self.bare_git.symbolic_ref('-m', msg, ref, dst)
949
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800950 def _InitMirrorHead(self):
951 dst = self.GetRemote(self.remote.name).ToLocal(self.revision)
952 msg = 'manifest set to %s' % self.revision
953 self.bare_git.SetHead(dst, message=msg)
954
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700955 def _InitWorkTree(self):
956 dotgit = os.path.join(self.worktree, '.git')
957 if not os.path.exists(dotgit):
958 os.makedirs(dotgit)
959
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700960 for name in ['config',
961 'description',
962 'hooks',
963 'info',
964 'logs',
965 'objects',
966 'packed-refs',
967 'refs',
968 'rr-cache',
969 'svn']:
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -0800970 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800971 src = os.path.join(self.gitdir, name)
972 dst = os.path.join(dotgit, name)
973 os.symlink(relpath(src, dst), dst)
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -0800974 except OSError, e:
975 if e.errno == errno.EPERM:
976 raise GitError('filesystem must support symlinks')
977 else:
978 raise
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700979
980 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
981 rev = self.bare_git.rev_parse('%s^0' % rev)
982
983 f = open(os.path.join(dotgit, HEAD), 'wb')
984 f.write("%s\n" % rev)
985 f.close()
986
987 cmd = ['read-tree', '--reset', '-u']
988 cmd.append('-v')
989 cmd.append('HEAD')
990 if GitCommand(self, cmd).Wait() != 0:
991 raise GitError("cannot initialize work tree")
992
993 def _gitdir_path(self, path):
994 return os.path.join(self.gitdir, path)
995
996 def _revlist(self, *args):
997 cmd = []
998 cmd.extend(args)
999 cmd.append('--')
1000 return self.work_git.rev_list(*args)
1001
1002 @property
1003 def _allrefs(self):
1004 return self.bare_git.ListRefs()
1005
1006 class _GitGetByExec(object):
1007 def __init__(self, project, bare):
1008 self._project = project
1009 self._bare = bare
1010
1011 def ListRefs(self, *args):
1012 cmdv = ['for-each-ref', '--format=%(objectname) %(refname)']
1013 cmdv.extend(args)
1014 p = GitCommand(self._project,
1015 cmdv,
1016 bare = self._bare,
1017 capture_stdout = True,
1018 capture_stderr = True)
1019 r = {}
1020 for line in p.process.stdout:
1021 id, name = line[:-1].split(' ', 2)
1022 r[name] = id
1023 if p.Wait() != 0:
1024 raise GitError('%s for-each-ref %s: %s' % (
1025 self._project.name,
1026 str(args),
1027 p.stderr))
1028 return r
1029
1030 def LsOthers(self):
1031 p = GitCommand(self._project,
1032 ['ls-files',
1033 '-z',
1034 '--others',
1035 '--exclude-standard'],
1036 bare = False,
1037 capture_stdout = True,
1038 capture_stderr = True)
1039 if p.Wait() == 0:
1040 out = p.stdout
1041 if out:
1042 return out[:-1].split("\0")
1043 return []
1044
1045 def DiffZ(self, name, *args):
1046 cmd = [name]
1047 cmd.append('-z')
1048 cmd.extend(args)
1049 p = GitCommand(self._project,
1050 cmd,
1051 bare = False,
1052 capture_stdout = True,
1053 capture_stderr = True)
1054 try:
1055 out = p.process.stdout.read()
1056 r = {}
1057 if out:
1058 out = iter(out[:-1].split('\0'))
1059 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07001060 try:
1061 info = out.next()
1062 path = out.next()
1063 except StopIteration:
1064 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001065
1066 class _Info(object):
1067 def __init__(self, path, omode, nmode, oid, nid, state):
1068 self.path = path
1069 self.src_path = None
1070 self.old_mode = omode
1071 self.new_mode = nmode
1072 self.old_id = oid
1073 self.new_id = nid
1074
1075 if len(state) == 1:
1076 self.status = state
1077 self.level = None
1078 else:
1079 self.status = state[:1]
1080 self.level = state[1:]
1081 while self.level.startswith('0'):
1082 self.level = self.level[1:]
1083
1084 info = info[1:].split(' ')
1085 info =_Info(path, *info)
1086 if info.status in ('R', 'C'):
1087 info.src_path = info.path
1088 info.path = out.next()
1089 r[info.path] = info
1090 return r
1091 finally:
1092 p.Wait()
1093
1094 def GetHead(self):
1095 return self.symbolic_ref(HEAD)
1096
1097 def SetHead(self, ref, message=None):
1098 cmdv = []
1099 if message is not None:
1100 cmdv.extend(['-m', message])
1101 cmdv.append(HEAD)
1102 cmdv.append(ref)
1103 self.symbolic_ref(*cmdv)
1104
1105 def DetachHead(self, new, message=None):
1106 cmdv = ['--no-deref']
1107 if message is not None:
1108 cmdv.extend(['-m', message])
1109 cmdv.append(HEAD)
1110 cmdv.append(new)
1111 self.update_ref(*cmdv)
1112
1113 def UpdateRef(self, name, new, old=None,
1114 message=None,
1115 detach=False):
1116 cmdv = []
1117 if message is not None:
1118 cmdv.extend(['-m', message])
1119 if detach:
1120 cmdv.append('--no-deref')
1121 cmdv.append(name)
1122 cmdv.append(new)
1123 if old is not None:
1124 cmdv.append(old)
1125 self.update_ref(*cmdv)
1126
1127 def DeleteRef(self, name, old=None):
1128 if not old:
1129 old = self.rev_parse(name)
1130 self.update_ref('-d', name, old)
1131
1132 def rev_list(self, *args):
1133 cmdv = ['rev-list']
1134 cmdv.extend(args)
1135 p = GitCommand(self._project,
1136 cmdv,
1137 bare = self._bare,
1138 capture_stdout = True,
1139 capture_stderr = True)
1140 r = []
1141 for line in p.process.stdout:
1142 r.append(line[:-1])
1143 if p.Wait() != 0:
1144 raise GitError('%s rev-list %s: %s' % (
1145 self._project.name,
1146 str(args),
1147 p.stderr))
1148 return r
1149
1150 def __getattr__(self, name):
1151 name = name.replace('_', '-')
1152 def runner(*args):
1153 cmdv = [name]
1154 cmdv.extend(args)
1155 p = GitCommand(self._project,
1156 cmdv,
1157 bare = self._bare,
1158 capture_stdout = True,
1159 capture_stderr = True)
1160 if p.Wait() != 0:
1161 raise GitError('%s %s: %s' % (
1162 self._project.name,
1163 name,
1164 p.stderr))
1165 r = p.stdout
1166 if r.endswith('\n') and r.index('\n') == len(r) - 1:
1167 return r[:-1]
1168 return r
1169 return runner
1170
1171
1172class MetaProject(Project):
1173 """A special project housed under .repo.
1174 """
1175 def __init__(self, manifest, name, gitdir, worktree):
1176 repodir = manifest.repodir
1177 Project.__init__(self,
1178 manifest = manifest,
1179 name = name,
1180 gitdir = gitdir,
1181 worktree = worktree,
1182 remote = Remote('origin'),
1183 relpath = '.repo/%s' % name,
1184 revision = 'refs/heads/master')
1185
1186 def PreSync(self):
1187 if self.Exists:
1188 cb = self.CurrentBranch
1189 if cb:
1190 base = self.GetBranch(cb).merge
1191 if base:
1192 self.revision = base
1193
1194 @property
1195 def HasChanges(self):
1196 """Has the remote received new commits not yet checked out?
1197 """
1198 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
1199 if self._revlist(not_rev(HEAD), rev):
1200 return True
1201 return False