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