blob: fd3f0b8d8944a377e24ad0d0176fdf46e125c555 [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
Shawn O. Pearced237b692009-04-17 18:49:50 -070031from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -070033def _lwrite(path, content):
34 lock = '%s.lock' % path
35
36 fd = open(lock, 'wb')
37 try:
38 fd.write(content)
39 finally:
40 fd.close()
41
42 try:
43 os.rename(lock, path)
44 except OSError:
45 os.remove(lock)
46 raise
47
Shawn O. Pearce48244782009-04-16 08:25:57 -070048def _error(fmt, *args):
49 msg = fmt % args
50 print >>sys.stderr, 'error: %s' % msg
51
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070052def not_rev(r):
53 return '^' + r
54
Shawn O. Pearceb54a3922009-01-05 16:18:58 -080055def sq(r):
56 return "'" + r.replace("'", "'\''") + "'"
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080057
58hook_list = None
59def repo_hooks():
60 global hook_list
61 if hook_list is None:
62 d = os.path.abspath(os.path.dirname(__file__))
63 d = os.path.join(d , 'hooks')
64 hook_list = map(lambda x: os.path.join(d, x), os.listdir(d))
65 return hook_list
66
67def relpath(dst, src):
68 src = os.path.dirname(src)
69 top = os.path.commonprefix([dst, src])
70 if top.endswith('/'):
71 top = top[:-1]
72 else:
73 top = os.path.dirname(top)
74
75 tmp = src
76 rel = ''
77 while top != tmp:
78 rel += '../'
79 tmp = os.path.dirname(tmp)
80 return rel + dst[len(top) + 1:]
81
82
Shawn O. Pearce632768b2008-10-23 11:58:52 -070083class DownloadedChange(object):
84 _commit_cache = None
85
86 def __init__(self, project, base, change_id, ps_id, commit):
87 self.project = project
88 self.base = base
89 self.change_id = change_id
90 self.ps_id = ps_id
91 self.commit = commit
92
93 @property
94 def commits(self):
95 if self._commit_cache is None:
96 self._commit_cache = self.project.bare_git.rev_list(
97 '--abbrev=8',
98 '--abbrev-commit',
99 '--pretty=oneline',
100 '--reverse',
101 '--date-order',
102 not_rev(self.base),
103 self.commit,
104 '--')
105 return self._commit_cache
106
107
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700108class ReviewableBranch(object):
109 _commit_cache = None
110
111 def __init__(self, project, branch, base):
112 self.project = project
113 self.branch = branch
114 self.base = base
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800115 self.replace_changes = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700116
117 @property
118 def name(self):
119 return self.branch.name
120
121 @property
122 def commits(self):
123 if self._commit_cache is None:
124 self._commit_cache = self.project.bare_git.rev_list(
125 '--abbrev=8',
126 '--abbrev-commit',
127 '--pretty=oneline',
128 '--reverse',
129 '--date-order',
130 not_rev(self.base),
131 R_HEADS + self.name,
132 '--')
133 return self._commit_cache
134
135 @property
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800136 def unabbrev_commits(self):
137 r = dict()
138 for commit in self.project.bare_git.rev_list(
139 not_rev(self.base),
140 R_HEADS + self.name,
141 '--'):
142 r[commit[0:8]] = commit
143 return r
144
145 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700146 def date(self):
147 return self.project.bare_git.log(
148 '--pretty=format:%cd',
149 '-n', '1',
150 R_HEADS + self.name,
151 '--')
152
Joe Onorato2896a792008-11-17 16:56:36 -0500153 def UploadForReview(self, people):
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800154 self.project.UploadForReview(self.name,
Joe Onorato2896a792008-11-17 16:56:36 -0500155 self.replace_changes,
156 people)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700157
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700158
159class StatusColoring(Coloring):
160 def __init__(self, config):
161 Coloring.__init__(self, config, 'status')
162 self.project = self.printer('header', attr = 'bold')
163 self.branch = self.printer('header', attr = 'bold')
164 self.nobranch = self.printer('nobranch', fg = 'red')
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700165 self.important = self.printer('important', fg = 'red')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700166
167 self.added = self.printer('added', fg = 'green')
168 self.changed = self.printer('changed', fg = 'red')
169 self.untracked = self.printer('untracked', fg = 'red')
170
171
172class DiffColoring(Coloring):
173 def __init__(self, config):
174 Coloring.__init__(self, config, 'diff')
175 self.project = self.printer('header', attr = 'bold')
176
177
178class _CopyFile:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800179 def __init__(self, src, dest, abssrc, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700180 self.src = src
181 self.dest = dest
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800182 self.abs_src = abssrc
183 self.abs_dest = absdest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700184
185 def _Copy(self):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800186 src = self.abs_src
187 dest = self.abs_dest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700188 # copy file if it does not exist or is out of date
189 if not os.path.exists(dest) or not filecmp.cmp(src, dest):
190 try:
191 # remove existing file first, since it might be read-only
192 if os.path.exists(dest):
193 os.remove(dest)
194 shutil.copy(src, dest)
195 # make the file read-only
196 mode = os.stat(dest)[stat.ST_MODE]
197 mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
198 os.chmod(dest, mode)
199 except IOError:
Shawn O. Pearce48244782009-04-16 08:25:57 -0700200 _error('Cannot copy file %s to %s', src, dest)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700201
202
203class Project(object):
204 def __init__(self,
205 manifest,
206 name,
207 remote,
208 gitdir,
209 worktree,
210 relpath,
211 revision):
212 self.manifest = manifest
213 self.name = name
214 self.remote = remote
215 self.gitdir = gitdir
216 self.worktree = worktree
217 self.relpath = relpath
218 self.revision = revision
219 self.snapshots = {}
220 self.extraRemotes = {}
221 self.copyfiles = []
222 self.config = GitConfig.ForRepository(
223 gitdir = self.gitdir,
224 defaults = self.manifest.globalConfig)
225
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800226 if self.worktree:
227 self.work_git = self._GitGetByExec(self, bare=False)
228 else:
229 self.work_git = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700230 self.bare_git = self._GitGetByExec(self, bare=True)
Shawn O. Pearced237b692009-04-17 18:49:50 -0700231 self.bare_ref = GitRefs(gitdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700232
233 @property
234 def Exists(self):
235 return os.path.isdir(self.gitdir)
236
237 @property
238 def CurrentBranch(self):
239 """Obtain the name of the currently checked out branch.
240 The branch name omits the 'refs/heads/' prefix.
241 None is returned if the project is on a detached HEAD.
242 """
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700243 b = self.work_git.GetHead()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700244 if b.startswith(R_HEADS):
245 return b[len(R_HEADS):]
246 return None
247
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700248 def IsRebaseInProgress(self):
249 w = self.worktree
250 g = os.path.join(w, '.git')
251 return os.path.exists(os.path.join(g, 'rebase-apply')) \
252 or os.path.exists(os.path.join(g, 'rebase-merge')) \
253 or os.path.exists(os.path.join(w, '.dotest'))
254
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700255 def IsDirty(self, consider_untracked=True):
256 """Is the working directory modified in some way?
257 """
258 self.work_git.update_index('-q',
259 '--unmerged',
260 '--ignore-missing',
261 '--refresh')
262 if self.work_git.DiffZ('diff-index','-M','--cached',HEAD):
263 return True
264 if self.work_git.DiffZ('diff-files'):
265 return True
266 if consider_untracked and self.work_git.LsOthers():
267 return True
268 return False
269
270 _userident_name = None
271 _userident_email = None
272
273 @property
274 def UserName(self):
275 """Obtain the user's personal name.
276 """
277 if self._userident_name is None:
278 self._LoadUserIdentity()
279 return self._userident_name
280
281 @property
282 def UserEmail(self):
283 """Obtain the user's email address. This is very likely
284 to be their Gerrit login.
285 """
286 if self._userident_email is None:
287 self._LoadUserIdentity()
288 return self._userident_email
289
290 def _LoadUserIdentity(self):
291 u = self.bare_git.var('GIT_COMMITTER_IDENT')
292 m = re.compile("^(.*) <([^>]*)> ").match(u)
293 if m:
294 self._userident_name = m.group(1)
295 self._userident_email = m.group(2)
296 else:
297 self._userident_name = ''
298 self._userident_email = ''
299
300 def GetRemote(self, name):
301 """Get the configuration for a single remote.
302 """
303 return self.config.GetRemote(name)
304
305 def GetBranch(self, name):
306 """Get the configuration for a single branch.
307 """
308 return self.config.GetBranch(name)
309
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700310 def GetBranches(self):
311 """Get all existing local branches.
312 """
313 current = self.CurrentBranch
Shawn O. Pearced237b692009-04-17 18:49:50 -0700314 all = self._allrefs
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700315 heads = {}
316 pubd = {}
317
318 for name, id in all.iteritems():
319 if name.startswith(R_HEADS):
320 name = name[len(R_HEADS):]
321 b = self.GetBranch(name)
322 b.current = name == current
323 b.published = None
324 b.revision = id
325 heads[name] = b
326
327 for name, id in all.iteritems():
328 if name.startswith(R_PUB):
329 name = name[len(R_PUB):]
330 b = heads.get(name)
331 if b:
332 b.published = id
333
334 return heads
335
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700336
337## Status Display ##
338
339 def PrintWorkTreeStatus(self):
340 """Prints the status of the repository to stdout.
341 """
342 if not os.path.isdir(self.worktree):
343 print ''
344 print 'project %s/' % self.relpath
345 print ' missing (run "repo sync")'
346 return
347
348 self.work_git.update_index('-q',
349 '--unmerged',
350 '--ignore-missing',
351 '--refresh')
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700352 rb = self.IsRebaseInProgress()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700353 di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD)
354 df = self.work_git.DiffZ('diff-files')
355 do = self.work_git.LsOthers()
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700356 if not rb and 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
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700369 if rb:
370 out.important('prior sync failed; rebase still in progress')
371 out.nl()
372
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700373 paths = list()
374 paths.extend(di.keys())
375 paths.extend(df.keys())
376 paths.extend(do)
377
378 paths = list(set(paths))
379 paths.sort()
380
381 for p in paths:
382 try: i = di[p]
383 except KeyError: i = None
384
385 try: f = df[p]
386 except KeyError: f = None
387
388 if i: i_status = i.status.upper()
389 else: i_status = '-'
390
391 if f: f_status = f.status.lower()
392 else: f_status = '-'
393
394 if i and i.src_path:
Shawn O. Pearcefe086752009-03-03 13:49:48 -0800395 line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700396 i.src_path, p, i.level)
397 else:
398 line = ' %s%s\t%s' % (i_status, f_status, p)
399
400 if i and not f:
401 out.added('%s', line)
402 elif (i and f) or (not i and f):
403 out.changed('%s', line)
404 elif not i and not f:
405 out.untracked('%s', line)
406 else:
407 out.write('%s', line)
408 out.nl()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700409 return 'DIRTY'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700410
411 def PrintWorkTreeDiff(self):
412 """Prints the status of the repository to stdout.
413 """
414 out = DiffColoring(self.config)
415 cmd = ['diff']
416 if out.is_on:
417 cmd.append('--color')
418 cmd.append(HEAD)
419 cmd.append('--')
420 p = GitCommand(self,
421 cmd,
422 capture_stdout = True,
423 capture_stderr = True)
424 has_diff = False
425 for line in p.process.stdout:
426 if not has_diff:
427 out.nl()
428 out.project('project %s/' % self.relpath)
429 out.nl()
430 has_diff = True
431 print line[:-1]
432 p.Wait()
433
434
435## Publish / Upload ##
436
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700437 def WasPublished(self, branch, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700438 """Was the branch published (uploaded) for code review?
439 If so, returns the SHA-1 hash of the last published
440 state for the branch.
441 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700442 key = R_PUB + branch
443 if all is None:
444 try:
445 return self.bare_git.rev_parse(key)
446 except GitError:
447 return None
448 else:
449 try:
450 return all[key]
451 except KeyError:
452 return None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700453
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700454 def CleanPublishedCache(self, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700455 """Prunes any stale published refs.
456 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700457 if all is None:
458 all = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700459 heads = set()
460 canrm = {}
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700461 for name, id in all.iteritems():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700462 if name.startswith(R_HEADS):
463 heads.add(name)
464 elif name.startswith(R_PUB):
465 canrm[name] = id
466
467 for name, id in canrm.iteritems():
468 n = name[len(R_PUB):]
469 if R_HEADS + n not in heads:
470 self.bare_git.DeleteRef(name, id)
471
472 def GetUploadableBranches(self):
473 """List any branches which can be uploaded for review.
474 """
475 heads = {}
476 pubed = {}
477
478 for name, id in self._allrefs.iteritems():
479 if name.startswith(R_HEADS):
480 heads[name[len(R_HEADS):]] = id
481 elif name.startswith(R_PUB):
482 pubed[name[len(R_PUB):]] = id
483
484 ready = []
485 for branch, id in heads.iteritems():
486 if branch in pubed and pubed[branch] == id:
487 continue
488
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800489 rb = self.GetUploadableBranch(branch)
490 if rb:
491 ready.append(rb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700492 return ready
493
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800494 def GetUploadableBranch(self, branch_name):
495 """Get a single uploadable branch, or None.
496 """
497 branch = self.GetBranch(branch_name)
498 base = branch.LocalMerge
499 if branch.LocalMerge:
500 rb = ReviewableBranch(self, branch, base)
501 if rb.commits:
502 return rb
503 return None
504
Joe Onorato2896a792008-11-17 16:56:36 -0500505 def UploadForReview(self, branch=None, replace_changes=None, people=([],[])):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700506 """Uploads the named branch for code review.
507 """
508 if branch is None:
509 branch = self.CurrentBranch
510 if branch is None:
511 raise GitError('not currently on a branch')
512
513 branch = self.GetBranch(branch)
514 if not branch.LocalMerge:
515 raise GitError('branch %s does not track a remote' % branch.name)
516 if not branch.remote.review:
517 raise GitError('remote %s has no review url' % branch.remote.name)
518
519 dest_branch = branch.merge
520 if not dest_branch.startswith(R_HEADS):
521 dest_branch = R_HEADS + dest_branch
522
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800523 if not branch.remote.projectname:
524 branch.remote.projectname = self.name
525 branch.remote.Save()
526
Shawn O. Pearce370e3fa2009-01-26 10:55:39 -0800527 if branch.remote.ReviewProtocol == 'ssh':
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800528 if dest_branch.startswith(R_HEADS):
529 dest_branch = dest_branch[len(R_HEADS):]
530
531 rp = ['gerrit receive-pack']
532 for e in people[0]:
533 rp.append('--reviewer=%s' % sq(e))
534 for e in people[1]:
535 rp.append('--cc=%s' % sq(e))
536
537 cmd = ['push']
538 cmd.append('--receive-pack=%s' % " ".join(rp))
539 cmd.append(branch.remote.SshReviewUrl(self.UserEmail))
540 cmd.append('%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch))
541 if replace_changes:
542 for change_id,commit_id in replace_changes.iteritems():
543 cmd.append('%s:refs/changes/%s/new' % (commit_id, change_id))
544 if GitCommand(self, cmd, bare = True).Wait() != 0:
545 raise UploadError('Upload failed')
546
547 else:
548 raise UploadError('Unsupported protocol %s' \
549 % branch.remote.review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700550
551 msg = "posted to %s for %s" % (branch.remote.review, dest_branch)
552 self.bare_git.UpdateRef(R_PUB + branch.name,
553 R_HEADS + branch.name,
554 message = msg)
555
556
557## Sync ##
558
559 def Sync_NetworkHalf(self):
560 """Perform only the network IO portion of the sync process.
561 Local working directory/branch state is not affected.
562 """
563 if not self.Exists:
564 print >>sys.stderr
565 print >>sys.stderr, 'Initializing project %s ...' % self.name
566 self._InitGitDir()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800567
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700568 self._InitRemote()
569 for r in self.extraRemotes.values():
570 if not self._RemoteFetch(r.name):
571 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700572 if not self._RemoteFetch():
573 return False
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800574
575 if self.worktree:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800576 self._InitMRef()
577 else:
578 self._InitMirrorHead()
579 try:
580 os.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
581 except OSError:
582 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700583 return True
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800584
585 def PostRepoUpgrade(self):
586 self._InitHooks()
587
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700588 def _CopyFiles(self):
589 for file in self.copyfiles:
590 file._Copy()
591
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700592 def Sync_LocalHalf(self, syncbuf):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700593 """Perform only the local IO portion of the sync process.
594 Network access is not required.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700595 """
596 self._InitWorkTree()
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700597 all = self.bare_ref.all
598 self.CleanPublishedCache(all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700599
600 rem = self.GetRemote(self.remote.name)
601 rev = rem.ToLocal(self.revision)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700602 if rev in all:
603 revid = all[rev]
604 elif IsId(rev):
605 revid = rev
606 else:
607 try:
608 revid = self.bare_git.rev_parse('--verify', '%s^0' % rev)
609 except GitError:
610 raise ManifestInvalidRevisionError(
611 'revision %s in %s not found' % (self.revision, self.name))
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800612
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700613 head = self.work_git.GetHead()
614 if head.startswith(R_HEADS):
615 branch = head[len(R_HEADS):]
616 try:
617 head = all[head]
618 except KeyError:
619 head = None
620 else:
621 branch = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700622
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700623 if branch is None or syncbuf.detach_head:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700624 # Currently on a detached HEAD. The user is assumed to
625 # not have any local modifications worth worrying about.
626 #
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700627 if self.IsRebaseInProgress():
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700628 syncbuf.fail(self, _PriorSyncFailedError())
629 return
630
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700631 if head == revid:
632 # No changes; don't do anything further.
633 #
634 return
635
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700636 lost = self._revlist(not_rev(rev), HEAD)
637 if lost:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700638 syncbuf.info(self, "discarding %d commits", len(lost))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700639 try:
640 self._Checkout(rev, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700641 except GitError, e:
642 syncbuf.fail(self, e)
643 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700644 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700645 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700646
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700647 if head == revid:
648 # No changes; don't do anything further.
649 #
650 return
651
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700652 branch = self.GetBranch(branch)
653 merge = branch.LocalMerge
654
655 if not merge:
656 # The current branch has no tracking configuration.
657 # Jump off it to a deatched HEAD.
658 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700659 syncbuf.info(self,
660 "leaving %s; does not track upstream",
661 branch.name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700662 try:
663 self._Checkout(rev, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700664 except GitError, e:
665 syncbuf.fail(self, e)
666 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700667 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700668 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700669
670 upstream_gain = self._revlist(not_rev(HEAD), rev)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700671 pub = self.WasPublished(branch.name, all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700672 if pub:
673 not_merged = self._revlist(not_rev(rev), pub)
674 if not_merged:
675 if upstream_gain:
676 # The user has published this branch and some of those
677 # commits are not yet merged upstream. We do not want
678 # to rewrite the published commits so we punt.
679 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700680 syncbuf.info(self,
681 "branch %s is published but is now %d commits behind",
682 branch.name,
683 len(upstream_gain))
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700684 return
Shawn O. Pearce23d77812008-10-30 11:06:57 -0700685 elif upstream_gain:
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700686 # We can fast-forward safely.
687 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700688 def _doff():
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700689 self._FastForward(rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700690 self._CopyFiles()
691 syncbuf.later1(self, _doff)
692 return
Shawn O. Pearce23d77812008-10-30 11:06:57 -0700693 else:
694 # Trivially no changes in the upstream.
695 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700696 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700697
698 if merge == rev:
699 try:
700 old_merge = self.bare_git.rev_parse('%s@{1}' % merge)
701 except GitError:
702 old_merge = merge
Shawn O. Pearce07346002008-10-21 07:09:27 -0700703 if old_merge == '0000000000000000000000000000000000000000' \
704 or old_merge == '':
705 old_merge = merge
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700706 else:
707 # The upstream switched on us. Time to cross our fingers
708 # and pray that the old upstream also wasn't in the habit
709 # of rebasing itself.
710 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700711 syncbuf.info(self, "manifest switched %s...%s", merge, rev)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700712 old_merge = merge
713
714 if rev == old_merge:
715 upstream_lost = []
716 else:
717 upstream_lost = self._revlist(not_rev(rev), old_merge)
718
719 if not upstream_lost and not upstream_gain:
720 # Trivially no changes caused by the upstream.
721 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700722 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700723
724 if self.IsDirty(consider_untracked=False):
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700725 syncbuf.fail(self, _DirtyError())
726 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700727
728 if upstream_lost:
729 # Upstream rebased. Not everything in HEAD
730 # may have been caused by the user.
731 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700732 syncbuf.info(self,
733 "discarding %d commits removed from upstream",
734 len(upstream_lost))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700735
736 branch.remote = rem
737 branch.merge = self.revision
738 branch.Save()
739
740 my_changes = self._revlist(not_rev(old_merge), HEAD)
741 if my_changes:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700742 def _dorebase():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700743 self._Rebase(upstream = old_merge, onto = rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700744 self._CopyFiles()
745 syncbuf.later2(self, _dorebase)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700746 elif upstream_lost:
747 try:
748 self._ResetHard(rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700749 self._CopyFiles()
750 except GitError, e:
751 syncbuf.fail(self, e)
752 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700753 else:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700754 def _doff():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700755 self._FastForward(rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700756 self._CopyFiles()
757 syncbuf.later1(self, _doff)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700758
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800759 def AddCopyFile(self, src, dest, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700760 # dest should already be an absolute path, but src is project relative
761 # make src an absolute path
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800762 abssrc = os.path.join(self.worktree, src)
763 self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700764
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700765 def DownloadPatchSet(self, change_id, patch_id):
766 """Download a single patch set of a single change to FETCH_HEAD.
767 """
768 remote = self.GetRemote(self.remote.name)
769
770 cmd = ['fetch', remote.name]
771 cmd.append('refs/changes/%2.2d/%d/%d' \
772 % (change_id % 100, change_id, patch_id))
773 cmd.extend(map(lambda x: str(x), remote.fetch))
774 if GitCommand(self, cmd, bare=True).Wait() != 0:
775 return None
776 return DownloadedChange(self,
777 remote.ToLocal(self.revision),
778 change_id,
779 patch_id,
780 self.bare_git.rev_parse('FETCH_HEAD'))
781
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700782
783## Branch Management ##
784
785 def StartBranch(self, name):
786 """Create a new branch off the manifest's revision.
787 """
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700788 head = self.work_git.GetHead()
789 if head == (R_HEADS + name):
790 return True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700791
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700792 all = self.bare_ref.all
793 if (R_HEADS + name) in all:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700794 return GitCommand(self,
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700795 ['checkout', name, '--'],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700796 capture_stdout = True,
797 capture_stderr = True).Wait() == 0
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700798
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700799 branch = self.GetBranch(name)
800 branch.remote = self.GetRemote(self.remote.name)
801 branch.merge = self.revision
802
803 rev = branch.LocalMerge
804 if rev in all:
805 revid = all[rev]
806 elif IsId(rev):
807 revid = rev
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700808 else:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700809 revid = None
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700810
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700811 if head.startswith(R_HEADS):
812 try:
813 head = all[head]
814 except KeyError:
815 head = None
816
817 if revid and head and revid == head:
818 ref = os.path.join(self.gitdir, R_HEADS + name)
819 try:
820 os.makedirs(os.path.dirname(ref))
821 except OSError:
822 pass
823 _lwrite(ref, '%s\n' % revid)
824 _lwrite(os.path.join(self.worktree, '.git', HEAD),
825 'ref: %s%s\n' % (R_HEADS, name))
826 branch.Save()
827 return True
828
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700829 if GitCommand(self,
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700830 ['checkout', '-b', branch.name, rev],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700831 capture_stdout = True,
832 capture_stderr = True).Wait() == 0:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700833 branch.Save()
834 return True
835 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700836
Wink Saville02d79452009-04-10 13:01:24 -0700837 def CheckoutBranch(self, name):
838 """Checkout a local topic branch.
839 """
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700840 rev = R_HEADS + name
841 head = self.work_git.GetHead()
842 if head == rev:
843 # Already on the branch
844 #
845 return True
Wink Saville02d79452009-04-10 13:01:24 -0700846
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700847 all = self.bare_ref.all
Wink Saville02d79452009-04-10 13:01:24 -0700848 try:
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700849 revid = all[rev]
850 except KeyError:
851 # Branch does not exist in this project
852 #
853 return False
854
855 if head.startswith(R_HEADS):
856 try:
857 head = all[head]
858 except KeyError:
859 head = None
860
861 if head == revid:
862 # Same revision; just update HEAD to point to the new
863 # target branch, but otherwise take no other action.
864 #
865 _lwrite(os.path.join(self.worktree, '.git', HEAD),
866 'ref: %s%s\n' % (R_HEADS, name))
867 return True
Wink Saville02d79452009-04-10 13:01:24 -0700868
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700869 return GitCommand(self,
870 ['checkout', name, '--'],
871 capture_stdout = True,
872 capture_stderr = True).Wait() == 0
Wink Saville02d79452009-04-10 13:01:24 -0700873
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800874 def AbandonBranch(self, name):
875 """Destroy a local topic branch.
876 """
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700877 rev = R_HEADS + name
878 all = self.bare_ref.all
879 if rev not in all:
880 # Doesn't exist; assume already abandoned.
881 #
882 return True
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800883
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700884 head = self.work_git.GetHead()
885 if head == rev:
886 # We can't destroy the branch while we are sitting
887 # on it. Switch to a detached HEAD.
888 #
889 head = all[head]
890
891 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
892 if rev in all:
893 revid = all[rev]
894 elif IsId(rev):
895 revid = rev
896 else:
897 revid = None
898
899 if revid and head == revid:
900 _lwrite(os.path.join(self.worktree, '.git', HEAD),
901 '%s\n' % revid)
902 else:
903 self._Checkout(rev, quiet=True)
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800904
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700905 return GitCommand(self,
906 ['branch', '-D', name],
907 capture_stdout = True,
908 capture_stderr = True).Wait() == 0
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800909
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700910 def PruneHeads(self):
911 """Prune any topic branches already merged into upstream.
912 """
913 cb = self.CurrentBranch
914 kill = []
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800915 left = self._allrefs
916 for name in left.keys():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700917 if name.startswith(R_HEADS):
918 name = name[len(R_HEADS):]
919 if cb is None or name != cb:
920 kill.append(name)
921
922 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
923 if cb is not None \
924 and not self._revlist(HEAD + '...' + rev) \
925 and not self.IsDirty(consider_untracked = False):
926 self.work_git.DetachHead(HEAD)
927 kill.append(cb)
928
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700929 if kill:
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700930 old = self.bare_git.GetHead()
931 if old is None:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700932 old = 'refs/heads/please_never_use_this_as_a_branch_name'
933
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700934 try:
935 self.bare_git.DetachHead(rev)
936
937 b = ['branch', '-d']
938 b.extend(kill)
939 b = GitCommand(self, b, bare=True,
940 capture_stdout=True,
941 capture_stderr=True)
942 b.Wait()
943 finally:
944 self.bare_git.SetHead(old)
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800945 left = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700946
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800947 for branch in kill:
948 if (R_HEADS + branch) not in left:
949 self.CleanPublishedCache()
950 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700951
952 if cb and cb not in kill:
953 kill.append(cb)
Shawn O. Pearce7c6c64d2009-03-02 12:38:13 -0800954 kill.sort()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700955
956 kept = []
957 for branch in kill:
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800958 if (R_HEADS + branch) in left:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700959 branch = self.GetBranch(branch)
960 base = branch.LocalMerge
961 if not base:
962 base = rev
963 kept.append(ReviewableBranch(self, branch, base))
964 return kept
965
966
967## Direct Git Commands ##
968
969 def _RemoteFetch(self, name=None):
970 if not name:
971 name = self.remote.name
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800972 cmd = ['fetch']
973 if not self.worktree:
974 cmd.append('--update-head-ok')
975 cmd.append(name)
976 return GitCommand(self, cmd, bare = True).Wait() == 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700977
978 def _Checkout(self, rev, quiet=False):
979 cmd = ['checkout']
980 if quiet:
981 cmd.append('-q')
982 cmd.append(rev)
983 cmd.append('--')
984 if GitCommand(self, cmd).Wait() != 0:
985 if self._allrefs:
986 raise GitError('%s checkout %s ' % (self.name, rev))
987
988 def _ResetHard(self, rev, quiet=True):
989 cmd = ['reset', '--hard']
990 if quiet:
991 cmd.append('-q')
992 cmd.append(rev)
993 if GitCommand(self, cmd).Wait() != 0:
994 raise GitError('%s reset --hard %s ' % (self.name, rev))
995
996 def _Rebase(self, upstream, onto = None):
Shawn O. Pearce19a83d82009-04-16 08:14:26 -0700997 cmd = ['rebase']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700998 if onto is not None:
999 cmd.extend(['--onto', onto])
1000 cmd.append(upstream)
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07001001 if GitCommand(self, cmd).Wait() != 0:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001002 raise GitError('%s rebase %s ' % (self.name, upstream))
1003
1004 def _FastForward(self, head):
1005 cmd = ['merge', head]
1006 if GitCommand(self, cmd).Wait() != 0:
1007 raise GitError('%s merge %s ' % (self.name, head))
1008
1009 def _InitGitDir(self):
1010 if not os.path.exists(self.gitdir):
1011 os.makedirs(self.gitdir)
1012 self.bare_git.init()
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -08001013
1014 if self.manifest.IsMirror:
1015 self.config.SetString('core.bare', 'true')
1016 else:
1017 self.config.SetString('core.bare', None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001018
1019 hooks = self._gitdir_path('hooks')
Shawn O. Pearcede646812008-10-29 14:38:12 -07001020 try:
1021 to_rm = os.listdir(hooks)
1022 except OSError:
1023 to_rm = []
1024 for old_hook in to_rm:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001025 os.remove(os.path.join(hooks, old_hook))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001026 self._InitHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001027
1028 m = self.manifest.manifestProject.config
1029 for key in ['user.name', 'user.email']:
1030 if m.Has(key, include_defaults = False):
1031 self.config.SetString(key, m.GetString(key))
1032
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001033 def _InitHooks(self):
1034 hooks = self._gitdir_path('hooks')
1035 if not os.path.exists(hooks):
1036 os.makedirs(hooks)
1037 for stock_hook in repo_hooks():
1038 dst = os.path.join(hooks, os.path.basename(stock_hook))
1039 try:
1040 os.symlink(relpath(stock_hook, dst), dst)
1041 except OSError, e:
1042 if e.errno == errno.EEXIST:
1043 pass
1044 elif e.errno == errno.EPERM:
1045 raise GitError('filesystem must support symlinks')
1046 else:
1047 raise
1048
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001049 def _InitRemote(self):
1050 if self.remote.fetchUrl:
1051 remote = self.GetRemote(self.remote.name)
1052
1053 url = self.remote.fetchUrl
1054 while url.endswith('/'):
1055 url = url[:-1]
1056 url += '/%s.git' % self.name
1057 remote.url = url
1058 remote.review = self.remote.reviewUrl
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -08001059 if remote.projectname is None:
1060 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001061
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001062 if self.worktree:
1063 remote.ResetFetch(mirror=False)
1064 else:
1065 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001066 remote.Save()
1067
1068 for r in self.extraRemotes.values():
1069 remote = self.GetRemote(r.name)
1070 remote.url = r.fetchUrl
1071 remote.review = r.reviewUrl
Shawn O. Pearceae6e0942008-11-06 10:25:35 -08001072 if r.projectName:
1073 remote.projectname = r.projectName
1074 elif remote.projectname is None:
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -08001075 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001076 remote.ResetFetch()
1077 remote.Save()
1078
1079 def _InitMRef(self):
1080 if self.manifest.branch:
1081 msg = 'manifest set to %s' % self.revision
1082 ref = R_M + self.manifest.branch
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -07001083 cur = self.bare_ref.symref(ref)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001084
1085 if IsId(self.revision):
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -07001086 if cur != '' or self.bare_ref.get(ref) != self.revision:
1087 dst = self.revision + '^0'
1088 self.bare_git.UpdateRef(ref, dst, message = msg, detach = True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001089 else:
1090 remote = self.GetRemote(self.remote.name)
1091 dst = remote.ToLocal(self.revision)
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -07001092 if cur != dst:
1093 self.bare_git.symbolic_ref('-m', msg, ref, dst)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001094
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001095 def _InitMirrorHead(self):
1096 dst = self.GetRemote(self.remote.name).ToLocal(self.revision)
1097 msg = 'manifest set to %s' % self.revision
1098 self.bare_git.SetHead(dst, message=msg)
1099
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001100 def _InitWorkTree(self):
1101 dotgit = os.path.join(self.worktree, '.git')
1102 if not os.path.exists(dotgit):
1103 os.makedirs(dotgit)
1104
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001105 for name in ['config',
1106 'description',
1107 'hooks',
1108 'info',
1109 'logs',
1110 'objects',
1111 'packed-refs',
1112 'refs',
1113 'rr-cache',
1114 'svn']:
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001115 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001116 src = os.path.join(self.gitdir, name)
1117 dst = os.path.join(dotgit, name)
1118 os.symlink(relpath(src, dst), dst)
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001119 except OSError, e:
1120 if e.errno == errno.EPERM:
1121 raise GitError('filesystem must support symlinks')
1122 else:
1123 raise
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001124
1125 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
1126 rev = self.bare_git.rev_parse('%s^0' % rev)
1127
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -07001128 _lwrite(os.path.join(dotgit, HEAD), '%s\n' % rev)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001129
1130 cmd = ['read-tree', '--reset', '-u']
1131 cmd.append('-v')
1132 cmd.append('HEAD')
1133 if GitCommand(self, cmd).Wait() != 0:
1134 raise GitError("cannot initialize work tree")
1135
1136 def _gitdir_path(self, path):
1137 return os.path.join(self.gitdir, path)
1138
1139 def _revlist(self, *args):
1140 cmd = []
1141 cmd.extend(args)
1142 cmd.append('--')
1143 return self.work_git.rev_list(*args)
1144
1145 @property
1146 def _allrefs(self):
Shawn O. Pearced237b692009-04-17 18:49:50 -07001147 return self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001148
1149 class _GitGetByExec(object):
1150 def __init__(self, project, bare):
1151 self._project = project
1152 self._bare = bare
1153
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001154 def LsOthers(self):
1155 p = GitCommand(self._project,
1156 ['ls-files',
1157 '-z',
1158 '--others',
1159 '--exclude-standard'],
1160 bare = False,
1161 capture_stdout = True,
1162 capture_stderr = True)
1163 if p.Wait() == 0:
1164 out = p.stdout
1165 if out:
1166 return out[:-1].split("\0")
1167 return []
1168
1169 def DiffZ(self, name, *args):
1170 cmd = [name]
1171 cmd.append('-z')
1172 cmd.extend(args)
1173 p = GitCommand(self._project,
1174 cmd,
1175 bare = False,
1176 capture_stdout = True,
1177 capture_stderr = True)
1178 try:
1179 out = p.process.stdout.read()
1180 r = {}
1181 if out:
1182 out = iter(out[:-1].split('\0'))
1183 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07001184 try:
1185 info = out.next()
1186 path = out.next()
1187 except StopIteration:
1188 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001189
1190 class _Info(object):
1191 def __init__(self, path, omode, nmode, oid, nid, state):
1192 self.path = path
1193 self.src_path = None
1194 self.old_mode = omode
1195 self.new_mode = nmode
1196 self.old_id = oid
1197 self.new_id = nid
1198
1199 if len(state) == 1:
1200 self.status = state
1201 self.level = None
1202 else:
1203 self.status = state[:1]
1204 self.level = state[1:]
1205 while self.level.startswith('0'):
1206 self.level = self.level[1:]
1207
1208 info = info[1:].split(' ')
1209 info =_Info(path, *info)
1210 if info.status in ('R', 'C'):
1211 info.src_path = info.path
1212 info.path = out.next()
1213 r[info.path] = info
1214 return r
1215 finally:
1216 p.Wait()
1217
1218 def GetHead(self):
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001219 if self._bare:
1220 path = os.path.join(self._project.gitdir, HEAD)
1221 else:
1222 path = os.path.join(self._project.worktree, '.git', HEAD)
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -07001223 fd = open(path, 'rb')
1224 try:
1225 line = fd.read()
1226 finally:
1227 fd.close()
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001228 if line.startswith('ref: '):
1229 return line[5:-1]
1230 return line[:-1]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001231
1232 def SetHead(self, ref, message=None):
1233 cmdv = []
1234 if message is not None:
1235 cmdv.extend(['-m', message])
1236 cmdv.append(HEAD)
1237 cmdv.append(ref)
1238 self.symbolic_ref(*cmdv)
1239
1240 def DetachHead(self, new, message=None):
1241 cmdv = ['--no-deref']
1242 if message is not None:
1243 cmdv.extend(['-m', message])
1244 cmdv.append(HEAD)
1245 cmdv.append(new)
1246 self.update_ref(*cmdv)
1247
1248 def UpdateRef(self, name, new, old=None,
1249 message=None,
1250 detach=False):
1251 cmdv = []
1252 if message is not None:
1253 cmdv.extend(['-m', message])
1254 if detach:
1255 cmdv.append('--no-deref')
1256 cmdv.append(name)
1257 cmdv.append(new)
1258 if old is not None:
1259 cmdv.append(old)
1260 self.update_ref(*cmdv)
1261
1262 def DeleteRef(self, name, old=None):
1263 if not old:
1264 old = self.rev_parse(name)
1265 self.update_ref('-d', name, old)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001266 self._project.bare_ref.deleted(name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001267
1268 def rev_list(self, *args):
1269 cmdv = ['rev-list']
1270 cmdv.extend(args)
1271 p = GitCommand(self._project,
1272 cmdv,
1273 bare = self._bare,
1274 capture_stdout = True,
1275 capture_stderr = True)
1276 r = []
1277 for line in p.process.stdout:
1278 r.append(line[:-1])
1279 if p.Wait() != 0:
1280 raise GitError('%s rev-list %s: %s' % (
1281 self._project.name,
1282 str(args),
1283 p.stderr))
1284 return r
1285
1286 def __getattr__(self, name):
1287 name = name.replace('_', '-')
1288 def runner(*args):
1289 cmdv = [name]
1290 cmdv.extend(args)
1291 p = GitCommand(self._project,
1292 cmdv,
1293 bare = self._bare,
1294 capture_stdout = True,
1295 capture_stderr = True)
1296 if p.Wait() != 0:
1297 raise GitError('%s %s: %s' % (
1298 self._project.name,
1299 name,
1300 p.stderr))
1301 r = p.stdout
1302 if r.endswith('\n') and r.index('\n') == len(r) - 1:
1303 return r[:-1]
1304 return r
1305 return runner
1306
1307
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001308class _PriorSyncFailedError(Exception):
1309 def __str__(self):
1310 return 'prior sync failed; rebase still in progress'
1311
1312class _DirtyError(Exception):
1313 def __str__(self):
1314 return 'contains uncommitted changes'
1315
1316class _InfoMessage(object):
1317 def __init__(self, project, text):
1318 self.project = project
1319 self.text = text
1320
1321 def Print(self, syncbuf):
1322 syncbuf.out.info('%s/: %s', self.project.relpath, self.text)
1323 syncbuf.out.nl()
1324
1325class _Failure(object):
1326 def __init__(self, project, why):
1327 self.project = project
1328 self.why = why
1329
1330 def Print(self, syncbuf):
1331 syncbuf.out.fail('error: %s/: %s',
1332 self.project.relpath,
1333 str(self.why))
1334 syncbuf.out.nl()
1335
1336class _Later(object):
1337 def __init__(self, project, action):
1338 self.project = project
1339 self.action = action
1340
1341 def Run(self, syncbuf):
1342 out = syncbuf.out
1343 out.project('project %s/', self.project.relpath)
1344 out.nl()
1345 try:
1346 self.action()
1347 out.nl()
1348 return True
1349 except GitError, e:
1350 out.nl()
1351 return False
1352
1353class _SyncColoring(Coloring):
1354 def __init__(self, config):
1355 Coloring.__init__(self, config, 'reposync')
1356 self.project = self.printer('header', attr = 'bold')
1357 self.info = self.printer('info')
1358 self.fail = self.printer('fail', fg='red')
1359
1360class SyncBuffer(object):
1361 def __init__(self, config, detach_head=False):
1362 self._messages = []
1363 self._failures = []
1364 self._later_queue1 = []
1365 self._later_queue2 = []
1366
1367 self.out = _SyncColoring(config)
1368 self.out.redirect(sys.stderr)
1369
1370 self.detach_head = detach_head
1371 self.clean = True
1372
1373 def info(self, project, fmt, *args):
1374 self._messages.append(_InfoMessage(project, fmt % args))
1375
1376 def fail(self, project, err=None):
1377 self._failures.append(_Failure(project, err))
1378 self.clean = False
1379
1380 def later1(self, project, what):
1381 self._later_queue1.append(_Later(project, what))
1382
1383 def later2(self, project, what):
1384 self._later_queue2.append(_Later(project, what))
1385
1386 def Finish(self):
1387 self._PrintMessages()
1388 self._RunLater()
1389 self._PrintMessages()
1390 return self.clean
1391
1392 def _RunLater(self):
1393 for q in ['_later_queue1', '_later_queue2']:
1394 if not self._RunQueue(q):
1395 return
1396
1397 def _RunQueue(self, queue):
1398 for m in getattr(self, queue):
1399 if not m.Run(self):
1400 self.clean = False
1401 return False
1402 setattr(self, queue, [])
1403 return True
1404
1405 def _PrintMessages(self):
1406 for m in self._messages:
1407 m.Print(self)
1408 for m in self._failures:
1409 m.Print(self)
1410
1411 self._messages = []
1412 self._failures = []
1413
1414
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001415class MetaProject(Project):
1416 """A special project housed under .repo.
1417 """
1418 def __init__(self, manifest, name, gitdir, worktree):
1419 repodir = manifest.repodir
1420 Project.__init__(self,
1421 manifest = manifest,
1422 name = name,
1423 gitdir = gitdir,
1424 worktree = worktree,
1425 remote = Remote('origin'),
1426 relpath = '.repo/%s' % name,
1427 revision = 'refs/heads/master')
1428
1429 def PreSync(self):
1430 if self.Exists:
1431 cb = self.CurrentBranch
1432 if cb:
1433 base = self.GetBranch(cb).merge
1434 if base:
1435 self.revision = base
1436
1437 @property
Shawn O. Pearcef6906872009-04-18 10:49:00 -07001438 def LastFetch(self):
1439 try:
1440 fh = os.path.join(self.gitdir, 'FETCH_HEAD')
1441 return os.path.getmtime(fh)
1442 except OSError:
1443 return 0
1444
1445 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001446 def HasChanges(self):
1447 """Has the remote received new commits not yet checked out?
1448 """
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001449 if not self.remote or not self.revision:
1450 return False
1451
1452 all = self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001453 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001454 if rev in all:
1455 revid = all[rev]
1456 else:
1457 revid = rev
1458
1459 head = self.work_git.GetHead()
1460 if head.startswith(R_HEADS):
1461 try:
1462 head = all[head]
1463 except KeyError:
1464 head = None
1465
1466 if revid == head:
1467 return False
1468 elif self._revlist(not_rev(HEAD), rev):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001469 return True
1470 return False