blob: 4e8fa0e041676fcbfde139da847c9d9efd313547 [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)
Matthew Buckett2daf6672009-07-11 09:43:47 -0400206 else:
207 dir = os.path.dirname(dest)
208 if not os.path.isdir(dir):
209 os.makedirs(dir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700210 shutil.copy(src, dest)
211 # make the file read-only
212 mode = os.stat(dest)[stat.ST_MODE]
213 mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
214 os.chmod(dest, mode)
215 except IOError:
Shawn O. Pearce48244782009-04-16 08:25:57 -0700216 _error('Cannot copy file %s to %s', src, dest)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700217
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700218class RemoteSpec(object):
219 def __init__(self,
220 name,
221 url = None,
222 review = None):
223 self.name = name
224 self.url = url
225 self.review = review
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700226
227class Project(object):
228 def __init__(self,
229 manifest,
230 name,
231 remote,
232 gitdir,
233 worktree,
234 relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700235 revisionExpr,
236 revisionId):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700237 self.manifest = manifest
238 self.name = name
239 self.remote = remote
240 self.gitdir = gitdir
241 self.worktree = worktree
242 self.relpath = relpath
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700243 self.revisionExpr = revisionExpr
244
245 if revisionId is None \
246 and revisionExpr \
247 and IsId(revisionExpr):
248 self.revisionId = revisionExpr
249 else:
250 self.revisionId = revisionId
251
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700252 self.snapshots = {}
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700253 self.copyfiles = []
254 self.config = GitConfig.ForRepository(
255 gitdir = self.gitdir,
256 defaults = self.manifest.globalConfig)
257
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800258 if self.worktree:
259 self.work_git = self._GitGetByExec(self, bare=False)
260 else:
261 self.work_git = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700262 self.bare_git = self._GitGetByExec(self, bare=True)
Shawn O. Pearced237b692009-04-17 18:49:50 -0700263 self.bare_ref = GitRefs(gitdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700264
265 @property
266 def Exists(self):
267 return os.path.isdir(self.gitdir)
268
269 @property
270 def CurrentBranch(self):
271 """Obtain the name of the currently checked out branch.
272 The branch name omits the 'refs/heads/' prefix.
273 None is returned if the project is on a detached HEAD.
274 """
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700275 b = self.work_git.GetHead()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700276 if b.startswith(R_HEADS):
277 return b[len(R_HEADS):]
278 return None
279
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700280 def IsRebaseInProgress(self):
281 w = self.worktree
282 g = os.path.join(w, '.git')
283 return os.path.exists(os.path.join(g, 'rebase-apply')) \
284 or os.path.exists(os.path.join(g, 'rebase-merge')) \
285 or os.path.exists(os.path.join(w, '.dotest'))
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200286
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700287 def IsDirty(self, consider_untracked=True):
288 """Is the working directory modified in some way?
289 """
290 self.work_git.update_index('-q',
291 '--unmerged',
292 '--ignore-missing',
293 '--refresh')
294 if self.work_git.DiffZ('diff-index','-M','--cached',HEAD):
295 return True
296 if self.work_git.DiffZ('diff-files'):
297 return True
298 if consider_untracked and self.work_git.LsOthers():
299 return True
300 return False
301
302 _userident_name = None
303 _userident_email = None
304
305 @property
306 def UserName(self):
307 """Obtain the user's personal name.
308 """
309 if self._userident_name is None:
310 self._LoadUserIdentity()
311 return self._userident_name
312
313 @property
314 def UserEmail(self):
315 """Obtain the user's email address. This is very likely
316 to be their Gerrit login.
317 """
318 if self._userident_email is None:
319 self._LoadUserIdentity()
320 return self._userident_email
321
322 def _LoadUserIdentity(self):
323 u = self.bare_git.var('GIT_COMMITTER_IDENT')
324 m = re.compile("^(.*) <([^>]*)> ").match(u)
325 if m:
326 self._userident_name = m.group(1)
327 self._userident_email = m.group(2)
328 else:
329 self._userident_name = ''
330 self._userident_email = ''
331
332 def GetRemote(self, name):
333 """Get the configuration for a single remote.
334 """
335 return self.config.GetRemote(name)
336
337 def GetBranch(self, name):
338 """Get the configuration for a single branch.
339 """
340 return self.config.GetBranch(name)
341
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700342 def GetBranches(self):
343 """Get all existing local branches.
344 """
345 current = self.CurrentBranch
Shawn O. Pearced237b692009-04-17 18:49:50 -0700346 all = self._allrefs
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700347 heads = {}
348 pubd = {}
349
350 for name, id in all.iteritems():
351 if name.startswith(R_HEADS):
352 name = name[len(R_HEADS):]
353 b = self.GetBranch(name)
354 b.current = name == current
355 b.published = None
356 b.revision = id
357 heads[name] = b
358
359 for name, id in all.iteritems():
360 if name.startswith(R_PUB):
361 name = name[len(R_PUB):]
362 b = heads.get(name)
363 if b:
364 b.published = id
365
366 return heads
367
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700368
369## Status Display ##
370
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500371 def HasChanges(self):
372 """Returns true if there are uncommitted changes.
373 """
374 self.work_git.update_index('-q',
375 '--unmerged',
376 '--ignore-missing',
377 '--refresh')
378 if self.IsRebaseInProgress():
379 return True
380
381 if self.work_git.DiffZ('diff-index', '--cached', HEAD):
382 return True
383
384 if self.work_git.DiffZ('diff-files'):
385 return True
386
387 if self.work_git.LsOthers():
388 return True
389
390 return False
391
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700392 def PrintWorkTreeStatus(self):
393 """Prints the status of the repository to stdout.
394 """
395 if not os.path.isdir(self.worktree):
396 print ''
397 print 'project %s/' % self.relpath
398 print ' missing (run "repo sync")'
399 return
400
401 self.work_git.update_index('-q',
402 '--unmerged',
403 '--ignore-missing',
404 '--refresh')
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700405 rb = self.IsRebaseInProgress()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700406 di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD)
407 df = self.work_git.DiffZ('diff-files')
408 do = self.work_git.LsOthers()
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700409 if not rb and not di and not df and not do:
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700410 return 'CLEAN'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700411
412 out = StatusColoring(self.config)
413 out.project('project %-40s', self.relpath + '/')
414
415 branch = self.CurrentBranch
416 if branch is None:
417 out.nobranch('(*** NO BRANCH ***)')
418 else:
419 out.branch('branch %s', branch)
420 out.nl()
421
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700422 if rb:
423 out.important('prior sync failed; rebase still in progress')
424 out.nl()
425
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700426 paths = list()
427 paths.extend(di.keys())
428 paths.extend(df.keys())
429 paths.extend(do)
430
431 paths = list(set(paths))
432 paths.sort()
433
434 for p in paths:
435 try: i = di[p]
436 except KeyError: i = None
437
438 try: f = df[p]
439 except KeyError: f = None
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200440
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700441 if i: i_status = i.status.upper()
442 else: i_status = '-'
443
444 if f: f_status = f.status.lower()
445 else: f_status = '-'
446
447 if i and i.src_path:
Shawn O. Pearcefe086752009-03-03 13:49:48 -0800448 line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700449 i.src_path, p, i.level)
450 else:
451 line = ' %s%s\t%s' % (i_status, f_status, p)
452
453 if i and not f:
454 out.added('%s', line)
455 elif (i and f) or (not i and f):
456 out.changed('%s', line)
457 elif not i and not f:
458 out.untracked('%s', line)
459 else:
460 out.write('%s', line)
461 out.nl()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700462 return 'DIRTY'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700463
464 def PrintWorkTreeDiff(self):
465 """Prints the status of the repository to stdout.
466 """
467 out = DiffColoring(self.config)
468 cmd = ['diff']
469 if out.is_on:
470 cmd.append('--color')
471 cmd.append(HEAD)
472 cmd.append('--')
473 p = GitCommand(self,
474 cmd,
475 capture_stdout = True,
476 capture_stderr = True)
477 has_diff = False
478 for line in p.process.stdout:
479 if not has_diff:
480 out.nl()
481 out.project('project %s/' % self.relpath)
482 out.nl()
483 has_diff = True
484 print line[:-1]
485 p.Wait()
486
487
488## Publish / Upload ##
489
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700490 def WasPublished(self, branch, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700491 """Was the branch published (uploaded) for code review?
492 If so, returns the SHA-1 hash of the last published
493 state for the branch.
494 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700495 key = R_PUB + branch
496 if all is None:
497 try:
498 return self.bare_git.rev_parse(key)
499 except GitError:
500 return None
501 else:
502 try:
503 return all[key]
504 except KeyError:
505 return None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700506
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700507 def CleanPublishedCache(self, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700508 """Prunes any stale published refs.
509 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700510 if all is None:
511 all = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700512 heads = set()
513 canrm = {}
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700514 for name, id in all.iteritems():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700515 if name.startswith(R_HEADS):
516 heads.add(name)
517 elif name.startswith(R_PUB):
518 canrm[name] = id
519
520 for name, id in canrm.iteritems():
521 n = name[len(R_PUB):]
522 if R_HEADS + n not in heads:
523 self.bare_git.DeleteRef(name, id)
524
525 def GetUploadableBranches(self):
526 """List any branches which can be uploaded for review.
527 """
528 heads = {}
529 pubed = {}
530
531 for name, id in self._allrefs.iteritems():
532 if name.startswith(R_HEADS):
533 heads[name[len(R_HEADS):]] = id
534 elif name.startswith(R_PUB):
535 pubed[name[len(R_PUB):]] = id
536
537 ready = []
538 for branch, id in heads.iteritems():
539 if branch in pubed and pubed[branch] == id:
540 continue
541
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800542 rb = self.GetUploadableBranch(branch)
543 if rb:
544 ready.append(rb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700545 return ready
546
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800547 def GetUploadableBranch(self, branch_name):
548 """Get a single uploadable branch, or None.
549 """
550 branch = self.GetBranch(branch_name)
551 base = branch.LocalMerge
552 if branch.LocalMerge:
553 rb = ReviewableBranch(self, branch, base)
554 if rb.commits:
555 return rb
556 return None
557
Joe Onorato2896a792008-11-17 16:56:36 -0500558 def UploadForReview(self, branch=None, replace_changes=None, people=([],[])):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700559 """Uploads the named branch for code review.
560 """
561 if branch is None:
562 branch = self.CurrentBranch
563 if branch is None:
564 raise GitError('not currently on a branch')
565
566 branch = self.GetBranch(branch)
567 if not branch.LocalMerge:
568 raise GitError('branch %s does not track a remote' % branch.name)
569 if not branch.remote.review:
570 raise GitError('remote %s has no review url' % branch.remote.name)
571
572 dest_branch = branch.merge
573 if not dest_branch.startswith(R_HEADS):
574 dest_branch = R_HEADS + dest_branch
575
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800576 if not branch.remote.projectname:
577 branch.remote.projectname = self.name
578 branch.remote.Save()
579
Shawn O. Pearce370e3fa2009-01-26 10:55:39 -0800580 if branch.remote.ReviewProtocol == 'ssh':
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800581 if dest_branch.startswith(R_HEADS):
582 dest_branch = dest_branch[len(R_HEADS):]
583
584 rp = ['gerrit receive-pack']
585 for e in people[0]:
586 rp.append('--reviewer=%s' % sq(e))
587 for e in people[1]:
588 rp.append('--cc=%s' % sq(e))
589
590 cmd = ['push']
591 cmd.append('--receive-pack=%s' % " ".join(rp))
592 cmd.append(branch.remote.SshReviewUrl(self.UserEmail))
593 cmd.append('%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch))
594 if replace_changes:
595 for change_id,commit_id in replace_changes.iteritems():
596 cmd.append('%s:refs/changes/%s/new' % (commit_id, change_id))
597 if GitCommand(self, cmd, bare = True).Wait() != 0:
598 raise UploadError('Upload failed')
599
600 else:
601 raise UploadError('Unsupported protocol %s' \
602 % branch.remote.review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700603
604 msg = "posted to %s for %s" % (branch.remote.review, dest_branch)
605 self.bare_git.UpdateRef(R_PUB + branch.name,
606 R_HEADS + branch.name,
607 message = msg)
608
609
610## Sync ##
611
612 def Sync_NetworkHalf(self):
613 """Perform only the network IO portion of the sync process.
614 Local working directory/branch state is not affected.
615 """
616 if not self.Exists:
617 print >>sys.stderr
618 print >>sys.stderr, 'Initializing project %s ...' % self.name
619 self._InitGitDir()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800620
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700621 self._InitRemote()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700622 if not self._RemoteFetch():
623 return False
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800624
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200625 #Check that the requested ref was found after fetch
626 #
627 try:
628 self.GetRevisionId()
629 except ManifestInvalidRevisionError:
630 # if the ref is a tag. We can try fetching
631 # the tag manually as a last resort
632 #
633 rev = self.revisionExpr
634 if rev.startswith(R_TAGS):
635 self._RemoteFetch(None, rev[len(R_TAGS):])
636
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800637 if self.worktree:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800638 self._InitMRef()
639 else:
640 self._InitMirrorHead()
641 try:
642 os.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
643 except OSError:
644 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700645 return True
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800646
647 def PostRepoUpgrade(self):
648 self._InitHooks()
649
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700650 def _CopyFiles(self):
651 for file in self.copyfiles:
652 file._Copy()
653
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700654 def GetRevisionId(self, all=None):
655 if self.revisionId:
656 return self.revisionId
657
658 rem = self.GetRemote(self.remote.name)
659 rev = rem.ToLocal(self.revisionExpr)
660
661 if all is not None and rev in all:
662 return all[rev]
663
664 try:
665 return self.bare_git.rev_parse('--verify', '%s^0' % rev)
666 except GitError:
667 raise ManifestInvalidRevisionError(
668 'revision %s in %s not found' % (self.revisionExpr,
669 self.name))
670
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700671 def Sync_LocalHalf(self, syncbuf):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700672 """Perform only the local IO portion of the sync process.
673 Network access is not required.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700674 """
675 self._InitWorkTree()
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700676 all = self.bare_ref.all
677 self.CleanPublishedCache(all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700678
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700679 revid = self.GetRevisionId(all)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700680 head = self.work_git.GetHead()
681 if head.startswith(R_HEADS):
682 branch = head[len(R_HEADS):]
683 try:
684 head = all[head]
685 except KeyError:
686 head = None
687 else:
688 branch = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700689
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700690 if branch is None or syncbuf.detach_head:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700691 # Currently on a detached HEAD. The user is assumed to
692 # not have any local modifications worth worrying about.
693 #
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700694 if self.IsRebaseInProgress():
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700695 syncbuf.fail(self, _PriorSyncFailedError())
696 return
697
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700698 if head == revid:
699 # No changes; don't do anything further.
700 #
701 return
702
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700703 lost = self._revlist(not_rev(revid), HEAD)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700704 if lost:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700705 syncbuf.info(self, "discarding %d commits", len(lost))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700706 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700707 self._Checkout(revid, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700708 except GitError, e:
709 syncbuf.fail(self, e)
710 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700711 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700712 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700713
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700714 if head == revid:
715 # No changes; don't do anything further.
716 #
717 return
718
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700719 branch = self.GetBranch(branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700720
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700721 if not branch.LocalMerge:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700722 # The current branch has no tracking configuration.
723 # Jump off it to a deatched HEAD.
724 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700725 syncbuf.info(self,
726 "leaving %s; does not track upstream",
727 branch.name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700728 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700729 self._Checkout(revid, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700730 except GitError, e:
731 syncbuf.fail(self, e)
732 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700733 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700734 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700735
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700736 upstream_gain = self._revlist(not_rev(HEAD), revid)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700737 pub = self.WasPublished(branch.name, all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700738 if pub:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700739 not_merged = self._revlist(not_rev(revid), pub)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700740 if not_merged:
741 if upstream_gain:
742 # The user has published this branch and some of those
743 # commits are not yet merged upstream. We do not want
744 # to rewrite the published commits so we punt.
745 #
Daniel Sandler4c50dee2010-03-02 15:38:03 -0500746 syncbuf.fail(self,
747 "branch %s is published (but not merged) and is now %d commits behind"
748 % (branch.name, len(upstream_gain)))
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700749 return
Shawn O. Pearce05f66b62009-04-21 08:26:32 -0700750 elif pub == head:
751 # All published commits are merged, and thus we are a
752 # strict subset. We can fast-forward safely.
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700753 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700754 def _doff():
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700755 self._FastForward(revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700756 self._CopyFiles()
757 syncbuf.later1(self, _doff)
758 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700759
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700760 # Examine the local commits not in the remote. Find the
761 # last one attributed to this user, if any.
762 #
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700763 local_changes = self._revlist(not_rev(revid), HEAD, format='%H %ce')
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700764 last_mine = None
765 cnt_mine = 0
766 for commit in local_changes:
Shawn O. Pearceaa4982e2009-12-30 18:38:27 -0800767 commit_id, committer_email = commit.split(' ', 1)
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700768 if committer_email == self.UserEmail:
769 last_mine = commit_id
770 cnt_mine += 1
771
Shawn O. Pearceda88ff42009-06-03 11:09:12 -0700772 if not upstream_gain and cnt_mine == len(local_changes):
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700773 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700774
775 if self.IsDirty(consider_untracked=False):
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700776 syncbuf.fail(self, _DirtyError())
777 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700778
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700779 # If the upstream switched on us, warn the user.
780 #
781 if branch.merge != self.revisionExpr:
782 if branch.merge and self.revisionExpr:
783 syncbuf.info(self,
784 'manifest switched %s...%s',
785 branch.merge,
786 self.revisionExpr)
787 elif branch.merge:
788 syncbuf.info(self,
789 'manifest no longer tracks %s',
790 branch.merge)
791
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700792 if cnt_mine < len(local_changes):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700793 # Upstream rebased. Not everything in HEAD
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700794 # was created by this user.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700795 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700796 syncbuf.info(self,
797 "discarding %d commits removed from upstream",
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700798 len(local_changes) - cnt_mine)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700799
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700800 branch.remote = self.GetRemote(self.remote.name)
801 branch.merge = self.revisionExpr
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700802 branch.Save()
803
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700804 if cnt_mine > 0:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700805 def _dorebase():
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700806 self._Rebase(upstream = '%s^1' % last_mine, onto = revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700807 self._CopyFiles()
808 syncbuf.later2(self, _dorebase)
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700809 elif local_changes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700810 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700811 self._ResetHard(revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700812 self._CopyFiles()
813 except GitError, e:
814 syncbuf.fail(self, e)
815 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700816 else:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700817 def _doff():
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700818 self._FastForward(revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700819 self._CopyFiles()
820 syncbuf.later1(self, _doff)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700821
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800822 def AddCopyFile(self, src, dest, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700823 # dest should already be an absolute path, but src is project relative
824 # make src an absolute path
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800825 abssrc = os.path.join(self.worktree, src)
826 self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700827
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700828 def DownloadPatchSet(self, change_id, patch_id):
829 """Download a single patch set of a single change to FETCH_HEAD.
830 """
831 remote = self.GetRemote(self.remote.name)
832
833 cmd = ['fetch', remote.name]
834 cmd.append('refs/changes/%2.2d/%d/%d' \
835 % (change_id % 100, change_id, patch_id))
836 cmd.extend(map(lambda x: str(x), remote.fetch))
837 if GitCommand(self, cmd, bare=True).Wait() != 0:
838 return None
839 return DownloadedChange(self,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700840 self.GetRevisionId(),
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700841 change_id,
842 patch_id,
843 self.bare_git.rev_parse('FETCH_HEAD'))
844
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700845
846## Branch Management ##
847
848 def StartBranch(self, name):
849 """Create a new branch off the manifest's revision.
850 """
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700851 head = self.work_git.GetHead()
852 if head == (R_HEADS + name):
853 return True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700854
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700855 all = self.bare_ref.all
856 if (R_HEADS + name) in all:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700857 return GitCommand(self,
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700858 ['checkout', name, '--'],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700859 capture_stdout = True,
860 capture_stderr = True).Wait() == 0
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700861
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700862 branch = self.GetBranch(name)
863 branch.remote = self.GetRemote(self.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700864 branch.merge = self.revisionExpr
865 revid = self.GetRevisionId(all)
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700866
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700867 if head.startswith(R_HEADS):
868 try:
869 head = all[head]
870 except KeyError:
871 head = None
872
873 if revid and head and revid == head:
874 ref = os.path.join(self.gitdir, R_HEADS + name)
875 try:
876 os.makedirs(os.path.dirname(ref))
877 except OSError:
878 pass
879 _lwrite(ref, '%s\n' % revid)
880 _lwrite(os.path.join(self.worktree, '.git', HEAD),
881 'ref: %s%s\n' % (R_HEADS, name))
882 branch.Save()
883 return True
884
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700885 if GitCommand(self,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700886 ['checkout', '-b', branch.name, revid],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700887 capture_stdout = True,
888 capture_stderr = True).Wait() == 0:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700889 branch.Save()
890 return True
891 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700892
Wink Saville02d79452009-04-10 13:01:24 -0700893 def CheckoutBranch(self, name):
894 """Checkout a local topic branch.
895 """
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700896 rev = R_HEADS + name
897 head = self.work_git.GetHead()
898 if head == rev:
899 # Already on the branch
900 #
901 return True
Wink Saville02d79452009-04-10 13:01:24 -0700902
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700903 all = self.bare_ref.all
Wink Saville02d79452009-04-10 13:01:24 -0700904 try:
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700905 revid = all[rev]
906 except KeyError:
907 # Branch does not exist in this project
908 #
909 return False
910
911 if head.startswith(R_HEADS):
912 try:
913 head = all[head]
914 except KeyError:
915 head = None
916
917 if head == revid:
918 # Same revision; just update HEAD to point to the new
919 # target branch, but otherwise take no other action.
920 #
921 _lwrite(os.path.join(self.worktree, '.git', HEAD),
922 'ref: %s%s\n' % (R_HEADS, name))
923 return True
Wink Saville02d79452009-04-10 13:01:24 -0700924
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700925 return GitCommand(self,
926 ['checkout', name, '--'],
927 capture_stdout = True,
928 capture_stderr = True).Wait() == 0
Wink Saville02d79452009-04-10 13:01:24 -0700929
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800930 def AbandonBranch(self, name):
931 """Destroy a local topic branch.
932 """
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700933 rev = R_HEADS + name
934 all = self.bare_ref.all
935 if rev not in all:
936 # Doesn't exist; assume already abandoned.
937 #
938 return True
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800939
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700940 head = self.work_git.GetHead()
941 if head == rev:
942 # We can't destroy the branch while we are sitting
943 # on it. Switch to a detached HEAD.
944 #
945 head = all[head]
946
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700947 revid = self.GetRevisionId(all)
948 if head == revid:
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700949 _lwrite(os.path.join(self.worktree, '.git', HEAD),
950 '%s\n' % revid)
951 else:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700952 self._Checkout(revid, quiet=True)
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800953
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700954 return GitCommand(self,
955 ['branch', '-D', name],
956 capture_stdout = True,
957 capture_stderr = True).Wait() == 0
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800958
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700959 def PruneHeads(self):
960 """Prune any topic branches already merged into upstream.
961 """
962 cb = self.CurrentBranch
963 kill = []
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800964 left = self._allrefs
965 for name in left.keys():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700966 if name.startswith(R_HEADS):
967 name = name[len(R_HEADS):]
968 if cb is None or name != cb:
969 kill.append(name)
970
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700971 rev = self.GetRevisionId(left)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700972 if cb is not None \
973 and not self._revlist(HEAD + '...' + rev) \
974 and not self.IsDirty(consider_untracked = False):
975 self.work_git.DetachHead(HEAD)
976 kill.append(cb)
977
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700978 if kill:
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700979 old = self.bare_git.GetHead()
980 if old is None:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700981 old = 'refs/heads/please_never_use_this_as_a_branch_name'
982
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700983 try:
984 self.bare_git.DetachHead(rev)
985
986 b = ['branch', '-d']
987 b.extend(kill)
988 b = GitCommand(self, b, bare=True,
989 capture_stdout=True,
990 capture_stderr=True)
991 b.Wait()
992 finally:
993 self.bare_git.SetHead(old)
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800994 left = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700995
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800996 for branch in kill:
997 if (R_HEADS + branch) not in left:
998 self.CleanPublishedCache()
999 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001000
1001 if cb and cb not in kill:
1002 kill.append(cb)
Shawn O. Pearce7c6c64d2009-03-02 12:38:13 -08001003 kill.sort()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001004
1005 kept = []
1006 for branch in kill:
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -08001007 if (R_HEADS + branch) in left:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001008 branch = self.GetBranch(branch)
1009 base = branch.LocalMerge
1010 if not base:
1011 base = rev
1012 kept.append(ReviewableBranch(self, branch, base))
1013 return kept
1014
1015
1016## Direct Git Commands ##
1017
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +02001018 def _RemoteFetch(self, name=None, tag=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001019 if not name:
1020 name = self.remote.name
Shawn O. Pearcefb231612009-04-10 18:53:46 -07001021
1022 ssh_proxy = False
1023 if self.GetRemote(name).PreConnectFetch():
1024 ssh_proxy = True
1025
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001026 cmd = ['fetch']
1027 if not self.worktree:
1028 cmd.append('--update-head-ok')
1029 cmd.append(name)
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +02001030 if tag is not None:
1031 cmd.append('tag')
1032 cmd.append(tag)
Shawn O. Pearcefb231612009-04-10 18:53:46 -07001033 return GitCommand(self,
1034 cmd,
1035 bare = True,
1036 ssh_proxy = ssh_proxy).Wait() == 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001037
1038 def _Checkout(self, rev, quiet=False):
1039 cmd = ['checkout']
1040 if quiet:
1041 cmd.append('-q')
1042 cmd.append(rev)
1043 cmd.append('--')
1044 if GitCommand(self, cmd).Wait() != 0:
1045 if self._allrefs:
1046 raise GitError('%s checkout %s ' % (self.name, rev))
1047
1048 def _ResetHard(self, rev, quiet=True):
1049 cmd = ['reset', '--hard']
1050 if quiet:
1051 cmd.append('-q')
1052 cmd.append(rev)
1053 if GitCommand(self, cmd).Wait() != 0:
1054 raise GitError('%s reset --hard %s ' % (self.name, rev))
1055
1056 def _Rebase(self, upstream, onto = None):
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07001057 cmd = ['rebase']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001058 if onto is not None:
1059 cmd.extend(['--onto', onto])
1060 cmd.append(upstream)
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07001061 if GitCommand(self, cmd).Wait() != 0:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001062 raise GitError('%s rebase %s ' % (self.name, upstream))
1063
1064 def _FastForward(self, head):
1065 cmd = ['merge', head]
1066 if GitCommand(self, cmd).Wait() != 0:
1067 raise GitError('%s merge %s ' % (self.name, head))
1068
1069 def _InitGitDir(self):
1070 if not os.path.exists(self.gitdir):
1071 os.makedirs(self.gitdir)
1072 self.bare_git.init()
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -08001073
1074 if self.manifest.IsMirror:
1075 self.config.SetString('core.bare', 'true')
1076 else:
1077 self.config.SetString('core.bare', None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001078
1079 hooks = self._gitdir_path('hooks')
Shawn O. Pearcede646812008-10-29 14:38:12 -07001080 try:
1081 to_rm = os.listdir(hooks)
1082 except OSError:
1083 to_rm = []
1084 for old_hook in to_rm:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001085 os.remove(os.path.join(hooks, old_hook))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001086 self._InitHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001087
1088 m = self.manifest.manifestProject.config
1089 for key in ['user.name', 'user.email']:
1090 if m.Has(key, include_defaults = False):
1091 self.config.SetString(key, m.GetString(key))
1092
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001093 def _InitHooks(self):
1094 hooks = self._gitdir_path('hooks')
1095 if not os.path.exists(hooks):
1096 os.makedirs(hooks)
1097 for stock_hook in repo_hooks():
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001098 name = os.path.basename(stock_hook)
1099
1100 if name in ('commit-msg') and not self.remote.review:
1101 # Don't install a Gerrit Code Review hook if this
1102 # project does not appear to use it for reviews.
1103 #
1104 continue
1105
1106 dst = os.path.join(hooks, name)
1107 if os.path.islink(dst):
1108 continue
1109 if os.path.exists(dst):
1110 if filecmp.cmp(stock_hook, dst, shallow=False):
1111 os.remove(dst)
1112 else:
1113 _error("%s: Not replacing %s hook", self.relpath, name)
1114 continue
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001115 try:
1116 os.symlink(relpath(stock_hook, dst), dst)
1117 except OSError, e:
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001118 if e.errno == errno.EPERM:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001119 raise GitError('filesystem must support symlinks')
1120 else:
1121 raise
1122
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001123 def _InitRemote(self):
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001124 if self.remote.url:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001125 remote = self.GetRemote(self.remote.name)
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001126 remote.url = self.remote.url
1127 remote.review = self.remote.review
1128 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001129
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001130 if self.worktree:
1131 remote.ResetFetch(mirror=False)
1132 else:
1133 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001134 remote.Save()
1135
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001136 def _InitMRef(self):
1137 if self.manifest.branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001138 self._InitAnyMRef(R_M + self.manifest.branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001139
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001140 def _InitMirrorHead(self):
Shawn O. Pearcefe200ee2009-06-01 15:28:21 -07001141 self._InitAnyMRef(HEAD)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001142
1143 def _InitAnyMRef(self, ref):
1144 cur = self.bare_ref.symref(ref)
1145
1146 if self.revisionId:
1147 if cur != '' or self.bare_ref.get(ref) != self.revisionId:
1148 msg = 'manifest set to %s' % self.revisionId
1149 dst = self.revisionId + '^0'
1150 self.bare_git.UpdateRef(ref, dst, message = msg, detach = True)
1151 else:
1152 remote = self.GetRemote(self.remote.name)
1153 dst = remote.ToLocal(self.revisionExpr)
1154 if cur != dst:
1155 msg = 'manifest set to %s' % self.revisionExpr
1156 self.bare_git.symbolic_ref('-m', msg, ref, dst)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001157
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001158 def _InitWorkTree(self):
1159 dotgit = os.path.join(self.worktree, '.git')
1160 if not os.path.exists(dotgit):
1161 os.makedirs(dotgit)
1162
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001163 for name in ['config',
1164 'description',
1165 'hooks',
1166 'info',
1167 'logs',
1168 'objects',
1169 'packed-refs',
1170 'refs',
1171 'rr-cache',
1172 'svn']:
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001173 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001174 src = os.path.join(self.gitdir, name)
1175 dst = os.path.join(dotgit, name)
Nico Sallembiend63060f2010-01-20 10:27:50 -08001176 if os.path.islink(dst) or not os.path.exists(dst):
1177 os.symlink(relpath(src, dst), dst)
1178 else:
1179 raise GitError('cannot overwrite a local work tree')
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001180 except OSError, e:
1181 if e.errno == errno.EPERM:
1182 raise GitError('filesystem must support symlinks')
1183 else:
1184 raise
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001185
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001186 _lwrite(os.path.join(dotgit, HEAD), '%s\n' % self.GetRevisionId())
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001187
1188 cmd = ['read-tree', '--reset', '-u']
1189 cmd.append('-v')
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001190 cmd.append(HEAD)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001191 if GitCommand(self, cmd).Wait() != 0:
1192 raise GitError("cannot initialize work tree")
Shawn O. Pearce93609662009-04-21 10:50:33 -07001193 self._CopyFiles()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001194
1195 def _gitdir_path(self, path):
1196 return os.path.join(self.gitdir, path)
1197
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001198 def _revlist(self, *args, **kw):
1199 a = []
1200 a.extend(args)
1201 a.append('--')
1202 return self.work_git.rev_list(*a, **kw)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001203
1204 @property
1205 def _allrefs(self):
Shawn O. Pearced237b692009-04-17 18:49:50 -07001206 return self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001207
1208 class _GitGetByExec(object):
1209 def __init__(self, project, bare):
1210 self._project = project
1211 self._bare = bare
1212
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001213 def LsOthers(self):
1214 p = GitCommand(self._project,
1215 ['ls-files',
1216 '-z',
1217 '--others',
1218 '--exclude-standard'],
1219 bare = False,
1220 capture_stdout = True,
1221 capture_stderr = True)
1222 if p.Wait() == 0:
1223 out = p.stdout
1224 if out:
1225 return out[:-1].split("\0")
1226 return []
1227
1228 def DiffZ(self, name, *args):
1229 cmd = [name]
1230 cmd.append('-z')
1231 cmd.extend(args)
1232 p = GitCommand(self._project,
1233 cmd,
1234 bare = False,
1235 capture_stdout = True,
1236 capture_stderr = True)
1237 try:
1238 out = p.process.stdout.read()
1239 r = {}
1240 if out:
1241 out = iter(out[:-1].split('\0'))
1242 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07001243 try:
1244 info = out.next()
1245 path = out.next()
1246 except StopIteration:
1247 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001248
1249 class _Info(object):
1250 def __init__(self, path, omode, nmode, oid, nid, state):
1251 self.path = path
1252 self.src_path = None
1253 self.old_mode = omode
1254 self.new_mode = nmode
1255 self.old_id = oid
1256 self.new_id = nid
1257
1258 if len(state) == 1:
1259 self.status = state
1260 self.level = None
1261 else:
1262 self.status = state[:1]
1263 self.level = state[1:]
1264 while self.level.startswith('0'):
1265 self.level = self.level[1:]
1266
1267 info = info[1:].split(' ')
1268 info =_Info(path, *info)
1269 if info.status in ('R', 'C'):
1270 info.src_path = info.path
1271 info.path = out.next()
1272 r[info.path] = info
1273 return r
1274 finally:
1275 p.Wait()
1276
1277 def GetHead(self):
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001278 if self._bare:
1279 path = os.path.join(self._project.gitdir, HEAD)
1280 else:
1281 path = os.path.join(self._project.worktree, '.git', HEAD)
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -07001282 fd = open(path, 'rb')
1283 try:
1284 line = fd.read()
1285 finally:
1286 fd.close()
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001287 if line.startswith('ref: '):
1288 return line[5:-1]
1289 return line[:-1]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001290
1291 def SetHead(self, ref, message=None):
1292 cmdv = []
1293 if message is not None:
1294 cmdv.extend(['-m', message])
1295 cmdv.append(HEAD)
1296 cmdv.append(ref)
1297 self.symbolic_ref(*cmdv)
1298
1299 def DetachHead(self, new, message=None):
1300 cmdv = ['--no-deref']
1301 if message is not None:
1302 cmdv.extend(['-m', message])
1303 cmdv.append(HEAD)
1304 cmdv.append(new)
1305 self.update_ref(*cmdv)
1306
1307 def UpdateRef(self, name, new, old=None,
1308 message=None,
1309 detach=False):
1310 cmdv = []
1311 if message is not None:
1312 cmdv.extend(['-m', message])
1313 if detach:
1314 cmdv.append('--no-deref')
1315 cmdv.append(name)
1316 cmdv.append(new)
1317 if old is not None:
1318 cmdv.append(old)
1319 self.update_ref(*cmdv)
1320
1321 def DeleteRef(self, name, old=None):
1322 if not old:
1323 old = self.rev_parse(name)
1324 self.update_ref('-d', name, old)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001325 self._project.bare_ref.deleted(name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001326
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001327 def rev_list(self, *args, **kw):
1328 if 'format' in kw:
1329 cmdv = ['log', '--pretty=format:%s' % kw['format']]
1330 else:
1331 cmdv = ['rev-list']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001332 cmdv.extend(args)
1333 p = GitCommand(self._project,
1334 cmdv,
1335 bare = self._bare,
1336 capture_stdout = True,
1337 capture_stderr = True)
1338 r = []
1339 for line in p.process.stdout:
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001340 if line[-1] == '\n':
1341 line = line[:-1]
1342 r.append(line)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001343 if p.Wait() != 0:
1344 raise GitError('%s rev-list %s: %s' % (
1345 self._project.name,
1346 str(args),
1347 p.stderr))
1348 return r
1349
1350 def __getattr__(self, name):
1351 name = name.replace('_', '-')
1352 def runner(*args):
1353 cmdv = [name]
1354 cmdv.extend(args)
1355 p = GitCommand(self._project,
1356 cmdv,
1357 bare = self._bare,
1358 capture_stdout = True,
1359 capture_stderr = True)
1360 if p.Wait() != 0:
1361 raise GitError('%s %s: %s' % (
1362 self._project.name,
1363 name,
1364 p.stderr))
1365 r = p.stdout
1366 if r.endswith('\n') and r.index('\n') == len(r) - 1:
1367 return r[:-1]
1368 return r
1369 return runner
1370
1371
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001372class _PriorSyncFailedError(Exception):
1373 def __str__(self):
1374 return 'prior sync failed; rebase still in progress'
1375
1376class _DirtyError(Exception):
1377 def __str__(self):
1378 return 'contains uncommitted changes'
1379
1380class _InfoMessage(object):
1381 def __init__(self, project, text):
1382 self.project = project
1383 self.text = text
1384
1385 def Print(self, syncbuf):
1386 syncbuf.out.info('%s/: %s', self.project.relpath, self.text)
1387 syncbuf.out.nl()
1388
1389class _Failure(object):
1390 def __init__(self, project, why):
1391 self.project = project
1392 self.why = why
1393
1394 def Print(self, syncbuf):
1395 syncbuf.out.fail('error: %s/: %s',
1396 self.project.relpath,
1397 str(self.why))
1398 syncbuf.out.nl()
1399
1400class _Later(object):
1401 def __init__(self, project, action):
1402 self.project = project
1403 self.action = action
1404
1405 def Run(self, syncbuf):
1406 out = syncbuf.out
1407 out.project('project %s/', self.project.relpath)
1408 out.nl()
1409 try:
1410 self.action()
1411 out.nl()
1412 return True
1413 except GitError, e:
1414 out.nl()
1415 return False
1416
1417class _SyncColoring(Coloring):
1418 def __init__(self, config):
1419 Coloring.__init__(self, config, 'reposync')
1420 self.project = self.printer('header', attr = 'bold')
1421 self.info = self.printer('info')
1422 self.fail = self.printer('fail', fg='red')
1423
1424class SyncBuffer(object):
1425 def __init__(self, config, detach_head=False):
1426 self._messages = []
1427 self._failures = []
1428 self._later_queue1 = []
1429 self._later_queue2 = []
1430
1431 self.out = _SyncColoring(config)
1432 self.out.redirect(sys.stderr)
1433
1434 self.detach_head = detach_head
1435 self.clean = True
1436
1437 def info(self, project, fmt, *args):
1438 self._messages.append(_InfoMessage(project, fmt % args))
1439
1440 def fail(self, project, err=None):
1441 self._failures.append(_Failure(project, err))
1442 self.clean = False
1443
1444 def later1(self, project, what):
1445 self._later_queue1.append(_Later(project, what))
1446
1447 def later2(self, project, what):
1448 self._later_queue2.append(_Later(project, what))
1449
1450 def Finish(self):
1451 self._PrintMessages()
1452 self._RunLater()
1453 self._PrintMessages()
1454 return self.clean
1455
1456 def _RunLater(self):
1457 for q in ['_later_queue1', '_later_queue2']:
1458 if not self._RunQueue(q):
1459 return
1460
1461 def _RunQueue(self, queue):
1462 for m in getattr(self, queue):
1463 if not m.Run(self):
1464 self.clean = False
1465 return False
1466 setattr(self, queue, [])
1467 return True
1468
1469 def _PrintMessages(self):
1470 for m in self._messages:
1471 m.Print(self)
1472 for m in self._failures:
1473 m.Print(self)
1474
1475 self._messages = []
1476 self._failures = []
1477
1478
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001479class MetaProject(Project):
1480 """A special project housed under .repo.
1481 """
1482 def __init__(self, manifest, name, gitdir, worktree):
1483 repodir = manifest.repodir
1484 Project.__init__(self,
1485 manifest = manifest,
1486 name = name,
1487 gitdir = gitdir,
1488 worktree = worktree,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001489 remote = RemoteSpec('origin'),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001490 relpath = '.repo/%s' % name,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001491 revisionExpr = 'refs/heads/master',
1492 revisionId = None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001493
1494 def PreSync(self):
1495 if self.Exists:
1496 cb = self.CurrentBranch
1497 if cb:
1498 base = self.GetBranch(cb).merge
1499 if base:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001500 self.revisionExpr = base
1501 self.revisionId = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001502
1503 @property
Shawn O. Pearcef6906872009-04-18 10:49:00 -07001504 def LastFetch(self):
1505 try:
1506 fh = os.path.join(self.gitdir, 'FETCH_HEAD')
1507 return os.path.getmtime(fh)
1508 except OSError:
1509 return 0
1510
1511 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001512 def HasChanges(self):
1513 """Has the remote received new commits not yet checked out?
1514 """
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001515 if not self.remote or not self.revisionExpr:
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001516 return False
1517
1518 all = self.bare_ref.all
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001519 revid = self.GetRevisionId(all)
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001520 head = self.work_git.GetHead()
1521 if head.startswith(R_HEADS):
1522 try:
1523 head = all[head]
1524 except KeyError:
1525 head = None
1526
1527 if revid == head:
1528 return False
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001529 elif self._revlist(not_rev(HEAD), revid):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001530 return True
1531 return False