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