blob: bd35c65e60d2cb19304afd965d96546d5f9cad1c [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:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800181 def __init__(self, src, dest, abssrc, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700182 self.src = src
183 self.dest = dest
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800184 self.abs_src = abssrc
185 self.abs_dest = absdest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700186
187 def _Copy(self):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800188 src = self.abs_src
189 dest = self.abs_dest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700190 # copy file if it does not exist or is out of date
191 if not os.path.exists(dest) or not filecmp.cmp(src, dest):
192 try:
193 # remove existing file first, since it might be read-only
194 if os.path.exists(dest):
195 os.remove(dest)
196 shutil.copy(src, dest)
197 # make the file read-only
198 mode = os.stat(dest)[stat.ST_MODE]
199 mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
200 os.chmod(dest, mode)
201 except IOError:
202 print >>sys.stderr, \
203 'error: Cannot copy file %s to %s' \
204 % (src, dest)
205
206
207class Project(object):
208 def __init__(self,
209 manifest,
210 name,
211 remote,
212 gitdir,
213 worktree,
214 relpath,
215 revision):
216 self.manifest = manifest
217 self.name = name
218 self.remote = remote
219 self.gitdir = gitdir
220 self.worktree = worktree
221 self.relpath = relpath
222 self.revision = revision
223 self.snapshots = {}
224 self.extraRemotes = {}
225 self.copyfiles = []
226 self.config = GitConfig.ForRepository(
227 gitdir = self.gitdir,
228 defaults = self.manifest.globalConfig)
229
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800230 if self.worktree:
231 self.work_git = self._GitGetByExec(self, bare=False)
232 else:
233 self.work_git = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700234 self.bare_git = self._GitGetByExec(self, bare=True)
235
236 @property
237 def Exists(self):
238 return os.path.isdir(self.gitdir)
239
240 @property
241 def CurrentBranch(self):
242 """Obtain the name of the currently checked out branch.
243 The branch name omits the 'refs/heads/' prefix.
244 None is returned if the project is on a detached HEAD.
245 """
246 try:
247 b = self.work_git.GetHead()
248 except GitError:
249 return None
250 if b.startswith(R_HEADS):
251 return b[len(R_HEADS):]
252 return None
253
254 def IsDirty(self, consider_untracked=True):
255 """Is the working directory modified in some way?
256 """
257 self.work_git.update_index('-q',
258 '--unmerged',
259 '--ignore-missing',
260 '--refresh')
261 if self.work_git.DiffZ('diff-index','-M','--cached',HEAD):
262 return True
263 if self.work_git.DiffZ('diff-files'):
264 return True
265 if consider_untracked and self.work_git.LsOthers():
266 return True
267 return False
268
269 _userident_name = None
270 _userident_email = None
271
272 @property
273 def UserName(self):
274 """Obtain the user's personal name.
275 """
276 if self._userident_name is None:
277 self._LoadUserIdentity()
278 return self._userident_name
279
280 @property
281 def UserEmail(self):
282 """Obtain the user's email address. This is very likely
283 to be their Gerrit login.
284 """
285 if self._userident_email is None:
286 self._LoadUserIdentity()
287 return self._userident_email
288
289 def _LoadUserIdentity(self):
290 u = self.bare_git.var('GIT_COMMITTER_IDENT')
291 m = re.compile("^(.*) <([^>]*)> ").match(u)
292 if m:
293 self._userident_name = m.group(1)
294 self._userident_email = m.group(2)
295 else:
296 self._userident_name = ''
297 self._userident_email = ''
298
299 def GetRemote(self, name):
300 """Get the configuration for a single remote.
301 """
302 return self.config.GetRemote(name)
303
304 def GetBranch(self, name):
305 """Get the configuration for a single branch.
306 """
307 return self.config.GetBranch(name)
308
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700309 def GetBranches(self):
310 """Get all existing local branches.
311 """
312 current = self.CurrentBranch
313 all = self.bare_git.ListRefs()
314 heads = {}
315 pubd = {}
316
317 for name, id in all.iteritems():
318 if name.startswith(R_HEADS):
319 name = name[len(R_HEADS):]
320 b = self.GetBranch(name)
321 b.current = name == current
322 b.published = None
323 b.revision = id
324 heads[name] = b
325
326 for name, id in all.iteritems():
327 if name.startswith(R_PUB):
328 name = name[len(R_PUB):]
329 b = heads.get(name)
330 if b:
331 b.published = id
332
333 return heads
334
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700335
336## Status Display ##
337
338 def PrintWorkTreeStatus(self):
339 """Prints the status of the repository to stdout.
340 """
341 if not os.path.isdir(self.worktree):
342 print ''
343 print 'project %s/' % self.relpath
344 print ' missing (run "repo sync")'
345 return
346
347 self.work_git.update_index('-q',
348 '--unmerged',
349 '--ignore-missing',
350 '--refresh')
351 di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD)
352 df = self.work_git.DiffZ('diff-files')
353 do = self.work_git.LsOthers()
354 if not di and not df and not do:
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700355 return 'CLEAN'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700356
357 out = StatusColoring(self.config)
358 out.project('project %-40s', self.relpath + '/')
359
360 branch = self.CurrentBranch
361 if branch is None:
362 out.nobranch('(*** NO BRANCH ***)')
363 else:
364 out.branch('branch %s', branch)
365 out.nl()
366
367 paths = list()
368 paths.extend(di.keys())
369 paths.extend(df.keys())
370 paths.extend(do)
371
372 paths = list(set(paths))
373 paths.sort()
374
375 for p in paths:
376 try: i = di[p]
377 except KeyError: i = None
378
379 try: f = df[p]
380 except KeyError: f = None
381
382 if i: i_status = i.status.upper()
383 else: i_status = '-'
384
385 if f: f_status = f.status.lower()
386 else: f_status = '-'
387
388 if i and i.src_path:
Shawn O. Pearcefe086752009-03-03 13:49:48 -0800389 line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700390 i.src_path, p, i.level)
391 else:
392 line = ' %s%s\t%s' % (i_status, f_status, p)
393
394 if i and not f:
395 out.added('%s', line)
396 elif (i and f) or (not i and f):
397 out.changed('%s', line)
398 elif not i and not f:
399 out.untracked('%s', line)
400 else:
401 out.write('%s', line)
402 out.nl()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700403 return 'DIRTY'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700404
405 def PrintWorkTreeDiff(self):
406 """Prints the status of the repository to stdout.
407 """
408 out = DiffColoring(self.config)
409 cmd = ['diff']
410 if out.is_on:
411 cmd.append('--color')
412 cmd.append(HEAD)
413 cmd.append('--')
414 p = GitCommand(self,
415 cmd,
416 capture_stdout = True,
417 capture_stderr = True)
418 has_diff = False
419 for line in p.process.stdout:
420 if not has_diff:
421 out.nl()
422 out.project('project %s/' % self.relpath)
423 out.nl()
424 has_diff = True
425 print line[:-1]
426 p.Wait()
427
428
429## Publish / Upload ##
430
431 def WasPublished(self, branch):
432 """Was the branch published (uploaded) for code review?
433 If so, returns the SHA-1 hash of the last published
434 state for the branch.
435 """
436 try:
437 return self.bare_git.rev_parse(R_PUB + branch)
438 except GitError:
439 return None
440
441 def CleanPublishedCache(self):
442 """Prunes any stale published refs.
443 """
444 heads = set()
445 canrm = {}
446 for name, id in self._allrefs.iteritems():
447 if name.startswith(R_HEADS):
448 heads.add(name)
449 elif name.startswith(R_PUB):
450 canrm[name] = id
451
452 for name, id in canrm.iteritems():
453 n = name[len(R_PUB):]
454 if R_HEADS + n not in heads:
455 self.bare_git.DeleteRef(name, id)
456
457 def GetUploadableBranches(self):
458 """List any branches which can be uploaded for review.
459 """
460 heads = {}
461 pubed = {}
462
463 for name, id in self._allrefs.iteritems():
464 if name.startswith(R_HEADS):
465 heads[name[len(R_HEADS):]] = id
466 elif name.startswith(R_PUB):
467 pubed[name[len(R_PUB):]] = id
468
469 ready = []
470 for branch, id in heads.iteritems():
471 if branch in pubed and pubed[branch] == id:
472 continue
473
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800474 rb = self.GetUploadableBranch(branch)
475 if rb:
476 ready.append(rb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700477 return ready
478
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800479 def GetUploadableBranch(self, branch_name):
480 """Get a single uploadable branch, or None.
481 """
482 branch = self.GetBranch(branch_name)
483 base = branch.LocalMerge
484 if branch.LocalMerge:
485 rb = ReviewableBranch(self, branch, base)
486 if rb.commits:
487 return rb
488 return None
489
Joe Onorato2896a792008-11-17 16:56:36 -0500490 def UploadForReview(self, branch=None, replace_changes=None, people=([],[])):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700491 """Uploads the named branch for code review.
492 """
493 if branch is None:
494 branch = self.CurrentBranch
495 if branch is None:
496 raise GitError('not currently on a branch')
497
498 branch = self.GetBranch(branch)
499 if not branch.LocalMerge:
500 raise GitError('branch %s does not track a remote' % branch.name)
501 if not branch.remote.review:
502 raise GitError('remote %s has no review url' % branch.remote.name)
503
504 dest_branch = branch.merge
505 if not dest_branch.startswith(R_HEADS):
506 dest_branch = R_HEADS + dest_branch
507
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800508 if not branch.remote.projectname:
509 branch.remote.projectname = self.name
510 branch.remote.Save()
511
Shawn O. Pearce370e3fa2009-01-26 10:55:39 -0800512 if branch.remote.ReviewProtocol == 'ssh':
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800513 if dest_branch.startswith(R_HEADS):
514 dest_branch = dest_branch[len(R_HEADS):]
515
516 rp = ['gerrit receive-pack']
517 for e in people[0]:
518 rp.append('--reviewer=%s' % sq(e))
519 for e in people[1]:
520 rp.append('--cc=%s' % sq(e))
521
522 cmd = ['push']
523 cmd.append('--receive-pack=%s' % " ".join(rp))
524 cmd.append(branch.remote.SshReviewUrl(self.UserEmail))
525 cmd.append('%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch))
526 if replace_changes:
527 for change_id,commit_id in replace_changes.iteritems():
528 cmd.append('%s:refs/changes/%s/new' % (commit_id, change_id))
529 if GitCommand(self, cmd, bare = True).Wait() != 0:
530 raise UploadError('Upload failed')
531
532 else:
533 raise UploadError('Unsupported protocol %s' \
534 % branch.remote.review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700535
536 msg = "posted to %s for %s" % (branch.remote.review, dest_branch)
537 self.bare_git.UpdateRef(R_PUB + branch.name,
538 R_HEADS + branch.name,
539 message = msg)
540
541
542## Sync ##
543
544 def Sync_NetworkHalf(self):
545 """Perform only the network IO portion of the sync process.
546 Local working directory/branch state is not affected.
547 """
548 if not self.Exists:
549 print >>sys.stderr
550 print >>sys.stderr, 'Initializing project %s ...' % self.name
551 self._InitGitDir()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800552
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700553 self._InitRemote()
554 for r in self.extraRemotes.values():
555 if not self._RemoteFetch(r.name):
556 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700557 if not self._RemoteFetch():
558 return False
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800559
560 if self.worktree:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800561 self._InitMRef()
562 else:
563 self._InitMirrorHead()
564 try:
565 os.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
566 except OSError:
567 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700568 return True
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800569
570 def PostRepoUpgrade(self):
571 self._InitHooks()
572
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700573 def _CopyFiles(self):
574 for file in self.copyfiles:
575 file._Copy()
576
Shawn O. Pearce3e768c92009-04-10 16:59:36 -0700577 def Sync_LocalHalf(self, detach_head=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700578 """Perform only the local IO portion of the sync process.
579 Network access is not required.
580
581 Return:
582 True: the sync was successful
583 False: the sync requires user input
584 """
585 self._InitWorkTree()
586 self.CleanPublishedCache()
587
588 rem = self.GetRemote(self.remote.name)
589 rev = rem.ToLocal(self.revision)
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800590 try:
591 self.bare_git.rev_parse('--verify', '%s^0' % rev)
592 except GitError:
593 raise ManifestInvalidRevisionError(
594 'revision %s in %s not found' % (self.revision, self.name))
595
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700596 branch = self.CurrentBranch
597
Shawn O. Pearce3e768c92009-04-10 16:59:36 -0700598 if branch is None or detach_head:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700599 # Currently on a detached HEAD. The user is assumed to
600 # not have any local modifications worth worrying about.
601 #
602 lost = self._revlist(not_rev(rev), HEAD)
603 if lost:
604 _info("[%s] Discarding %d commits", self.name, len(lost))
605 try:
606 self._Checkout(rev, quiet=True)
607 except GitError:
608 return False
609 self._CopyFiles()
610 return True
611
612 branch = self.GetBranch(branch)
613 merge = branch.LocalMerge
614
615 if not merge:
616 # The current branch has no tracking configuration.
617 # Jump off it to a deatched HEAD.
618 #
619 _info("[%s] Leaving %s"
620 " (does not track any upstream)",
621 self.name,
622 branch.name)
623 try:
624 self._Checkout(rev, quiet=True)
625 except GitError:
626 return False
627 self._CopyFiles()
628 return True
629
630 upstream_gain = self._revlist(not_rev(HEAD), rev)
631 pub = self.WasPublished(branch.name)
632 if pub:
633 not_merged = self._revlist(not_rev(rev), pub)
634 if not_merged:
635 if upstream_gain:
636 # The user has published this branch and some of those
637 # commits are not yet merged upstream. We do not want
638 # to rewrite the published commits so we punt.
639 #
640 _info("[%s] Branch %s is published,"
641 " but is now %d commits behind.",
642 self.name, branch.name, len(upstream_gain))
643 _info("[%s] Consider merging or rebasing the"
644 " unpublished commits.", self.name)
645 return True
Shawn O. Pearce23d77812008-10-30 11:06:57 -0700646 elif upstream_gain:
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700647 # We can fast-forward safely.
648 #
649 try:
650 self._FastForward(rev)
651 except GitError:
652 return False
653 self._CopyFiles()
654 return True
Shawn O. Pearce23d77812008-10-30 11:06:57 -0700655 else:
656 # Trivially no changes in the upstream.
657 #
658 return True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700659
660 if merge == rev:
661 try:
662 old_merge = self.bare_git.rev_parse('%s@{1}' % merge)
663 except GitError:
664 old_merge = merge
Shawn O. Pearce07346002008-10-21 07:09:27 -0700665 if old_merge == '0000000000000000000000000000000000000000' \
666 or old_merge == '':
667 old_merge = merge
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700668 else:
669 # The upstream switched on us. Time to cross our fingers
670 # and pray that the old upstream also wasn't in the habit
671 # of rebasing itself.
672 #
673 _info("[%s] Manifest switched from %s to %s",
674 self.name, merge, rev)
675 old_merge = merge
676
677 if rev == old_merge:
678 upstream_lost = []
679 else:
680 upstream_lost = self._revlist(not_rev(rev), old_merge)
681
682 if not upstream_lost and not upstream_gain:
683 # Trivially no changes caused by the upstream.
684 #
685 return True
686
687 if self.IsDirty(consider_untracked=False):
688 _warn('[%s] commit (or discard) uncommitted changes'
689 ' before sync', self.name)
690 return False
691
692 if upstream_lost:
693 # Upstream rebased. Not everything in HEAD
694 # may have been caused by the user.
695 #
696 _info("[%s] Discarding %d commits removed from upstream",
697 self.name, len(upstream_lost))
698
699 branch.remote = rem
700 branch.merge = self.revision
701 branch.Save()
702
703 my_changes = self._revlist(not_rev(old_merge), HEAD)
704 if my_changes:
705 try:
706 self._Rebase(upstream = old_merge, onto = rev)
707 except GitError:
708 return False
709 elif upstream_lost:
710 try:
711 self._ResetHard(rev)
712 except GitError:
713 return False
714 else:
715 try:
716 self._FastForward(rev)
717 except GitError:
718 return False
719
720 self._CopyFiles()
721 return True
722
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800723 def AddCopyFile(self, src, dest, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700724 # dest should already be an absolute path, but src is project relative
725 # make src an absolute path
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800726 abssrc = os.path.join(self.worktree, src)
727 self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700728
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700729 def DownloadPatchSet(self, change_id, patch_id):
730 """Download a single patch set of a single change to FETCH_HEAD.
731 """
732 remote = self.GetRemote(self.remote.name)
733
734 cmd = ['fetch', remote.name]
735 cmd.append('refs/changes/%2.2d/%d/%d' \
736 % (change_id % 100, change_id, patch_id))
737 cmd.extend(map(lambda x: str(x), remote.fetch))
738 if GitCommand(self, cmd, bare=True).Wait() != 0:
739 return None
740 return DownloadedChange(self,
741 remote.ToLocal(self.revision),
742 change_id,
743 patch_id,
744 self.bare_git.rev_parse('FETCH_HEAD'))
745
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700746
747## Branch Management ##
748
749 def StartBranch(self, name):
750 """Create a new branch off the manifest's revision.
751 """
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700752 try:
753 self.bare_git.rev_parse(R_HEADS + name)
754 exists = True
755 except GitError:
756 exists = False;
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700757
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700758 if exists:
759 if name == self.CurrentBranch:
760 return True
761 else:
762 cmd = ['checkout', name, '--']
763 return GitCommand(self, cmd).Wait() == 0
764
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700765 else:
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700766 branch = self.GetBranch(name)
767 branch.remote = self.GetRemote(self.remote.name)
768 branch.merge = self.revision
769
770 rev = branch.LocalMerge
771 cmd = ['checkout', '-b', branch.name, rev]
772 if GitCommand(self, cmd).Wait() == 0:
773 branch.Save()
774 return True
775 else:
776 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700777
Wink Saville02d79452009-04-10 13:01:24 -0700778 def CheckoutBranch(self, name):
779 """Checkout a local topic branch.
780 """
781
782 # Be sure the branch exists
783 try:
784 tip_rev = self.bare_git.rev_parse(R_HEADS + name)
785 except GitError:
786 return False;
787
788 # Do the checkout
789 cmd = ['checkout', name, '--']
Shawn O. Pearce2675c3f2009-04-10 16:20:25 -0700790 return GitCommand(self, cmd).Wait() == 0
Wink Saville02d79452009-04-10 13:01:24 -0700791
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800792 def AbandonBranch(self, name):
793 """Destroy a local topic branch.
794 """
795 try:
796 tip_rev = self.bare_git.rev_parse(R_HEADS + name)
797 except GitError:
798 return
799
800 if self.CurrentBranch == name:
801 self._Checkout(
802 self.GetRemote(self.remote.name).ToLocal(self.revision),
803 quiet=True)
804
805 cmd = ['branch', '-D', name]
806 GitCommand(self, cmd, capture_stdout=True).Wait()
807
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700808 def PruneHeads(self):
809 """Prune any topic branches already merged into upstream.
810 """
811 cb = self.CurrentBranch
812 kill = []
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800813 left = self._allrefs
814 for name in left.keys():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700815 if name.startswith(R_HEADS):
816 name = name[len(R_HEADS):]
817 if cb is None or name != cb:
818 kill.append(name)
819
820 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
821 if cb is not None \
822 and not self._revlist(HEAD + '...' + rev) \
823 and not self.IsDirty(consider_untracked = False):
824 self.work_git.DetachHead(HEAD)
825 kill.append(cb)
826
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700827 if kill:
828 try:
829 old = self.bare_git.GetHead()
830 except GitError:
831 old = 'refs/heads/please_never_use_this_as_a_branch_name'
832
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700833 try:
834 self.bare_git.DetachHead(rev)
835
836 b = ['branch', '-d']
837 b.extend(kill)
838 b = GitCommand(self, b, bare=True,
839 capture_stdout=True,
840 capture_stderr=True)
841 b.Wait()
842 finally:
843 self.bare_git.SetHead(old)
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800844 left = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700845
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800846 for branch in kill:
847 if (R_HEADS + branch) not in left:
848 self.CleanPublishedCache()
849 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700850
851 if cb and cb not in kill:
852 kill.append(cb)
Shawn O. Pearce7c6c64d2009-03-02 12:38:13 -0800853 kill.sort()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700854
855 kept = []
856 for branch in kill:
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800857 if (R_HEADS + branch) in left:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700858 branch = self.GetBranch(branch)
859 base = branch.LocalMerge
860 if not base:
861 base = rev
862 kept.append(ReviewableBranch(self, branch, base))
863 return kept
864
865
866## Direct Git Commands ##
867
868 def _RemoteFetch(self, name=None):
869 if not name:
870 name = self.remote.name
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800871 cmd = ['fetch']
872 if not self.worktree:
873 cmd.append('--update-head-ok')
874 cmd.append(name)
875 return GitCommand(self, cmd, bare = True).Wait() == 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700876
877 def _Checkout(self, rev, quiet=False):
878 cmd = ['checkout']
879 if quiet:
880 cmd.append('-q')
881 cmd.append(rev)
882 cmd.append('--')
883 if GitCommand(self, cmd).Wait() != 0:
884 if self._allrefs:
885 raise GitError('%s checkout %s ' % (self.name, rev))
886
887 def _ResetHard(self, rev, quiet=True):
888 cmd = ['reset', '--hard']
889 if quiet:
890 cmd.append('-q')
891 cmd.append(rev)
892 if GitCommand(self, cmd).Wait() != 0:
893 raise GitError('%s reset --hard %s ' % (self.name, rev))
894
895 def _Rebase(self, upstream, onto = None):
896 cmd = ['rebase', '-i']
897 if onto is not None:
898 cmd.extend(['--onto', onto])
899 cmd.append(upstream)
900 if GitCommand(self, cmd, disable_editor=True).Wait() != 0:
901 raise GitError('%s rebase %s ' % (self.name, upstream))
902
903 def _FastForward(self, head):
904 cmd = ['merge', head]
905 if GitCommand(self, cmd).Wait() != 0:
906 raise GitError('%s merge %s ' % (self.name, head))
907
908 def _InitGitDir(self):
909 if not os.path.exists(self.gitdir):
910 os.makedirs(self.gitdir)
911 self.bare_git.init()
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -0800912
913 if self.manifest.IsMirror:
914 self.config.SetString('core.bare', 'true')
915 else:
916 self.config.SetString('core.bare', None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700917
918 hooks = self._gitdir_path('hooks')
Shawn O. Pearcede646812008-10-29 14:38:12 -0700919 try:
920 to_rm = os.listdir(hooks)
921 except OSError:
922 to_rm = []
923 for old_hook in to_rm:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700924 os.remove(os.path.join(hooks, old_hook))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800925 self._InitHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700926
927 m = self.manifest.manifestProject.config
928 for key in ['user.name', 'user.email']:
929 if m.Has(key, include_defaults = False):
930 self.config.SetString(key, m.GetString(key))
931
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800932 def _InitHooks(self):
933 hooks = self._gitdir_path('hooks')
934 if not os.path.exists(hooks):
935 os.makedirs(hooks)
936 for stock_hook in repo_hooks():
937 dst = os.path.join(hooks, os.path.basename(stock_hook))
938 try:
939 os.symlink(relpath(stock_hook, dst), dst)
940 except OSError, e:
941 if e.errno == errno.EEXIST:
942 pass
943 elif e.errno == errno.EPERM:
944 raise GitError('filesystem must support symlinks')
945 else:
946 raise
947
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700948 def _InitRemote(self):
949 if self.remote.fetchUrl:
950 remote = self.GetRemote(self.remote.name)
951
952 url = self.remote.fetchUrl
953 while url.endswith('/'):
954 url = url[:-1]
955 url += '/%s.git' % self.name
956 remote.url = url
957 remote.review = self.remote.reviewUrl
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800958 if remote.projectname is None:
959 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700960
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800961 if self.worktree:
962 remote.ResetFetch(mirror=False)
963 else:
964 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700965 remote.Save()
966
967 for r in self.extraRemotes.values():
968 remote = self.GetRemote(r.name)
969 remote.url = r.fetchUrl
970 remote.review = r.reviewUrl
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800971 if r.projectName:
972 remote.projectname = r.projectName
973 elif remote.projectname is None:
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800974 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700975 remote.ResetFetch()
976 remote.Save()
977
978 def _InitMRef(self):
979 if self.manifest.branch:
980 msg = 'manifest set to %s' % self.revision
981 ref = R_M + self.manifest.branch
982
983 if IsId(self.revision):
Marcelo E. Magallon21f73852008-12-31 04:44:37 +0000984 dst = self.revision + '^0'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700985 self.bare_git.UpdateRef(ref, dst, message = msg, detach = True)
986 else:
987 remote = self.GetRemote(self.remote.name)
988 dst = remote.ToLocal(self.revision)
989 self.bare_git.symbolic_ref('-m', msg, ref, dst)
990
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800991 def _InitMirrorHead(self):
992 dst = self.GetRemote(self.remote.name).ToLocal(self.revision)
993 msg = 'manifest set to %s' % self.revision
994 self.bare_git.SetHead(dst, message=msg)
995
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700996 def _InitWorkTree(self):
997 dotgit = os.path.join(self.worktree, '.git')
998 if not os.path.exists(dotgit):
999 os.makedirs(dotgit)
1000
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001001 for name in ['config',
1002 'description',
1003 'hooks',
1004 'info',
1005 'logs',
1006 'objects',
1007 'packed-refs',
1008 'refs',
1009 'rr-cache',
1010 'svn']:
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001011 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001012 src = os.path.join(self.gitdir, name)
1013 dst = os.path.join(dotgit, name)
1014 os.symlink(relpath(src, dst), dst)
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001015 except OSError, e:
1016 if e.errno == errno.EPERM:
1017 raise GitError('filesystem must support symlinks')
1018 else:
1019 raise
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001020
1021 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
1022 rev = self.bare_git.rev_parse('%s^0' % rev)
1023
1024 f = open(os.path.join(dotgit, HEAD), 'wb')
1025 f.write("%s\n" % rev)
1026 f.close()
1027
1028 cmd = ['read-tree', '--reset', '-u']
1029 cmd.append('-v')
1030 cmd.append('HEAD')
1031 if GitCommand(self, cmd).Wait() != 0:
1032 raise GitError("cannot initialize work tree")
1033
1034 def _gitdir_path(self, path):
1035 return os.path.join(self.gitdir, path)
1036
1037 def _revlist(self, *args):
1038 cmd = []
1039 cmd.extend(args)
1040 cmd.append('--')
1041 return self.work_git.rev_list(*args)
1042
1043 @property
1044 def _allrefs(self):
1045 return self.bare_git.ListRefs()
1046
1047 class _GitGetByExec(object):
1048 def __init__(self, project, bare):
1049 self._project = project
1050 self._bare = bare
1051
1052 def ListRefs(self, *args):
1053 cmdv = ['for-each-ref', '--format=%(objectname) %(refname)']
1054 cmdv.extend(args)
1055 p = GitCommand(self._project,
1056 cmdv,
1057 bare = self._bare,
1058 capture_stdout = True,
1059 capture_stderr = True)
1060 r = {}
1061 for line in p.process.stdout:
1062 id, name = line[:-1].split(' ', 2)
1063 r[name] = id
1064 if p.Wait() != 0:
1065 raise GitError('%s for-each-ref %s: %s' % (
1066 self._project.name,
1067 str(args),
1068 p.stderr))
1069 return r
1070
1071 def LsOthers(self):
1072 p = GitCommand(self._project,
1073 ['ls-files',
1074 '-z',
1075 '--others',
1076 '--exclude-standard'],
1077 bare = False,
1078 capture_stdout = True,
1079 capture_stderr = True)
1080 if p.Wait() == 0:
1081 out = p.stdout
1082 if out:
1083 return out[:-1].split("\0")
1084 return []
1085
1086 def DiffZ(self, name, *args):
1087 cmd = [name]
1088 cmd.append('-z')
1089 cmd.extend(args)
1090 p = GitCommand(self._project,
1091 cmd,
1092 bare = False,
1093 capture_stdout = True,
1094 capture_stderr = True)
1095 try:
1096 out = p.process.stdout.read()
1097 r = {}
1098 if out:
1099 out = iter(out[:-1].split('\0'))
1100 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07001101 try:
1102 info = out.next()
1103 path = out.next()
1104 except StopIteration:
1105 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001106
1107 class _Info(object):
1108 def __init__(self, path, omode, nmode, oid, nid, state):
1109 self.path = path
1110 self.src_path = None
1111 self.old_mode = omode
1112 self.new_mode = nmode
1113 self.old_id = oid
1114 self.new_id = nid
1115
1116 if len(state) == 1:
1117 self.status = state
1118 self.level = None
1119 else:
1120 self.status = state[:1]
1121 self.level = state[1:]
1122 while self.level.startswith('0'):
1123 self.level = self.level[1:]
1124
1125 info = info[1:].split(' ')
1126 info =_Info(path, *info)
1127 if info.status in ('R', 'C'):
1128 info.src_path = info.path
1129 info.path = out.next()
1130 r[info.path] = info
1131 return r
1132 finally:
1133 p.Wait()
1134
1135 def GetHead(self):
1136 return self.symbolic_ref(HEAD)
1137
1138 def SetHead(self, ref, message=None):
1139 cmdv = []
1140 if message is not None:
1141 cmdv.extend(['-m', message])
1142 cmdv.append(HEAD)
1143 cmdv.append(ref)
1144 self.symbolic_ref(*cmdv)
1145
1146 def DetachHead(self, new, message=None):
1147 cmdv = ['--no-deref']
1148 if message is not None:
1149 cmdv.extend(['-m', message])
1150 cmdv.append(HEAD)
1151 cmdv.append(new)
1152 self.update_ref(*cmdv)
1153
1154 def UpdateRef(self, name, new, old=None,
1155 message=None,
1156 detach=False):
1157 cmdv = []
1158 if message is not None:
1159 cmdv.extend(['-m', message])
1160 if detach:
1161 cmdv.append('--no-deref')
1162 cmdv.append(name)
1163 cmdv.append(new)
1164 if old is not None:
1165 cmdv.append(old)
1166 self.update_ref(*cmdv)
1167
1168 def DeleteRef(self, name, old=None):
1169 if not old:
1170 old = self.rev_parse(name)
1171 self.update_ref('-d', name, old)
1172
1173 def rev_list(self, *args):
1174 cmdv = ['rev-list']
1175 cmdv.extend(args)
1176 p = GitCommand(self._project,
1177 cmdv,
1178 bare = self._bare,
1179 capture_stdout = True,
1180 capture_stderr = True)
1181 r = []
1182 for line in p.process.stdout:
1183 r.append(line[:-1])
1184 if p.Wait() != 0:
1185 raise GitError('%s rev-list %s: %s' % (
1186 self._project.name,
1187 str(args),
1188 p.stderr))
1189 return r
1190
1191 def __getattr__(self, name):
1192 name = name.replace('_', '-')
1193 def runner(*args):
1194 cmdv = [name]
1195 cmdv.extend(args)
1196 p = GitCommand(self._project,
1197 cmdv,
1198 bare = self._bare,
1199 capture_stdout = True,
1200 capture_stderr = True)
1201 if p.Wait() != 0:
1202 raise GitError('%s %s: %s' % (
1203 self._project.name,
1204 name,
1205 p.stderr))
1206 r = p.stdout
1207 if r.endswith('\n') and r.index('\n') == len(r) - 1:
1208 return r[:-1]
1209 return r
1210 return runner
1211
1212
1213class MetaProject(Project):
1214 """A special project housed under .repo.
1215 """
1216 def __init__(self, manifest, name, gitdir, worktree):
1217 repodir = manifest.repodir
1218 Project.__init__(self,
1219 manifest = manifest,
1220 name = name,
1221 gitdir = gitdir,
1222 worktree = worktree,
1223 remote = Remote('origin'),
1224 relpath = '.repo/%s' % name,
1225 revision = 'refs/heads/master')
1226
1227 def PreSync(self):
1228 if self.Exists:
1229 cb = self.CurrentBranch
1230 if cb:
1231 base = self.GetBranch(cb).merge
1232 if base:
1233 self.revision = base
1234
1235 @property
1236 def HasChanges(self):
1237 """Has the remote received new commits not yet checked out?
1238 """
1239 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
1240 if self._revlist(not_rev(HEAD), rev):
1241 return True
1242 return False