blob: cab47152b66abe45cf98b66aae5033d353235fbe [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
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700711 # If the upstream switched on us, warn the user.
712 #
713 if merge != rev:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700714 syncbuf.info(self, "manifest switched %s...%s", merge, rev)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700715
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700716 # Examine the local commits not in the remote. Find the
717 # last one attributed to this user, if any.
718 #
719 local_changes = self._revlist(
720 not_rev(merge),
721 HEAD,
722 format='%H %ce')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700723
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700724 last_mine = None
725 cnt_mine = 0
726 for commit in local_changes:
727 commit_id, committer_email = commit.split(' ', 2)
728 if committer_email == self.UserEmail:
729 last_mine = commit_id
730 cnt_mine += 1
731
732 if not local_changes and not upstream_gain:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700733 # 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
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700741 if cnt_mine < len(local_changes):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700742 # Upstream rebased. Not everything in HEAD
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700743 # was created by this user.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700744 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700745 syncbuf.info(self,
746 "discarding %d commits removed from upstream",
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700747 len(local_changes) - cnt_mine)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700748
749 branch.remote = rem
750 branch.merge = self.revision
751 branch.Save()
752
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700753 if cnt_mine > 0:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700754 def _dorebase():
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700755 self._Rebase(upstream = '%s^1' % last_mine, onto = rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700756 self._CopyFiles()
757 syncbuf.later2(self, _dorebase)
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700758 elif local_changes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700759 try:
760 self._ResetHard(rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700761 self._CopyFiles()
762 except GitError, e:
763 syncbuf.fail(self, e)
764 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700765 else:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700766 def _doff():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700767 self._FastForward(rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700768 self._CopyFiles()
769 syncbuf.later1(self, _doff)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700770
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800771 def AddCopyFile(self, src, dest, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700772 # dest should already be an absolute path, but src is project relative
773 # make src an absolute path
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800774 abssrc = os.path.join(self.worktree, src)
775 self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700776
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700777 def DownloadPatchSet(self, change_id, patch_id):
778 """Download a single patch set of a single change to FETCH_HEAD.
779 """
780 remote = self.GetRemote(self.remote.name)
781
782 cmd = ['fetch', remote.name]
783 cmd.append('refs/changes/%2.2d/%d/%d' \
784 % (change_id % 100, change_id, patch_id))
785 cmd.extend(map(lambda x: str(x), remote.fetch))
786 if GitCommand(self, cmd, bare=True).Wait() != 0:
787 return None
788 return DownloadedChange(self,
789 remote.ToLocal(self.revision),
790 change_id,
791 patch_id,
792 self.bare_git.rev_parse('FETCH_HEAD'))
793
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700794
795## Branch Management ##
796
797 def StartBranch(self, name):
798 """Create a new branch off the manifest's revision.
799 """
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700800 head = self.work_git.GetHead()
801 if head == (R_HEADS + name):
802 return True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700803
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700804 all = self.bare_ref.all
805 if (R_HEADS + name) in all:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700806 return GitCommand(self,
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700807 ['checkout', name, '--'],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700808 capture_stdout = True,
809 capture_stderr = True).Wait() == 0
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700810
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700811 branch = self.GetBranch(name)
812 branch.remote = self.GetRemote(self.remote.name)
813 branch.merge = self.revision
814
815 rev = branch.LocalMerge
816 if rev in all:
817 revid = all[rev]
818 elif IsId(rev):
819 revid = rev
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700820 else:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700821 revid = None
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700822
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700823 if head.startswith(R_HEADS):
824 try:
825 head = all[head]
826 except KeyError:
827 head = None
828
829 if revid and head and revid == head:
830 ref = os.path.join(self.gitdir, R_HEADS + name)
831 try:
832 os.makedirs(os.path.dirname(ref))
833 except OSError:
834 pass
835 _lwrite(ref, '%s\n' % revid)
836 _lwrite(os.path.join(self.worktree, '.git', HEAD),
837 'ref: %s%s\n' % (R_HEADS, name))
838 branch.Save()
839 return True
840
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700841 if GitCommand(self,
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700842 ['checkout', '-b', branch.name, rev],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700843 capture_stdout = True,
844 capture_stderr = True).Wait() == 0:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700845 branch.Save()
846 return True
847 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700848
Wink Saville02d79452009-04-10 13:01:24 -0700849 def CheckoutBranch(self, name):
850 """Checkout a local topic branch.
851 """
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700852 rev = R_HEADS + name
853 head = self.work_git.GetHead()
854 if head == rev:
855 # Already on the branch
856 #
857 return True
Wink Saville02d79452009-04-10 13:01:24 -0700858
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700859 all = self.bare_ref.all
Wink Saville02d79452009-04-10 13:01:24 -0700860 try:
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700861 revid = all[rev]
862 except KeyError:
863 # Branch does not exist in this project
864 #
865 return False
866
867 if head.startswith(R_HEADS):
868 try:
869 head = all[head]
870 except KeyError:
871 head = None
872
873 if head == revid:
874 # Same revision; just update HEAD to point to the new
875 # target branch, but otherwise take no other action.
876 #
877 _lwrite(os.path.join(self.worktree, '.git', HEAD),
878 'ref: %s%s\n' % (R_HEADS, name))
879 return True
Wink Saville02d79452009-04-10 13:01:24 -0700880
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700881 return GitCommand(self,
882 ['checkout', name, '--'],
883 capture_stdout = True,
884 capture_stderr = True).Wait() == 0
Wink Saville02d79452009-04-10 13:01:24 -0700885
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800886 def AbandonBranch(self, name):
887 """Destroy a local topic branch.
888 """
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700889 rev = R_HEADS + name
890 all = self.bare_ref.all
891 if rev not in all:
892 # Doesn't exist; assume already abandoned.
893 #
894 return True
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800895
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700896 head = self.work_git.GetHead()
897 if head == rev:
898 # We can't destroy the branch while we are sitting
899 # on it. Switch to a detached HEAD.
900 #
901 head = all[head]
902
903 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
904 if rev in all:
905 revid = all[rev]
906 elif IsId(rev):
907 revid = rev
908 else:
909 revid = None
910
911 if revid and head == revid:
912 _lwrite(os.path.join(self.worktree, '.git', HEAD),
913 '%s\n' % revid)
914 else:
915 self._Checkout(rev, quiet=True)
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800916
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700917 return GitCommand(self,
918 ['branch', '-D', name],
919 capture_stdout = True,
920 capture_stderr = True).Wait() == 0
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800921
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700922 def PruneHeads(self):
923 """Prune any topic branches already merged into upstream.
924 """
925 cb = self.CurrentBranch
926 kill = []
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800927 left = self._allrefs
928 for name in left.keys():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700929 if name.startswith(R_HEADS):
930 name = name[len(R_HEADS):]
931 if cb is None or name != cb:
932 kill.append(name)
933
934 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
935 if cb is not None \
936 and not self._revlist(HEAD + '...' + rev) \
937 and not self.IsDirty(consider_untracked = False):
938 self.work_git.DetachHead(HEAD)
939 kill.append(cb)
940
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700941 if kill:
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700942 old = self.bare_git.GetHead()
943 if old is None:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700944 old = 'refs/heads/please_never_use_this_as_a_branch_name'
945
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700946 try:
947 self.bare_git.DetachHead(rev)
948
949 b = ['branch', '-d']
950 b.extend(kill)
951 b = GitCommand(self, b, bare=True,
952 capture_stdout=True,
953 capture_stderr=True)
954 b.Wait()
955 finally:
956 self.bare_git.SetHead(old)
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800957 left = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700958
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800959 for branch in kill:
960 if (R_HEADS + branch) not in left:
961 self.CleanPublishedCache()
962 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700963
964 if cb and cb not in kill:
965 kill.append(cb)
Shawn O. Pearce7c6c64d2009-03-02 12:38:13 -0800966 kill.sort()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700967
968 kept = []
969 for branch in kill:
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800970 if (R_HEADS + branch) in left:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700971 branch = self.GetBranch(branch)
972 base = branch.LocalMerge
973 if not base:
974 base = rev
975 kept.append(ReviewableBranch(self, branch, base))
976 return kept
977
978
979## Direct Git Commands ##
980
981 def _RemoteFetch(self, name=None):
982 if not name:
983 name = self.remote.name
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700984
985 ssh_proxy = False
986 if self.GetRemote(name).PreConnectFetch():
987 ssh_proxy = True
988
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800989 cmd = ['fetch']
990 if not self.worktree:
991 cmd.append('--update-head-ok')
992 cmd.append(name)
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700993 return GitCommand(self,
994 cmd,
995 bare = True,
996 ssh_proxy = ssh_proxy).Wait() == 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700997
998 def _Checkout(self, rev, quiet=False):
999 cmd = ['checkout']
1000 if quiet:
1001 cmd.append('-q')
1002 cmd.append(rev)
1003 cmd.append('--')
1004 if GitCommand(self, cmd).Wait() != 0:
1005 if self._allrefs:
1006 raise GitError('%s checkout %s ' % (self.name, rev))
1007
1008 def _ResetHard(self, rev, quiet=True):
1009 cmd = ['reset', '--hard']
1010 if quiet:
1011 cmd.append('-q')
1012 cmd.append(rev)
1013 if GitCommand(self, cmd).Wait() != 0:
1014 raise GitError('%s reset --hard %s ' % (self.name, rev))
1015
1016 def _Rebase(self, upstream, onto = None):
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07001017 cmd = ['rebase']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001018 if onto is not None:
1019 cmd.extend(['--onto', onto])
1020 cmd.append(upstream)
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07001021 if GitCommand(self, cmd).Wait() != 0:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001022 raise GitError('%s rebase %s ' % (self.name, upstream))
1023
1024 def _FastForward(self, head):
1025 cmd = ['merge', head]
1026 if GitCommand(self, cmd).Wait() != 0:
1027 raise GitError('%s merge %s ' % (self.name, head))
1028
1029 def _InitGitDir(self):
1030 if not os.path.exists(self.gitdir):
1031 os.makedirs(self.gitdir)
1032 self.bare_git.init()
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -08001033
1034 if self.manifest.IsMirror:
1035 self.config.SetString('core.bare', 'true')
1036 else:
1037 self.config.SetString('core.bare', None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001038
1039 hooks = self._gitdir_path('hooks')
Shawn O. Pearcede646812008-10-29 14:38:12 -07001040 try:
1041 to_rm = os.listdir(hooks)
1042 except OSError:
1043 to_rm = []
1044 for old_hook in to_rm:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001045 os.remove(os.path.join(hooks, old_hook))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001046 self._InitHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001047
1048 m = self.manifest.manifestProject.config
1049 for key in ['user.name', 'user.email']:
1050 if m.Has(key, include_defaults = False):
1051 self.config.SetString(key, m.GetString(key))
1052
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001053 def _InitHooks(self):
1054 hooks = self._gitdir_path('hooks')
1055 if not os.path.exists(hooks):
1056 os.makedirs(hooks)
1057 for stock_hook in repo_hooks():
1058 dst = os.path.join(hooks, os.path.basename(stock_hook))
1059 try:
1060 os.symlink(relpath(stock_hook, dst), dst)
1061 except OSError, e:
1062 if e.errno == errno.EEXIST:
1063 pass
1064 elif e.errno == errno.EPERM:
1065 raise GitError('filesystem must support symlinks')
1066 else:
1067 raise
1068
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001069 def _InitRemote(self):
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001070 if self.remote.url:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001071 remote = self.GetRemote(self.remote.name)
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001072 remote.url = self.remote.url
1073 remote.review = self.remote.review
1074 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001075
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001076 if self.worktree:
1077 remote.ResetFetch(mirror=False)
1078 else:
1079 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001080 remote.Save()
1081
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001082 def _InitMRef(self):
1083 if self.manifest.branch:
1084 msg = 'manifest set to %s' % self.revision
1085 ref = R_M + self.manifest.branch
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -07001086 cur = self.bare_ref.symref(ref)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001087
1088 if IsId(self.revision):
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -07001089 if cur != '' or self.bare_ref.get(ref) != self.revision:
1090 dst = self.revision + '^0'
1091 self.bare_git.UpdateRef(ref, dst, message = msg, detach = True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001092 else:
1093 remote = self.GetRemote(self.remote.name)
1094 dst = remote.ToLocal(self.revision)
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -07001095 if cur != dst:
1096 self.bare_git.symbolic_ref('-m', msg, ref, dst)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001097
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001098 def _InitMirrorHead(self):
1099 dst = self.GetRemote(self.remote.name).ToLocal(self.revision)
1100 msg = 'manifest set to %s' % self.revision
1101 self.bare_git.SetHead(dst, message=msg)
1102
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001103 def _InitWorkTree(self):
1104 dotgit = os.path.join(self.worktree, '.git')
1105 if not os.path.exists(dotgit):
1106 os.makedirs(dotgit)
1107
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001108 for name in ['config',
1109 'description',
1110 'hooks',
1111 'info',
1112 'logs',
1113 'objects',
1114 'packed-refs',
1115 'refs',
1116 'rr-cache',
1117 'svn']:
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001118 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001119 src = os.path.join(self.gitdir, name)
1120 dst = os.path.join(dotgit, name)
1121 os.symlink(relpath(src, dst), dst)
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001122 except OSError, e:
1123 if e.errno == errno.EPERM:
1124 raise GitError('filesystem must support symlinks')
1125 else:
1126 raise
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001127
1128 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
1129 rev = self.bare_git.rev_parse('%s^0' % rev)
1130
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -07001131 _lwrite(os.path.join(dotgit, HEAD), '%s\n' % rev)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001132
1133 cmd = ['read-tree', '--reset', '-u']
1134 cmd.append('-v')
1135 cmd.append('HEAD')
1136 if GitCommand(self, cmd).Wait() != 0:
1137 raise GitError("cannot initialize work tree")
Shawn O. Pearce93609662009-04-21 10:50:33 -07001138 self._CopyFiles()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001139
1140 def _gitdir_path(self, path):
1141 return os.path.join(self.gitdir, path)
1142
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001143 def _revlist(self, *args, **kw):
1144 a = []
1145 a.extend(args)
1146 a.append('--')
1147 return self.work_git.rev_list(*a, **kw)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001148
1149 @property
1150 def _allrefs(self):
Shawn O. Pearced237b692009-04-17 18:49:50 -07001151 return self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001152
1153 class _GitGetByExec(object):
1154 def __init__(self, project, bare):
1155 self._project = project
1156 self._bare = bare
1157
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001158 def LsOthers(self):
1159 p = GitCommand(self._project,
1160 ['ls-files',
1161 '-z',
1162 '--others',
1163 '--exclude-standard'],
1164 bare = False,
1165 capture_stdout = True,
1166 capture_stderr = True)
1167 if p.Wait() == 0:
1168 out = p.stdout
1169 if out:
1170 return out[:-1].split("\0")
1171 return []
1172
1173 def DiffZ(self, name, *args):
1174 cmd = [name]
1175 cmd.append('-z')
1176 cmd.extend(args)
1177 p = GitCommand(self._project,
1178 cmd,
1179 bare = False,
1180 capture_stdout = True,
1181 capture_stderr = True)
1182 try:
1183 out = p.process.stdout.read()
1184 r = {}
1185 if out:
1186 out = iter(out[:-1].split('\0'))
1187 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07001188 try:
1189 info = out.next()
1190 path = out.next()
1191 except StopIteration:
1192 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001193
1194 class _Info(object):
1195 def __init__(self, path, omode, nmode, oid, nid, state):
1196 self.path = path
1197 self.src_path = None
1198 self.old_mode = omode
1199 self.new_mode = nmode
1200 self.old_id = oid
1201 self.new_id = nid
1202
1203 if len(state) == 1:
1204 self.status = state
1205 self.level = None
1206 else:
1207 self.status = state[:1]
1208 self.level = state[1:]
1209 while self.level.startswith('0'):
1210 self.level = self.level[1:]
1211
1212 info = info[1:].split(' ')
1213 info =_Info(path, *info)
1214 if info.status in ('R', 'C'):
1215 info.src_path = info.path
1216 info.path = out.next()
1217 r[info.path] = info
1218 return r
1219 finally:
1220 p.Wait()
1221
1222 def GetHead(self):
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001223 if self._bare:
1224 path = os.path.join(self._project.gitdir, HEAD)
1225 else:
1226 path = os.path.join(self._project.worktree, '.git', HEAD)
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -07001227 fd = open(path, 'rb')
1228 try:
1229 line = fd.read()
1230 finally:
1231 fd.close()
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001232 if line.startswith('ref: '):
1233 return line[5:-1]
1234 return line[:-1]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001235
1236 def SetHead(self, ref, message=None):
1237 cmdv = []
1238 if message is not None:
1239 cmdv.extend(['-m', message])
1240 cmdv.append(HEAD)
1241 cmdv.append(ref)
1242 self.symbolic_ref(*cmdv)
1243
1244 def DetachHead(self, new, message=None):
1245 cmdv = ['--no-deref']
1246 if message is not None:
1247 cmdv.extend(['-m', message])
1248 cmdv.append(HEAD)
1249 cmdv.append(new)
1250 self.update_ref(*cmdv)
1251
1252 def UpdateRef(self, name, new, old=None,
1253 message=None,
1254 detach=False):
1255 cmdv = []
1256 if message is not None:
1257 cmdv.extend(['-m', message])
1258 if detach:
1259 cmdv.append('--no-deref')
1260 cmdv.append(name)
1261 cmdv.append(new)
1262 if old is not None:
1263 cmdv.append(old)
1264 self.update_ref(*cmdv)
1265
1266 def DeleteRef(self, name, old=None):
1267 if not old:
1268 old = self.rev_parse(name)
1269 self.update_ref('-d', name, old)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001270 self._project.bare_ref.deleted(name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001271
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001272 def rev_list(self, *args, **kw):
1273 if 'format' in kw:
1274 cmdv = ['log', '--pretty=format:%s' % kw['format']]
1275 else:
1276 cmdv = ['rev-list']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001277 cmdv.extend(args)
1278 p = GitCommand(self._project,
1279 cmdv,
1280 bare = self._bare,
1281 capture_stdout = True,
1282 capture_stderr = True)
1283 r = []
1284 for line in p.process.stdout:
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001285 if line[-1] == '\n':
1286 line = line[:-1]
1287 r.append(line)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001288 if p.Wait() != 0:
1289 raise GitError('%s rev-list %s: %s' % (
1290 self._project.name,
1291 str(args),
1292 p.stderr))
1293 return r
1294
1295 def __getattr__(self, name):
1296 name = name.replace('_', '-')
1297 def runner(*args):
1298 cmdv = [name]
1299 cmdv.extend(args)
1300 p = GitCommand(self._project,
1301 cmdv,
1302 bare = self._bare,
1303 capture_stdout = True,
1304 capture_stderr = True)
1305 if p.Wait() != 0:
1306 raise GitError('%s %s: %s' % (
1307 self._project.name,
1308 name,
1309 p.stderr))
1310 r = p.stdout
1311 if r.endswith('\n') and r.index('\n') == len(r) - 1:
1312 return r[:-1]
1313 return r
1314 return runner
1315
1316
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001317class _PriorSyncFailedError(Exception):
1318 def __str__(self):
1319 return 'prior sync failed; rebase still in progress'
1320
1321class _DirtyError(Exception):
1322 def __str__(self):
1323 return 'contains uncommitted changes'
1324
1325class _InfoMessage(object):
1326 def __init__(self, project, text):
1327 self.project = project
1328 self.text = text
1329
1330 def Print(self, syncbuf):
1331 syncbuf.out.info('%s/: %s', self.project.relpath, self.text)
1332 syncbuf.out.nl()
1333
1334class _Failure(object):
1335 def __init__(self, project, why):
1336 self.project = project
1337 self.why = why
1338
1339 def Print(self, syncbuf):
1340 syncbuf.out.fail('error: %s/: %s',
1341 self.project.relpath,
1342 str(self.why))
1343 syncbuf.out.nl()
1344
1345class _Later(object):
1346 def __init__(self, project, action):
1347 self.project = project
1348 self.action = action
1349
1350 def Run(self, syncbuf):
1351 out = syncbuf.out
1352 out.project('project %s/', self.project.relpath)
1353 out.nl()
1354 try:
1355 self.action()
1356 out.nl()
1357 return True
1358 except GitError, e:
1359 out.nl()
1360 return False
1361
1362class _SyncColoring(Coloring):
1363 def __init__(self, config):
1364 Coloring.__init__(self, config, 'reposync')
1365 self.project = self.printer('header', attr = 'bold')
1366 self.info = self.printer('info')
1367 self.fail = self.printer('fail', fg='red')
1368
1369class SyncBuffer(object):
1370 def __init__(self, config, detach_head=False):
1371 self._messages = []
1372 self._failures = []
1373 self._later_queue1 = []
1374 self._later_queue2 = []
1375
1376 self.out = _SyncColoring(config)
1377 self.out.redirect(sys.stderr)
1378
1379 self.detach_head = detach_head
1380 self.clean = True
1381
1382 def info(self, project, fmt, *args):
1383 self._messages.append(_InfoMessage(project, fmt % args))
1384
1385 def fail(self, project, err=None):
1386 self._failures.append(_Failure(project, err))
1387 self.clean = False
1388
1389 def later1(self, project, what):
1390 self._later_queue1.append(_Later(project, what))
1391
1392 def later2(self, project, what):
1393 self._later_queue2.append(_Later(project, what))
1394
1395 def Finish(self):
1396 self._PrintMessages()
1397 self._RunLater()
1398 self._PrintMessages()
1399 return self.clean
1400
1401 def _RunLater(self):
1402 for q in ['_later_queue1', '_later_queue2']:
1403 if not self._RunQueue(q):
1404 return
1405
1406 def _RunQueue(self, queue):
1407 for m in getattr(self, queue):
1408 if not m.Run(self):
1409 self.clean = False
1410 return False
1411 setattr(self, queue, [])
1412 return True
1413
1414 def _PrintMessages(self):
1415 for m in self._messages:
1416 m.Print(self)
1417 for m in self._failures:
1418 m.Print(self)
1419
1420 self._messages = []
1421 self._failures = []
1422
1423
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001424class MetaProject(Project):
1425 """A special project housed under .repo.
1426 """
1427 def __init__(self, manifest, name, gitdir, worktree):
1428 repodir = manifest.repodir
1429 Project.__init__(self,
1430 manifest = manifest,
1431 name = name,
1432 gitdir = gitdir,
1433 worktree = worktree,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001434 remote = RemoteSpec('origin'),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001435 relpath = '.repo/%s' % name,
1436 revision = 'refs/heads/master')
1437
1438 def PreSync(self):
1439 if self.Exists:
1440 cb = self.CurrentBranch
1441 if cb:
1442 base = self.GetBranch(cb).merge
1443 if base:
1444 self.revision = base
1445
1446 @property
Shawn O. Pearcef6906872009-04-18 10:49:00 -07001447 def LastFetch(self):
1448 try:
1449 fh = os.path.join(self.gitdir, 'FETCH_HEAD')
1450 return os.path.getmtime(fh)
1451 except OSError:
1452 return 0
1453
1454 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001455 def HasChanges(self):
1456 """Has the remote received new commits not yet checked out?
1457 """
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001458 if not self.remote or not self.revision:
1459 return False
1460
1461 all = self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001462 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001463 if rev in all:
1464 revid = all[rev]
1465 else:
1466 revid = rev
1467
1468 head = self.work_git.GetHead()
1469 if head.startswith(R_HEADS):
1470 try:
1471 head = all[head]
1472 except KeyError:
1473 head = None
1474
1475 if revid == head:
1476 return False
1477 elif self._revlist(not_rev(HEAD), rev):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001478 return True
1479 return False