blob: 5ccf33eb114f97acd3609f1cc7153ea4b8ae9ad9 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001# Copyright (C) 2008 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -080015import errno
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070016import filecmp
17import os
18import re
19import shutil
20import stat
21import sys
22import urllib2
23
24from color import Coloring
25from git_command import GitCommand
26from git_config import GitConfig, IsId
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027from error import GitError, ImportError, UploadError
Shawn O. Pearce559b8462009-03-02 12:56:08 -080028from error import ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029from remote import Remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070030
Shawn O. Pearced237b692009-04-17 18:49:50 -070031from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -070033def _lwrite(path, content):
34 lock = '%s.lock' % path
35
36 fd = open(lock, 'wb')
37 try:
38 fd.write(content)
39 finally:
40 fd.close()
41
42 try:
43 os.rename(lock, path)
44 except OSError:
45 os.remove(lock)
46 raise
47
Shawn O. Pearce48244782009-04-16 08:25:57 -070048def _error(fmt, *args):
49 msg = fmt % args
50 print >>sys.stderr, 'error: %s' % msg
51
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070052def not_rev(r):
53 return '^' + r
54
Shawn O. Pearceb54a3922009-01-05 16:18:58 -080055def sq(r):
56 return "'" + r.replace("'", "'\''") + "'"
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080057
58hook_list = None
59def repo_hooks():
60 global hook_list
61 if hook_list is None:
62 d = os.path.abspath(os.path.dirname(__file__))
63 d = os.path.join(d , 'hooks')
64 hook_list = map(lambda x: os.path.join(d, x), os.listdir(d))
65 return hook_list
66
67def relpath(dst, src):
68 src = os.path.dirname(src)
69 top = os.path.commonprefix([dst, src])
70 if top.endswith('/'):
71 top = top[:-1]
72 else:
73 top = os.path.dirname(top)
74
75 tmp = src
76 rel = ''
77 while top != tmp:
78 rel += '../'
79 tmp = os.path.dirname(tmp)
80 return rel + dst[len(top) + 1:]
81
82
Shawn O. Pearce632768b2008-10-23 11:58:52 -070083class DownloadedChange(object):
84 _commit_cache = None
85
86 def __init__(self, project, base, change_id, ps_id, commit):
87 self.project = project
88 self.base = base
89 self.change_id = change_id
90 self.ps_id = ps_id
91 self.commit = commit
92
93 @property
94 def commits(self):
95 if self._commit_cache is None:
96 self._commit_cache = self.project.bare_git.rev_list(
97 '--abbrev=8',
98 '--abbrev-commit',
99 '--pretty=oneline',
100 '--reverse',
101 '--date-order',
102 not_rev(self.base),
103 self.commit,
104 '--')
105 return self._commit_cache
106
107
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700108class ReviewableBranch(object):
109 _commit_cache = None
110
111 def __init__(self, project, branch, base):
112 self.project = project
113 self.branch = branch
114 self.base = base
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800115 self.replace_changes = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700116
117 @property
118 def name(self):
119 return self.branch.name
120
121 @property
122 def commits(self):
123 if self._commit_cache is None:
124 self._commit_cache = self.project.bare_git.rev_list(
125 '--abbrev=8',
126 '--abbrev-commit',
127 '--pretty=oneline',
128 '--reverse',
129 '--date-order',
130 not_rev(self.base),
131 R_HEADS + self.name,
132 '--')
133 return self._commit_cache
134
135 @property
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800136 def unabbrev_commits(self):
137 r = dict()
138 for commit in self.project.bare_git.rev_list(
139 not_rev(self.base),
140 R_HEADS + self.name,
141 '--'):
142 r[commit[0:8]] = commit
143 return r
144
145 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700146 def date(self):
147 return self.project.bare_git.log(
148 '--pretty=format:%cd',
149 '-n', '1',
150 R_HEADS + self.name,
151 '--')
152
Joe Onorato2896a792008-11-17 16:56:36 -0500153 def UploadForReview(self, people):
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800154 self.project.UploadForReview(self.name,
Joe Onorato2896a792008-11-17 16:56:36 -0500155 self.replace_changes,
156 people)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700157
Ficus Kirkpatrickbc7ef672009-05-04 12:45:11 -0700158 def GetPublishedRefs(self):
159 refs = {}
160 output = self.project.bare_git.ls_remote(
161 self.branch.remote.SshReviewUrl(self.project.UserEmail),
162 'refs/changes/*')
163 for line in output.split('\n'):
164 try:
165 (sha, ref) = line.split()
166 refs[sha] = ref
167 except ValueError:
168 pass
169
170 return refs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700171
172class StatusColoring(Coloring):
173 def __init__(self, config):
174 Coloring.__init__(self, config, 'status')
175 self.project = self.printer('header', attr = 'bold')
176 self.branch = self.printer('header', attr = 'bold')
177 self.nobranch = self.printer('nobranch', fg = 'red')
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700178 self.important = self.printer('important', fg = 'red')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700179
180 self.added = self.printer('added', fg = 'green')
181 self.changed = self.printer('changed', fg = 'red')
182 self.untracked = self.printer('untracked', fg = 'red')
183
184
185class DiffColoring(Coloring):
186 def __init__(self, config):
187 Coloring.__init__(self, config, 'diff')
188 self.project = self.printer('header', attr = 'bold')
189
190
191class _CopyFile:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800192 def __init__(self, src, dest, abssrc, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700193 self.src = src
194 self.dest = dest
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800195 self.abs_src = abssrc
196 self.abs_dest = absdest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700197
198 def _Copy(self):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800199 src = self.abs_src
200 dest = self.abs_dest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700201 # copy file if it does not exist or is out of date
202 if not os.path.exists(dest) or not filecmp.cmp(src, dest):
203 try:
204 # remove existing file first, since it might be read-only
205 if os.path.exists(dest):
206 os.remove(dest)
207 shutil.copy(src, dest)
208 # make the file read-only
209 mode = os.stat(dest)[stat.ST_MODE]
210 mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
211 os.chmod(dest, mode)
212 except IOError:
Shawn O. Pearce48244782009-04-16 08:25:57 -0700213 _error('Cannot copy file %s to %s', src, dest)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700214
215
216class Project(object):
217 def __init__(self,
218 manifest,
219 name,
220 remote,
221 gitdir,
222 worktree,
223 relpath,
224 revision):
225 self.manifest = manifest
226 self.name = name
227 self.remote = remote
228 self.gitdir = gitdir
229 self.worktree = worktree
230 self.relpath = relpath
231 self.revision = revision
232 self.snapshots = {}
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700233 self.copyfiles = []
234 self.config = GitConfig.ForRepository(
235 gitdir = self.gitdir,
236 defaults = self.manifest.globalConfig)
237
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800238 if self.worktree:
239 self.work_git = self._GitGetByExec(self, bare=False)
240 else:
241 self.work_git = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700242 self.bare_git = self._GitGetByExec(self, bare=True)
Shawn O. Pearced237b692009-04-17 18:49:50 -0700243 self.bare_ref = GitRefs(gitdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700244
245 @property
246 def Exists(self):
247 return os.path.isdir(self.gitdir)
248
249 @property
250 def CurrentBranch(self):
251 """Obtain the name of the currently checked out branch.
252 The branch name omits the 'refs/heads/' prefix.
253 None is returned if the project is on a detached HEAD.
254 """
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700255 b = self.work_git.GetHead()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700256 if b.startswith(R_HEADS):
257 return b[len(R_HEADS):]
258 return None
259
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700260 def IsRebaseInProgress(self):
261 w = self.worktree
262 g = os.path.join(w, '.git')
263 return os.path.exists(os.path.join(g, 'rebase-apply')) \
264 or os.path.exists(os.path.join(g, 'rebase-merge')) \
265 or os.path.exists(os.path.join(w, '.dotest'))
266
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700267 def IsDirty(self, consider_untracked=True):
268 """Is the working directory modified in some way?
269 """
270 self.work_git.update_index('-q',
271 '--unmerged',
272 '--ignore-missing',
273 '--refresh')
274 if self.work_git.DiffZ('diff-index','-M','--cached',HEAD):
275 return True
276 if self.work_git.DiffZ('diff-files'):
277 return True
278 if consider_untracked and self.work_git.LsOthers():
279 return True
280 return False
281
282 _userident_name = None
283 _userident_email = None
284
285 @property
286 def UserName(self):
287 """Obtain the user's personal name.
288 """
289 if self._userident_name is None:
290 self._LoadUserIdentity()
291 return self._userident_name
292
293 @property
294 def UserEmail(self):
295 """Obtain the user's email address. This is very likely
296 to be their Gerrit login.
297 """
298 if self._userident_email is None:
299 self._LoadUserIdentity()
300 return self._userident_email
301
302 def _LoadUserIdentity(self):
303 u = self.bare_git.var('GIT_COMMITTER_IDENT')
304 m = re.compile("^(.*) <([^>]*)> ").match(u)
305 if m:
306 self._userident_name = m.group(1)
307 self._userident_email = m.group(2)
308 else:
309 self._userident_name = ''
310 self._userident_email = ''
311
312 def GetRemote(self, name):
313 """Get the configuration for a single remote.
314 """
315 return self.config.GetRemote(name)
316
317 def GetBranch(self, name):
318 """Get the configuration for a single branch.
319 """
320 return self.config.GetBranch(name)
321
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700322 def GetBranches(self):
323 """Get all existing local branches.
324 """
325 current = self.CurrentBranch
Shawn O. Pearced237b692009-04-17 18:49:50 -0700326 all = self._allrefs
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700327 heads = {}
328 pubd = {}
329
330 for name, id in all.iteritems():
331 if name.startswith(R_HEADS):
332 name = name[len(R_HEADS):]
333 b = self.GetBranch(name)
334 b.current = name == current
335 b.published = None
336 b.revision = id
337 heads[name] = b
338
339 for name, id in all.iteritems():
340 if name.startswith(R_PUB):
341 name = name[len(R_PUB):]
342 b = heads.get(name)
343 if b:
344 b.published = id
345
346 return heads
347
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700348
349## Status Display ##
350
351 def PrintWorkTreeStatus(self):
352 """Prints the status of the repository to stdout.
353 """
354 if not os.path.isdir(self.worktree):
355 print ''
356 print 'project %s/' % self.relpath
357 print ' missing (run "repo sync")'
358 return
359
360 self.work_git.update_index('-q',
361 '--unmerged',
362 '--ignore-missing',
363 '--refresh')
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700364 rb = self.IsRebaseInProgress()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700365 di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD)
366 df = self.work_git.DiffZ('diff-files')
367 do = self.work_git.LsOthers()
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700368 if not rb and not di and not df and not do:
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700369 return 'CLEAN'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700370
371 out = StatusColoring(self.config)
372 out.project('project %-40s', self.relpath + '/')
373
374 branch = self.CurrentBranch
375 if branch is None:
376 out.nobranch('(*** NO BRANCH ***)')
377 else:
378 out.branch('branch %s', branch)
379 out.nl()
380
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700381 if rb:
382 out.important('prior sync failed; rebase still in progress')
383 out.nl()
384
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700385 paths = list()
386 paths.extend(di.keys())
387 paths.extend(df.keys())
388 paths.extend(do)
389
390 paths = list(set(paths))
391 paths.sort()
392
393 for p in paths:
394 try: i = di[p]
395 except KeyError: i = None
396
397 try: f = df[p]
398 except KeyError: f = None
399
400 if i: i_status = i.status.upper()
401 else: i_status = '-'
402
403 if f: f_status = f.status.lower()
404 else: f_status = '-'
405
406 if i and i.src_path:
Shawn O. Pearcefe086752009-03-03 13:49:48 -0800407 line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700408 i.src_path, p, i.level)
409 else:
410 line = ' %s%s\t%s' % (i_status, f_status, p)
411
412 if i and not f:
413 out.added('%s', line)
414 elif (i and f) or (not i and f):
415 out.changed('%s', line)
416 elif not i and not f:
417 out.untracked('%s', line)
418 else:
419 out.write('%s', line)
420 out.nl()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700421 return 'DIRTY'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700422
423 def PrintWorkTreeDiff(self):
424 """Prints the status of the repository to stdout.
425 """
426 out = DiffColoring(self.config)
427 cmd = ['diff']
428 if out.is_on:
429 cmd.append('--color')
430 cmd.append(HEAD)
431 cmd.append('--')
432 p = GitCommand(self,
433 cmd,
434 capture_stdout = True,
435 capture_stderr = True)
436 has_diff = False
437 for line in p.process.stdout:
438 if not has_diff:
439 out.nl()
440 out.project('project %s/' % self.relpath)
441 out.nl()
442 has_diff = True
443 print line[:-1]
444 p.Wait()
445
446
447## Publish / Upload ##
448
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700449 def WasPublished(self, branch, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700450 """Was the branch published (uploaded) for code review?
451 If so, returns the SHA-1 hash of the last published
452 state for the branch.
453 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700454 key = R_PUB + branch
455 if all is None:
456 try:
457 return self.bare_git.rev_parse(key)
458 except GitError:
459 return None
460 else:
461 try:
462 return all[key]
463 except KeyError:
464 return None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700465
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700466 def CleanPublishedCache(self, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700467 """Prunes any stale published refs.
468 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700469 if all is None:
470 all = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700471 heads = set()
472 canrm = {}
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700473 for name, id in all.iteritems():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700474 if name.startswith(R_HEADS):
475 heads.add(name)
476 elif name.startswith(R_PUB):
477 canrm[name] = id
478
479 for name, id in canrm.iteritems():
480 n = name[len(R_PUB):]
481 if R_HEADS + n not in heads:
482 self.bare_git.DeleteRef(name, id)
483
484 def GetUploadableBranches(self):
485 """List any branches which can be uploaded for review.
486 """
487 heads = {}
488 pubed = {}
489
490 for name, id in self._allrefs.iteritems():
491 if name.startswith(R_HEADS):
492 heads[name[len(R_HEADS):]] = id
493 elif name.startswith(R_PUB):
494 pubed[name[len(R_PUB):]] = id
495
496 ready = []
497 for branch, id in heads.iteritems():
498 if branch in pubed and pubed[branch] == id:
499 continue
500
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800501 rb = self.GetUploadableBranch(branch)
502 if rb:
503 ready.append(rb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700504 return ready
505
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800506 def GetUploadableBranch(self, branch_name):
507 """Get a single uploadable branch, or None.
508 """
509 branch = self.GetBranch(branch_name)
510 base = branch.LocalMerge
511 if branch.LocalMerge:
512 rb = ReviewableBranch(self, branch, base)
513 if rb.commits:
514 return rb
515 return None
516
Joe Onorato2896a792008-11-17 16:56:36 -0500517 def UploadForReview(self, branch=None, replace_changes=None, people=([],[])):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700518 """Uploads the named branch for code review.
519 """
520 if branch is None:
521 branch = self.CurrentBranch
522 if branch is None:
523 raise GitError('not currently on a branch')
524
525 branch = self.GetBranch(branch)
526 if not branch.LocalMerge:
527 raise GitError('branch %s does not track a remote' % branch.name)
528 if not branch.remote.review:
529 raise GitError('remote %s has no review url' % branch.remote.name)
530
531 dest_branch = branch.merge
532 if not dest_branch.startswith(R_HEADS):
533 dest_branch = R_HEADS + dest_branch
534
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800535 if not branch.remote.projectname:
536 branch.remote.projectname = self.name
537 branch.remote.Save()
538
Shawn O. Pearce370e3fa2009-01-26 10:55:39 -0800539 if branch.remote.ReviewProtocol == 'ssh':
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800540 if dest_branch.startswith(R_HEADS):
541 dest_branch = dest_branch[len(R_HEADS):]
542
543 rp = ['gerrit receive-pack']
544 for e in people[0]:
545 rp.append('--reviewer=%s' % sq(e))
546 for e in people[1]:
547 rp.append('--cc=%s' % sq(e))
548
549 cmd = ['push']
550 cmd.append('--receive-pack=%s' % " ".join(rp))
551 cmd.append(branch.remote.SshReviewUrl(self.UserEmail))
552 cmd.append('%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch))
553 if replace_changes:
554 for change_id,commit_id in replace_changes.iteritems():
555 cmd.append('%s:refs/changes/%s/new' % (commit_id, change_id))
556 if GitCommand(self, cmd, bare = True).Wait() != 0:
557 raise UploadError('Upload failed')
558
559 else:
560 raise UploadError('Unsupported protocol %s' \
561 % branch.remote.review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700562
563 msg = "posted to %s for %s" % (branch.remote.review, dest_branch)
564 self.bare_git.UpdateRef(R_PUB + branch.name,
565 R_HEADS + branch.name,
566 message = msg)
567
568
569## Sync ##
570
571 def Sync_NetworkHalf(self):
572 """Perform only the network IO portion of the sync process.
573 Local working directory/branch state is not affected.
574 """
575 if not self.Exists:
576 print >>sys.stderr
577 print >>sys.stderr, 'Initializing project %s ...' % self.name
578 self._InitGitDir()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800579
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700580 self._InitRemote()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700581 if not self._RemoteFetch():
582 return False
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800583
584 if self.worktree:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800585 self._InitMRef()
586 else:
587 self._InitMirrorHead()
588 try:
589 os.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
590 except OSError:
591 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700592 return True
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800593
594 def PostRepoUpgrade(self):
595 self._InitHooks()
596
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700597 def _CopyFiles(self):
598 for file in self.copyfiles:
599 file._Copy()
600
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700601 def Sync_LocalHalf(self, syncbuf):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700602 """Perform only the local IO portion of the sync process.
603 Network access is not required.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700604 """
605 self._InitWorkTree()
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700606 all = self.bare_ref.all
607 self.CleanPublishedCache(all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700608
609 rem = self.GetRemote(self.remote.name)
610 rev = rem.ToLocal(self.revision)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700611 if rev in all:
612 revid = all[rev]
613 elif IsId(rev):
614 revid = rev
615 else:
616 try:
617 revid = self.bare_git.rev_parse('--verify', '%s^0' % rev)
618 except GitError:
619 raise ManifestInvalidRevisionError(
620 'revision %s in %s not found' % (self.revision, self.name))
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800621
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700622 head = self.work_git.GetHead()
623 if head.startswith(R_HEADS):
624 branch = head[len(R_HEADS):]
625 try:
626 head = all[head]
627 except KeyError:
628 head = None
629 else:
630 branch = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700631
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700632 if branch is None or syncbuf.detach_head:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700633 # Currently on a detached HEAD. The user is assumed to
634 # not have any local modifications worth worrying about.
635 #
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700636 if self.IsRebaseInProgress():
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700637 syncbuf.fail(self, _PriorSyncFailedError())
638 return
639
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700640 if head == revid:
641 # No changes; don't do anything further.
642 #
643 return
644
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700645 lost = self._revlist(not_rev(rev), HEAD)
646 if lost:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700647 syncbuf.info(self, "discarding %d commits", len(lost))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700648 try:
649 self._Checkout(rev, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700650 except GitError, e:
651 syncbuf.fail(self, e)
652 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700653 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700654 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700655
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700656 if head == revid:
657 # No changes; don't do anything further.
658 #
659 return
660
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700661 branch = self.GetBranch(branch)
662 merge = branch.LocalMerge
663
664 if not merge:
665 # The current branch has no tracking configuration.
666 # Jump off it to a deatched HEAD.
667 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700668 syncbuf.info(self,
669 "leaving %s; does not track upstream",
670 branch.name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700671 try:
672 self._Checkout(rev, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700673 except GitError, e:
674 syncbuf.fail(self, e)
675 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700676 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700677 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700678
679 upstream_gain = self._revlist(not_rev(HEAD), rev)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700680 pub = self.WasPublished(branch.name, all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700681 if pub:
682 not_merged = self._revlist(not_rev(rev), pub)
683 if not_merged:
684 if upstream_gain:
685 # The user has published this branch and some of those
686 # commits are not yet merged upstream. We do not want
687 # to rewrite the published commits so we punt.
688 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700689 syncbuf.info(self,
690 "branch %s is published but is now %d commits behind",
691 branch.name,
692 len(upstream_gain))
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700693 return
Shawn O. Pearce05f66b62009-04-21 08:26:32 -0700694 elif pub == head:
695 # All published commits are merged, and thus we are a
696 # strict subset. We can fast-forward safely.
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700697 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700698 def _doff():
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700699 self._FastForward(rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700700 self._CopyFiles()
701 syncbuf.later1(self, _doff)
702 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700703
704 if merge == rev:
705 try:
706 old_merge = self.bare_git.rev_parse('%s@{1}' % merge)
707 except GitError:
708 old_merge = merge
Shawn O. Pearce07346002008-10-21 07:09:27 -0700709 if old_merge == '0000000000000000000000000000000000000000' \
710 or old_merge == '':
711 old_merge = merge
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700712 else:
713 # The upstream switched on us. Time to cross our fingers
714 # and pray that the old upstream also wasn't in the habit
715 # of rebasing itself.
716 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700717 syncbuf.info(self, "manifest switched %s...%s", merge, rev)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700718 old_merge = merge
719
720 if rev == old_merge:
721 upstream_lost = []
722 else:
723 upstream_lost = self._revlist(not_rev(rev), old_merge)
724
725 if not upstream_lost and not upstream_gain:
726 # Trivially no changes caused by the upstream.
727 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700728 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700729
730 if self.IsDirty(consider_untracked=False):
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700731 syncbuf.fail(self, _DirtyError())
732 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700733
734 if upstream_lost:
735 # Upstream rebased. Not everything in HEAD
736 # may have been caused by the user.
737 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700738 syncbuf.info(self,
739 "discarding %d commits removed from upstream",
740 len(upstream_lost))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700741
742 branch.remote = rem
743 branch.merge = self.revision
744 branch.Save()
745
746 my_changes = self._revlist(not_rev(old_merge), HEAD)
747 if my_changes:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700748 def _dorebase():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700749 self._Rebase(upstream = old_merge, onto = rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700750 self._CopyFiles()
751 syncbuf.later2(self, _dorebase)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700752 elif upstream_lost:
753 try:
754 self._ResetHard(rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700755 self._CopyFiles()
756 except GitError, e:
757 syncbuf.fail(self, e)
758 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700759 else:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700760 def _doff():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700761 self._FastForward(rev)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700762 self._CopyFiles()
763 syncbuf.later1(self, _doff)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700764
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800765 def AddCopyFile(self, src, dest, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700766 # dest should already be an absolute path, but src is project relative
767 # make src an absolute path
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800768 abssrc = os.path.join(self.worktree, src)
769 self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700770
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700771 def DownloadPatchSet(self, change_id, patch_id):
772 """Download a single patch set of a single change to FETCH_HEAD.
773 """
774 remote = self.GetRemote(self.remote.name)
775
776 cmd = ['fetch', remote.name]
777 cmd.append('refs/changes/%2.2d/%d/%d' \
778 % (change_id % 100, change_id, patch_id))
779 cmd.extend(map(lambda x: str(x), remote.fetch))
780 if GitCommand(self, cmd, bare=True).Wait() != 0:
781 return None
782 return DownloadedChange(self,
783 remote.ToLocal(self.revision),
784 change_id,
785 patch_id,
786 self.bare_git.rev_parse('FETCH_HEAD'))
787
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700788
789## Branch Management ##
790
791 def StartBranch(self, name):
792 """Create a new branch off the manifest's revision.
793 """
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700794 head = self.work_git.GetHead()
795 if head == (R_HEADS + name):
796 return True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700797
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700798 all = self.bare_ref.all
799 if (R_HEADS + name) in all:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700800 return GitCommand(self,
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700801 ['checkout', name, '--'],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700802 capture_stdout = True,
803 capture_stderr = True).Wait() == 0
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700804
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700805 branch = self.GetBranch(name)
806 branch.remote = self.GetRemote(self.remote.name)
807 branch.merge = self.revision
808
809 rev = branch.LocalMerge
810 if rev in all:
811 revid = all[rev]
812 elif IsId(rev):
813 revid = rev
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700814 else:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700815 revid = None
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700816
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700817 if head.startswith(R_HEADS):
818 try:
819 head = all[head]
820 except KeyError:
821 head = None
822
823 if revid and head and revid == head:
824 ref = os.path.join(self.gitdir, R_HEADS + name)
825 try:
826 os.makedirs(os.path.dirname(ref))
827 except OSError:
828 pass
829 _lwrite(ref, '%s\n' % revid)
830 _lwrite(os.path.join(self.worktree, '.git', HEAD),
831 'ref: %s%s\n' % (R_HEADS, name))
832 branch.Save()
833 return True
834
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700835 if GitCommand(self,
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700836 ['checkout', '-b', branch.name, rev],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700837 capture_stdout = True,
838 capture_stderr = True).Wait() == 0:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700839 branch.Save()
840 return True
841 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700842
Wink Saville02d79452009-04-10 13:01:24 -0700843 def CheckoutBranch(self, name):
844 """Checkout a local topic branch.
845 """
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700846 rev = R_HEADS + name
847 head = self.work_git.GetHead()
848 if head == rev:
849 # Already on the branch
850 #
851 return True
Wink Saville02d79452009-04-10 13:01:24 -0700852
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700853 all = self.bare_ref.all
Wink Saville02d79452009-04-10 13:01:24 -0700854 try:
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700855 revid = all[rev]
856 except KeyError:
857 # Branch does not exist in this project
858 #
859 return False
860
861 if head.startswith(R_HEADS):
862 try:
863 head = all[head]
864 except KeyError:
865 head = None
866
867 if head == revid:
868 # Same revision; just update HEAD to point to the new
869 # target branch, but otherwise take no other action.
870 #
871 _lwrite(os.path.join(self.worktree, '.git', HEAD),
872 'ref: %s%s\n' % (R_HEADS, name))
873 return True
Wink Saville02d79452009-04-10 13:01:24 -0700874
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700875 return GitCommand(self,
876 ['checkout', name, '--'],
877 capture_stdout = True,
878 capture_stderr = True).Wait() == 0
Wink Saville02d79452009-04-10 13:01:24 -0700879
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800880 def AbandonBranch(self, name):
881 """Destroy a local topic branch.
882 """
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700883 rev = R_HEADS + name
884 all = self.bare_ref.all
885 if rev not in all:
886 # Doesn't exist; assume already abandoned.
887 #
888 return True
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800889
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700890 head = self.work_git.GetHead()
891 if head == rev:
892 # We can't destroy the branch while we are sitting
893 # on it. Switch to a detached HEAD.
894 #
895 head = all[head]
896
897 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
898 if rev in all:
899 revid = all[rev]
900 elif IsId(rev):
901 revid = rev
902 else:
903 revid = None
904
905 if revid and head == revid:
906 _lwrite(os.path.join(self.worktree, '.git', HEAD),
907 '%s\n' % revid)
908 else:
909 self._Checkout(rev, quiet=True)
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800910
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700911 return GitCommand(self,
912 ['branch', '-D', name],
913 capture_stdout = True,
914 capture_stderr = True).Wait() == 0
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800915
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700916 def PruneHeads(self):
917 """Prune any topic branches already merged into upstream.
918 """
919 cb = self.CurrentBranch
920 kill = []
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800921 left = self._allrefs
922 for name in left.keys():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700923 if name.startswith(R_HEADS):
924 name = name[len(R_HEADS):]
925 if cb is None or name != cb:
926 kill.append(name)
927
928 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
929 if cb is not None \
930 and not self._revlist(HEAD + '...' + rev) \
931 and not self.IsDirty(consider_untracked = False):
932 self.work_git.DetachHead(HEAD)
933 kill.append(cb)
934
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700935 if kill:
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700936 old = self.bare_git.GetHead()
937 if old is None:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700938 old = 'refs/heads/please_never_use_this_as_a_branch_name'
939
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700940 try:
941 self.bare_git.DetachHead(rev)
942
943 b = ['branch', '-d']
944 b.extend(kill)
945 b = GitCommand(self, b, bare=True,
946 capture_stdout=True,
947 capture_stderr=True)
948 b.Wait()
949 finally:
950 self.bare_git.SetHead(old)
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800951 left = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700952
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800953 for branch in kill:
954 if (R_HEADS + branch) not in left:
955 self.CleanPublishedCache()
956 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700957
958 if cb and cb not in kill:
959 kill.append(cb)
Shawn O. Pearce7c6c64d2009-03-02 12:38:13 -0800960 kill.sort()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700961
962 kept = []
963 for branch in kill:
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800964 if (R_HEADS + branch) in left:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700965 branch = self.GetBranch(branch)
966 base = branch.LocalMerge
967 if not base:
968 base = rev
969 kept.append(ReviewableBranch(self, branch, base))
970 return kept
971
972
973## Direct Git Commands ##
974
975 def _RemoteFetch(self, name=None):
976 if not name:
977 name = self.remote.name
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700978
979 ssh_proxy = False
980 if self.GetRemote(name).PreConnectFetch():
981 ssh_proxy = True
982
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800983 cmd = ['fetch']
984 if not self.worktree:
985 cmd.append('--update-head-ok')
986 cmd.append(name)
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700987 return GitCommand(self,
988 cmd,
989 bare = True,
990 ssh_proxy = ssh_proxy).Wait() == 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700991
992 def _Checkout(self, rev, quiet=False):
993 cmd = ['checkout']
994 if quiet:
995 cmd.append('-q')
996 cmd.append(rev)
997 cmd.append('--')
998 if GitCommand(self, cmd).Wait() != 0:
999 if self._allrefs:
1000 raise GitError('%s checkout %s ' % (self.name, rev))
1001
1002 def _ResetHard(self, rev, quiet=True):
1003 cmd = ['reset', '--hard']
1004 if quiet:
1005 cmd.append('-q')
1006 cmd.append(rev)
1007 if GitCommand(self, cmd).Wait() != 0:
1008 raise GitError('%s reset --hard %s ' % (self.name, rev))
1009
1010 def _Rebase(self, upstream, onto = None):
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07001011 cmd = ['rebase']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001012 if onto is not None:
1013 cmd.extend(['--onto', onto])
1014 cmd.append(upstream)
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07001015 if GitCommand(self, cmd).Wait() != 0:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001016 raise GitError('%s rebase %s ' % (self.name, upstream))
1017
1018 def _FastForward(self, head):
1019 cmd = ['merge', head]
1020 if GitCommand(self, cmd).Wait() != 0:
1021 raise GitError('%s merge %s ' % (self.name, head))
1022
1023 def _InitGitDir(self):
1024 if not os.path.exists(self.gitdir):
1025 os.makedirs(self.gitdir)
1026 self.bare_git.init()
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -08001027
1028 if self.manifest.IsMirror:
1029 self.config.SetString('core.bare', 'true')
1030 else:
1031 self.config.SetString('core.bare', None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001032
1033 hooks = self._gitdir_path('hooks')
Shawn O. Pearcede646812008-10-29 14:38:12 -07001034 try:
1035 to_rm = os.listdir(hooks)
1036 except OSError:
1037 to_rm = []
1038 for old_hook in to_rm:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001039 os.remove(os.path.join(hooks, old_hook))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001040 self._InitHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001041
1042 m = self.manifest.manifestProject.config
1043 for key in ['user.name', 'user.email']:
1044 if m.Has(key, include_defaults = False):
1045 self.config.SetString(key, m.GetString(key))
1046
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001047 def _InitHooks(self):
1048 hooks = self._gitdir_path('hooks')
1049 if not os.path.exists(hooks):
1050 os.makedirs(hooks)
1051 for stock_hook in repo_hooks():
1052 dst = os.path.join(hooks, os.path.basename(stock_hook))
1053 try:
1054 os.symlink(relpath(stock_hook, dst), dst)
1055 except OSError, e:
1056 if e.errno == errno.EEXIST:
1057 pass
1058 elif e.errno == errno.EPERM:
1059 raise GitError('filesystem must support symlinks')
1060 else:
1061 raise
1062
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001063 def _InitRemote(self):
1064 if self.remote.fetchUrl:
1065 remote = self.GetRemote(self.remote.name)
1066
1067 url = self.remote.fetchUrl
1068 while url.endswith('/'):
1069 url = url[:-1]
1070 url += '/%s.git' % self.name
1071 remote.url = url
1072 remote.review = self.remote.reviewUrl
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -08001073 if remote.projectname is None:
1074 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001075
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001076 if self.worktree:
1077 remote.ResetFetch(mirror=False)
1078 else:
1079 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001080 remote.Save()
1081
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001082 def _InitMRef(self):
1083 if self.manifest.branch:
1084 msg = 'manifest set to %s' % self.revision
1085 ref = R_M + self.manifest.branch
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -07001086 cur = self.bare_ref.symref(ref)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001087
1088 if IsId(self.revision):
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -07001089 if cur != '' or self.bare_ref.get(ref) != self.revision:
1090 dst = self.revision + '^0'
1091 self.bare_git.UpdateRef(ref, dst, message = msg, detach = True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001092 else:
1093 remote = self.GetRemote(self.remote.name)
1094 dst = remote.ToLocal(self.revision)
Shawn O. Pearce0f3dd232009-04-17 20:32:44 -07001095 if cur != dst:
1096 self.bare_git.symbolic_ref('-m', msg, ref, dst)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001097
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001098 def _InitMirrorHead(self):
1099 dst = self.GetRemote(self.remote.name).ToLocal(self.revision)
1100 msg = 'manifest set to %s' % self.revision
1101 self.bare_git.SetHead(dst, message=msg)
1102
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001103 def _InitWorkTree(self):
1104 dotgit = os.path.join(self.worktree, '.git')
1105 if not os.path.exists(dotgit):
1106 os.makedirs(dotgit)
1107
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001108 for name in ['config',
1109 'description',
1110 'hooks',
1111 'info',
1112 'logs',
1113 'objects',
1114 'packed-refs',
1115 'refs',
1116 'rr-cache',
1117 'svn']:
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001118 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001119 src = os.path.join(self.gitdir, name)
1120 dst = os.path.join(dotgit, name)
1121 os.symlink(relpath(src, dst), dst)
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001122 except OSError, e:
1123 if e.errno == errno.EPERM:
1124 raise GitError('filesystem must support symlinks')
1125 else:
1126 raise
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001127
1128 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
1129 rev = self.bare_git.rev_parse('%s^0' % rev)
1130
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -07001131 _lwrite(os.path.join(dotgit, HEAD), '%s\n' % rev)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001132
1133 cmd = ['read-tree', '--reset', '-u']
1134 cmd.append('-v')
1135 cmd.append('HEAD')
1136 if GitCommand(self, cmd).Wait() != 0:
1137 raise GitError("cannot initialize work tree")
Shawn O. Pearce93609662009-04-21 10:50:33 -07001138 self._CopyFiles()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001139
1140 def _gitdir_path(self, path):
1141 return os.path.join(self.gitdir, path)
1142
1143 def _revlist(self, *args):
1144 cmd = []
1145 cmd.extend(args)
1146 cmd.append('--')
1147 return self.work_git.rev_list(*args)
1148
1149 @property
1150 def _allrefs(self):
Shawn O. Pearced237b692009-04-17 18:49:50 -07001151 return self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001152
1153 class _GitGetByExec(object):
1154 def __init__(self, project, bare):
1155 self._project = project
1156 self._bare = bare
1157
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001158 def LsOthers(self):
1159 p = GitCommand(self._project,
1160 ['ls-files',
1161 '-z',
1162 '--others',
1163 '--exclude-standard'],
1164 bare = False,
1165 capture_stdout = True,
1166 capture_stderr = True)
1167 if p.Wait() == 0:
1168 out = p.stdout
1169 if out:
1170 return out[:-1].split("\0")
1171 return []
1172
1173 def DiffZ(self, name, *args):
1174 cmd = [name]
1175 cmd.append('-z')
1176 cmd.extend(args)
1177 p = GitCommand(self._project,
1178 cmd,
1179 bare = False,
1180 capture_stdout = True,
1181 capture_stderr = True)
1182 try:
1183 out = p.process.stdout.read()
1184 r = {}
1185 if out:
1186 out = iter(out[:-1].split('\0'))
1187 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07001188 try:
1189 info = out.next()
1190 path = out.next()
1191 except StopIteration:
1192 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001193
1194 class _Info(object):
1195 def __init__(self, path, omode, nmode, oid, nid, state):
1196 self.path = path
1197 self.src_path = None
1198 self.old_mode = omode
1199 self.new_mode = nmode
1200 self.old_id = oid
1201 self.new_id = nid
1202
1203 if len(state) == 1:
1204 self.status = state
1205 self.level = None
1206 else:
1207 self.status = state[:1]
1208 self.level = state[1:]
1209 while self.level.startswith('0'):
1210 self.level = self.level[1:]
1211
1212 info = info[1:].split(' ')
1213 info =_Info(path, *info)
1214 if info.status in ('R', 'C'):
1215 info.src_path = info.path
1216 info.path = out.next()
1217 r[info.path] = info
1218 return r
1219 finally:
1220 p.Wait()
1221
1222 def GetHead(self):
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001223 if self._bare:
1224 path = os.path.join(self._project.gitdir, HEAD)
1225 else:
1226 path = os.path.join(self._project.worktree, '.git', HEAD)
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -07001227 fd = open(path, 'rb')
1228 try:
1229 line = fd.read()
1230 finally:
1231 fd.close()
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001232 if line.startswith('ref: '):
1233 return line[5:-1]
1234 return line[:-1]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001235
1236 def SetHead(self, ref, message=None):
1237 cmdv = []
1238 if message is not None:
1239 cmdv.extend(['-m', message])
1240 cmdv.append(HEAD)
1241 cmdv.append(ref)
1242 self.symbolic_ref(*cmdv)
1243
1244 def DetachHead(self, new, message=None):
1245 cmdv = ['--no-deref']
1246 if message is not None:
1247 cmdv.extend(['-m', message])
1248 cmdv.append(HEAD)
1249 cmdv.append(new)
1250 self.update_ref(*cmdv)
1251
1252 def UpdateRef(self, name, new, old=None,
1253 message=None,
1254 detach=False):
1255 cmdv = []
1256 if message is not None:
1257 cmdv.extend(['-m', message])
1258 if detach:
1259 cmdv.append('--no-deref')
1260 cmdv.append(name)
1261 cmdv.append(new)
1262 if old is not None:
1263 cmdv.append(old)
1264 self.update_ref(*cmdv)
1265
1266 def DeleteRef(self, name, old=None):
1267 if not old:
1268 old = self.rev_parse(name)
1269 self.update_ref('-d', name, old)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001270 self._project.bare_ref.deleted(name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001271
1272 def rev_list(self, *args):
1273 cmdv = ['rev-list']
1274 cmdv.extend(args)
1275 p = GitCommand(self._project,
1276 cmdv,
1277 bare = self._bare,
1278 capture_stdout = True,
1279 capture_stderr = True)
1280 r = []
1281 for line in p.process.stdout:
1282 r.append(line[:-1])
1283 if p.Wait() != 0:
1284 raise GitError('%s rev-list %s: %s' % (
1285 self._project.name,
1286 str(args),
1287 p.stderr))
1288 return r
1289
1290 def __getattr__(self, name):
1291 name = name.replace('_', '-')
1292 def runner(*args):
1293 cmdv = [name]
1294 cmdv.extend(args)
1295 p = GitCommand(self._project,
1296 cmdv,
1297 bare = self._bare,
1298 capture_stdout = True,
1299 capture_stderr = True)
1300 if p.Wait() != 0:
1301 raise GitError('%s %s: %s' % (
1302 self._project.name,
1303 name,
1304 p.stderr))
1305 r = p.stdout
1306 if r.endswith('\n') and r.index('\n') == len(r) - 1:
1307 return r[:-1]
1308 return r
1309 return runner
1310
1311
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001312class _PriorSyncFailedError(Exception):
1313 def __str__(self):
1314 return 'prior sync failed; rebase still in progress'
1315
1316class _DirtyError(Exception):
1317 def __str__(self):
1318 return 'contains uncommitted changes'
1319
1320class _InfoMessage(object):
1321 def __init__(self, project, text):
1322 self.project = project
1323 self.text = text
1324
1325 def Print(self, syncbuf):
1326 syncbuf.out.info('%s/: %s', self.project.relpath, self.text)
1327 syncbuf.out.nl()
1328
1329class _Failure(object):
1330 def __init__(self, project, why):
1331 self.project = project
1332 self.why = why
1333
1334 def Print(self, syncbuf):
1335 syncbuf.out.fail('error: %s/: %s',
1336 self.project.relpath,
1337 str(self.why))
1338 syncbuf.out.nl()
1339
1340class _Later(object):
1341 def __init__(self, project, action):
1342 self.project = project
1343 self.action = action
1344
1345 def Run(self, syncbuf):
1346 out = syncbuf.out
1347 out.project('project %s/', self.project.relpath)
1348 out.nl()
1349 try:
1350 self.action()
1351 out.nl()
1352 return True
1353 except GitError, e:
1354 out.nl()
1355 return False
1356
1357class _SyncColoring(Coloring):
1358 def __init__(self, config):
1359 Coloring.__init__(self, config, 'reposync')
1360 self.project = self.printer('header', attr = 'bold')
1361 self.info = self.printer('info')
1362 self.fail = self.printer('fail', fg='red')
1363
1364class SyncBuffer(object):
1365 def __init__(self, config, detach_head=False):
1366 self._messages = []
1367 self._failures = []
1368 self._later_queue1 = []
1369 self._later_queue2 = []
1370
1371 self.out = _SyncColoring(config)
1372 self.out.redirect(sys.stderr)
1373
1374 self.detach_head = detach_head
1375 self.clean = True
1376
1377 def info(self, project, fmt, *args):
1378 self._messages.append(_InfoMessage(project, fmt % args))
1379
1380 def fail(self, project, err=None):
1381 self._failures.append(_Failure(project, err))
1382 self.clean = False
1383
1384 def later1(self, project, what):
1385 self._later_queue1.append(_Later(project, what))
1386
1387 def later2(self, project, what):
1388 self._later_queue2.append(_Later(project, what))
1389
1390 def Finish(self):
1391 self._PrintMessages()
1392 self._RunLater()
1393 self._PrintMessages()
1394 return self.clean
1395
1396 def _RunLater(self):
1397 for q in ['_later_queue1', '_later_queue2']:
1398 if not self._RunQueue(q):
1399 return
1400
1401 def _RunQueue(self, queue):
1402 for m in getattr(self, queue):
1403 if not m.Run(self):
1404 self.clean = False
1405 return False
1406 setattr(self, queue, [])
1407 return True
1408
1409 def _PrintMessages(self):
1410 for m in self._messages:
1411 m.Print(self)
1412 for m in self._failures:
1413 m.Print(self)
1414
1415 self._messages = []
1416 self._failures = []
1417
1418
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001419class MetaProject(Project):
1420 """A special project housed under .repo.
1421 """
1422 def __init__(self, manifest, name, gitdir, worktree):
1423 repodir = manifest.repodir
1424 Project.__init__(self,
1425 manifest = manifest,
1426 name = name,
1427 gitdir = gitdir,
1428 worktree = worktree,
1429 remote = Remote('origin'),
1430 relpath = '.repo/%s' % name,
1431 revision = 'refs/heads/master')
1432
1433 def PreSync(self):
1434 if self.Exists:
1435 cb = self.CurrentBranch
1436 if cb:
1437 base = self.GetBranch(cb).merge
1438 if base:
1439 self.revision = base
1440
1441 @property
Shawn O. Pearcef6906872009-04-18 10:49:00 -07001442 def LastFetch(self):
1443 try:
1444 fh = os.path.join(self.gitdir, 'FETCH_HEAD')
1445 return os.path.getmtime(fh)
1446 except OSError:
1447 return 0
1448
1449 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001450 def HasChanges(self):
1451 """Has the remote received new commits not yet checked out?
1452 """
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001453 if not self.remote or not self.revision:
1454 return False
1455
1456 all = self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001457 rev = self.GetRemote(self.remote.name).ToLocal(self.revision)
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001458 if rev in all:
1459 revid = all[rev]
1460 else:
1461 revid = rev
1462
1463 head = self.work_git.GetHead()
1464 if head.startswith(R_HEADS):
1465 try:
1466 head = all[head]
1467 except KeyError:
1468 head = None
1469
1470 if revid == head:
1471 return False
1472 elif self._revlist(not_rev(HEAD), rev):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001473 return True
1474 return False