blob: 53fd38ee0b5e526c0e833b565d34ca6560bae955 [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 -070029
Shawn O. Pearced237b692009-04-17 18:49:50 -070030from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070031
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -070032def _lwrite(path, content):
33 lock = '%s.lock' % path
34
35 fd = open(lock, 'wb')
36 try:
37 fd.write(content)
38 finally:
39 fd.close()
40
41 try:
42 os.rename(lock, path)
43 except OSError:
44 os.remove(lock)
45 raise
46
Shawn O. Pearce48244782009-04-16 08:25:57 -070047def _error(fmt, *args):
48 msg = fmt % args
49 print >>sys.stderr, 'error: %s' % msg
50
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070051def not_rev(r):
52 return '^' + r
53
Shawn O. Pearceb54a3922009-01-05 16:18:58 -080054def sq(r):
55 return "'" + r.replace("'", "'\''") + "'"
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080056
57hook_list = None
58def repo_hooks():
59 global hook_list
60 if hook_list is None:
61 d = os.path.abspath(os.path.dirname(__file__))
62 d = os.path.join(d , 'hooks')
63 hook_list = map(lambda x: os.path.join(d, x), os.listdir(d))
64 return hook_list
65
66def relpath(dst, src):
67 src = os.path.dirname(src)
68 top = os.path.commonprefix([dst, src])
69 if top.endswith('/'):
70 top = top[:-1]
71 else:
72 top = os.path.dirname(top)
73
74 tmp = src
75 rel = ''
76 while top != tmp:
77 rel += '../'
78 tmp = os.path.dirname(tmp)
79 return rel + dst[len(top) + 1:]
80
81
Shawn O. Pearce632768b2008-10-23 11:58:52 -070082class DownloadedChange(object):
83 _commit_cache = None
84
85 def __init__(self, project, base, change_id, ps_id, commit):
86 self.project = project
87 self.base = base
88 self.change_id = change_id
89 self.ps_id = ps_id
90 self.commit = commit
91
92 @property
93 def commits(self):
94 if self._commit_cache is None:
95 self._commit_cache = self.project.bare_git.rev_list(
96 '--abbrev=8',
97 '--abbrev-commit',
98 '--pretty=oneline',
99 '--reverse',
100 '--date-order',
101 not_rev(self.base),
102 self.commit,
103 '--')
104 return self._commit_cache
105
106
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700107class ReviewableBranch(object):
108 _commit_cache = None
109
110 def __init__(self, project, branch, base):
111 self.project = project
112 self.branch = branch
113 self.base = base
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800114 self.replace_changes = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700115
116 @property
117 def name(self):
118 return self.branch.name
119
120 @property
121 def commits(self):
122 if self._commit_cache is None:
123 self._commit_cache = self.project.bare_git.rev_list(
124 '--abbrev=8',
125 '--abbrev-commit',
126 '--pretty=oneline',
127 '--reverse',
128 '--date-order',
129 not_rev(self.base),
130 R_HEADS + self.name,
131 '--')
132 return self._commit_cache
133
134 @property
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800135 def unabbrev_commits(self):
136 r = dict()
137 for commit in self.project.bare_git.rev_list(
138 not_rev(self.base),
139 R_HEADS + self.name,
140 '--'):
141 r[commit[0:8]] = commit
142 return r
143
144 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700145 def date(self):
146 return self.project.bare_git.log(
147 '--pretty=format:%cd',
148 '-n', '1',
149 R_HEADS + self.name,
150 '--')
151
Joe Onorato2896a792008-11-17 16:56:36 -0500152 def UploadForReview(self, people):
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800153 self.project.UploadForReview(self.name,
Joe Onorato2896a792008-11-17 16:56:36 -0500154 self.replace_changes,
155 people)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700156
Ficus Kirkpatrickbc7ef672009-05-04 12:45:11 -0700157 def GetPublishedRefs(self):
158 refs = {}
159 output = self.project.bare_git.ls_remote(
160 self.branch.remote.SshReviewUrl(self.project.UserEmail),
161 'refs/changes/*')
162 for line in output.split('\n'):
163 try:
164 (sha, ref) = line.split()
165 refs[sha] = ref
166 except ValueError:
167 pass
168
169 return refs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700170
171class StatusColoring(Coloring):
172 def __init__(self, config):
173 Coloring.__init__(self, config, 'status')
174 self.project = self.printer('header', attr = 'bold')
175 self.branch = self.printer('header', attr = 'bold')
176 self.nobranch = self.printer('nobranch', fg = 'red')
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700177 self.important = self.printer('important', fg = 'red')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700178
179 self.added = self.printer('added', fg = 'green')
180 self.changed = self.printer('changed', fg = 'red')
181 self.untracked = self.printer('untracked', fg = 'red')
182
183
184class DiffColoring(Coloring):
185 def __init__(self, config):
186 Coloring.__init__(self, config, 'diff')
187 self.project = self.printer('header', attr = 'bold')
188
189
190class _CopyFile:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800191 def __init__(self, src, dest, abssrc, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700192 self.src = src
193 self.dest = dest
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800194 self.abs_src = abssrc
195 self.abs_dest = absdest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700196
197 def _Copy(self):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800198 src = self.abs_src
199 dest = self.abs_dest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700200 # copy file if it does not exist or is out of date
201 if not os.path.exists(dest) or not filecmp.cmp(src, dest):
202 try:
203 # remove existing file first, since it might be read-only
204 if os.path.exists(dest):
205 os.remove(dest)
206 shutil.copy(src, dest)
207 # make the file read-only
208 mode = os.stat(dest)[stat.ST_MODE]
209 mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
210 os.chmod(dest, mode)
211 except IOError:
Shawn O. Pearce48244782009-04-16 08:25:57 -0700212 _error('Cannot copy file %s to %s', src, dest)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700213
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700214class RemoteSpec(object):
215 def __init__(self,
216 name,
217 url = None,
218 review = None):
219 self.name = name
220 self.url = url
221 self.review = review
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700222
223class Project(object):
224 def __init__(self,
225 manifest,
226 name,
227 remote,
228 gitdir,
229 worktree,
230 relpath,
231 revision):
232 self.manifest = manifest
233 self.name = name
234 self.remote = remote
235 self.gitdir = gitdir
236 self.worktree = worktree
237 self.relpath = relpath
238 self.revision = revision
239 self.snapshots = {}
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700240 self.copyfiles = []
241 self.config = GitConfig.ForRepository(
242 gitdir = self.gitdir,
243 defaults = self.manifest.globalConfig)
244
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800245 if self.worktree:
246 self.work_git = self._GitGetByExec(self, bare=False)
247 else:
248 self.work_git = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700249 self.bare_git = self._GitGetByExec(self, bare=True)
Shawn O. Pearced237b692009-04-17 18:49:50 -0700250 self.bare_ref = GitRefs(gitdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700251
252 @property
253 def Exists(self):
254 return os.path.isdir(self.gitdir)
255
256 @property
257 def CurrentBranch(self):
258 """Obtain the name of the currently checked out branch.
259 The branch name omits the 'refs/heads/' prefix.
260 None is returned if the project is on a detached HEAD.
261 """
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700262 b = self.work_git.GetHead()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700263 if b.startswith(R_HEADS):
264 return b[len(R_HEADS):]
265 return None
266
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700267 def IsRebaseInProgress(self):
268 w = self.worktree
269 g = os.path.join(w, '.git')
270 return os.path.exists(os.path.join(g, 'rebase-apply')) \
271 or os.path.exists(os.path.join(g, 'rebase-merge')) \
272 or os.path.exists(os.path.join(w, '.dotest'))
273
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700274 def IsDirty(self, consider_untracked=True):
275 """Is the working directory modified in some way?
276 """
277 self.work_git.update_index('-q',
278 '--unmerged',
279 '--ignore-missing',
280 '--refresh')
281 if self.work_git.DiffZ('diff-index','-M','--cached',HEAD):
282 return True
283 if self.work_git.DiffZ('diff-files'):
284 return True
285 if consider_untracked and self.work_git.LsOthers():
286 return True
287 return False
288
289 _userident_name = None
290 _userident_email = None
291
292 @property
293 def UserName(self):
294 """Obtain the user's personal name.
295 """
296 if self._userident_name is None:
297 self._LoadUserIdentity()
298 return self._userident_name
299
300 @property
301 def UserEmail(self):
302 """Obtain the user's email address. This is very likely
303 to be their Gerrit login.
304 """
305 if self._userident_email is None:
306 self._LoadUserIdentity()
307 return self._userident_email
308
309 def _LoadUserIdentity(self):
310 u = self.bare_git.var('GIT_COMMITTER_IDENT')
311 m = re.compile("^(.*) <([^>]*)> ").match(u)
312 if m:
313 self._userident_name = m.group(1)
314 self._userident_email = m.group(2)
315 else:
316 self._userident_name = ''
317 self._userident_email = ''
318
319 def GetRemote(self, name):
320 """Get the configuration for a single remote.
321 """
322 return self.config.GetRemote(name)
323
324 def GetBranch(self, name):
325 """Get the configuration for a single branch.
326 """
327 return self.config.GetBranch(name)
328
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700329 def GetBranches(self):
330 """Get all existing local branches.
331 """
332 current = self.CurrentBranch
Shawn O. Pearced237b692009-04-17 18:49:50 -0700333 all = self._allrefs
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700334 heads = {}
335 pubd = {}
336
337 for name, id in all.iteritems():
338 if name.startswith(R_HEADS):
339 name = name[len(R_HEADS):]
340 b = self.GetBranch(name)
341 b.current = name == current
342 b.published = None
343 b.revision = id
344 heads[name] = b
345
346 for name, id in all.iteritems():
347 if name.startswith(R_PUB):
348 name = name[len(R_PUB):]
349 b = heads.get(name)
350 if b:
351 b.published = id
352
353 return heads
354
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700355
356## Status Display ##
357
358 def PrintWorkTreeStatus(self):
359 """Prints the status of the repository to stdout.
360 """
361 if not os.path.isdir(self.worktree):
362 print ''
363 print 'project %s/' % self.relpath
364 print ' missing (run "repo sync")'
365 return
366
367 self.work_git.update_index('-q',
368 '--unmerged',
369 '--ignore-missing',
370 '--refresh')
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700371 rb = self.IsRebaseInProgress()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700372 di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD)
373 df = self.work_git.DiffZ('diff-files')
374 do = self.work_git.LsOthers()
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700375 if not rb and not di and not df and not do:
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700376 return 'CLEAN'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700377
378 out = StatusColoring(self.config)
379 out.project('project %-40s', self.relpath + '/')
380
381 branch = self.CurrentBranch
382 if branch is None:
383 out.nobranch('(*** NO BRANCH ***)')
384 else:
385 out.branch('branch %s', branch)
386 out.nl()
387
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700388 if rb:
389 out.important('prior sync failed; rebase still in progress')
390 out.nl()
391
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700392 paths = list()
393 paths.extend(di.keys())
394 paths.extend(df.keys())
395 paths.extend(do)
396
397 paths = list(set(paths))
398 paths.sort()
399
400 for p in paths:
401 try: i = di[p]
402 except KeyError: i = None
403
404 try: f = df[p]
405 except KeyError: f = None
406
407 if i: i_status = i.status.upper()
408 else: i_status = '-'
409
410 if f: f_status = f.status.lower()
411 else: f_status = '-'
412
413 if i and i.src_path:
Shawn O. Pearcefe086752009-03-03 13:49:48 -0800414 line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700415 i.src_path, p, i.level)
416 else:
417 line = ' %s%s\t%s' % (i_status, f_status, p)
418
419 if i and not f:
420 out.added('%s', line)
421 elif (i and f) or (not i and f):
422 out.changed('%s', line)
423 elif not i and not f:
424 out.untracked('%s', line)
425 else:
426 out.write('%s', line)
427 out.nl()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700428 return 'DIRTY'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700429
430 def PrintWorkTreeDiff(self):
431 """Prints the status of the repository to stdout.
432 """
433 out = DiffColoring(self.config)
434 cmd = ['diff']
435 if out.is_on:
436 cmd.append('--color')
437 cmd.append(HEAD)
438 cmd.append('--')
439 p = GitCommand(self,
440 cmd,
441 capture_stdout = True,
442 capture_stderr = True)
443 has_diff = False
444 for line in p.process.stdout:
445 if not has_diff:
446 out.nl()
447 out.project('project %s/' % self.relpath)
448 out.nl()
449 has_diff = True
450 print line[:-1]
451 p.Wait()
452
453
454## Publish / Upload ##
455
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700456 def WasPublished(self, branch, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700457 """Was the branch published (uploaded) for code review?
458 If so, returns the SHA-1 hash of the last published
459 state for the branch.
460 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700461 key = R_PUB + branch
462 if all is None:
463 try:
464 return self.bare_git.rev_parse(key)
465 except GitError:
466 return None
467 else:
468 try:
469 return all[key]
470 except KeyError:
471 return None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700472
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700473 def CleanPublishedCache(self, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700474 """Prunes any stale published refs.
475 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700476 if all is None:
477 all = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700478 heads = set()
479 canrm = {}
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700480 for name, id in all.iteritems():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700481 if name.startswith(R_HEADS):
482 heads.add(name)
483 elif name.startswith(R_PUB):
484 canrm[name] = id
485
486 for name, id in canrm.iteritems():
487 n = name[len(R_PUB):]
488 if R_HEADS + n not in heads:
489 self.bare_git.DeleteRef(name, id)
490
491 def GetUploadableBranches(self):
492 """List any branches which can be uploaded for review.
493 """
494 heads = {}
495 pubed = {}
496
497 for name, id in self._allrefs.iteritems():
498 if name.startswith(R_HEADS):
499 heads[name[len(R_HEADS):]] = id
500 elif name.startswith(R_PUB):
501 pubed[name[len(R_PUB):]] = id
502
503 ready = []
504 for branch, id in heads.iteritems():
505 if branch in pubed and pubed[branch] == id:
506 continue
507
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800508 rb = self.GetUploadableBranch(branch)
509 if rb:
510 ready.append(rb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700511 return ready
512
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800513 def GetUploadableBranch(self, branch_name):
514 """Get a single uploadable branch, or None.
515 """
516 branch = self.GetBranch(branch_name)
517 base = branch.LocalMerge
518 if branch.LocalMerge:
519 rb = ReviewableBranch(self, branch, base)
520 if rb.commits:
521 return rb
522 return None
523
Joe Onorato2896a792008-11-17 16:56:36 -0500524 def UploadForReview(self, branch=None, replace_changes=None, people=([],[])):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700525 """Uploads the named branch for code review.
526 """
527 if branch is None:
528 branch = self.CurrentBranch
529 if branch is None:
530 raise GitError('not currently on a branch')
531
532 branch = self.GetBranch(branch)
533 if not branch.LocalMerge:
534 raise GitError('branch %s does not track a remote' % branch.name)
535 if not branch.remote.review:
536 raise GitError('remote %s has no review url' % branch.remote.name)
537
538 dest_branch = branch.merge
539 if not dest_branch.startswith(R_HEADS):
540 dest_branch = R_HEADS + dest_branch
541
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800542 if not branch.remote.projectname:
543 branch.remote.projectname = self.name
544 branch.remote.Save()
545
Shawn O. Pearce370e3fa2009-01-26 10:55:39 -0800546 if branch.remote.ReviewProtocol == 'ssh':
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800547 if dest_branch.startswith(R_HEADS):
548 dest_branch = dest_branch[len(R_HEADS):]
549
550 rp = ['gerrit receive-pack']
551 for e in people[0]:
552 rp.append('--reviewer=%s' % sq(e))
553 for e in people[1]:
554 rp.append('--cc=%s' % sq(e))
555
556 cmd = ['push']
557 cmd.append('--receive-pack=%s' % " ".join(rp))
558 cmd.append(branch.remote.SshReviewUrl(self.UserEmail))
559 cmd.append('%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch))
560 if replace_changes:
561 for change_id,commit_id in replace_changes.iteritems():
562 cmd.append('%s:refs/changes/%s/new' % (commit_id, change_id))
563 if GitCommand(self, cmd, bare = True).Wait() != 0:
564 raise UploadError('Upload failed')
565
566 else:
567 raise UploadError('Unsupported protocol %s' \
568 % branch.remote.review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700569
570 msg = "posted to %s for %s" % (branch.remote.review, dest_branch)
571 self.bare_git.UpdateRef(R_PUB + branch.name,
572 R_HEADS + branch.name,
573 message = msg)
574
575
576## Sync ##
577
578 def Sync_NetworkHalf(self):
579 """Perform only the network IO portion of the sync process.
580 Local working directory/branch state is not affected.
581 """
582 if not self.Exists:
583 print >>sys.stderr
584 print >>sys.stderr, 'Initializing project %s ...' % self.name
585 self._InitGitDir()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800586
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700587 self._InitRemote()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700588 if not self._RemoteFetch():
589 return False
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800590
591 if self.worktree:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800592 self._InitMRef()
593 else:
594 self._InitMirrorHead()
595 try:
596 os.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
597 except OSError:
598 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700599 return True
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800600
601 def PostRepoUpgrade(self):
602 self._InitHooks()
603
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700604 def _CopyFiles(self):
605 for file in self.copyfiles:
606 file._Copy()
607
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700608 def Sync_LocalHalf(self, syncbuf):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700609 """Perform only the local IO portion of the sync process.
610 Network access is not required.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700611 """
612 self._InitWorkTree()
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700613 all = self.bare_ref.all
614 self.CleanPublishedCache(all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700615
616 rem = self.GetRemote(self.remote.name)
617 rev = rem.ToLocal(self.revision)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700618 if rev in all:
619 revid = all[rev]
620 elif IsId(rev):
621 revid = rev
622 else:
623 try:
624 revid = self.bare_git.rev_parse('--verify', '%s^0' % rev)
625 except GitError:
626 raise ManifestInvalidRevisionError(
627 'revision %s in %s not found' % (self.revision, self.name))
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800628
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700629 head = self.work_git.GetHead()
630 if head.startswith(R_HEADS):
631 branch = head[len(R_HEADS):]
632 try:
633 head = all[head]
634 except KeyError:
635 head = None
636 else:
637 branch = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700638
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700639 if branch is None or syncbuf.detach_head:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700640 # Currently on a detached HEAD. The user is assumed to
641 # not have any local modifications worth worrying about.
642 #
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700643 if self.IsRebaseInProgress():
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700644 syncbuf.fail(self, _PriorSyncFailedError())
645 return
646
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 lost = self._revlist(not_rev(rev), HEAD)
653 if lost:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700654 syncbuf.info(self, "discarding %d commits", len(lost))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700655 try:
656 self._Checkout(rev, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700657 except GitError, e:
658 syncbuf.fail(self, e)
659 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700660 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700661 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700662
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700663 if head == revid:
664 # No changes; don't do anything further.
665 #
666 return
667
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700668 branch = self.GetBranch(branch)
669 merge = branch.LocalMerge
670
671 if not merge:
672 # The current branch has no tracking configuration.
673 # Jump off it to a deatched HEAD.
674 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700675 syncbuf.info(self,
676 "leaving %s; does not track upstream",
677 branch.name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700678 try:
679 self._Checkout(rev, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700680 except GitError, e:
681 syncbuf.fail(self, e)
682 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700683 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700684 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700685
686 upstream_gain = self._revlist(not_rev(HEAD), rev)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700687 pub = self.WasPublished(branch.name, all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700688 if pub:
689 not_merged = self._revlist(not_rev(rev), pub)
690 if not_merged:
691 if upstream_gain:
692 # The user has published this branch and some of those
693 # commits are not yet merged upstream. We do not want
694 # to rewrite the published commits so we punt.
695 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700696 syncbuf.info(self,
697 "branch %s is published but is now %d commits behind",
698 branch.name,
699 len(upstream_gain))
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700700 return
Shawn O. Pearce05f66b62009-04-21 08:26:32 -0700701 elif pub == head:
702 # All published commits are merged, and thus we are a
703 # strict subset. We can fast-forward safely.
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700704 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700705 def _doff():
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700706 self._FastForward(rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700707 self._CopyFiles()
708 syncbuf.later1(self, _doff)
709 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700710
711 if merge == rev:
712 try:
713 old_merge = self.bare_git.rev_parse('%s@{1}' % merge)
714 except GitError:
715 old_merge = merge
Shawn O. Pearce07346002008-10-21 07:09:27 -0700716 if old_merge == '0000000000000000000000000000000000000000' \
717 or old_merge == '':
718 old_merge = merge
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700719 else:
720 # The upstream switched on us. Time to cross our fingers
721 # and pray that the old upstream also wasn't in the habit
722 # of rebasing itself.
723 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700724 syncbuf.info(self, "manifest switched %s...%s", merge, rev)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700725 old_merge = merge
726
727 if rev == old_merge:
728 upstream_lost = []
729 else:
730 upstream_lost = self._revlist(not_rev(rev), old_merge)
731
732 if not upstream_lost and not upstream_gain:
733 # Trivially no changes caused by the upstream.
734 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700735 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700736
737 if self.IsDirty(consider_untracked=False):
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700738 syncbuf.fail(self, _DirtyError())
739 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700740
741 if upstream_lost:
742 # Upstream rebased. Not everything in HEAD
743 # may have been caused by the user.
744 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700745 syncbuf.info(self,
746 "discarding %d commits removed from upstream",
747 len(upstream_lost))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700748
749 branch.remote = rem
750 branch.merge = self.revision
751 branch.Save()
752
753 my_changes = self._revlist(not_rev(old_merge), HEAD)
754 if my_changes:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700755 def _dorebase():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700756 self._Rebase(upstream = old_merge, onto = rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700757 self._CopyFiles()
758 syncbuf.later2(self, _dorebase)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700759 elif upstream_lost:
760 try:
761 self._ResetHard(rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700762 self._CopyFiles()
763 except GitError, e:
764 syncbuf.fail(self, e)
765 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700766 else:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700767 def _doff():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700768 self._FastForward(rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700769 self._CopyFiles()
770 syncbuf.later1(self, _doff)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700771
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800772 def AddCopyFile(self, src, dest, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700773 # dest should already be an absolute path, but src is project relative
774 # make src an absolute path
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800775 abssrc = os.path.join(self.worktree, src)
776 self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700777
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700778 def DownloadPatchSet(self, change_id, patch_id):
779 """Download a single patch set of a single change to FETCH_HEAD.
780 """
781 remote = self.GetRemote(self.remote.name)
782
783 cmd = ['fetch', remote.name]
784 cmd.append('refs/changes/%2.2d/%d/%d' \
785 % (change_id % 100, change_id, patch_id))
786 cmd.extend(map(lambda x: str(x), remote.fetch))
787 if GitCommand(self, cmd, bare=True).Wait() != 0:
788 return None
789 return DownloadedChange(self,
790 remote.ToLocal(self.revision),
791 change_id,
792 patch_id,
793 self.bare_git.rev_parse('FETCH_HEAD'))
794
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700795
796## Branch Management ##
797
798 def StartBranch(self, name):
799 """Create a new branch off the manifest's revision.
800 """
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700801 head = self.work_git.GetHead()
802 if head == (R_HEADS + name):
803 return True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700804
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700805 all = self.bare_ref.all
806 if (R_HEADS + name) in all:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700807 return GitCommand(self,
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700808 ['checkout', name, '--'],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700809 capture_stdout = True,
810 capture_stderr = True).Wait() == 0
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700811
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700812 branch = self.GetBranch(name)
813 branch.remote = self.GetRemote(self.remote.name)
814 branch.merge = self.revision
815
816 rev = branch.LocalMerge
817 if rev in all:
818 revid = all[rev]
819 elif IsId(rev):
820 revid = rev
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700821 else:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700822 revid = None
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700823
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700824 if head.startswith(R_HEADS):
825 try:
826 head = all[head]
827 except KeyError:
828 head = None
829
830 if revid and head and revid == head:
831 ref = os.path.join(self.gitdir, R_HEADS + name)
832 try:
833 os.makedirs(os.path.dirname(ref))
834 except OSError:
835 pass
836 _lwrite(ref, '%s\n' % revid)
837 _lwrite(os.path.join(self.worktree, '.git', HEAD),
838 'ref: %s%s\n' % (R_HEADS, name))
839 branch.Save()
840 return True
841
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700842 if GitCommand(self,
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700843 ['checkout', '-b', branch.name, rev],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700844 capture_stdout = True,
845 capture_stderr = True).Wait() == 0:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700846 branch.Save()
847 return True
848 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700849
Wink Saville02d79452009-04-10 13:01:24 -0700850 def CheckoutBranch(self, name):
851 """Checkout a local topic branch.
852 """
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700853 rev = R_HEADS + name
854 head = self.work_git.GetHead()
855 if head == rev:
856 # Already on the branch
857 #
858 return True
Wink Saville02d79452009-04-10 13:01:24 -0700859
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700860 all = self.bare_ref.all
Wink Saville02d79452009-04-10 13:01:24 -0700861 try:
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700862 revid = all[rev]
863 except KeyError:
864 # Branch does not exist in this project
865 #
866 return False
867
868 if head.startswith(R_HEADS):
869 try:
870 head = all[head]
871 except KeyError:
872 head = None
873
874 if head == revid:
875 # Same revision; just update HEAD to point to the new
876 # target branch, but otherwise take no other action.
877 #
878 _lwrite(os.path.join(self.worktree, '.git', HEAD),
879 'ref: %s%s\n' % (R_HEADS, name))
880 return True
Wink Saville02d79452009-04-10 13:01:24 -0700881
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700882 return GitCommand(self,
883 ['checkout', name, '--'],
884 capture_stdout = True,
885 capture_stderr = True).Wait() == 0
Wink Saville02d79452009-04-10 13:01:24 -0700886
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800887 def AbandonBranch(self, name):
888 """Destroy a local topic branch.
889 """
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700890 rev = R_HEADS + name
891 all = self.bare_ref.all
892 if rev not in all:
893 # Doesn't exist; assume already abandoned.
894 #
895 return True
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800896
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700897 head = self.work_git.GetHead()
898 if head == rev:
899 # We can't destroy the branch while we are sitting
900 # on it. Switch to a detached HEAD.
901 #
902 head = all[head]
903
904 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
905 if rev in all:
906 revid = all[rev]
907 elif IsId(rev):
908 revid = rev
909 else:
910 revid = None
911
912 if revid and head == revid:
913 _lwrite(os.path.join(self.worktree, '.git', HEAD),
914 '%s\n' % revid)
915 else:
916 self._Checkout(rev, quiet=True)
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800917
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700918 return GitCommand(self,
919 ['branch', '-D', name],
920 capture_stdout = True,
921 capture_stderr = True).Wait() == 0
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800922
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700923 def PruneHeads(self):
924 """Prune any topic branches already merged into upstream.
925 """
926 cb = self.CurrentBranch
927 kill = []
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800928 left = self._allrefs
929 for name in left.keys():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700930 if name.startswith(R_HEADS):
931 name = name[len(R_HEADS):]
932 if cb is None or name != cb:
933 kill.append(name)
934
935 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
936 if cb is not None \
937 and not self._revlist(HEAD + '...' + rev) \
938 and not self.IsDirty(consider_untracked = False):
939 self.work_git.DetachHead(HEAD)
940 kill.append(cb)
941
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700942 if kill:
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700943 old = self.bare_git.GetHead()
944 if old is None:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700945 old = 'refs/heads/please_never_use_this_as_a_branch_name'
946
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700947 try:
948 self.bare_git.DetachHead(rev)
949
950 b = ['branch', '-d']
951 b.extend(kill)
952 b = GitCommand(self, b, bare=True,
953 capture_stdout=True,
954 capture_stderr=True)
955 b.Wait()
956 finally:
957 self.bare_git.SetHead(old)
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800958 left = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700959
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800960 for branch in kill:
961 if (R_HEADS + branch) not in left:
962 self.CleanPublishedCache()
963 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700964
965 if cb and cb not in kill:
966 kill.append(cb)
Shawn O. Pearce7c6c64d2009-03-02 12:38:13 -0800967 kill.sort()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700968
969 kept = []
970 for branch in kill:
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800971 if (R_HEADS + branch) in left:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700972 branch = self.GetBranch(branch)
973 base = branch.LocalMerge
974 if not base:
975 base = rev
976 kept.append(ReviewableBranch(self, branch, base))
977 return kept
978
979
980## Direct Git Commands ##
981
982 def _RemoteFetch(self, name=None):
983 if not name:
984 name = self.remote.name
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700985
986 ssh_proxy = False
987 if self.GetRemote(name).PreConnectFetch():
988 ssh_proxy = True
989
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800990 cmd = ['fetch']
991 if not self.worktree:
992 cmd.append('--update-head-ok')
993 cmd.append(name)
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700994 return GitCommand(self,
995 cmd,
996 bare = True,
997 ssh_proxy = ssh_proxy).Wait() == 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700998
999 def _Checkout(self, rev, quiet=False):
1000 cmd = ['checkout']
1001 if quiet:
1002 cmd.append('-q')
1003 cmd.append(rev)
1004 cmd.append('--')
1005 if GitCommand(self, cmd).Wait() != 0:
1006 if self._allrefs:
1007 raise GitError('%s checkout %s ' % (self.name, rev))
1008
1009 def _ResetHard(self, rev, quiet=True):
1010 cmd = ['reset', '--hard']
1011 if quiet:
1012 cmd.append('-q')
1013 cmd.append(rev)
1014 if GitCommand(self, cmd).Wait() != 0:
1015 raise GitError('%s reset --hard %s ' % (self.name, rev))
1016
1017 def _Rebase(self, upstream, onto = None):
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07001018 cmd = ['rebase']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001019 if onto is not None:
1020 cmd.extend(['--onto', onto])
1021 cmd.append(upstream)
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07001022 if GitCommand(self, cmd).Wait() != 0:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001023 raise GitError('%s rebase %s ' % (self.name, upstream))
1024
1025 def _FastForward(self, head):
1026 cmd = ['merge', head]
1027 if GitCommand(self, cmd).Wait() != 0:
1028 raise GitError('%s merge %s ' % (self.name, head))
1029
1030 def _InitGitDir(self):
1031 if not os.path.exists(self.gitdir):
1032 os.makedirs(self.gitdir)
1033 self.bare_git.init()
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -08001034
1035 if self.manifest.IsMirror:
1036 self.config.SetString('core.bare', 'true')
1037 else:
1038 self.config.SetString('core.bare', None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001039
1040 hooks = self._gitdir_path('hooks')
Shawn O. Pearcede646812008-10-29 14:38:12 -07001041 try:
1042 to_rm = os.listdir(hooks)
1043 except OSError:
1044 to_rm = []
1045 for old_hook in to_rm:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001046 os.remove(os.path.join(hooks, old_hook))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001047 self._InitHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001048
1049 m = self.manifest.manifestProject.config
1050 for key in ['user.name', 'user.email']:
1051 if m.Has(key, include_defaults = False):
1052 self.config.SetString(key, m.GetString(key))
1053
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001054 def _InitHooks(self):
1055 hooks = self._gitdir_path('hooks')
1056 if not os.path.exists(hooks):
1057 os.makedirs(hooks)
1058 for stock_hook in repo_hooks():
1059 dst = os.path.join(hooks, os.path.basename(stock_hook))
1060 try:
1061 os.symlink(relpath(stock_hook, dst), dst)
1062 except OSError, e:
1063 if e.errno == errno.EEXIST:
1064 pass
1065 elif e.errno == errno.EPERM:
1066 raise GitError('filesystem must support symlinks')
1067 else:
1068 raise
1069
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001070 def _InitRemote(self):
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001071 if self.remote.url:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001072 remote = self.GetRemote(self.remote.name)
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001073 remote.url = self.remote.url
1074 remote.review = self.remote.review
1075 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001076
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001077 if self.worktree:
1078 remote.ResetFetch(mirror=False)
1079 else:
1080 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001081 remote.Save()
1082
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001083 def _InitMRef(self):
1084 if self.manifest.branch:
1085 msg = 'manifest set to %s' % self.revision
1086 ref = R_M + self.manifest.branch
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -07001087 cur = self.bare_ref.symref(ref)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001088
1089 if IsId(self.revision):
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -07001090 if cur != '' or self.bare_ref.get(ref) != self.revision:
1091 dst = self.revision + '^0'
1092 self.bare_git.UpdateRef(ref, dst, message = msg, detach = True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001093 else:
1094 remote = self.GetRemote(self.remote.name)
1095 dst = remote.ToLocal(self.revision)
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -07001096 if cur != dst:
1097 self.bare_git.symbolic_ref('-m', msg, ref, dst)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001098
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001099 def _InitMirrorHead(self):
1100 dst = self.GetRemote(self.remote.name).ToLocal(self.revision)
1101 msg = 'manifest set to %s' % self.revision
1102 self.bare_git.SetHead(dst, message=msg)
1103
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001104 def _InitWorkTree(self):
1105 dotgit = os.path.join(self.worktree, '.git')
1106 if not os.path.exists(dotgit):
1107 os.makedirs(dotgit)
1108
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001109 for name in ['config',
1110 'description',
1111 'hooks',
1112 'info',
1113 'logs',
1114 'objects',
1115 'packed-refs',
1116 'refs',
1117 'rr-cache',
1118 'svn']:
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001119 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001120 src = os.path.join(self.gitdir, name)
1121 dst = os.path.join(dotgit, name)
1122 os.symlink(relpath(src, dst), dst)
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001123 except OSError, e:
1124 if e.errno == errno.EPERM:
1125 raise GitError('filesystem must support symlinks')
1126 else:
1127 raise
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001128
1129 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
1130 rev = self.bare_git.rev_parse('%s^0' % rev)
1131
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -07001132 _lwrite(os.path.join(dotgit, HEAD), '%s\n' % rev)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001133
1134 cmd = ['read-tree', '--reset', '-u']
1135 cmd.append('-v')
1136 cmd.append('HEAD')
1137 if GitCommand(self, cmd).Wait() != 0:
1138 raise GitError("cannot initialize work tree")
Shawn O. Pearce93609662009-04-21 10:50:33 -07001139 self._CopyFiles()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001140
1141 def _gitdir_path(self, path):
1142 return os.path.join(self.gitdir, path)
1143
1144 def _revlist(self, *args):
1145 cmd = []
1146 cmd.extend(args)
1147 cmd.append('--')
1148 return self.work_git.rev_list(*args)
1149
1150 @property
1151 def _allrefs(self):
Shawn O. Pearced237b692009-04-17 18:49:50 -07001152 return self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001153
1154 class _GitGetByExec(object):
1155 def __init__(self, project, bare):
1156 self._project = project
1157 self._bare = bare
1158
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001159 def LsOthers(self):
1160 p = GitCommand(self._project,
1161 ['ls-files',
1162 '-z',
1163 '--others',
1164 '--exclude-standard'],
1165 bare = False,
1166 capture_stdout = True,
1167 capture_stderr = True)
1168 if p.Wait() == 0:
1169 out = p.stdout
1170 if out:
1171 return out[:-1].split("\0")
1172 return []
1173
1174 def DiffZ(self, name, *args):
1175 cmd = [name]
1176 cmd.append('-z')
1177 cmd.extend(args)
1178 p = GitCommand(self._project,
1179 cmd,
1180 bare = False,
1181 capture_stdout = True,
1182 capture_stderr = True)
1183 try:
1184 out = p.process.stdout.read()
1185 r = {}
1186 if out:
1187 out = iter(out[:-1].split('\0'))
1188 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07001189 try:
1190 info = out.next()
1191 path = out.next()
1192 except StopIteration:
1193 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001194
1195 class _Info(object):
1196 def __init__(self, path, omode, nmode, oid, nid, state):
1197 self.path = path
1198 self.src_path = None
1199 self.old_mode = omode
1200 self.new_mode = nmode
1201 self.old_id = oid
1202 self.new_id = nid
1203
1204 if len(state) == 1:
1205 self.status = state
1206 self.level = None
1207 else:
1208 self.status = state[:1]
1209 self.level = state[1:]
1210 while self.level.startswith('0'):
1211 self.level = self.level[1:]
1212
1213 info = info[1:].split(' ')
1214 info =_Info(path, *info)
1215 if info.status in ('R', 'C'):
1216 info.src_path = info.path
1217 info.path = out.next()
1218 r[info.path] = info
1219 return r
1220 finally:
1221 p.Wait()
1222
1223 def GetHead(self):
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001224 if self._bare:
1225 path = os.path.join(self._project.gitdir, HEAD)
1226 else:
1227 path = os.path.join(self._project.worktree, '.git', HEAD)
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -07001228 fd = open(path, 'rb')
1229 try:
1230 line = fd.read()
1231 finally:
1232 fd.close()
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001233 if line.startswith('ref: '):
1234 return line[5:-1]
1235 return line[:-1]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001236
1237 def SetHead(self, ref, message=None):
1238 cmdv = []
1239 if message is not None:
1240 cmdv.extend(['-m', message])
1241 cmdv.append(HEAD)
1242 cmdv.append(ref)
1243 self.symbolic_ref(*cmdv)
1244
1245 def DetachHead(self, new, message=None):
1246 cmdv = ['--no-deref']
1247 if message is not None:
1248 cmdv.extend(['-m', message])
1249 cmdv.append(HEAD)
1250 cmdv.append(new)
1251 self.update_ref(*cmdv)
1252
1253 def UpdateRef(self, name, new, old=None,
1254 message=None,
1255 detach=False):
1256 cmdv = []
1257 if message is not None:
1258 cmdv.extend(['-m', message])
1259 if detach:
1260 cmdv.append('--no-deref')
1261 cmdv.append(name)
1262 cmdv.append(new)
1263 if old is not None:
1264 cmdv.append(old)
1265 self.update_ref(*cmdv)
1266
1267 def DeleteRef(self, name, old=None):
1268 if not old:
1269 old = self.rev_parse(name)
1270 self.update_ref('-d', name, old)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001271 self._project.bare_ref.deleted(name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001272
1273 def rev_list(self, *args):
1274 cmdv = ['rev-list']
1275 cmdv.extend(args)
1276 p = GitCommand(self._project,
1277 cmdv,
1278 bare = self._bare,
1279 capture_stdout = True,
1280 capture_stderr = True)
1281 r = []
1282 for line in p.process.stdout:
1283 r.append(line[:-1])
1284 if p.Wait() != 0:
1285 raise GitError('%s rev-list %s: %s' % (
1286 self._project.name,
1287 str(args),
1288 p.stderr))
1289 return r
1290
1291 def __getattr__(self, name):
1292 name = name.replace('_', '-')
1293 def runner(*args):
1294 cmdv = [name]
1295 cmdv.extend(args)
1296 p = GitCommand(self._project,
1297 cmdv,
1298 bare = self._bare,
1299 capture_stdout = True,
1300 capture_stderr = True)
1301 if p.Wait() != 0:
1302 raise GitError('%s %s: %s' % (
1303 self._project.name,
1304 name,
1305 p.stderr))
1306 r = p.stdout
1307 if r.endswith('\n') and r.index('\n') == len(r) - 1:
1308 return r[:-1]
1309 return r
1310 return runner
1311
1312
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001313class _PriorSyncFailedError(Exception):
1314 def __str__(self):
1315 return 'prior sync failed; rebase still in progress'
1316
1317class _DirtyError(Exception):
1318 def __str__(self):
1319 return 'contains uncommitted changes'
1320
1321class _InfoMessage(object):
1322 def __init__(self, project, text):
1323 self.project = project
1324 self.text = text
1325
1326 def Print(self, syncbuf):
1327 syncbuf.out.info('%s/: %s', self.project.relpath, self.text)
1328 syncbuf.out.nl()
1329
1330class _Failure(object):
1331 def __init__(self, project, why):
1332 self.project = project
1333 self.why = why
1334
1335 def Print(self, syncbuf):
1336 syncbuf.out.fail('error: %s/: %s',
1337 self.project.relpath,
1338 str(self.why))
1339 syncbuf.out.nl()
1340
1341class _Later(object):
1342 def __init__(self, project, action):
1343 self.project = project
1344 self.action = action
1345
1346 def Run(self, syncbuf):
1347 out = syncbuf.out
1348 out.project('project %s/', self.project.relpath)
1349 out.nl()
1350 try:
1351 self.action()
1352 out.nl()
1353 return True
1354 except GitError, e:
1355 out.nl()
1356 return False
1357
1358class _SyncColoring(Coloring):
1359 def __init__(self, config):
1360 Coloring.__init__(self, config, 'reposync')
1361 self.project = self.printer('header', attr = 'bold')
1362 self.info = self.printer('info')
1363 self.fail = self.printer('fail', fg='red')
1364
1365class SyncBuffer(object):
1366 def __init__(self, config, detach_head=False):
1367 self._messages = []
1368 self._failures = []
1369 self._later_queue1 = []
1370 self._later_queue2 = []
1371
1372 self.out = _SyncColoring(config)
1373 self.out.redirect(sys.stderr)
1374
1375 self.detach_head = detach_head
1376 self.clean = True
1377
1378 def info(self, project, fmt, *args):
1379 self._messages.append(_InfoMessage(project, fmt % args))
1380
1381 def fail(self, project, err=None):
1382 self._failures.append(_Failure(project, err))
1383 self.clean = False
1384
1385 def later1(self, project, what):
1386 self._later_queue1.append(_Later(project, what))
1387
1388 def later2(self, project, what):
1389 self._later_queue2.append(_Later(project, what))
1390
1391 def Finish(self):
1392 self._PrintMessages()
1393 self._RunLater()
1394 self._PrintMessages()
1395 return self.clean
1396
1397 def _RunLater(self):
1398 for q in ['_later_queue1', '_later_queue2']:
1399 if not self._RunQueue(q):
1400 return
1401
1402 def _RunQueue(self, queue):
1403 for m in getattr(self, queue):
1404 if not m.Run(self):
1405 self.clean = False
1406 return False
1407 setattr(self, queue, [])
1408 return True
1409
1410 def _PrintMessages(self):
1411 for m in self._messages:
1412 m.Print(self)
1413 for m in self._failures:
1414 m.Print(self)
1415
1416 self._messages = []
1417 self._failures = []
1418
1419
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001420class MetaProject(Project):
1421 """A special project housed under .repo.
1422 """
1423 def __init__(self, manifest, name, gitdir, worktree):
1424 repodir = manifest.repodir
1425 Project.__init__(self,
1426 manifest = manifest,
1427 name = name,
1428 gitdir = gitdir,
1429 worktree = worktree,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001430 remote = RemoteSpec('origin'),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001431 relpath = '.repo/%s' % name,
1432 revision = 'refs/heads/master')
1433
1434 def PreSync(self):
1435 if self.Exists:
1436 cb = self.CurrentBranch
1437 if cb:
1438 base = self.GetBranch(cb).merge
1439 if base:
1440 self.revision = base
1441
1442 @property
Shawn O. Pearcef6906872009-04-18 10:49:00 -07001443 def LastFetch(self):
1444 try:
1445 fh = os.path.join(self.gitdir, 'FETCH_HEAD')
1446 return os.path.getmtime(fh)
1447 except OSError:
1448 return 0
1449
1450 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001451 def HasChanges(self):
1452 """Has the remote received new commits not yet checked out?
1453 """
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001454 if not self.remote or not self.revision:
1455 return False
1456
1457 all = self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001458 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001459 if rev in all:
1460 revid = all[rev]
1461 else:
1462 revid = rev
1463
1464 head = self.work_git.GetHead()
1465 if head.startswith(R_HEADS):
1466 try:
1467 head = all[head]
1468 except KeyError:
1469 head = None
1470
1471 if revid == head:
1472 return False
1473 elif self._revlist(not_rev(HEAD), rev):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001474 return True
1475 return False