blob: 4f560bd6722b1fa0499e23b6e23c93dd9242a95b [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:
355 return
356
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()
403
404 def PrintWorkTreeDiff(self):
405 """Prints the status of the repository to stdout.
406 """
407 out = DiffColoring(self.config)
408 cmd = ['diff']
409 if out.is_on:
410 cmd.append('--color')
411 cmd.append(HEAD)
412 cmd.append('--')
413 p = GitCommand(self,
414 cmd,
415 capture_stdout = True,
416 capture_stderr = True)
417 has_diff = False
418 for line in p.process.stdout:
419 if not has_diff:
420 out.nl()
421 out.project('project %s/' % self.relpath)
422 out.nl()
423 has_diff = True
424 print line[:-1]
425 p.Wait()
426
427
428## Publish / Upload ##
429
430 def WasPublished(self, branch):
431 """Was the branch published (uploaded) for code review?
432 If so, returns the SHA-1 hash of the last published
433 state for the branch.
434 """
435 try:
436 return self.bare_git.rev_parse(R_PUB + branch)
437 except GitError:
438 return None
439
440 def CleanPublishedCache(self):
441 """Prunes any stale published refs.
442 """
443 heads = set()
444 canrm = {}
445 for name, id in self._allrefs.iteritems():
446 if name.startswith(R_HEADS):
447 heads.add(name)
448 elif name.startswith(R_PUB):
449 canrm[name] = id
450
451 for name, id in canrm.iteritems():
452 n = name[len(R_PUB):]
453 if R_HEADS + n not in heads:
454 self.bare_git.DeleteRef(name, id)
455
456 def GetUploadableBranches(self):
457 """List any branches which can be uploaded for review.
458 """
459 heads = {}
460 pubed = {}
461
462 for name, id in self._allrefs.iteritems():
463 if name.startswith(R_HEADS):
464 heads[name[len(R_HEADS):]] = id
465 elif name.startswith(R_PUB):
466 pubed[name[len(R_PUB):]] = id
467
468 ready = []
469 for branch, id in heads.iteritems():
470 if branch in pubed and pubed[branch] == id:
471 continue
472
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800473 rb = self.GetUploadableBranch(branch)
474 if rb:
475 ready.append(rb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700476 return ready
477
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800478 def GetUploadableBranch(self, branch_name):
479 """Get a single uploadable branch, or None.
480 """
481 branch = self.GetBranch(branch_name)
482 base = branch.LocalMerge
483 if branch.LocalMerge:
484 rb = ReviewableBranch(self, branch, base)
485 if rb.commits:
486 return rb
487 return None
488
Joe Onorato2896a792008-11-17 16:56:36 -0500489 def UploadForReview(self, branch=None, replace_changes=None, people=([],[])):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700490 """Uploads the named branch for code review.
491 """
492 if branch is None:
493 branch = self.CurrentBranch
494 if branch is None:
495 raise GitError('not currently on a branch')
496
497 branch = self.GetBranch(branch)
498 if not branch.LocalMerge:
499 raise GitError('branch %s does not track a remote' % branch.name)
500 if not branch.remote.review:
501 raise GitError('remote %s has no review url' % branch.remote.name)
502
503 dest_branch = branch.merge
504 if not dest_branch.startswith(R_HEADS):
505 dest_branch = R_HEADS + dest_branch
506
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800507 if not branch.remote.projectname:
508 branch.remote.projectname = self.name
509 branch.remote.Save()
510
Shawn O. Pearce370e3fa2009-01-26 10:55:39 -0800511 if branch.remote.ReviewProtocol == 'ssh':
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800512 if dest_branch.startswith(R_HEADS):
513 dest_branch = dest_branch[len(R_HEADS):]
514
515 rp = ['gerrit receive-pack']
516 for e in people[0]:
517 rp.append('--reviewer=%s' % sq(e))
518 for e in people[1]:
519 rp.append('--cc=%s' % sq(e))
520
521 cmd = ['push']
522 cmd.append('--receive-pack=%s' % " ".join(rp))
523 cmd.append(branch.remote.SshReviewUrl(self.UserEmail))
524 cmd.append('%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch))
525 if replace_changes:
526 for change_id,commit_id in replace_changes.iteritems():
527 cmd.append('%s:refs/changes/%s/new' % (commit_id, change_id))
528 if GitCommand(self, cmd, bare = True).Wait() != 0:
529 raise UploadError('Upload failed')
530
531 else:
532 raise UploadError('Unsupported protocol %s' \
533 % branch.remote.review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700534
535 msg = "posted to %s for %s" % (branch.remote.review, dest_branch)
536 self.bare_git.UpdateRef(R_PUB + branch.name,
537 R_HEADS + branch.name,
538 message = msg)
539
540
541## Sync ##
542
543 def Sync_NetworkHalf(self):
544 """Perform only the network IO portion of the sync process.
545 Local working directory/branch state is not affected.
546 """
547 if not self.Exists:
548 print >>sys.stderr
549 print >>sys.stderr, 'Initializing project %s ...' % self.name
550 self._InitGitDir()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800551
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700552 self._InitRemote()
553 for r in self.extraRemotes.values():
554 if not self._RemoteFetch(r.name):
555 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700556 if not self._RemoteFetch():
557 return False
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800558
559 if self.worktree:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800560 self._InitMRef()
561 else:
562 self._InitMirrorHead()
563 try:
564 os.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
565 except OSError:
566 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700567 return True
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800568
569 def PostRepoUpgrade(self):
570 self._InitHooks()
571
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700572 def _CopyFiles(self):
573 for file in self.copyfiles:
574 file._Copy()
575
576 def Sync_LocalHalf(self):
577 """Perform only the local IO portion of the sync process.
578 Network access is not required.
579
580 Return:
581 True: the sync was successful
582 False: the sync requires user input
583 """
584 self._InitWorkTree()
585 self.CleanPublishedCache()
586
587 rem = self.GetRemote(self.remote.name)
588 rev = rem.ToLocal(self.revision)
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800589 try:
590 self.bare_git.rev_parse('--verify', '%s^0' % rev)
591 except GitError:
592 raise ManifestInvalidRevisionError(
593 'revision %s in %s not found' % (self.revision, self.name))
594
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700595 branch = self.CurrentBranch
596
597 if branch is None:
598 # Currently on a detached HEAD. The user is assumed to
599 # not have any local modifications worth worrying about.
600 #
601 lost = self._revlist(not_rev(rev), HEAD)
602 if lost:
603 _info("[%s] Discarding %d commits", self.name, len(lost))
604 try:
605 self._Checkout(rev, quiet=True)
606 except GitError:
607 return False
608 self._CopyFiles()
609 return True
610
611 branch = self.GetBranch(branch)
612 merge = branch.LocalMerge
613
614 if not merge:
615 # The current branch has no tracking configuration.
616 # Jump off it to a deatched HEAD.
617 #
618 _info("[%s] Leaving %s"
619 " (does not track any upstream)",
620 self.name,
621 branch.name)
622 try:
623 self._Checkout(rev, quiet=True)
624 except GitError:
625 return False
626 self._CopyFiles()
627 return True
628
629 upstream_gain = self._revlist(not_rev(HEAD), rev)
630 pub = self.WasPublished(branch.name)
631 if pub:
632 not_merged = self._revlist(not_rev(rev), pub)
633 if not_merged:
634 if upstream_gain:
635 # The user has published this branch and some of those
636 # commits are not yet merged upstream. We do not want
637 # to rewrite the published commits so we punt.
638 #
639 _info("[%s] Branch %s is published,"
640 " but is now %d commits behind.",
641 self.name, branch.name, len(upstream_gain))
642 _info("[%s] Consider merging or rebasing the"
643 " unpublished commits.", self.name)
644 return True
Shawn O. Pearce23d77812008-10-30 11:06:57 -0700645 elif upstream_gain:
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700646 # We can fast-forward safely.
647 #
648 try:
649 self._FastForward(rev)
650 except GitError:
651 return False
652 self._CopyFiles()
653 return True
Shawn O. Pearce23d77812008-10-30 11:06:57 -0700654 else:
655 # Trivially no changes in the upstream.
656 #
657 return True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700658
659 if merge == rev:
660 try:
661 old_merge = self.bare_git.rev_parse('%s@{1}' % merge)
662 except GitError:
663 old_merge = merge
Shawn O. Pearce07346002008-10-21 07:09:27 -0700664 if old_merge == '0000000000000000000000000000000000000000' \
665 or old_merge == '':
666 old_merge = merge
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700667 else:
668 # The upstream switched on us. Time to cross our fingers
669 # and pray that the old upstream also wasn't in the habit
670 # of rebasing itself.
671 #
672 _info("[%s] Manifest switched from %s to %s",
673 self.name, merge, rev)
674 old_merge = merge
675
676 if rev == old_merge:
677 upstream_lost = []
678 else:
679 upstream_lost = self._revlist(not_rev(rev), old_merge)
680
681 if not upstream_lost and not upstream_gain:
682 # Trivially no changes caused by the upstream.
683 #
684 return True
685
686 if self.IsDirty(consider_untracked=False):
687 _warn('[%s] commit (or discard) uncommitted changes'
688 ' before sync', self.name)
689 return False
690
691 if upstream_lost:
692 # Upstream rebased. Not everything in HEAD
693 # may have been caused by the user.
694 #
695 _info("[%s] Discarding %d commits removed from upstream",
696 self.name, len(upstream_lost))
697
698 branch.remote = rem
699 branch.merge = self.revision
700 branch.Save()
701
702 my_changes = self._revlist(not_rev(old_merge), HEAD)
703 if my_changes:
704 try:
705 self._Rebase(upstream = old_merge, onto = rev)
706 except GitError:
707 return False
708 elif upstream_lost:
709 try:
710 self._ResetHard(rev)
711 except GitError:
712 return False
713 else:
714 try:
715 self._FastForward(rev)
716 except GitError:
717 return False
718
719 self._CopyFiles()
720 return True
721
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800722 def AddCopyFile(self, src, dest, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700723 # dest should already be an absolute path, but src is project relative
724 # make src an absolute path
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800725 abssrc = os.path.join(self.worktree, src)
726 self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700727
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700728 def DownloadPatchSet(self, change_id, patch_id):
729 """Download a single patch set of a single change to FETCH_HEAD.
730 """
731 remote = self.GetRemote(self.remote.name)
732
733 cmd = ['fetch', remote.name]
734 cmd.append('refs/changes/%2.2d/%d/%d' \
735 % (change_id % 100, change_id, patch_id))
736 cmd.extend(map(lambda x: str(x), remote.fetch))
737 if GitCommand(self, cmd, bare=True).Wait() != 0:
738 return None
739 return DownloadedChange(self,
740 remote.ToLocal(self.revision),
741 change_id,
742 patch_id,
743 self.bare_git.rev_parse('FETCH_HEAD'))
744
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700745
746## Branch Management ##
747
748 def StartBranch(self, name):
749 """Create a new branch off the manifest's revision.
750 """
751 branch = self.GetBranch(name)
752 branch.remote = self.GetRemote(self.remote.name)
753 branch.merge = self.revision
754
755 rev = branch.LocalMerge
756 cmd = ['checkout', '-b', branch.name, rev]
757 if GitCommand(self, cmd).Wait() == 0:
758 branch.Save()
759 else:
760 raise GitError('%s checkout %s ' % (self.name, rev))
761
Wink Saville02d79452009-04-10 13:01:24 -0700762 def CheckoutBranch(self, name):
763 """Checkout a local topic branch.
764 """
765
766 # Be sure the branch exists
767 try:
768 tip_rev = self.bare_git.rev_parse(R_HEADS + name)
769 except GitError:
770 return False;
771
772 # Do the checkout
773 cmd = ['checkout', name, '--']
774 return GitCommand(self, cmd, capture_stdout=True).Wait() == 0
775
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800776 def AbandonBranch(self, name):
777 """Destroy a local topic branch.
778 """
779 try:
780 tip_rev = self.bare_git.rev_parse(R_HEADS + name)
781 except GitError:
782 return
783
784 if self.CurrentBranch == name:
785 self._Checkout(
786 self.GetRemote(self.remote.name).ToLocal(self.revision),
787 quiet=True)
788
789 cmd = ['branch', '-D', name]
790 GitCommand(self, cmd, capture_stdout=True).Wait()
791
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700792 def PruneHeads(self):
793 """Prune any topic branches already merged into upstream.
794 """
795 cb = self.CurrentBranch
796 kill = []
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800797 left = self._allrefs
798 for name in left.keys():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700799 if name.startswith(R_HEADS):
800 name = name[len(R_HEADS):]
801 if cb is None or name != cb:
802 kill.append(name)
803
804 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
805 if cb is not None \
806 and not self._revlist(HEAD + '...' + rev) \
807 and not self.IsDirty(consider_untracked = False):
808 self.work_git.DetachHead(HEAD)
809 kill.append(cb)
810
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700811 if kill:
812 try:
813 old = self.bare_git.GetHead()
814 except GitError:
815 old = 'refs/heads/please_never_use_this_as_a_branch_name'
816
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700817 try:
818 self.bare_git.DetachHead(rev)
819
820 b = ['branch', '-d']
821 b.extend(kill)
822 b = GitCommand(self, b, bare=True,
823 capture_stdout=True,
824 capture_stderr=True)
825 b.Wait()
826 finally:
827 self.bare_git.SetHead(old)
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800828 left = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700829
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800830 for branch in kill:
831 if (R_HEADS + branch) not in left:
832 self.CleanPublishedCache()
833 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700834
835 if cb and cb not in kill:
836 kill.append(cb)
Shawn O. Pearce7c6c64d2009-03-02 12:38:13 -0800837 kill.sort()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700838
839 kept = []
840 for branch in kill:
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800841 if (R_HEADS + branch) in left:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700842 branch = self.GetBranch(branch)
843 base = branch.LocalMerge
844 if not base:
845 base = rev
846 kept.append(ReviewableBranch(self, branch, base))
847 return kept
848
849
850## Direct Git Commands ##
851
852 def _RemoteFetch(self, name=None):
853 if not name:
854 name = self.remote.name
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800855 cmd = ['fetch']
856 if not self.worktree:
857 cmd.append('--update-head-ok')
858 cmd.append(name)
859 return GitCommand(self, cmd, bare = True).Wait() == 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700860
861 def _Checkout(self, rev, quiet=False):
862 cmd = ['checkout']
863 if quiet:
864 cmd.append('-q')
865 cmd.append(rev)
866 cmd.append('--')
867 if GitCommand(self, cmd).Wait() != 0:
868 if self._allrefs:
869 raise GitError('%s checkout %s ' % (self.name, rev))
870
871 def _ResetHard(self, rev, quiet=True):
872 cmd = ['reset', '--hard']
873 if quiet:
874 cmd.append('-q')
875 cmd.append(rev)
876 if GitCommand(self, cmd).Wait() != 0:
877 raise GitError('%s reset --hard %s ' % (self.name, rev))
878
879 def _Rebase(self, upstream, onto = None):
880 cmd = ['rebase', '-i']
881 if onto is not None:
882 cmd.extend(['--onto', onto])
883 cmd.append(upstream)
884 if GitCommand(self, cmd, disable_editor=True).Wait() != 0:
885 raise GitError('%s rebase %s ' % (self.name, upstream))
886
887 def _FastForward(self, head):
888 cmd = ['merge', head]
889 if GitCommand(self, cmd).Wait() != 0:
890 raise GitError('%s merge %s ' % (self.name, head))
891
892 def _InitGitDir(self):
893 if not os.path.exists(self.gitdir):
894 os.makedirs(self.gitdir)
895 self.bare_git.init()
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -0800896
897 if self.manifest.IsMirror:
898 self.config.SetString('core.bare', 'true')
899 else:
900 self.config.SetString('core.bare', None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700901
902 hooks = self._gitdir_path('hooks')
Shawn O. Pearcede646812008-10-29 14:38:12 -0700903 try:
904 to_rm = os.listdir(hooks)
905 except OSError:
906 to_rm = []
907 for old_hook in to_rm:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700908 os.remove(os.path.join(hooks, old_hook))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800909 self._InitHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700910
911 m = self.manifest.manifestProject.config
912 for key in ['user.name', 'user.email']:
913 if m.Has(key, include_defaults = False):
914 self.config.SetString(key, m.GetString(key))
915
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800916 def _InitHooks(self):
917 hooks = self._gitdir_path('hooks')
918 if not os.path.exists(hooks):
919 os.makedirs(hooks)
920 for stock_hook in repo_hooks():
921 dst = os.path.join(hooks, os.path.basename(stock_hook))
922 try:
923 os.symlink(relpath(stock_hook, dst), dst)
924 except OSError, e:
925 if e.errno == errno.EEXIST:
926 pass
927 elif e.errno == errno.EPERM:
928 raise GitError('filesystem must support symlinks')
929 else:
930 raise
931
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700932 def _InitRemote(self):
933 if self.remote.fetchUrl:
934 remote = self.GetRemote(self.remote.name)
935
936 url = self.remote.fetchUrl
937 while url.endswith('/'):
938 url = url[:-1]
939 url += '/%s.git' % self.name
940 remote.url = url
941 remote.review = self.remote.reviewUrl
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800942 if remote.projectname is None:
943 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700944
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800945 if self.worktree:
946 remote.ResetFetch(mirror=False)
947 else:
948 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700949 remote.Save()
950
951 for r in self.extraRemotes.values():
952 remote = self.GetRemote(r.name)
953 remote.url = r.fetchUrl
954 remote.review = r.reviewUrl
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800955 if r.projectName:
956 remote.projectname = r.projectName
957 elif remote.projectname is None:
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800958 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700959 remote.ResetFetch()
960 remote.Save()
961
962 def _InitMRef(self):
963 if self.manifest.branch:
964 msg = 'manifest set to %s' % self.revision
965 ref = R_M + self.manifest.branch
966
967 if IsId(self.revision):
Marcelo E. Magallon21f73852008-12-31 04:44:37 +0000968 dst = self.revision + '^0'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700969 self.bare_git.UpdateRef(ref, dst, message = msg, detach = True)
970 else:
971 remote = self.GetRemote(self.remote.name)
972 dst = remote.ToLocal(self.revision)
973 self.bare_git.symbolic_ref('-m', msg, ref, dst)
974
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800975 def _InitMirrorHead(self):
976 dst = self.GetRemote(self.remote.name).ToLocal(self.revision)
977 msg = 'manifest set to %s' % self.revision
978 self.bare_git.SetHead(dst, message=msg)
979
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700980 def _InitWorkTree(self):
981 dotgit = os.path.join(self.worktree, '.git')
982 if not os.path.exists(dotgit):
983 os.makedirs(dotgit)
984
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700985 for name in ['config',
986 'description',
987 'hooks',
988 'info',
989 'logs',
990 'objects',
991 'packed-refs',
992 'refs',
993 'rr-cache',
994 'svn']:
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -0800995 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800996 src = os.path.join(self.gitdir, name)
997 dst = os.path.join(dotgit, name)
998 os.symlink(relpath(src, dst), dst)
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -0800999 except OSError, e:
1000 if e.errno == errno.EPERM:
1001 raise GitError('filesystem must support symlinks')
1002 else:
1003 raise
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001004
1005 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
1006 rev = self.bare_git.rev_parse('%s^0' % rev)
1007
1008 f = open(os.path.join(dotgit, HEAD), 'wb')
1009 f.write("%s\n" % rev)
1010 f.close()
1011
1012 cmd = ['read-tree', '--reset', '-u']
1013 cmd.append('-v')
1014 cmd.append('HEAD')
1015 if GitCommand(self, cmd).Wait() != 0:
1016 raise GitError("cannot initialize work tree")
1017
1018 def _gitdir_path(self, path):
1019 return os.path.join(self.gitdir, path)
1020
1021 def _revlist(self, *args):
1022 cmd = []
1023 cmd.extend(args)
1024 cmd.append('--')
1025 return self.work_git.rev_list(*args)
1026
1027 @property
1028 def _allrefs(self):
1029 return self.bare_git.ListRefs()
1030
1031 class _GitGetByExec(object):
1032 def __init__(self, project, bare):
1033 self._project = project
1034 self._bare = bare
1035
1036 def ListRefs(self, *args):
1037 cmdv = ['for-each-ref', '--format=%(objectname) %(refname)']
1038 cmdv.extend(args)
1039 p = GitCommand(self._project,
1040 cmdv,
1041 bare = self._bare,
1042 capture_stdout = True,
1043 capture_stderr = True)
1044 r = {}
1045 for line in p.process.stdout:
1046 id, name = line[:-1].split(' ', 2)
1047 r[name] = id
1048 if p.Wait() != 0:
1049 raise GitError('%s for-each-ref %s: %s' % (
1050 self._project.name,
1051 str(args),
1052 p.stderr))
1053 return r
1054
1055 def LsOthers(self):
1056 p = GitCommand(self._project,
1057 ['ls-files',
1058 '-z',
1059 '--others',
1060 '--exclude-standard'],
1061 bare = False,
1062 capture_stdout = True,
1063 capture_stderr = True)
1064 if p.Wait() == 0:
1065 out = p.stdout
1066 if out:
1067 return out[:-1].split("\0")
1068 return []
1069
1070 def DiffZ(self, name, *args):
1071 cmd = [name]
1072 cmd.append('-z')
1073 cmd.extend(args)
1074 p = GitCommand(self._project,
1075 cmd,
1076 bare = False,
1077 capture_stdout = True,
1078 capture_stderr = True)
1079 try:
1080 out = p.process.stdout.read()
1081 r = {}
1082 if out:
1083 out = iter(out[:-1].split('\0'))
1084 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07001085 try:
1086 info = out.next()
1087 path = out.next()
1088 except StopIteration:
1089 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001090
1091 class _Info(object):
1092 def __init__(self, path, omode, nmode, oid, nid, state):
1093 self.path = path
1094 self.src_path = None
1095 self.old_mode = omode
1096 self.new_mode = nmode
1097 self.old_id = oid
1098 self.new_id = nid
1099
1100 if len(state) == 1:
1101 self.status = state
1102 self.level = None
1103 else:
1104 self.status = state[:1]
1105 self.level = state[1:]
1106 while self.level.startswith('0'):
1107 self.level = self.level[1:]
1108
1109 info = info[1:].split(' ')
1110 info =_Info(path, *info)
1111 if info.status in ('R', 'C'):
1112 info.src_path = info.path
1113 info.path = out.next()
1114 r[info.path] = info
1115 return r
1116 finally:
1117 p.Wait()
1118
1119 def GetHead(self):
1120 return self.symbolic_ref(HEAD)
1121
1122 def SetHead(self, ref, message=None):
1123 cmdv = []
1124 if message is not None:
1125 cmdv.extend(['-m', message])
1126 cmdv.append(HEAD)
1127 cmdv.append(ref)
1128 self.symbolic_ref(*cmdv)
1129
1130 def DetachHead(self, new, message=None):
1131 cmdv = ['--no-deref']
1132 if message is not None:
1133 cmdv.extend(['-m', message])
1134 cmdv.append(HEAD)
1135 cmdv.append(new)
1136 self.update_ref(*cmdv)
1137
1138 def UpdateRef(self, name, new, old=None,
1139 message=None,
1140 detach=False):
1141 cmdv = []
1142 if message is not None:
1143 cmdv.extend(['-m', message])
1144 if detach:
1145 cmdv.append('--no-deref')
1146 cmdv.append(name)
1147 cmdv.append(new)
1148 if old is not None:
1149 cmdv.append(old)
1150 self.update_ref(*cmdv)
1151
1152 def DeleteRef(self, name, old=None):
1153 if not old:
1154 old = self.rev_parse(name)
1155 self.update_ref('-d', name, old)
1156
1157 def rev_list(self, *args):
1158 cmdv = ['rev-list']
1159 cmdv.extend(args)
1160 p = GitCommand(self._project,
1161 cmdv,
1162 bare = self._bare,
1163 capture_stdout = True,
1164 capture_stderr = True)
1165 r = []
1166 for line in p.process.stdout:
1167 r.append(line[:-1])
1168 if p.Wait() != 0:
1169 raise GitError('%s rev-list %s: %s' % (
1170 self._project.name,
1171 str(args),
1172 p.stderr))
1173 return r
1174
1175 def __getattr__(self, name):
1176 name = name.replace('_', '-')
1177 def runner(*args):
1178 cmdv = [name]
1179 cmdv.extend(args)
1180 p = GitCommand(self._project,
1181 cmdv,
1182 bare = self._bare,
1183 capture_stdout = True,
1184 capture_stderr = True)
1185 if p.Wait() != 0:
1186 raise GitError('%s %s: %s' % (
1187 self._project.name,
1188 name,
1189 p.stderr))
1190 r = p.stdout
1191 if r.endswith('\n') and r.index('\n') == len(r) - 1:
1192 return r[:-1]
1193 return r
1194 return runner
1195
1196
1197class MetaProject(Project):
1198 """A special project housed under .repo.
1199 """
1200 def __init__(self, manifest, name, gitdir, worktree):
1201 repodir = manifest.repodir
1202 Project.__init__(self,
1203 manifest = manifest,
1204 name = name,
1205 gitdir = gitdir,
1206 worktree = worktree,
1207 remote = Remote('origin'),
1208 relpath = '.repo/%s' % name,
1209 revision = 'refs/heads/master')
1210
1211 def PreSync(self):
1212 if self.Exists:
1213 cb = self.CurrentBranch
1214 if cb:
1215 base = self.GetBranch(cb).merge
1216 if base:
1217 self.revision = base
1218
1219 @property
1220 def HasChanges(self):
1221 """Has the remote received new commits not yet checked out?
1222 """
1223 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
1224 if self._revlist(not_rev(HEAD), rev):
1225 return True
1226 return False