blob: 33cb3444047bfa1f24413b15ab31ef532b9c0ad5 [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
Shawn O. Pearce48244782009-04-16 08:25:57 -070037def _error(fmt, *args):
38 msg = fmt % args
39 print >>sys.stderr, 'error: %s' % msg
40
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070041def _warn(fmt, *args):
42 msg = fmt % args
43 print >>sys.stderr, 'warn: %s' % msg
44
45def _info(fmt, *args):
46 msg = fmt % args
47 print >>sys.stderr, 'info: %s' % msg
48
49def not_rev(r):
50 return '^' + r
51
Shawn O. Pearceb54a3922009-01-05 16:18:58 -080052def sq(r):
53 return "'" + r.replace("'", "'\''") + "'"
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080054
55hook_list = None
56def repo_hooks():
57 global hook_list
58 if hook_list is None:
59 d = os.path.abspath(os.path.dirname(__file__))
60 d = os.path.join(d , 'hooks')
61 hook_list = map(lambda x: os.path.join(d, x), os.listdir(d))
62 return hook_list
63
64def relpath(dst, src):
65 src = os.path.dirname(src)
66 top = os.path.commonprefix([dst, src])
67 if top.endswith('/'):
68 top = top[:-1]
69 else:
70 top = os.path.dirname(top)
71
72 tmp = src
73 rel = ''
74 while top != tmp:
75 rel += '../'
76 tmp = os.path.dirname(tmp)
77 return rel + dst[len(top) + 1:]
78
79
Shawn O. Pearce632768b2008-10-23 11:58:52 -070080class DownloadedChange(object):
81 _commit_cache = None
82
83 def __init__(self, project, base, change_id, ps_id, commit):
84 self.project = project
85 self.base = base
86 self.change_id = change_id
87 self.ps_id = ps_id
88 self.commit = commit
89
90 @property
91 def commits(self):
92 if self._commit_cache is None:
93 self._commit_cache = self.project.bare_git.rev_list(
94 '--abbrev=8',
95 '--abbrev-commit',
96 '--pretty=oneline',
97 '--reverse',
98 '--date-order',
99 not_rev(self.base),
100 self.commit,
101 '--')
102 return self._commit_cache
103
104
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700105class ReviewableBranch(object):
106 _commit_cache = None
107
108 def __init__(self, project, branch, base):
109 self.project = project
110 self.branch = branch
111 self.base = base
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800112 self.replace_changes = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700113
114 @property
115 def name(self):
116 return self.branch.name
117
118 @property
119 def commits(self):
120 if self._commit_cache is None:
121 self._commit_cache = self.project.bare_git.rev_list(
122 '--abbrev=8',
123 '--abbrev-commit',
124 '--pretty=oneline',
125 '--reverse',
126 '--date-order',
127 not_rev(self.base),
128 R_HEADS + self.name,
129 '--')
130 return self._commit_cache
131
132 @property
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800133 def unabbrev_commits(self):
134 r = dict()
135 for commit in self.project.bare_git.rev_list(
136 not_rev(self.base),
137 R_HEADS + self.name,
138 '--'):
139 r[commit[0:8]] = commit
140 return r
141
142 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700143 def date(self):
144 return self.project.bare_git.log(
145 '--pretty=format:%cd',
146 '-n', '1',
147 R_HEADS + self.name,
148 '--')
149
Joe Onorato2896a792008-11-17 16:56:36 -0500150 def UploadForReview(self, people):
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800151 self.project.UploadForReview(self.name,
Joe Onorato2896a792008-11-17 16:56:36 -0500152 self.replace_changes,
153 people)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700154
155 @property
156 def tip_url(self):
157 me = self.project.GetBranch(self.name)
158 commit = self.project.bare_git.rev_parse(R_HEADS + self.name)
159 return 'http://%s/r/%s' % (me.remote.review, commit[0:12])
160
Shawn O. Pearce0758d2f2008-10-22 13:13:40 -0700161 @property
162 def owner_email(self):
163 return self.project.UserEmail
164
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700165
166class StatusColoring(Coloring):
167 def __init__(self, config):
168 Coloring.__init__(self, config, 'status')
169 self.project = self.printer('header', attr = 'bold')
170 self.branch = self.printer('header', attr = 'bold')
171 self.nobranch = self.printer('nobranch', fg = 'red')
172
173 self.added = self.printer('added', fg = 'green')
174 self.changed = self.printer('changed', fg = 'red')
175 self.untracked = self.printer('untracked', fg = 'red')
176
177
178class DiffColoring(Coloring):
179 def __init__(self, config):
180 Coloring.__init__(self, config, 'diff')
181 self.project = self.printer('header', attr = 'bold')
182
183
184class _CopyFile:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800185 def __init__(self, src, dest, abssrc, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700186 self.src = src
187 self.dest = dest
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800188 self.abs_src = abssrc
189 self.abs_dest = absdest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700190
191 def _Copy(self):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800192 src = self.abs_src
193 dest = self.abs_dest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700194 # copy file if it does not exist or is out of date
195 if not os.path.exists(dest) or not filecmp.cmp(src, dest):
196 try:
197 # remove existing file first, since it might be read-only
198 if os.path.exists(dest):
199 os.remove(dest)
200 shutil.copy(src, dest)
201 # make the file read-only
202 mode = os.stat(dest)[stat.ST_MODE]
203 mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
204 os.chmod(dest, mode)
205 except IOError:
Shawn O. Pearce48244782009-04-16 08:25:57 -0700206 _error('Cannot copy file %s to %s', src, dest)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700207
208
209class Project(object):
210 def __init__(self,
211 manifest,
212 name,
213 remote,
214 gitdir,
215 worktree,
216 relpath,
217 revision):
218 self.manifest = manifest
219 self.name = name
220 self.remote = remote
221 self.gitdir = gitdir
222 self.worktree = worktree
223 self.relpath = relpath
224 self.revision = revision
225 self.snapshots = {}
226 self.extraRemotes = {}
227 self.copyfiles = []
228 self.config = GitConfig.ForRepository(
229 gitdir = self.gitdir,
230 defaults = self.manifest.globalConfig)
231
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800232 if self.worktree:
233 self.work_git = self._GitGetByExec(self, bare=False)
234 else:
235 self.work_git = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700236 self.bare_git = self._GitGetByExec(self, bare=True)
237
238 @property
239 def Exists(self):
240 return os.path.isdir(self.gitdir)
241
242 @property
243 def CurrentBranch(self):
244 """Obtain the name of the currently checked out branch.
245 The branch name omits the 'refs/heads/' prefix.
246 None is returned if the project is on a detached HEAD.
247 """
248 try:
249 b = self.work_git.GetHead()
250 except GitError:
251 return None
252 if b.startswith(R_HEADS):
253 return b[len(R_HEADS):]
254 return None
255
256 def IsDirty(self, consider_untracked=True):
257 """Is the working directory modified in some way?
258 """
259 self.work_git.update_index('-q',
260 '--unmerged',
261 '--ignore-missing',
262 '--refresh')
263 if self.work_git.DiffZ('diff-index','-M','--cached',HEAD):
264 return True
265 if self.work_git.DiffZ('diff-files'):
266 return True
267 if consider_untracked and self.work_git.LsOthers():
268 return True
269 return False
270
271 _userident_name = None
272 _userident_email = None
273
274 @property
275 def UserName(self):
276 """Obtain the user's personal name.
277 """
278 if self._userident_name is None:
279 self._LoadUserIdentity()
280 return self._userident_name
281
282 @property
283 def UserEmail(self):
284 """Obtain the user's email address. This is very likely
285 to be their Gerrit login.
286 """
287 if self._userident_email is None:
288 self._LoadUserIdentity()
289 return self._userident_email
290
291 def _LoadUserIdentity(self):
292 u = self.bare_git.var('GIT_COMMITTER_IDENT')
293 m = re.compile("^(.*) <([^>]*)> ").match(u)
294 if m:
295 self._userident_name = m.group(1)
296 self._userident_email = m.group(2)
297 else:
298 self._userident_name = ''
299 self._userident_email = ''
300
301 def GetRemote(self, name):
302 """Get the configuration for a single remote.
303 """
304 return self.config.GetRemote(name)
305
306 def GetBranch(self, name):
307 """Get the configuration for a single branch.
308 """
309 return self.config.GetBranch(name)
310
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700311 def GetBranches(self):
312 """Get all existing local branches.
313 """
314 current = self.CurrentBranch
315 all = self.bare_git.ListRefs()
316 heads = {}
317 pubd = {}
318
319 for name, id in all.iteritems():
320 if name.startswith(R_HEADS):
321 name = name[len(R_HEADS):]
322 b = self.GetBranch(name)
323 b.current = name == current
324 b.published = None
325 b.revision = id
326 heads[name] = b
327
328 for name, id in all.iteritems():
329 if name.startswith(R_PUB):
330 name = name[len(R_PUB):]
331 b = heads.get(name)
332 if b:
333 b.published = id
334
335 return heads
336
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700337
338## Status Display ##
339
340 def PrintWorkTreeStatus(self):
341 """Prints the status of the repository to stdout.
342 """
343 if not os.path.isdir(self.worktree):
344 print ''
345 print 'project %s/' % self.relpath
346 print ' missing (run "repo sync")'
347 return
348
349 self.work_git.update_index('-q',
350 '--unmerged',
351 '--ignore-missing',
352 '--refresh')
353 di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD)
354 df = self.work_git.DiffZ('diff-files')
355 do = self.work_git.LsOthers()
356 if not di and not df and not do:
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700357 return 'CLEAN'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700358
359 out = StatusColoring(self.config)
360 out.project('project %-40s', self.relpath + '/')
361
362 branch = self.CurrentBranch
363 if branch is None:
364 out.nobranch('(*** NO BRANCH ***)')
365 else:
366 out.branch('branch %s', branch)
367 out.nl()
368
369 paths = list()
370 paths.extend(di.keys())
371 paths.extend(df.keys())
372 paths.extend(do)
373
374 paths = list(set(paths))
375 paths.sort()
376
377 for p in paths:
378 try: i = di[p]
379 except KeyError: i = None
380
381 try: f = df[p]
382 except KeyError: f = None
383
384 if i: i_status = i.status.upper()
385 else: i_status = '-'
386
387 if f: f_status = f.status.lower()
388 else: f_status = '-'
389
390 if i and i.src_path:
Shawn O. Pearcefe086752009-03-03 13:49:48 -0800391 line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700392 i.src_path, p, i.level)
393 else:
394 line = ' %s%s\t%s' % (i_status, f_status, p)
395
396 if i and not f:
397 out.added('%s', line)
398 elif (i and f) or (not i and f):
399 out.changed('%s', line)
400 elif not i and not f:
401 out.untracked('%s', line)
402 else:
403 out.write('%s', line)
404 out.nl()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700405 return 'DIRTY'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700406
407 def PrintWorkTreeDiff(self):
408 """Prints the status of the repository to stdout.
409 """
410 out = DiffColoring(self.config)
411 cmd = ['diff']
412 if out.is_on:
413 cmd.append('--color')
414 cmd.append(HEAD)
415 cmd.append('--')
416 p = GitCommand(self,
417 cmd,
418 capture_stdout = True,
419 capture_stderr = True)
420 has_diff = False
421 for line in p.process.stdout:
422 if not has_diff:
423 out.nl()
424 out.project('project %s/' % self.relpath)
425 out.nl()
426 has_diff = True
427 print line[:-1]
428 p.Wait()
429
430
431## Publish / Upload ##
432
433 def WasPublished(self, branch):
434 """Was the branch published (uploaded) for code review?
435 If so, returns the SHA-1 hash of the last published
436 state for the branch.
437 """
438 try:
439 return self.bare_git.rev_parse(R_PUB + branch)
440 except GitError:
441 return None
442
443 def CleanPublishedCache(self):
444 """Prunes any stale published refs.
445 """
446 heads = set()
447 canrm = {}
448 for name, id in self._allrefs.iteritems():
449 if name.startswith(R_HEADS):
450 heads.add(name)
451 elif name.startswith(R_PUB):
452 canrm[name] = id
453
454 for name, id in canrm.iteritems():
455 n = name[len(R_PUB):]
456 if R_HEADS + n not in heads:
457 self.bare_git.DeleteRef(name, id)
458
459 def GetUploadableBranches(self):
460 """List any branches which can be uploaded for review.
461 """
462 heads = {}
463 pubed = {}
464
465 for name, id in self._allrefs.iteritems():
466 if name.startswith(R_HEADS):
467 heads[name[len(R_HEADS):]] = id
468 elif name.startswith(R_PUB):
469 pubed[name[len(R_PUB):]] = id
470
471 ready = []
472 for branch, id in heads.iteritems():
473 if branch in pubed and pubed[branch] == id:
474 continue
475
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800476 rb = self.GetUploadableBranch(branch)
477 if rb:
478 ready.append(rb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700479 return ready
480
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800481 def GetUploadableBranch(self, branch_name):
482 """Get a single uploadable branch, or None.
483 """
484 branch = self.GetBranch(branch_name)
485 base = branch.LocalMerge
486 if branch.LocalMerge:
487 rb = ReviewableBranch(self, branch, base)
488 if rb.commits:
489 return rb
490 return None
491
Joe Onorato2896a792008-11-17 16:56:36 -0500492 def UploadForReview(self, branch=None, replace_changes=None, people=([],[])):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700493 """Uploads the named branch for code review.
494 """
495 if branch is None:
496 branch = self.CurrentBranch
497 if branch is None:
498 raise GitError('not currently on a branch')
499
500 branch = self.GetBranch(branch)
501 if not branch.LocalMerge:
502 raise GitError('branch %s does not track a remote' % branch.name)
503 if not branch.remote.review:
504 raise GitError('remote %s has no review url' % branch.remote.name)
505
506 dest_branch = branch.merge
507 if not dest_branch.startswith(R_HEADS):
508 dest_branch = R_HEADS + dest_branch
509
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800510 if not branch.remote.projectname:
511 branch.remote.projectname = self.name
512 branch.remote.Save()
513
Shawn O. Pearce370e3fa2009-01-26 10:55:39 -0800514 if branch.remote.ReviewProtocol == 'ssh':
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800515 if dest_branch.startswith(R_HEADS):
516 dest_branch = dest_branch[len(R_HEADS):]
517
518 rp = ['gerrit receive-pack']
519 for e in people[0]:
520 rp.append('--reviewer=%s' % sq(e))
521 for e in people[1]:
522 rp.append('--cc=%s' % sq(e))
523
524 cmd = ['push']
525 cmd.append('--receive-pack=%s' % " ".join(rp))
526 cmd.append(branch.remote.SshReviewUrl(self.UserEmail))
527 cmd.append('%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch))
528 if replace_changes:
529 for change_id,commit_id in replace_changes.iteritems():
530 cmd.append('%s:refs/changes/%s/new' % (commit_id, change_id))
531 if GitCommand(self, cmd, bare = True).Wait() != 0:
532 raise UploadError('Upload failed')
533
534 else:
535 raise UploadError('Unsupported protocol %s' \
536 % branch.remote.review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700537
538 msg = "posted to %s for %s" % (branch.remote.review, dest_branch)
539 self.bare_git.UpdateRef(R_PUB + branch.name,
540 R_HEADS + branch.name,
541 message = msg)
542
543
544## Sync ##
545
546 def Sync_NetworkHalf(self):
547 """Perform only the network IO portion of the sync process.
548 Local working directory/branch state is not affected.
549 """
550 if not self.Exists:
551 print >>sys.stderr
552 print >>sys.stderr, 'Initializing project %s ...' % self.name
553 self._InitGitDir()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800554
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700555 self._InitRemote()
556 for r in self.extraRemotes.values():
557 if not self._RemoteFetch(r.name):
558 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700559 if not self._RemoteFetch():
560 return False
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800561
562 if self.worktree:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800563 self._InitMRef()
564 else:
565 self._InitMirrorHead()
566 try:
567 os.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
568 except OSError:
569 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700570 return True
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800571
572 def PostRepoUpgrade(self):
573 self._InitHooks()
574
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700575 def _CopyFiles(self):
576 for file in self.copyfiles:
577 file._Copy()
578
Shawn O. Pearce3e768c92009-04-10 16:59:36 -0700579 def Sync_LocalHalf(self, detach_head=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700580 """Perform only the local IO portion of the sync process.
581 Network access is not required.
582
583 Return:
584 True: the sync was successful
585 False: the sync requires user input
586 """
587 self._InitWorkTree()
588 self.CleanPublishedCache()
589
590 rem = self.GetRemote(self.remote.name)
591 rev = rem.ToLocal(self.revision)
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800592 try:
593 self.bare_git.rev_parse('--verify', '%s^0' % rev)
594 except GitError:
595 raise ManifestInvalidRevisionError(
596 'revision %s in %s not found' % (self.revision, self.name))
597
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700598 branch = self.CurrentBranch
599
Shawn O. Pearce3e768c92009-04-10 16:59:36 -0700600 if branch is None or detach_head:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700601 # Currently on a detached HEAD. The user is assumed to
602 # not have any local modifications worth worrying about.
603 #
604 lost = self._revlist(not_rev(rev), HEAD)
605 if lost:
606 _info("[%s] Discarding %d commits", self.name, len(lost))
607 try:
608 self._Checkout(rev, quiet=True)
609 except GitError:
610 return False
611 self._CopyFiles()
612 return True
613
614 branch = self.GetBranch(branch)
615 merge = branch.LocalMerge
616
617 if not merge:
618 # The current branch has no tracking configuration.
619 # Jump off it to a deatched HEAD.
620 #
621 _info("[%s] Leaving %s"
622 " (does not track any upstream)",
623 self.name,
624 branch.name)
625 try:
626 self._Checkout(rev, quiet=True)
627 except GitError:
628 return False
629 self._CopyFiles()
630 return True
631
632 upstream_gain = self._revlist(not_rev(HEAD), rev)
633 pub = self.WasPublished(branch.name)
634 if pub:
635 not_merged = self._revlist(not_rev(rev), pub)
636 if not_merged:
637 if upstream_gain:
638 # The user has published this branch and some of those
639 # commits are not yet merged upstream. We do not want
640 # to rewrite the published commits so we punt.
641 #
642 _info("[%s] Branch %s is published,"
643 " but is now %d commits behind.",
644 self.name, branch.name, len(upstream_gain))
645 _info("[%s] Consider merging or rebasing the"
646 " unpublished commits.", self.name)
647 return True
Shawn O. Pearce23d77812008-10-30 11:06:57 -0700648 elif upstream_gain:
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700649 # We can fast-forward safely.
650 #
651 try:
652 self._FastForward(rev)
653 except GitError:
654 return False
655 self._CopyFiles()
656 return True
Shawn O. Pearce23d77812008-10-30 11:06:57 -0700657 else:
658 # Trivially no changes in the upstream.
659 #
660 return True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700661
662 if merge == rev:
663 try:
664 old_merge = self.bare_git.rev_parse('%s@{1}' % merge)
665 except GitError:
666 old_merge = merge
Shawn O. Pearce07346002008-10-21 07:09:27 -0700667 if old_merge == '0000000000000000000000000000000000000000' \
668 or old_merge == '':
669 old_merge = merge
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700670 else:
671 # The upstream switched on us. Time to cross our fingers
672 # and pray that the old upstream also wasn't in the habit
673 # of rebasing itself.
674 #
675 _info("[%s] Manifest switched from %s to %s",
676 self.name, merge, rev)
677 old_merge = merge
678
679 if rev == old_merge:
680 upstream_lost = []
681 else:
682 upstream_lost = self._revlist(not_rev(rev), old_merge)
683
684 if not upstream_lost and not upstream_gain:
685 # Trivially no changes caused by the upstream.
686 #
687 return True
688
689 if self.IsDirty(consider_untracked=False):
690 _warn('[%s] commit (or discard) uncommitted changes'
691 ' before sync', self.name)
692 return False
693
694 if upstream_lost:
695 # Upstream rebased. Not everything in HEAD
696 # may have been caused by the user.
697 #
698 _info("[%s] Discarding %d commits removed from upstream",
699 self.name, len(upstream_lost))
700
701 branch.remote = rem
702 branch.merge = self.revision
703 branch.Save()
704
705 my_changes = self._revlist(not_rev(old_merge), HEAD)
706 if my_changes:
707 try:
708 self._Rebase(upstream = old_merge, onto = rev)
709 except GitError:
710 return False
711 elif upstream_lost:
712 try:
713 self._ResetHard(rev)
714 except GitError:
715 return False
716 else:
717 try:
718 self._FastForward(rev)
719 except GitError:
720 return False
721
722 self._CopyFiles()
723 return True
724
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800725 def AddCopyFile(self, src, dest, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700726 # dest should already be an absolute path, but src is project relative
727 # make src an absolute path
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800728 abssrc = os.path.join(self.worktree, src)
729 self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700730
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700731 def DownloadPatchSet(self, change_id, patch_id):
732 """Download a single patch set of a single change to FETCH_HEAD.
733 """
734 remote = self.GetRemote(self.remote.name)
735
736 cmd = ['fetch', remote.name]
737 cmd.append('refs/changes/%2.2d/%d/%d' \
738 % (change_id % 100, change_id, patch_id))
739 cmd.extend(map(lambda x: str(x), remote.fetch))
740 if GitCommand(self, cmd, bare=True).Wait() != 0:
741 return None
742 return DownloadedChange(self,
743 remote.ToLocal(self.revision),
744 change_id,
745 patch_id,
746 self.bare_git.rev_parse('FETCH_HEAD'))
747
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700748
749## Branch Management ##
750
751 def StartBranch(self, name):
752 """Create a new branch off the manifest's revision.
753 """
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700754 try:
755 self.bare_git.rev_parse(R_HEADS + name)
756 exists = True
757 except GitError:
758 exists = False;
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700759
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700760 if exists:
761 if name == self.CurrentBranch:
762 return True
763 else:
764 cmd = ['checkout', name, '--']
765 return GitCommand(self, cmd).Wait() == 0
766
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700767 else:
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700768 branch = self.GetBranch(name)
769 branch.remote = self.GetRemote(self.remote.name)
770 branch.merge = self.revision
771
772 rev = branch.LocalMerge
773 cmd = ['checkout', '-b', branch.name, rev]
774 if GitCommand(self, cmd).Wait() == 0:
775 branch.Save()
776 return True
777 else:
778 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700779
Wink Saville02d79452009-04-10 13:01:24 -0700780 def CheckoutBranch(self, name):
781 """Checkout a local topic branch.
782 """
783
784 # Be sure the branch exists
785 try:
786 tip_rev = self.bare_git.rev_parse(R_HEADS + name)
787 except GitError:
788 return False;
789
790 # Do the checkout
791 cmd = ['checkout', name, '--']
Shawn O. Pearce2675c3f2009-04-10 16:20:25 -0700792 return GitCommand(self, cmd).Wait() == 0
Wink Saville02d79452009-04-10 13:01:24 -0700793
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800794 def AbandonBranch(self, name):
795 """Destroy a local topic branch.
796 """
797 try:
798 tip_rev = self.bare_git.rev_parse(R_HEADS + name)
799 except GitError:
800 return
801
802 if self.CurrentBranch == name:
803 self._Checkout(
804 self.GetRemote(self.remote.name).ToLocal(self.revision),
805 quiet=True)
806
807 cmd = ['branch', '-D', name]
808 GitCommand(self, cmd, capture_stdout=True).Wait()
809
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700810 def PruneHeads(self):
811 """Prune any topic branches already merged into upstream.
812 """
813 cb = self.CurrentBranch
814 kill = []
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800815 left = self._allrefs
816 for name in left.keys():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700817 if name.startswith(R_HEADS):
818 name = name[len(R_HEADS):]
819 if cb is None or name != cb:
820 kill.append(name)
821
822 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
823 if cb is not None \
824 and not self._revlist(HEAD + '...' + rev) \
825 and not self.IsDirty(consider_untracked = False):
826 self.work_git.DetachHead(HEAD)
827 kill.append(cb)
828
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700829 if kill:
830 try:
831 old = self.bare_git.GetHead()
832 except GitError:
833 old = 'refs/heads/please_never_use_this_as_a_branch_name'
834
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700835 try:
836 self.bare_git.DetachHead(rev)
837
838 b = ['branch', '-d']
839 b.extend(kill)
840 b = GitCommand(self, b, bare=True,
841 capture_stdout=True,
842 capture_stderr=True)
843 b.Wait()
844 finally:
845 self.bare_git.SetHead(old)
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800846 left = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700847
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800848 for branch in kill:
849 if (R_HEADS + branch) not in left:
850 self.CleanPublishedCache()
851 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700852
853 if cb and cb not in kill:
854 kill.append(cb)
Shawn O. Pearce7c6c64d2009-03-02 12:38:13 -0800855 kill.sort()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700856
857 kept = []
858 for branch in kill:
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800859 if (R_HEADS + branch) in left:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700860 branch = self.GetBranch(branch)
861 base = branch.LocalMerge
862 if not base:
863 base = rev
864 kept.append(ReviewableBranch(self, branch, base))
865 return kept
866
867
868## Direct Git Commands ##
869
870 def _RemoteFetch(self, name=None):
871 if not name:
872 name = self.remote.name
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800873 cmd = ['fetch']
874 if not self.worktree:
875 cmd.append('--update-head-ok')
876 cmd.append(name)
877 return GitCommand(self, cmd, bare = True).Wait() == 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700878
879 def _Checkout(self, rev, quiet=False):
880 cmd = ['checkout']
881 if quiet:
882 cmd.append('-q')
883 cmd.append(rev)
884 cmd.append('--')
885 if GitCommand(self, cmd).Wait() != 0:
886 if self._allrefs:
887 raise GitError('%s checkout %s ' % (self.name, rev))
888
889 def _ResetHard(self, rev, quiet=True):
890 cmd = ['reset', '--hard']
891 if quiet:
892 cmd.append('-q')
893 cmd.append(rev)
894 if GitCommand(self, cmd).Wait() != 0:
895 raise GitError('%s reset --hard %s ' % (self.name, rev))
896
897 def _Rebase(self, upstream, onto = None):
Shawn O. Pearce19a83d82009-04-16 08:14:26 -0700898 cmd = ['rebase']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700899 if onto is not None:
900 cmd.extend(['--onto', onto])
901 cmd.append(upstream)
Shawn O. Pearce19a83d82009-04-16 08:14:26 -0700902 if GitCommand(self, cmd).Wait() != 0:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700903 raise GitError('%s rebase %s ' % (self.name, upstream))
904
905 def _FastForward(self, head):
906 cmd = ['merge', head]
907 if GitCommand(self, cmd).Wait() != 0:
908 raise GitError('%s merge %s ' % (self.name, head))
909
910 def _InitGitDir(self):
911 if not os.path.exists(self.gitdir):
912 os.makedirs(self.gitdir)
913 self.bare_git.init()
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -0800914
915 if self.manifest.IsMirror:
916 self.config.SetString('core.bare', 'true')
917 else:
918 self.config.SetString('core.bare', None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700919
920 hooks = self._gitdir_path('hooks')
Shawn O. Pearcede646812008-10-29 14:38:12 -0700921 try:
922 to_rm = os.listdir(hooks)
923 except OSError:
924 to_rm = []
925 for old_hook in to_rm:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700926 os.remove(os.path.join(hooks, old_hook))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800927 self._InitHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700928
929 m = self.manifest.manifestProject.config
930 for key in ['user.name', 'user.email']:
931 if m.Has(key, include_defaults = False):
932 self.config.SetString(key, m.GetString(key))
933
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800934 def _InitHooks(self):
935 hooks = self._gitdir_path('hooks')
936 if not os.path.exists(hooks):
937 os.makedirs(hooks)
938 for stock_hook in repo_hooks():
939 dst = os.path.join(hooks, os.path.basename(stock_hook))
940 try:
941 os.symlink(relpath(stock_hook, dst), dst)
942 except OSError, e:
943 if e.errno == errno.EEXIST:
944 pass
945 elif e.errno == errno.EPERM:
946 raise GitError('filesystem must support symlinks')
947 else:
948 raise
949
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700950 def _InitRemote(self):
951 if self.remote.fetchUrl:
952 remote = self.GetRemote(self.remote.name)
953
954 url = self.remote.fetchUrl
955 while url.endswith('/'):
956 url = url[:-1]
957 url += '/%s.git' % self.name
958 remote.url = url
959 remote.review = self.remote.reviewUrl
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800960 if remote.projectname is None:
961 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700962
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800963 if self.worktree:
964 remote.ResetFetch(mirror=False)
965 else:
966 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700967 remote.Save()
968
969 for r in self.extraRemotes.values():
970 remote = self.GetRemote(r.name)
971 remote.url = r.fetchUrl
972 remote.review = r.reviewUrl
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800973 if r.projectName:
974 remote.projectname = r.projectName
975 elif remote.projectname is None:
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800976 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700977 remote.ResetFetch()
978 remote.Save()
979
980 def _InitMRef(self):
981 if self.manifest.branch:
982 msg = 'manifest set to %s' % self.revision
983 ref = R_M + self.manifest.branch
984
985 if IsId(self.revision):
Marcelo E. Magallon21f73852008-12-31 04:44:37 +0000986 dst = self.revision + '^0'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700987 self.bare_git.UpdateRef(ref, dst, message = msg, detach = True)
988 else:
989 remote = self.GetRemote(self.remote.name)
990 dst = remote.ToLocal(self.revision)
991 self.bare_git.symbolic_ref('-m', msg, ref, dst)
992
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800993 def _InitMirrorHead(self):
994 dst = self.GetRemote(self.remote.name).ToLocal(self.revision)
995 msg = 'manifest set to %s' % self.revision
996 self.bare_git.SetHead(dst, message=msg)
997
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700998 def _InitWorkTree(self):
999 dotgit = os.path.join(self.worktree, '.git')
1000 if not os.path.exists(dotgit):
1001 os.makedirs(dotgit)
1002
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001003 for name in ['config',
1004 'description',
1005 'hooks',
1006 'info',
1007 'logs',
1008 'objects',
1009 'packed-refs',
1010 'refs',
1011 'rr-cache',
1012 'svn']:
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001013 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001014 src = os.path.join(self.gitdir, name)
1015 dst = os.path.join(dotgit, name)
1016 os.symlink(relpath(src, dst), dst)
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001017 except OSError, e:
1018 if e.errno == errno.EPERM:
1019 raise GitError('filesystem must support symlinks')
1020 else:
1021 raise
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001022
1023 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
1024 rev = self.bare_git.rev_parse('%s^0' % rev)
1025
1026 f = open(os.path.join(dotgit, HEAD), 'wb')
1027 f.write("%s\n" % rev)
1028 f.close()
1029
1030 cmd = ['read-tree', '--reset', '-u']
1031 cmd.append('-v')
1032 cmd.append('HEAD')
1033 if GitCommand(self, cmd).Wait() != 0:
1034 raise GitError("cannot initialize work tree")
1035
1036 def _gitdir_path(self, path):
1037 return os.path.join(self.gitdir, path)
1038
1039 def _revlist(self, *args):
1040 cmd = []
1041 cmd.extend(args)
1042 cmd.append('--')
1043 return self.work_git.rev_list(*args)
1044
1045 @property
1046 def _allrefs(self):
1047 return self.bare_git.ListRefs()
1048
1049 class _GitGetByExec(object):
1050 def __init__(self, project, bare):
1051 self._project = project
1052 self._bare = bare
1053
1054 def ListRefs(self, *args):
1055 cmdv = ['for-each-ref', '--format=%(objectname) %(refname)']
1056 cmdv.extend(args)
1057 p = GitCommand(self._project,
1058 cmdv,
1059 bare = self._bare,
1060 capture_stdout = True,
1061 capture_stderr = True)
1062 r = {}
1063 for line in p.process.stdout:
1064 id, name = line[:-1].split(' ', 2)
1065 r[name] = id
1066 if p.Wait() != 0:
1067 raise GitError('%s for-each-ref %s: %s' % (
1068 self._project.name,
1069 str(args),
1070 p.stderr))
1071 return r
1072
1073 def LsOthers(self):
1074 p = GitCommand(self._project,
1075 ['ls-files',
1076 '-z',
1077 '--others',
1078 '--exclude-standard'],
1079 bare = False,
1080 capture_stdout = True,
1081 capture_stderr = True)
1082 if p.Wait() == 0:
1083 out = p.stdout
1084 if out:
1085 return out[:-1].split("\0")
1086 return []
1087
1088 def DiffZ(self, name, *args):
1089 cmd = [name]
1090 cmd.append('-z')
1091 cmd.extend(args)
1092 p = GitCommand(self._project,
1093 cmd,
1094 bare = False,
1095 capture_stdout = True,
1096 capture_stderr = True)
1097 try:
1098 out = p.process.stdout.read()
1099 r = {}
1100 if out:
1101 out = iter(out[:-1].split('\0'))
1102 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07001103 try:
1104 info = out.next()
1105 path = out.next()
1106 except StopIteration:
1107 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001108
1109 class _Info(object):
1110 def __init__(self, path, omode, nmode, oid, nid, state):
1111 self.path = path
1112 self.src_path = None
1113 self.old_mode = omode
1114 self.new_mode = nmode
1115 self.old_id = oid
1116 self.new_id = nid
1117
1118 if len(state) == 1:
1119 self.status = state
1120 self.level = None
1121 else:
1122 self.status = state[:1]
1123 self.level = state[1:]
1124 while self.level.startswith('0'):
1125 self.level = self.level[1:]
1126
1127 info = info[1:].split(' ')
1128 info =_Info(path, *info)
1129 if info.status in ('R', 'C'):
1130 info.src_path = info.path
1131 info.path = out.next()
1132 r[info.path] = info
1133 return r
1134 finally:
1135 p.Wait()
1136
1137 def GetHead(self):
1138 return self.symbolic_ref(HEAD)
1139
1140 def SetHead(self, ref, message=None):
1141 cmdv = []
1142 if message is not None:
1143 cmdv.extend(['-m', message])
1144 cmdv.append(HEAD)
1145 cmdv.append(ref)
1146 self.symbolic_ref(*cmdv)
1147
1148 def DetachHead(self, new, message=None):
1149 cmdv = ['--no-deref']
1150 if message is not None:
1151 cmdv.extend(['-m', message])
1152 cmdv.append(HEAD)
1153 cmdv.append(new)
1154 self.update_ref(*cmdv)
1155
1156 def UpdateRef(self, name, new, old=None,
1157 message=None,
1158 detach=False):
1159 cmdv = []
1160 if message is not None:
1161 cmdv.extend(['-m', message])
1162 if detach:
1163 cmdv.append('--no-deref')
1164 cmdv.append(name)
1165 cmdv.append(new)
1166 if old is not None:
1167 cmdv.append(old)
1168 self.update_ref(*cmdv)
1169
1170 def DeleteRef(self, name, old=None):
1171 if not old:
1172 old = self.rev_parse(name)
1173 self.update_ref('-d', name, old)
1174
1175 def rev_list(self, *args):
1176 cmdv = ['rev-list']
1177 cmdv.extend(args)
1178 p = GitCommand(self._project,
1179 cmdv,
1180 bare = self._bare,
1181 capture_stdout = True,
1182 capture_stderr = True)
1183 r = []
1184 for line in p.process.stdout:
1185 r.append(line[:-1])
1186 if p.Wait() != 0:
1187 raise GitError('%s rev-list %s: %s' % (
1188 self._project.name,
1189 str(args),
1190 p.stderr))
1191 return r
1192
1193 def __getattr__(self, name):
1194 name = name.replace('_', '-')
1195 def runner(*args):
1196 cmdv = [name]
1197 cmdv.extend(args)
1198 p = GitCommand(self._project,
1199 cmdv,
1200 bare = self._bare,
1201 capture_stdout = True,
1202 capture_stderr = True)
1203 if p.Wait() != 0:
1204 raise GitError('%s %s: %s' % (
1205 self._project.name,
1206 name,
1207 p.stderr))
1208 r = p.stdout
1209 if r.endswith('\n') and r.index('\n') == len(r) - 1:
1210 return r[:-1]
1211 return r
1212 return runner
1213
1214
1215class MetaProject(Project):
1216 """A special project housed under .repo.
1217 """
1218 def __init__(self, manifest, name, gitdir, worktree):
1219 repodir = manifest.repodir
1220 Project.__init__(self,
1221 manifest = manifest,
1222 name = name,
1223 gitdir = gitdir,
1224 worktree = worktree,
1225 remote = Remote('origin'),
1226 relpath = '.repo/%s' % name,
1227 revision = 'refs/heads/master')
1228
1229 def PreSync(self):
1230 if self.Exists:
1231 cb = self.CurrentBranch
1232 if cb:
1233 base = self.GetBranch(cb).merge
1234 if base:
1235 self.revision = base
1236
1237 @property
1238 def HasChanges(self):
1239 """Has the remote received new commits not yet checked out?
1240 """
1241 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
1242 if self._revlist(not_rev(HEAD), rev):
1243 return True
1244 return False