blob: 5a143a76dc78a0e17a3c05d1affe3f2224e81197 [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. Pearcecc6c7962009-07-03 15:29:02 -070030from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB
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,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700231 revisionExpr,
232 revisionId):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700233 self.manifest = manifest
234 self.name = name
235 self.remote = remote
236 self.gitdir = gitdir
237 self.worktree = worktree
238 self.relpath = relpath
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700239 self.revisionExpr = revisionExpr
240
241 if revisionId is None \
242 and revisionExpr \
243 and IsId(revisionExpr):
244 self.revisionId = revisionExpr
245 else:
246 self.revisionId = revisionId
247
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700248 self.snapshots = {}
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700249 self.copyfiles = []
250 self.config = GitConfig.ForRepository(
251 gitdir = self.gitdir,
252 defaults = self.manifest.globalConfig)
253
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800254 if self.worktree:
255 self.work_git = self._GitGetByExec(self, bare=False)
256 else:
257 self.work_git = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700258 self.bare_git = self._GitGetByExec(self, bare=True)
Shawn O. Pearced237b692009-04-17 18:49:50 -0700259 self.bare_ref = GitRefs(gitdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700260
261 @property
262 def Exists(self):
263 return os.path.isdir(self.gitdir)
264
265 @property
266 def CurrentBranch(self):
267 """Obtain the name of the currently checked out branch.
268 The branch name omits the 'refs/heads/' prefix.
269 None is returned if the project is on a detached HEAD.
270 """
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700271 b = self.work_git.GetHead()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700272 if b.startswith(R_HEADS):
273 return b[len(R_HEADS):]
274 return None
275
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700276 def IsRebaseInProgress(self):
277 w = self.worktree
278 g = os.path.join(w, '.git')
279 return os.path.exists(os.path.join(g, 'rebase-apply')) \
280 or os.path.exists(os.path.join(g, 'rebase-merge')) \
281 or os.path.exists(os.path.join(w, '.dotest'))
282
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700283 def IsDirty(self, consider_untracked=True):
284 """Is the working directory modified in some way?
285 """
286 self.work_git.update_index('-q',
287 '--unmerged',
288 '--ignore-missing',
289 '--refresh')
290 if self.work_git.DiffZ('diff-index','-M','--cached',HEAD):
291 return True
292 if self.work_git.DiffZ('diff-files'):
293 return True
294 if consider_untracked and self.work_git.LsOthers():
295 return True
296 return False
297
298 _userident_name = None
299 _userident_email = None
300
301 @property
302 def UserName(self):
303 """Obtain the user's personal name.
304 """
305 if self._userident_name is None:
306 self._LoadUserIdentity()
307 return self._userident_name
308
309 @property
310 def UserEmail(self):
311 """Obtain the user's email address. This is very likely
312 to be their Gerrit login.
313 """
314 if self._userident_email is None:
315 self._LoadUserIdentity()
316 return self._userident_email
317
318 def _LoadUserIdentity(self):
319 u = self.bare_git.var('GIT_COMMITTER_IDENT')
320 m = re.compile("^(.*) <([^>]*)> ").match(u)
321 if m:
322 self._userident_name = m.group(1)
323 self._userident_email = m.group(2)
324 else:
325 self._userident_name = ''
326 self._userident_email = ''
327
328 def GetRemote(self, name):
329 """Get the configuration for a single remote.
330 """
331 return self.config.GetRemote(name)
332
333 def GetBranch(self, name):
334 """Get the configuration for a single branch.
335 """
336 return self.config.GetBranch(name)
337
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700338 def GetBranches(self):
339 """Get all existing local branches.
340 """
341 current = self.CurrentBranch
Shawn O. Pearced237b692009-04-17 18:49:50 -0700342 all = self._allrefs
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700343 heads = {}
344 pubd = {}
345
346 for name, id in all.iteritems():
347 if name.startswith(R_HEADS):
348 name = name[len(R_HEADS):]
349 b = self.GetBranch(name)
350 b.current = name == current
351 b.published = None
352 b.revision = id
353 heads[name] = b
354
355 for name, id in all.iteritems():
356 if name.startswith(R_PUB):
357 name = name[len(R_PUB):]
358 b = heads.get(name)
359 if b:
360 b.published = id
361
362 return heads
363
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700364
365## Status Display ##
366
367 def PrintWorkTreeStatus(self):
368 """Prints the status of the repository to stdout.
369 """
370 if not os.path.isdir(self.worktree):
371 print ''
372 print 'project %s/' % self.relpath
373 print ' missing (run "repo sync")'
374 return
375
376 self.work_git.update_index('-q',
377 '--unmerged',
378 '--ignore-missing',
379 '--refresh')
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700380 rb = self.IsRebaseInProgress()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700381 di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD)
382 df = self.work_git.DiffZ('diff-files')
383 do = self.work_git.LsOthers()
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700384 if not rb and not di and not df and not do:
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700385 return 'CLEAN'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700386
387 out = StatusColoring(self.config)
388 out.project('project %-40s', self.relpath + '/')
389
390 branch = self.CurrentBranch
391 if branch is None:
392 out.nobranch('(*** NO BRANCH ***)')
393 else:
394 out.branch('branch %s', branch)
395 out.nl()
396
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700397 if rb:
398 out.important('prior sync failed; rebase still in progress')
399 out.nl()
400
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700401 paths = list()
402 paths.extend(di.keys())
403 paths.extend(df.keys())
404 paths.extend(do)
405
406 paths = list(set(paths))
407 paths.sort()
408
409 for p in paths:
410 try: i = di[p]
411 except KeyError: i = None
412
413 try: f = df[p]
414 except KeyError: f = None
415
416 if i: i_status = i.status.upper()
417 else: i_status = '-'
418
419 if f: f_status = f.status.lower()
420 else: f_status = '-'
421
422 if i and i.src_path:
Shawn O. Pearcefe086752009-03-03 13:49:48 -0800423 line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700424 i.src_path, p, i.level)
425 else:
426 line = ' %s%s\t%s' % (i_status, f_status, p)
427
428 if i and not f:
429 out.added('%s', line)
430 elif (i and f) or (not i and f):
431 out.changed('%s', line)
432 elif not i and not f:
433 out.untracked('%s', line)
434 else:
435 out.write('%s', line)
436 out.nl()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700437 return 'DIRTY'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700438
439 def PrintWorkTreeDiff(self):
440 """Prints the status of the repository to stdout.
441 """
442 out = DiffColoring(self.config)
443 cmd = ['diff']
444 if out.is_on:
445 cmd.append('--color')
446 cmd.append(HEAD)
447 cmd.append('--')
448 p = GitCommand(self,
449 cmd,
450 capture_stdout = True,
451 capture_stderr = True)
452 has_diff = False
453 for line in p.process.stdout:
454 if not has_diff:
455 out.nl()
456 out.project('project %s/' % self.relpath)
457 out.nl()
458 has_diff = True
459 print line[:-1]
460 p.Wait()
461
462
463## Publish / Upload ##
464
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700465 def WasPublished(self, branch, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700466 """Was the branch published (uploaded) for code review?
467 If so, returns the SHA-1 hash of the last published
468 state for the branch.
469 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700470 key = R_PUB + branch
471 if all is None:
472 try:
473 return self.bare_git.rev_parse(key)
474 except GitError:
475 return None
476 else:
477 try:
478 return all[key]
479 except KeyError:
480 return None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700481
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700482 def CleanPublishedCache(self, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700483 """Prunes any stale published refs.
484 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700485 if all is None:
486 all = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700487 heads = set()
488 canrm = {}
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700489 for name, id in all.iteritems():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700490 if name.startswith(R_HEADS):
491 heads.add(name)
492 elif name.startswith(R_PUB):
493 canrm[name] = id
494
495 for name, id in canrm.iteritems():
496 n = name[len(R_PUB):]
497 if R_HEADS + n not in heads:
498 self.bare_git.DeleteRef(name, id)
499
500 def GetUploadableBranches(self):
501 """List any branches which can be uploaded for review.
502 """
503 heads = {}
504 pubed = {}
505
506 for name, id in self._allrefs.iteritems():
507 if name.startswith(R_HEADS):
508 heads[name[len(R_HEADS):]] = id
509 elif name.startswith(R_PUB):
510 pubed[name[len(R_PUB):]] = id
511
512 ready = []
513 for branch, id in heads.iteritems():
514 if branch in pubed and pubed[branch] == id:
515 continue
516
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800517 rb = self.GetUploadableBranch(branch)
518 if rb:
519 ready.append(rb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700520 return ready
521
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800522 def GetUploadableBranch(self, branch_name):
523 """Get a single uploadable branch, or None.
524 """
525 branch = self.GetBranch(branch_name)
526 base = branch.LocalMerge
527 if branch.LocalMerge:
528 rb = ReviewableBranch(self, branch, base)
529 if rb.commits:
530 return rb
531 return None
532
Joe Onorato2896a792008-11-17 16:56:36 -0500533 def UploadForReview(self, branch=None, replace_changes=None, people=([],[])):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700534 """Uploads the named branch for code review.
535 """
536 if branch is None:
537 branch = self.CurrentBranch
538 if branch is None:
539 raise GitError('not currently on a branch')
540
541 branch = self.GetBranch(branch)
542 if not branch.LocalMerge:
543 raise GitError('branch %s does not track a remote' % branch.name)
544 if not branch.remote.review:
545 raise GitError('remote %s has no review url' % branch.remote.name)
546
547 dest_branch = branch.merge
548 if not dest_branch.startswith(R_HEADS):
549 dest_branch = R_HEADS + dest_branch
550
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800551 if not branch.remote.projectname:
552 branch.remote.projectname = self.name
553 branch.remote.Save()
554
Shawn O. Pearce370e3fa2009-01-26 10:55:39 -0800555 if branch.remote.ReviewProtocol == 'ssh':
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800556 if dest_branch.startswith(R_HEADS):
557 dest_branch = dest_branch[len(R_HEADS):]
558
559 rp = ['gerrit receive-pack']
560 for e in people[0]:
561 rp.append('--reviewer=%s' % sq(e))
562 for e in people[1]:
563 rp.append('--cc=%s' % sq(e))
564
565 cmd = ['push']
566 cmd.append('--receive-pack=%s' % " ".join(rp))
567 cmd.append(branch.remote.SshReviewUrl(self.UserEmail))
568 cmd.append('%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch))
569 if replace_changes:
570 for change_id,commit_id in replace_changes.iteritems():
571 cmd.append('%s:refs/changes/%s/new' % (commit_id, change_id))
572 if GitCommand(self, cmd, bare = True).Wait() != 0:
573 raise UploadError('Upload failed')
574
575 else:
576 raise UploadError('Unsupported protocol %s' \
577 % branch.remote.review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700578
579 msg = "posted to %s for %s" % (branch.remote.review, dest_branch)
580 self.bare_git.UpdateRef(R_PUB + branch.name,
581 R_HEADS + branch.name,
582 message = msg)
583
584
585## Sync ##
586
587 def Sync_NetworkHalf(self):
588 """Perform only the network IO portion of the sync process.
589 Local working directory/branch state is not affected.
590 """
591 if not self.Exists:
592 print >>sys.stderr
593 print >>sys.stderr, 'Initializing project %s ...' % self.name
594 self._InitGitDir()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800595
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700596 self._InitRemote()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700597 if not self._RemoteFetch():
598 return False
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800599
600 if self.worktree:
Shawn O. Pearcecc6c7962009-07-03 15:29:02 -0700601 self.manifest.SetMRefs(self)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800602 else:
603 self._InitMirrorHead()
604 try:
605 os.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
606 except OSError:
607 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700608 return True
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800609
610 def PostRepoUpgrade(self):
611 self._InitHooks()
612
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700613 def _CopyFiles(self):
614 for file in self.copyfiles:
615 file._Copy()
616
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700617 def GetRevisionId(self, all=None):
618 if self.revisionId:
619 return self.revisionId
620
621 rem = self.GetRemote(self.remote.name)
622 rev = rem.ToLocal(self.revisionExpr)
623
624 if all is not None and rev in all:
625 return all[rev]
626
627 try:
628 return self.bare_git.rev_parse('--verify', '%s^0' % rev)
629 except GitError:
630 raise ManifestInvalidRevisionError(
631 'revision %s in %s not found' % (self.revisionExpr,
632 self.name))
633
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700634 def Sync_LocalHalf(self, syncbuf):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700635 """Perform only the local IO portion of the sync process.
636 Network access is not required.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700637 """
638 self._InitWorkTree()
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700639 all = self.bare_ref.all
640 self.CleanPublishedCache(all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700641
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700642 revid = self.GetRevisionId(all)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700643 head = self.work_git.GetHead()
644 if head.startswith(R_HEADS):
645 branch = head[len(R_HEADS):]
646 try:
647 head = all[head]
648 except KeyError:
649 head = None
650 else:
651 branch = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700652
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700653 if branch is None or syncbuf.detach_head:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700654 # Currently on a detached HEAD. The user is assumed to
655 # not have any local modifications worth worrying about.
656 #
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700657 if self.IsRebaseInProgress():
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700658 syncbuf.fail(self, _PriorSyncFailedError())
659 return
660
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700661 if head == revid:
662 # No changes; don't do anything further.
663 #
664 return
665
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700666 lost = self._revlist(not_rev(revid), HEAD)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700667 if lost:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700668 syncbuf.info(self, "discarding %d commits", len(lost))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700669 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700670 self._Checkout(revid, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700671 except GitError, e:
672 syncbuf.fail(self, e)
673 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700674 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700675 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700676
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700677 if head == revid:
678 # No changes; don't do anything further.
679 #
680 return
681
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700682 branch = self.GetBranch(branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700683
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700684 if not branch.LocalMerge:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700685 # The current branch has no tracking configuration.
686 # Jump off it to a deatched HEAD.
687 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700688 syncbuf.info(self,
689 "leaving %s; does not track upstream",
690 branch.name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700691 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700692 self._Checkout(revid, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700693 except GitError, e:
694 syncbuf.fail(self, e)
695 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700696 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700697 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700698
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700699 upstream_gain = self._revlist(not_rev(HEAD), revid)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700700 pub = self.WasPublished(branch.name, all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700701 if pub:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700702 not_merged = self._revlist(not_rev(revid), pub)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700703 if not_merged:
704 if upstream_gain:
705 # The user has published this branch and some of those
706 # commits are not yet merged upstream. We do not want
707 # to rewrite the published commits so we punt.
708 #
Daniel Sandler4c50dee2010-03-02 15:38:03 -0500709 syncbuf.fail(self,
710 "branch %s is published (but not merged) and is now %d commits behind"
711 % (branch.name, len(upstream_gain)))
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700712 return
Shawn O. Pearce05f66b62009-04-21 08:26:32 -0700713 elif pub == head:
714 # All published commits are merged, and thus we are a
715 # strict subset. We can fast-forward safely.
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700716 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700717 def _doff():
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700718 self._FastForward(revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700719 self._CopyFiles()
720 syncbuf.later1(self, _doff)
721 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700722
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700723 # Examine the local commits not in the remote. Find the
724 # last one attributed to this user, if any.
725 #
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700726 local_changes = self._revlist(not_rev(revid), HEAD, format='%H %ce')
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700727 last_mine = None
728 cnt_mine = 0
729 for commit in local_changes:
Shawn O. Pearceaa4982e2009-12-30 18:38:27 -0800730 commit_id, committer_email = commit.split(' ', 1)
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700731 if committer_email == self.UserEmail:
732 last_mine = commit_id
733 cnt_mine += 1
734
Shawn O. Pearceda88ff42009-06-03 11:09:12 -0700735 if not upstream_gain and cnt_mine == len(local_changes):
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700736 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700737
738 if self.IsDirty(consider_untracked=False):
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700739 syncbuf.fail(self, _DirtyError())
740 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700741
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700742 # If the upstream switched on us, warn the user.
743 #
744 if branch.merge != self.revisionExpr:
745 if branch.merge and self.revisionExpr:
746 syncbuf.info(self,
747 'manifest switched %s...%s',
748 branch.merge,
749 self.revisionExpr)
750 elif branch.merge:
751 syncbuf.info(self,
752 'manifest no longer tracks %s',
753 branch.merge)
754
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700755 if cnt_mine < len(local_changes):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700756 # Upstream rebased. Not everything in HEAD
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700757 # was created by this user.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700758 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700759 syncbuf.info(self,
760 "discarding %d commits removed from upstream",
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700761 len(local_changes) - cnt_mine)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700762
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700763 branch.remote = self.GetRemote(self.remote.name)
764 branch.merge = self.revisionExpr
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700765 branch.Save()
766
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700767 if cnt_mine > 0:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700768 def _dorebase():
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700769 self._Rebase(upstream = '%s^1' % last_mine, onto = revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700770 self._CopyFiles()
771 syncbuf.later2(self, _dorebase)
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700772 elif local_changes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700773 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700774 self._ResetHard(revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700775 self._CopyFiles()
776 except GitError, e:
777 syncbuf.fail(self, e)
778 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700779 else:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700780 def _doff():
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700781 self._FastForward(revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700782 self._CopyFiles()
783 syncbuf.later1(self, _doff)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700784
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800785 def AddCopyFile(self, src, dest, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700786 # dest should already be an absolute path, but src is project relative
787 # make src an absolute path
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800788 abssrc = os.path.join(self.worktree, src)
789 self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700790
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700791 def DownloadPatchSet(self, change_id, patch_id):
792 """Download a single patch set of a single change to FETCH_HEAD.
793 """
794 remote = self.GetRemote(self.remote.name)
795
796 cmd = ['fetch', remote.name]
797 cmd.append('refs/changes/%2.2d/%d/%d' \
798 % (change_id % 100, change_id, patch_id))
799 cmd.extend(map(lambda x: str(x), remote.fetch))
800 if GitCommand(self, cmd, bare=True).Wait() != 0:
801 return None
802 return DownloadedChange(self,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700803 self.GetRevisionId(),
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700804 change_id,
805 patch_id,
806 self.bare_git.rev_parse('FETCH_HEAD'))
807
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700808
809## Branch Management ##
810
811 def StartBranch(self, name):
812 """Create a new branch off the manifest's revision.
813 """
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700814 head = self.work_git.GetHead()
815 if head == (R_HEADS + name):
816 return True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700817
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700818 all = self.bare_ref.all
819 if (R_HEADS + name) in all:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700820 return GitCommand(self,
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700821 ['checkout', name, '--'],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700822 capture_stdout = True,
823 capture_stderr = True).Wait() == 0
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700824
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700825 branch = self.GetBranch(name)
826 branch.remote = self.GetRemote(self.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700827 branch.merge = self.revisionExpr
828 revid = self.GetRevisionId(all)
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700829
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700830 if head.startswith(R_HEADS):
831 try:
832 head = all[head]
833 except KeyError:
834 head = None
835
836 if revid and head and revid == head:
837 ref = os.path.join(self.gitdir, R_HEADS + name)
838 try:
839 os.makedirs(os.path.dirname(ref))
840 except OSError:
841 pass
842 _lwrite(ref, '%s\n' % revid)
843 _lwrite(os.path.join(self.worktree, '.git', HEAD),
844 'ref: %s%s\n' % (R_HEADS, name))
845 branch.Save()
846 return True
847
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700848 if GitCommand(self,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700849 ['checkout', '-b', branch.name, revid],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700850 capture_stdout = True,
851 capture_stderr = True).Wait() == 0:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700852 branch.Save()
853 return True
854 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700855
Wink Saville02d79452009-04-10 13:01:24 -0700856 def CheckoutBranch(self, name):
857 """Checkout a local topic branch.
858 """
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700859 rev = R_HEADS + name
860 head = self.work_git.GetHead()
861 if head == rev:
862 # Already on the branch
863 #
864 return True
Wink Saville02d79452009-04-10 13:01:24 -0700865
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700866 all = self.bare_ref.all
Wink Saville02d79452009-04-10 13:01:24 -0700867 try:
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700868 revid = all[rev]
869 except KeyError:
870 # Branch does not exist in this project
871 #
872 return False
Wink Saville02d79452009-04-10 13:01:24 -0700873
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700874 if head.startswith(R_HEADS):
875 try:
876 head = all[head]
877 except KeyError:
878 head = None
879
880 if head == revid:
881 # Same revision; just update HEAD to point to the new
882 # target branch, but otherwise take no other action.
883 #
884 _lwrite(os.path.join(self.worktree, '.git', HEAD),
885 'ref: %s%s\n' % (R_HEADS, name))
886 return True
887
888 return GitCommand(self,
889 ['checkout', name, '--'],
890 capture_stdout = True,
891 capture_stderr = True).Wait() == 0
Wink Saville02d79452009-04-10 13:01:24 -0700892
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800893 def AbandonBranch(self, name):
894 """Destroy a local topic branch.
895 """
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700896 rev = R_HEADS + name
897 all = self.bare_ref.all
898 if rev not in all:
899 # Doesn't exist; assume already abandoned.
900 #
901 return True
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800902
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700903 head = self.work_git.GetHead()
904 if head == rev:
905 # We can't destroy the branch while we are sitting
906 # on it. Switch to a detached HEAD.
907 #
908 head = all[head]
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800909
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700910 revid = self.GetRevisionId(all)
911 if head == revid:
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700912 _lwrite(os.path.join(self.worktree, '.git', HEAD),
913 '%s\n' % revid)
914 else:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700915 self._Checkout(revid, quiet=True)
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700916
917 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
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700934 rev = self.GetRevisionId(left)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700935 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():
Shawn O. Pearcea949fa52009-08-22 18:17:46 -07001058 name = os.path.basename(stock_hook)
1059
1060 if name in ('commit-msg') and not self.remote.review:
1061 # Don't install a Gerrit Code Review hook if this
1062 # project does not appear to use it for reviews.
1063 #
1064 continue
1065
1066 dst = os.path.join(hooks, name)
1067 if os.path.islink(dst):
1068 continue
1069 if os.path.exists(dst):
1070 if filecmp.cmp(stock_hook, dst, shallow=False):
1071 os.remove(dst)
1072 else:
1073 _error("%s: Not replacing %s hook", self.relpath, name)
1074 continue
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001075 try:
1076 os.symlink(relpath(stock_hook, dst), dst)
1077 except OSError, e:
Shawn O. Pearcea949fa52009-08-22 18:17:46 -07001078 if e.errno == errno.EPERM:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001079 raise GitError('filesystem must support symlinks')
1080 else:
1081 raise
1082
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001083 def _InitRemote(self):
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001084 if self.remote.url:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001085 remote = self.GetRemote(self.remote.name)
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001086 remote.url = self.remote.url
1087 remote.review = self.remote.review
1088 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001089
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001090 if self.worktree:
1091 remote.ResetFetch(mirror=False)
1092 else:
1093 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001094 remote.Save()
1095
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001096 def _InitMirrorHead(self):
Shawn O. Pearcefe200ee2009-06-01 15:28:21 -07001097 self._InitAnyMRef(HEAD)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001098
1099 def _InitAnyMRef(self, ref):
1100 cur = self.bare_ref.symref(ref)
1101
1102 if self.revisionId:
1103 if cur != '' or self.bare_ref.get(ref) != self.revisionId:
1104 msg = 'manifest set to %s' % self.revisionId
1105 dst = self.revisionId + '^0'
1106 self.bare_git.UpdateRef(ref, dst, message = msg, detach = True)
1107 else:
1108 remote = self.GetRemote(self.remote.name)
1109 dst = remote.ToLocal(self.revisionExpr)
1110 if cur != dst:
1111 msg = 'manifest set to %s' % self.revisionExpr
1112 self.bare_git.symbolic_ref('-m', msg, ref, dst)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001113
Shawn O. Pearceca3d8ff2009-06-04 19:49:36 -07001114 def _LinkWorkTree(self, relink=False):
1115 dotgit = os.path.join(self.worktree, '.git')
1116 if not relink:
1117 os.makedirs(dotgit)
1118
1119 for name in ['config',
1120 'description',
1121 'hooks',
1122 'info',
1123 'logs',
1124 'objects',
1125 'packed-refs',
1126 'refs',
1127 'rr-cache',
1128 'svn']:
1129 try:
1130 src = os.path.join(self.gitdir, name)
1131 dst = os.path.join(dotgit, name)
1132 if relink:
1133 os.remove(dst)
Shawn O. Pearce69b1e8a2010-03-06 19:29:56 -08001134 if os.path.islink(dst) or not os.path.exists(dst):
1135 os.symlink(relpath(src, dst), dst)
1136 else:
1137 raise GitError('cannot overwrite a local work tree')
Shawn O. Pearceca3d8ff2009-06-04 19:49:36 -07001138 except OSError, e:
1139 if e.errno == errno.EPERM:
1140 raise GitError('filesystem must support symlinks')
1141 else:
1142 raise
1143
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001144 def _InitWorkTree(self):
1145 dotgit = os.path.join(self.worktree, '.git')
1146 if not os.path.exists(dotgit):
Shawn O. Pearceca3d8ff2009-06-04 19:49:36 -07001147 self._LinkWorkTree()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001148
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001149 _lwrite(os.path.join(dotgit, HEAD), '%s\n' % self.GetRevisionId())
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001150
1151 cmd = ['read-tree', '--reset', '-u']
1152 cmd.append('-v')
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001153 cmd.append(HEAD)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001154 if GitCommand(self, cmd).Wait() != 0:
1155 raise GitError("cannot initialize work tree")
Shawn O. Pearce93609662009-04-21 10:50:33 -07001156 self._CopyFiles()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001157
1158 def _gitdir_path(self, path):
1159 return os.path.join(self.gitdir, path)
1160
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001161 def _revlist(self, *args, **kw):
1162 a = []
1163 a.extend(args)
1164 a.append('--')
1165 return self.work_git.rev_list(*a, **kw)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001166
1167 @property
1168 def _allrefs(self):
Shawn O. Pearced237b692009-04-17 18:49:50 -07001169 return self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001170
1171 class _GitGetByExec(object):
1172 def __init__(self, project, bare):
1173 self._project = project
1174 self._bare = bare
1175
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001176 def LsOthers(self):
1177 p = GitCommand(self._project,
1178 ['ls-files',
1179 '-z',
1180 '--others',
1181 '--exclude-standard'],
1182 bare = False,
1183 capture_stdout = True,
1184 capture_stderr = True)
1185 if p.Wait() == 0:
1186 out = p.stdout
1187 if out:
1188 return out[:-1].split("\0")
1189 return []
1190
1191 def DiffZ(self, name, *args):
1192 cmd = [name]
1193 cmd.append('-z')
1194 cmd.extend(args)
1195 p = GitCommand(self._project,
1196 cmd,
1197 bare = False,
1198 capture_stdout = True,
1199 capture_stderr = True)
1200 try:
1201 out = p.process.stdout.read()
1202 r = {}
1203 if out:
1204 out = iter(out[:-1].split('\0'))
1205 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07001206 try:
1207 info = out.next()
1208 path = out.next()
1209 except StopIteration:
1210 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001211
1212 class _Info(object):
1213 def __init__(self, path, omode, nmode, oid, nid, state):
1214 self.path = path
1215 self.src_path = None
1216 self.old_mode = omode
1217 self.new_mode = nmode
1218 self.old_id = oid
1219 self.new_id = nid
1220
1221 if len(state) == 1:
1222 self.status = state
1223 self.level = None
1224 else:
1225 self.status = state[:1]
1226 self.level = state[1:]
1227 while self.level.startswith('0'):
1228 self.level = self.level[1:]
1229
1230 info = info[1:].split(' ')
1231 info =_Info(path, *info)
1232 if info.status in ('R', 'C'):
1233 info.src_path = info.path
1234 info.path = out.next()
1235 r[info.path] = info
1236 return r
1237 finally:
1238 p.Wait()
1239
1240 def GetHead(self):
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001241 if self._bare:
1242 path = os.path.join(self._project.gitdir, HEAD)
1243 else:
1244 path = os.path.join(self._project.worktree, '.git', HEAD)
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -07001245 fd = open(path, 'rb')
1246 try:
1247 line = fd.read()
1248 finally:
1249 fd.close()
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001250 if line.startswith('ref: '):
1251 return line[5:-1]
1252 return line[:-1]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001253
1254 def SetHead(self, ref, message=None):
1255 cmdv = []
1256 if message is not None:
1257 cmdv.extend(['-m', message])
1258 cmdv.append(HEAD)
1259 cmdv.append(ref)
1260 self.symbolic_ref(*cmdv)
1261
1262 def DetachHead(self, new, message=None):
1263 cmdv = ['--no-deref']
1264 if message is not None:
1265 cmdv.extend(['-m', message])
1266 cmdv.append(HEAD)
1267 cmdv.append(new)
1268 self.update_ref(*cmdv)
1269
1270 def UpdateRef(self, name, new, old=None,
1271 message=None,
1272 detach=False):
1273 cmdv = []
1274 if message is not None:
1275 cmdv.extend(['-m', message])
1276 if detach:
1277 cmdv.append('--no-deref')
1278 cmdv.append(name)
1279 cmdv.append(new)
1280 if old is not None:
1281 cmdv.append(old)
1282 self.update_ref(*cmdv)
1283
1284 def DeleteRef(self, name, old=None):
1285 if not old:
1286 old = self.rev_parse(name)
1287 self.update_ref('-d', name, old)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001288 self._project.bare_ref.deleted(name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001289
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001290 def rev_list(self, *args, **kw):
1291 if 'format' in kw:
1292 cmdv = ['log', '--pretty=format:%s' % kw['format']]
1293 else:
1294 cmdv = ['rev-list']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001295 cmdv.extend(args)
1296 p = GitCommand(self._project,
1297 cmdv,
1298 bare = self._bare,
1299 capture_stdout = True,
1300 capture_stderr = True)
1301 r = []
1302 for line in p.process.stdout:
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001303 if line[-1] == '\n':
1304 line = line[:-1]
1305 r.append(line)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001306 if p.Wait() != 0:
1307 raise GitError('%s rev-list %s: %s' % (
1308 self._project.name,
1309 str(args),
1310 p.stderr))
1311 return r
1312
1313 def __getattr__(self, name):
1314 name = name.replace('_', '-')
1315 def runner(*args):
1316 cmdv = [name]
1317 cmdv.extend(args)
1318 p = GitCommand(self._project,
1319 cmdv,
1320 bare = self._bare,
1321 capture_stdout = True,
1322 capture_stderr = True)
1323 if p.Wait() != 0:
1324 raise GitError('%s %s: %s' % (
1325 self._project.name,
1326 name,
1327 p.stderr))
1328 r = p.stdout
1329 if r.endswith('\n') and r.index('\n') == len(r) - 1:
1330 return r[:-1]
1331 return r
1332 return runner
1333
1334
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001335class _PriorSyncFailedError(Exception):
1336 def __str__(self):
1337 return 'prior sync failed; rebase still in progress'
1338
1339class _DirtyError(Exception):
1340 def __str__(self):
1341 return 'contains uncommitted changes'
1342
1343class _InfoMessage(object):
1344 def __init__(self, project, text):
1345 self.project = project
1346 self.text = text
1347
1348 def Print(self, syncbuf):
1349 syncbuf.out.info('%s/: %s', self.project.relpath, self.text)
1350 syncbuf.out.nl()
1351
1352class _Failure(object):
1353 def __init__(self, project, why):
1354 self.project = project
1355 self.why = why
1356
1357 def Print(self, syncbuf):
1358 syncbuf.out.fail('error: %s/: %s',
1359 self.project.relpath,
1360 str(self.why))
1361 syncbuf.out.nl()
1362
1363class _Later(object):
1364 def __init__(self, project, action):
1365 self.project = project
1366 self.action = action
1367
1368 def Run(self, syncbuf):
1369 out = syncbuf.out
1370 out.project('project %s/', self.project.relpath)
1371 out.nl()
1372 try:
1373 self.action()
1374 out.nl()
1375 return True
1376 except GitError, e:
1377 out.nl()
1378 return False
1379
1380class _SyncColoring(Coloring):
1381 def __init__(self, config):
1382 Coloring.__init__(self, config, 'reposync')
1383 self.project = self.printer('header', attr = 'bold')
1384 self.info = self.printer('info')
1385 self.fail = self.printer('fail', fg='red')
1386
1387class SyncBuffer(object):
1388 def __init__(self, config, detach_head=False):
1389 self._messages = []
1390 self._failures = []
1391 self._later_queue1 = []
1392 self._later_queue2 = []
1393
1394 self.out = _SyncColoring(config)
1395 self.out.redirect(sys.stderr)
1396
1397 self.detach_head = detach_head
1398 self.clean = True
1399
1400 def info(self, project, fmt, *args):
1401 self._messages.append(_InfoMessage(project, fmt % args))
1402
1403 def fail(self, project, err=None):
1404 self._failures.append(_Failure(project, err))
1405 self.clean = False
1406
1407 def later1(self, project, what):
1408 self._later_queue1.append(_Later(project, what))
1409
1410 def later2(self, project, what):
1411 self._later_queue2.append(_Later(project, what))
1412
1413 def Finish(self):
1414 self._PrintMessages()
1415 self._RunLater()
1416 self._PrintMessages()
1417 return self.clean
1418
1419 def _RunLater(self):
1420 for q in ['_later_queue1', '_later_queue2']:
1421 if not self._RunQueue(q):
1422 return
1423
1424 def _RunQueue(self, queue):
1425 for m in getattr(self, queue):
1426 if not m.Run(self):
1427 self.clean = False
1428 return False
1429 setattr(self, queue, [])
1430 return True
1431
1432 def _PrintMessages(self):
1433 for m in self._messages:
1434 m.Print(self)
1435 for m in self._failures:
1436 m.Print(self)
1437
1438 self._messages = []
1439 self._failures = []
1440
1441
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001442class MetaProject(Project):
1443 """A special project housed under .repo.
1444 """
Shawn O. Pearcea7ce0962009-07-03 18:04:27 -07001445 def __init__(self, manifest, name, gitdir, worktree, relpath=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001446 repodir = manifest.repodir
Shawn O. Pearcea7ce0962009-07-03 18:04:27 -07001447 if relpath is None:
1448 relpath = '.repo/%s' % name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001449 Project.__init__(self,
1450 manifest = manifest,
1451 name = name,
1452 gitdir = gitdir,
1453 worktree = worktree,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001454 remote = RemoteSpec('origin'),
Shawn O. Pearcea7ce0962009-07-03 18:04:27 -07001455 relpath = relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001456 revisionExpr = 'refs/heads/master',
1457 revisionId = None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001458
1459 def PreSync(self):
1460 if self.Exists:
1461 cb = self.CurrentBranch
1462 if cb:
Shawn O. Pearce5f947bb2009-07-03 17:24:17 -07001463 cb = self.GetBranch(cb)
1464 if cb.merge:
1465 self.revisionExpr = cb.merge
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001466 self.revisionId = None
Shawn O. Pearce5f947bb2009-07-03 17:24:17 -07001467 if cb.remote and cb.remote.name:
1468 self.remote.name = cb.remote.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001469
1470 @property
Shawn O. Pearcef6906872009-04-18 10:49:00 -07001471 def LastFetch(self):
1472 try:
1473 fh = os.path.join(self.gitdir, 'FETCH_HEAD')
1474 return os.path.getmtime(fh)
1475 except OSError:
1476 return 0
1477
1478 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001479 def HasChanges(self):
1480 """Has the remote received new commits not yet checked out?
1481 """
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001482 if not self.remote or not self.revisionExpr:
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001483 return False
1484
1485 all = self.bare_ref.all
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001486 revid = self.GetRevisionId(all)
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001487 head = self.work_git.GetHead()
1488 if head.startswith(R_HEADS):
1489 try:
1490 head = all[head]
1491 except KeyError:
1492 head = None
1493
1494 if revid == head:
1495 return False
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001496 elif self._revlist(not_rev(HEAD), revid):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001497 return True
1498 return False