blob: 4780316c81db45fe248b58a3cb1a12a3db32551d [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 = []
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800773 left = self._allrefs
774 for name in left.keys():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700775 if name.startswith(R_HEADS):
776 name = name[len(R_HEADS):]
777 if cb is None or name != cb:
778 kill.append(name)
779
780 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
781 if cb is not None \
782 and not self._revlist(HEAD + '...' + rev) \
783 and not self.IsDirty(consider_untracked = False):
784 self.work_git.DetachHead(HEAD)
785 kill.append(cb)
786
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700787 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
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700793 try:
794 self.bare_git.DetachHead(rev)
795
796 b = ['branch', '-d']
797 b.extend(kill)
798 b = GitCommand(self, b, bare=True,
799 capture_stdout=True,
800 capture_stderr=True)
801 b.Wait()
802 finally:
803 self.bare_git.SetHead(old)
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800804 left = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700805
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800806 for branch in kill:
807 if (R_HEADS + branch) not in left:
808 self.CleanPublishedCache()
809 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700810
811 if cb and cb not in kill:
812 kill.append(cb)
Shawn O. Pearce7c6c64d2009-03-02 12:38:13 -0800813 kill.sort()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700814
815 kept = []
816 for branch in kill:
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800817 if (R_HEADS + branch) in left:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700818 branch = self.GetBranch(branch)
819 base = branch.LocalMerge
820 if not base:
821 base = rev
822 kept.append(ReviewableBranch(self, branch, base))
823 return kept
824
825
826## Direct Git Commands ##
827
828 def _RemoteFetch(self, name=None):
829 if not name:
830 name = self.remote.name
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800831 cmd = ['fetch']
832 if not self.worktree:
833 cmd.append('--update-head-ok')
834 cmd.append(name)
835 return GitCommand(self, cmd, bare = True).Wait() == 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700836
837 def _Checkout(self, rev, quiet=False):
838 cmd = ['checkout']
839 if quiet:
840 cmd.append('-q')
841 cmd.append(rev)
842 cmd.append('--')
843 if GitCommand(self, cmd).Wait() != 0:
844 if self._allrefs:
845 raise GitError('%s checkout %s ' % (self.name, rev))
846
847 def _ResetHard(self, rev, quiet=True):
848 cmd = ['reset', '--hard']
849 if quiet:
850 cmd.append('-q')
851 cmd.append(rev)
852 if GitCommand(self, cmd).Wait() != 0:
853 raise GitError('%s reset --hard %s ' % (self.name, rev))
854
855 def _Rebase(self, upstream, onto = None):
856 cmd = ['rebase', '-i']
857 if onto is not None:
858 cmd.extend(['--onto', onto])
859 cmd.append(upstream)
860 if GitCommand(self, cmd, disable_editor=True).Wait() != 0:
861 raise GitError('%s rebase %s ' % (self.name, upstream))
862
863 def _FastForward(self, head):
864 cmd = ['merge', head]
865 if GitCommand(self, cmd).Wait() != 0:
866 raise GitError('%s merge %s ' % (self.name, head))
867
868 def _InitGitDir(self):
869 if not os.path.exists(self.gitdir):
870 os.makedirs(self.gitdir)
871 self.bare_git.init()
872 self.config.SetString('core.bare', None)
873
874 hooks = self._gitdir_path('hooks')
Shawn O. Pearcede646812008-10-29 14:38:12 -0700875 try:
876 to_rm = os.listdir(hooks)
877 except OSError:
878 to_rm = []
879 for old_hook in to_rm:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700880 os.remove(os.path.join(hooks, old_hook))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800881 self._InitHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700882
883 m = self.manifest.manifestProject.config
884 for key in ['user.name', 'user.email']:
885 if m.Has(key, include_defaults = False):
886 self.config.SetString(key, m.GetString(key))
887
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800888 def _InitHooks(self):
889 hooks = self._gitdir_path('hooks')
890 if not os.path.exists(hooks):
891 os.makedirs(hooks)
892 for stock_hook in repo_hooks():
893 dst = os.path.join(hooks, os.path.basename(stock_hook))
894 try:
895 os.symlink(relpath(stock_hook, dst), dst)
896 except OSError, e:
897 if e.errno == errno.EEXIST:
898 pass
899 elif e.errno == errno.EPERM:
900 raise GitError('filesystem must support symlinks')
901 else:
902 raise
903
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700904 def _InitRemote(self):
905 if self.remote.fetchUrl:
906 remote = self.GetRemote(self.remote.name)
907
908 url = self.remote.fetchUrl
909 while url.endswith('/'):
910 url = url[:-1]
911 url += '/%s.git' % self.name
912 remote.url = url
913 remote.review = self.remote.reviewUrl
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800914 if remote.projectname is None:
915 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700916
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800917 if self.worktree:
918 remote.ResetFetch(mirror=False)
919 else:
920 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700921 remote.Save()
922
923 for r in self.extraRemotes.values():
924 remote = self.GetRemote(r.name)
925 remote.url = r.fetchUrl
926 remote.review = r.reviewUrl
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800927 if r.projectName:
928 remote.projectname = r.projectName
929 elif remote.projectname is None:
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800930 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700931 remote.ResetFetch()
932 remote.Save()
933
934 def _InitMRef(self):
935 if self.manifest.branch:
936 msg = 'manifest set to %s' % self.revision
937 ref = R_M + self.manifest.branch
938
939 if IsId(self.revision):
Marcelo E. Magallon21f73852008-12-31 04:44:37 +0000940 dst = self.revision + '^0'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700941 self.bare_git.UpdateRef(ref, dst, message = msg, detach = True)
942 else:
943 remote = self.GetRemote(self.remote.name)
944 dst = remote.ToLocal(self.revision)
945 self.bare_git.symbolic_ref('-m', msg, ref, dst)
946
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800947 def _InitMirrorHead(self):
948 dst = self.GetRemote(self.remote.name).ToLocal(self.revision)
949 msg = 'manifest set to %s' % self.revision
950 self.bare_git.SetHead(dst, message=msg)
951
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700952 def _InitWorkTree(self):
953 dotgit = os.path.join(self.worktree, '.git')
954 if not os.path.exists(dotgit):
955 os.makedirs(dotgit)
956
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700957 for name in ['config',
958 'description',
959 'hooks',
960 'info',
961 'logs',
962 'objects',
963 'packed-refs',
964 'refs',
965 'rr-cache',
966 'svn']:
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -0800967 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800968 src = os.path.join(self.gitdir, name)
969 dst = os.path.join(dotgit, name)
970 os.symlink(relpath(src, dst), dst)
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -0800971 except OSError, e:
972 if e.errno == errno.EPERM:
973 raise GitError('filesystem must support symlinks')
974 else:
975 raise
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700976
977 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
978 rev = self.bare_git.rev_parse('%s^0' % rev)
979
980 f = open(os.path.join(dotgit, HEAD), 'wb')
981 f.write("%s\n" % rev)
982 f.close()
983
984 cmd = ['read-tree', '--reset', '-u']
985 cmd.append('-v')
986 cmd.append('HEAD')
987 if GitCommand(self, cmd).Wait() != 0:
988 raise GitError("cannot initialize work tree")
989
990 def _gitdir_path(self, path):
991 return os.path.join(self.gitdir, path)
992
993 def _revlist(self, *args):
994 cmd = []
995 cmd.extend(args)
996 cmd.append('--')
997 return self.work_git.rev_list(*args)
998
999 @property
1000 def _allrefs(self):
1001 return self.bare_git.ListRefs()
1002
1003 class _GitGetByExec(object):
1004 def __init__(self, project, bare):
1005 self._project = project
1006 self._bare = bare
1007
1008 def ListRefs(self, *args):
1009 cmdv = ['for-each-ref', '--format=%(objectname) %(refname)']
1010 cmdv.extend(args)
1011 p = GitCommand(self._project,
1012 cmdv,
1013 bare = self._bare,
1014 capture_stdout = True,
1015 capture_stderr = True)
1016 r = {}
1017 for line in p.process.stdout:
1018 id, name = line[:-1].split(' ', 2)
1019 r[name] = id
1020 if p.Wait() != 0:
1021 raise GitError('%s for-each-ref %s: %s' % (
1022 self._project.name,
1023 str(args),
1024 p.stderr))
1025 return r
1026
1027 def LsOthers(self):
1028 p = GitCommand(self._project,
1029 ['ls-files',
1030 '-z',
1031 '--others',
1032 '--exclude-standard'],
1033 bare = False,
1034 capture_stdout = True,
1035 capture_stderr = True)
1036 if p.Wait() == 0:
1037 out = p.stdout
1038 if out:
1039 return out[:-1].split("\0")
1040 return []
1041
1042 def DiffZ(self, name, *args):
1043 cmd = [name]
1044 cmd.append('-z')
1045 cmd.extend(args)
1046 p = GitCommand(self._project,
1047 cmd,
1048 bare = False,
1049 capture_stdout = True,
1050 capture_stderr = True)
1051 try:
1052 out = p.process.stdout.read()
1053 r = {}
1054 if out:
1055 out = iter(out[:-1].split('\0'))
1056 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07001057 try:
1058 info = out.next()
1059 path = out.next()
1060 except StopIteration:
1061 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001062
1063 class _Info(object):
1064 def __init__(self, path, omode, nmode, oid, nid, state):
1065 self.path = path
1066 self.src_path = None
1067 self.old_mode = omode
1068 self.new_mode = nmode
1069 self.old_id = oid
1070 self.new_id = nid
1071
1072 if len(state) == 1:
1073 self.status = state
1074 self.level = None
1075 else:
1076 self.status = state[:1]
1077 self.level = state[1:]
1078 while self.level.startswith('0'):
1079 self.level = self.level[1:]
1080
1081 info = info[1:].split(' ')
1082 info =_Info(path, *info)
1083 if info.status in ('R', 'C'):
1084 info.src_path = info.path
1085 info.path = out.next()
1086 r[info.path] = info
1087 return r
1088 finally:
1089 p.Wait()
1090
1091 def GetHead(self):
1092 return self.symbolic_ref(HEAD)
1093
1094 def SetHead(self, ref, message=None):
1095 cmdv = []
1096 if message is not None:
1097 cmdv.extend(['-m', message])
1098 cmdv.append(HEAD)
1099 cmdv.append(ref)
1100 self.symbolic_ref(*cmdv)
1101
1102 def DetachHead(self, new, message=None):
1103 cmdv = ['--no-deref']
1104 if message is not None:
1105 cmdv.extend(['-m', message])
1106 cmdv.append(HEAD)
1107 cmdv.append(new)
1108 self.update_ref(*cmdv)
1109
1110 def UpdateRef(self, name, new, old=None,
1111 message=None,
1112 detach=False):
1113 cmdv = []
1114 if message is not None:
1115 cmdv.extend(['-m', message])
1116 if detach:
1117 cmdv.append('--no-deref')
1118 cmdv.append(name)
1119 cmdv.append(new)
1120 if old is not None:
1121 cmdv.append(old)
1122 self.update_ref(*cmdv)
1123
1124 def DeleteRef(self, name, old=None):
1125 if not old:
1126 old = self.rev_parse(name)
1127 self.update_ref('-d', name, old)
1128
1129 def rev_list(self, *args):
1130 cmdv = ['rev-list']
1131 cmdv.extend(args)
1132 p = GitCommand(self._project,
1133 cmdv,
1134 bare = self._bare,
1135 capture_stdout = True,
1136 capture_stderr = True)
1137 r = []
1138 for line in p.process.stdout:
1139 r.append(line[:-1])
1140 if p.Wait() != 0:
1141 raise GitError('%s rev-list %s: %s' % (
1142 self._project.name,
1143 str(args),
1144 p.stderr))
1145 return r
1146
1147 def __getattr__(self, name):
1148 name = name.replace('_', '-')
1149 def runner(*args):
1150 cmdv = [name]
1151 cmdv.extend(args)
1152 p = GitCommand(self._project,
1153 cmdv,
1154 bare = self._bare,
1155 capture_stdout = True,
1156 capture_stderr = True)
1157 if p.Wait() != 0:
1158 raise GitError('%s %s: %s' % (
1159 self._project.name,
1160 name,
1161 p.stderr))
1162 r = p.stdout
1163 if r.endswith('\n') and r.index('\n') == len(r) - 1:
1164 return r[:-1]
1165 return r
1166 return runner
1167
1168
1169class MetaProject(Project):
1170 """A special project housed under .repo.
1171 """
1172 def __init__(self, manifest, name, gitdir, worktree):
1173 repodir = manifest.repodir
1174 Project.__init__(self,
1175 manifest = manifest,
1176 name = name,
1177 gitdir = gitdir,
1178 worktree = worktree,
1179 remote = Remote('origin'),
1180 relpath = '.repo/%s' % name,
1181 revision = 'refs/heads/master')
1182
1183 def PreSync(self):
1184 if self.Exists:
1185 cb = self.CurrentBranch
1186 if cb:
1187 base = self.GetBranch(cb).merge
1188 if base:
1189 self.revision = base
1190
1191 @property
1192 def HasChanges(self):
1193 """Has the remote received new commits not yet checked out?
1194 """
1195 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
1196 if self._revlist(not_rev(HEAD), rev):
1197 return True
1198 return False