blob: ce85b8639c2c5f67fb9a363e322e8d00e32d63a4 [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
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700152 def UploadForReview(self, people, auto_topic=False):
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,
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700155 people,
156 auto_topic=auto_topic)
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)
Matthew Buckett2daf6672009-07-11 09:43:47 -0400207 else:
208 dir = os.path.dirname(dest)
209 if not os.path.isdir(dir):
210 os.makedirs(dir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700211 shutil.copy(src, dest)
212 # make the file read-only
213 mode = os.stat(dest)[stat.ST_MODE]
214 mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
215 os.chmod(dest, mode)
216 except IOError:
Shawn O. Pearce48244782009-04-16 08:25:57 -0700217 _error('Cannot copy file %s to %s', src, dest)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700218
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700219class RemoteSpec(object):
220 def __init__(self,
221 name,
222 url = None,
223 review = None):
224 self.name = name
225 self.url = url
226 self.review = review
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700227
228class Project(object):
229 def __init__(self,
230 manifest,
231 name,
232 remote,
233 gitdir,
234 worktree,
235 relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700236 revisionExpr,
237 revisionId):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700238 self.manifest = manifest
239 self.name = name
240 self.remote = remote
241 self.gitdir = gitdir
242 self.worktree = worktree
243 self.relpath = relpath
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700244 self.revisionExpr = revisionExpr
245
246 if revisionId is None \
247 and revisionExpr \
248 and IsId(revisionExpr):
249 self.revisionId = revisionExpr
250 else:
251 self.revisionId = revisionId
252
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700253 self.snapshots = {}
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700254 self.copyfiles = []
255 self.config = GitConfig.ForRepository(
256 gitdir = self.gitdir,
257 defaults = self.manifest.globalConfig)
258
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800259 if self.worktree:
260 self.work_git = self._GitGetByExec(self, bare=False)
261 else:
262 self.work_git = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700263 self.bare_git = self._GitGetByExec(self, bare=True)
Shawn O. Pearced237b692009-04-17 18:49:50 -0700264 self.bare_ref = GitRefs(gitdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700265
266 @property
267 def Exists(self):
268 return os.path.isdir(self.gitdir)
269
270 @property
271 def CurrentBranch(self):
272 """Obtain the name of the currently checked out branch.
273 The branch name omits the 'refs/heads/' prefix.
274 None is returned if the project is on a detached HEAD.
275 """
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700276 b = self.work_git.GetHead()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700277 if b.startswith(R_HEADS):
278 return b[len(R_HEADS):]
279 return None
280
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700281 def IsRebaseInProgress(self):
282 w = self.worktree
283 g = os.path.join(w, '.git')
284 return os.path.exists(os.path.join(g, 'rebase-apply')) \
285 or os.path.exists(os.path.join(g, 'rebase-merge')) \
286 or os.path.exists(os.path.join(w, '.dotest'))
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200287
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700288 def IsDirty(self, consider_untracked=True):
289 """Is the working directory modified in some way?
290 """
291 self.work_git.update_index('-q',
292 '--unmerged',
293 '--ignore-missing',
294 '--refresh')
295 if self.work_git.DiffZ('diff-index','-M','--cached',HEAD):
296 return True
297 if self.work_git.DiffZ('diff-files'):
298 return True
299 if consider_untracked and self.work_git.LsOthers():
300 return True
301 return False
302
303 _userident_name = None
304 _userident_email = None
305
306 @property
307 def UserName(self):
308 """Obtain the user's personal name.
309 """
310 if self._userident_name is None:
311 self._LoadUserIdentity()
312 return self._userident_name
313
314 @property
315 def UserEmail(self):
316 """Obtain the user's email address. This is very likely
317 to be their Gerrit login.
318 """
319 if self._userident_email is None:
320 self._LoadUserIdentity()
321 return self._userident_email
322
323 def _LoadUserIdentity(self):
324 u = self.bare_git.var('GIT_COMMITTER_IDENT')
325 m = re.compile("^(.*) <([^>]*)> ").match(u)
326 if m:
327 self._userident_name = m.group(1)
328 self._userident_email = m.group(2)
329 else:
330 self._userident_name = ''
331 self._userident_email = ''
332
333 def GetRemote(self, name):
334 """Get the configuration for a single remote.
335 """
336 return self.config.GetRemote(name)
337
338 def GetBranch(self, name):
339 """Get the configuration for a single branch.
340 """
341 return self.config.GetBranch(name)
342
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700343 def GetBranches(self):
344 """Get all existing local branches.
345 """
346 current = self.CurrentBranch
Shawn O. Pearced237b692009-04-17 18:49:50 -0700347 all = self._allrefs
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700348 heads = {}
349 pubd = {}
350
351 for name, id in all.iteritems():
352 if name.startswith(R_HEADS):
353 name = name[len(R_HEADS):]
354 b = self.GetBranch(name)
355 b.current = name == current
356 b.published = None
357 b.revision = id
358 heads[name] = b
359
360 for name, id in all.iteritems():
361 if name.startswith(R_PUB):
362 name = name[len(R_PUB):]
363 b = heads.get(name)
364 if b:
365 b.published = id
366
367 return heads
368
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700369
370## Status Display ##
371
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500372 def HasChanges(self):
373 """Returns true if there are uncommitted changes.
374 """
375 self.work_git.update_index('-q',
376 '--unmerged',
377 '--ignore-missing',
378 '--refresh')
379 if self.IsRebaseInProgress():
380 return True
381
382 if self.work_git.DiffZ('diff-index', '--cached', HEAD):
383 return True
384
385 if self.work_git.DiffZ('diff-files'):
386 return True
387
388 if self.work_git.LsOthers():
389 return True
390
391 return False
392
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700393 def PrintWorkTreeStatus(self):
394 """Prints the status of the repository to stdout.
395 """
396 if not os.path.isdir(self.worktree):
397 print ''
398 print 'project %s/' % self.relpath
399 print ' missing (run "repo sync")'
400 return
401
402 self.work_git.update_index('-q',
403 '--unmerged',
404 '--ignore-missing',
405 '--refresh')
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700406 rb = self.IsRebaseInProgress()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700407 di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD)
408 df = self.work_git.DiffZ('diff-files')
409 do = self.work_git.LsOthers()
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700410 if not rb and not di and not df and not do:
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700411 return 'CLEAN'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700412
413 out = StatusColoring(self.config)
414 out.project('project %-40s', self.relpath + '/')
415
416 branch = self.CurrentBranch
417 if branch is None:
418 out.nobranch('(*** NO BRANCH ***)')
419 else:
420 out.branch('branch %s', branch)
421 out.nl()
422
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700423 if rb:
424 out.important('prior sync failed; rebase still in progress')
425 out.nl()
426
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700427 paths = list()
428 paths.extend(di.keys())
429 paths.extend(df.keys())
430 paths.extend(do)
431
432 paths = list(set(paths))
433 paths.sort()
434
435 for p in paths:
436 try: i = di[p]
437 except KeyError: i = None
438
439 try: f = df[p]
440 except KeyError: f = None
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200441
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700442 if i: i_status = i.status.upper()
443 else: i_status = '-'
444
445 if f: f_status = f.status.lower()
446 else: f_status = '-'
447
448 if i and i.src_path:
Shawn O. Pearcefe086752009-03-03 13:49:48 -0800449 line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700450 i.src_path, p, i.level)
451 else:
452 line = ' %s%s\t%s' % (i_status, f_status, p)
453
454 if i and not f:
455 out.added('%s', line)
456 elif (i and f) or (not i and f):
457 out.changed('%s', line)
458 elif not i and not f:
459 out.untracked('%s', line)
460 else:
461 out.write('%s', line)
462 out.nl()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700463 return 'DIRTY'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700464
465 def PrintWorkTreeDiff(self):
466 """Prints the status of the repository to stdout.
467 """
468 out = DiffColoring(self.config)
469 cmd = ['diff']
470 if out.is_on:
471 cmd.append('--color')
472 cmd.append(HEAD)
473 cmd.append('--')
474 p = GitCommand(self,
475 cmd,
476 capture_stdout = True,
477 capture_stderr = True)
478 has_diff = False
479 for line in p.process.stdout:
480 if not has_diff:
481 out.nl()
482 out.project('project %s/' % self.relpath)
483 out.nl()
484 has_diff = True
485 print line[:-1]
486 p.Wait()
487
488
489## Publish / Upload ##
490
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700491 def WasPublished(self, branch, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700492 """Was the branch published (uploaded) for code review?
493 If so, returns the SHA-1 hash of the last published
494 state for the branch.
495 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700496 key = R_PUB + branch
497 if all is None:
498 try:
499 return self.bare_git.rev_parse(key)
500 except GitError:
501 return None
502 else:
503 try:
504 return all[key]
505 except KeyError:
506 return None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700507
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700508 def CleanPublishedCache(self, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700509 """Prunes any stale published refs.
510 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700511 if all is None:
512 all = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700513 heads = set()
514 canrm = {}
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700515 for name, id in all.iteritems():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700516 if name.startswith(R_HEADS):
517 heads.add(name)
518 elif name.startswith(R_PUB):
519 canrm[name] = id
520
521 for name, id in canrm.iteritems():
522 n = name[len(R_PUB):]
523 if R_HEADS + n not in heads:
524 self.bare_git.DeleteRef(name, id)
525
526 def GetUploadableBranches(self):
527 """List any branches which can be uploaded for review.
528 """
529 heads = {}
530 pubed = {}
531
532 for name, id in self._allrefs.iteritems():
533 if name.startswith(R_HEADS):
534 heads[name[len(R_HEADS):]] = id
535 elif name.startswith(R_PUB):
536 pubed[name[len(R_PUB):]] = id
537
538 ready = []
539 for branch, id in heads.iteritems():
540 if branch in pubed and pubed[branch] == id:
541 continue
542
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800543 rb = self.GetUploadableBranch(branch)
544 if rb:
545 ready.append(rb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700546 return ready
547
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800548 def GetUploadableBranch(self, branch_name):
549 """Get a single uploadable branch, or None.
550 """
551 branch = self.GetBranch(branch_name)
552 base = branch.LocalMerge
553 if branch.LocalMerge:
554 rb = ReviewableBranch(self, branch, base)
555 if rb.commits:
556 return rb
557 return None
558
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700559 def UploadForReview(self, branch=None,
560 replace_changes=None,
561 people=([],[]),
562 auto_topic=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700563 """Uploads the named branch for code review.
564 """
565 if branch is None:
566 branch = self.CurrentBranch
567 if branch is None:
568 raise GitError('not currently on a branch')
569
570 branch = self.GetBranch(branch)
571 if not branch.LocalMerge:
572 raise GitError('branch %s does not track a remote' % branch.name)
573 if not branch.remote.review:
574 raise GitError('remote %s has no review url' % branch.remote.name)
575
576 dest_branch = branch.merge
577 if not dest_branch.startswith(R_HEADS):
578 dest_branch = R_HEADS + dest_branch
579
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800580 if not branch.remote.projectname:
581 branch.remote.projectname = self.name
582 branch.remote.Save()
583
Shawn O. Pearce370e3fa2009-01-26 10:55:39 -0800584 if branch.remote.ReviewProtocol == 'ssh':
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800585 if dest_branch.startswith(R_HEADS):
586 dest_branch = dest_branch[len(R_HEADS):]
587
588 rp = ['gerrit receive-pack']
589 for e in people[0]:
590 rp.append('--reviewer=%s' % sq(e))
591 for e in people[1]:
592 rp.append('--cc=%s' % sq(e))
593
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700594 ref_spec = '%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch)
595 if auto_topic:
596 ref_spec = ref_spec + '/' + branch.name
597
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800598 cmd = ['push']
599 cmd.append('--receive-pack=%s' % " ".join(rp))
600 cmd.append(branch.remote.SshReviewUrl(self.UserEmail))
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700601 cmd.append(ref_spec)
602
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800603 if replace_changes:
604 for change_id,commit_id in replace_changes.iteritems():
605 cmd.append('%s:refs/changes/%s/new' % (commit_id, change_id))
606 if GitCommand(self, cmd, bare = True).Wait() != 0:
607 raise UploadError('Upload failed')
608
609 else:
610 raise UploadError('Unsupported protocol %s' \
611 % branch.remote.review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700612
613 msg = "posted to %s for %s" % (branch.remote.review, dest_branch)
614 self.bare_git.UpdateRef(R_PUB + branch.name,
615 R_HEADS + branch.name,
616 message = msg)
617
618
619## Sync ##
620
Shawn O. Pearce16614f82010-10-29 12:05:43 -0700621 def Sync_NetworkHalf(self, quiet=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700622 """Perform only the network IO portion of the sync process.
623 Local working directory/branch state is not affected.
624 """
Shawn O. Pearce88443382010-10-08 10:02:09 +0200625 is_new = not self.Exists
626 if is_new:
Shawn O. Pearce16614f82010-10-29 12:05:43 -0700627 if not quiet:
628 print >>sys.stderr
629 print >>sys.stderr, 'Initializing project %s ...' % self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700630 self._InitGitDir()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800631
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700632 self._InitRemote()
Shawn O. Pearce16614f82010-10-29 12:05:43 -0700633 if not self._RemoteFetch(initial=is_new, quiet=quiet):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700634 return False
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800635
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200636 #Check that the requested ref was found after fetch
637 #
638 try:
639 self.GetRevisionId()
640 except ManifestInvalidRevisionError:
641 # if the ref is a tag. We can try fetching
642 # the tag manually as a last resort
643 #
644 rev = self.revisionExpr
645 if rev.startswith(R_TAGS):
Shawn O. Pearce16614f82010-10-29 12:05:43 -0700646 self._RemoteFetch(None, rev[len(R_TAGS):], quiet=quiet)
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200647
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800648 if self.worktree:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800649 self._InitMRef()
650 else:
651 self._InitMirrorHead()
652 try:
653 os.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
654 except OSError:
655 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700656 return True
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800657
658 def PostRepoUpgrade(self):
659 self._InitHooks()
660
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700661 def _CopyFiles(self):
662 for file in self.copyfiles:
663 file._Copy()
664
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700665 def GetRevisionId(self, all=None):
666 if self.revisionId:
667 return self.revisionId
668
669 rem = self.GetRemote(self.remote.name)
670 rev = rem.ToLocal(self.revisionExpr)
671
672 if all is not None and rev in all:
673 return all[rev]
674
675 try:
676 return self.bare_git.rev_parse('--verify', '%s^0' % rev)
677 except GitError:
678 raise ManifestInvalidRevisionError(
679 'revision %s in %s not found' % (self.revisionExpr,
680 self.name))
681
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700682 def Sync_LocalHalf(self, syncbuf):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700683 """Perform only the local IO portion of the sync process.
684 Network access is not required.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700685 """
686 self._InitWorkTree()
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700687 all = self.bare_ref.all
688 self.CleanPublishedCache(all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700689
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700690 revid = self.GetRevisionId(all)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700691 head = self.work_git.GetHead()
692 if head.startswith(R_HEADS):
693 branch = head[len(R_HEADS):]
694 try:
695 head = all[head]
696 except KeyError:
697 head = None
698 else:
699 branch = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700700
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700701 if branch is None or syncbuf.detach_head:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700702 # Currently on a detached HEAD. The user is assumed to
703 # not have any local modifications worth worrying about.
704 #
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700705 if self.IsRebaseInProgress():
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700706 syncbuf.fail(self, _PriorSyncFailedError())
707 return
708
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700709 if head == revid:
710 # No changes; don't do anything further.
711 #
712 return
713
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700714 lost = self._revlist(not_rev(revid), HEAD)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700715 if lost:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700716 syncbuf.info(self, "discarding %d commits", len(lost))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700717 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700718 self._Checkout(revid, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700719 except GitError, e:
720 syncbuf.fail(self, e)
721 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700722 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700723 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700724
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700725 if head == revid:
726 # No changes; don't do anything further.
727 #
728 return
729
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700730 branch = self.GetBranch(branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700731
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700732 if not branch.LocalMerge:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700733 # The current branch has no tracking configuration.
734 # Jump off it to a deatched HEAD.
735 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700736 syncbuf.info(self,
737 "leaving %s; does not track upstream",
738 branch.name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700739 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700740 self._Checkout(revid, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700741 except GitError, e:
742 syncbuf.fail(self, e)
743 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700744 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700745 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700746
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700747 upstream_gain = self._revlist(not_rev(HEAD), revid)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700748 pub = self.WasPublished(branch.name, all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700749 if pub:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700750 not_merged = self._revlist(not_rev(revid), pub)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700751 if not_merged:
752 if upstream_gain:
753 # The user has published this branch and some of those
754 # commits are not yet merged upstream. We do not want
755 # to rewrite the published commits so we punt.
756 #
Daniel Sandler4c50dee2010-03-02 15:38:03 -0500757 syncbuf.fail(self,
758 "branch %s is published (but not merged) and is now %d commits behind"
759 % (branch.name, len(upstream_gain)))
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700760 return
Shawn O. Pearce05f66b62009-04-21 08:26:32 -0700761 elif pub == head:
762 # All published commits are merged, and thus we are a
763 # strict subset. We can fast-forward safely.
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700764 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700765 def _doff():
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700766 self._FastForward(revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700767 self._CopyFiles()
768 syncbuf.later1(self, _doff)
769 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700770
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700771 # Examine the local commits not in the remote. Find the
772 # last one attributed to this user, if any.
773 #
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700774 local_changes = self._revlist(not_rev(revid), HEAD, format='%H %ce')
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700775 last_mine = None
776 cnt_mine = 0
777 for commit in local_changes:
Shawn O. Pearceaa4982e2009-12-30 18:38:27 -0800778 commit_id, committer_email = commit.split(' ', 1)
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700779 if committer_email == self.UserEmail:
780 last_mine = commit_id
781 cnt_mine += 1
782
Shawn O. Pearceda88ff42009-06-03 11:09:12 -0700783 if not upstream_gain and cnt_mine == len(local_changes):
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700784 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700785
786 if self.IsDirty(consider_untracked=False):
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700787 syncbuf.fail(self, _DirtyError())
788 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700789
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700790 # If the upstream switched on us, warn the user.
791 #
792 if branch.merge != self.revisionExpr:
793 if branch.merge and self.revisionExpr:
794 syncbuf.info(self,
795 'manifest switched %s...%s',
796 branch.merge,
797 self.revisionExpr)
798 elif branch.merge:
799 syncbuf.info(self,
800 'manifest no longer tracks %s',
801 branch.merge)
802
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700803 if cnt_mine < len(local_changes):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700804 # Upstream rebased. Not everything in HEAD
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700805 # was created by this user.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700806 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700807 syncbuf.info(self,
808 "discarding %d commits removed from upstream",
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700809 len(local_changes) - cnt_mine)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700810
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700811 branch.remote = self.GetRemote(self.remote.name)
812 branch.merge = self.revisionExpr
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700813 branch.Save()
814
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700815 if cnt_mine > 0:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700816 def _dorebase():
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700817 self._Rebase(upstream = '%s^1' % last_mine, onto = revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700818 self._CopyFiles()
819 syncbuf.later2(self, _dorebase)
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700820 elif local_changes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700821 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700822 self._ResetHard(revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700823 self._CopyFiles()
824 except GitError, e:
825 syncbuf.fail(self, e)
826 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700827 else:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700828 def _doff():
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700829 self._FastForward(revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700830 self._CopyFiles()
831 syncbuf.later1(self, _doff)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700832
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800833 def AddCopyFile(self, src, dest, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700834 # dest should already be an absolute path, but src is project relative
835 # make src an absolute path
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800836 abssrc = os.path.join(self.worktree, src)
837 self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700838
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700839 def DownloadPatchSet(self, change_id, patch_id):
840 """Download a single patch set of a single change to FETCH_HEAD.
841 """
842 remote = self.GetRemote(self.remote.name)
843
844 cmd = ['fetch', remote.name]
845 cmd.append('refs/changes/%2.2d/%d/%d' \
846 % (change_id % 100, change_id, patch_id))
847 cmd.extend(map(lambda x: str(x), remote.fetch))
848 if GitCommand(self, cmd, bare=True).Wait() != 0:
849 return None
850 return DownloadedChange(self,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700851 self.GetRevisionId(),
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700852 change_id,
853 patch_id,
854 self.bare_git.rev_parse('FETCH_HEAD'))
855
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700856
857## Branch Management ##
858
859 def StartBranch(self, name):
860 """Create a new branch off the manifest's revision.
861 """
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700862 head = self.work_git.GetHead()
863 if head == (R_HEADS + name):
864 return True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700865
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700866 all = self.bare_ref.all
867 if (R_HEADS + name) in all:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700868 return GitCommand(self,
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700869 ['checkout', name, '--'],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700870 capture_stdout = True,
871 capture_stderr = True).Wait() == 0
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700872
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700873 branch = self.GetBranch(name)
874 branch.remote = self.GetRemote(self.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700875 branch.merge = self.revisionExpr
876 revid = self.GetRevisionId(all)
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700877
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700878 if head.startswith(R_HEADS):
879 try:
880 head = all[head]
881 except KeyError:
882 head = None
883
884 if revid and head and revid == head:
885 ref = os.path.join(self.gitdir, R_HEADS + name)
886 try:
887 os.makedirs(os.path.dirname(ref))
888 except OSError:
889 pass
890 _lwrite(ref, '%s\n' % revid)
891 _lwrite(os.path.join(self.worktree, '.git', HEAD),
892 'ref: %s%s\n' % (R_HEADS, name))
893 branch.Save()
894 return True
895
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700896 if GitCommand(self,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700897 ['checkout', '-b', branch.name, revid],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700898 capture_stdout = True,
899 capture_stderr = True).Wait() == 0:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700900 branch.Save()
901 return True
902 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700903
Wink Saville02d79452009-04-10 13:01:24 -0700904 def CheckoutBranch(self, name):
905 """Checkout a local topic branch.
906 """
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700907 rev = R_HEADS + name
908 head = self.work_git.GetHead()
909 if head == rev:
910 # Already on the branch
911 #
912 return True
Wink Saville02d79452009-04-10 13:01:24 -0700913
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700914 all = self.bare_ref.all
Wink Saville02d79452009-04-10 13:01:24 -0700915 try:
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700916 revid = all[rev]
917 except KeyError:
918 # Branch does not exist in this project
919 #
920 return False
921
922 if head.startswith(R_HEADS):
923 try:
924 head = all[head]
925 except KeyError:
926 head = None
927
928 if head == revid:
929 # Same revision; just update HEAD to point to the new
930 # target branch, but otherwise take no other action.
931 #
932 _lwrite(os.path.join(self.worktree, '.git', HEAD),
933 'ref: %s%s\n' % (R_HEADS, name))
934 return True
Wink Saville02d79452009-04-10 13:01:24 -0700935
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700936 return GitCommand(self,
937 ['checkout', name, '--'],
938 capture_stdout = True,
939 capture_stderr = True).Wait() == 0
Wink Saville02d79452009-04-10 13:01:24 -0700940
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800941 def AbandonBranch(self, name):
942 """Destroy a local topic branch.
943 """
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700944 rev = R_HEADS + name
945 all = self.bare_ref.all
946 if rev not in all:
947 # Doesn't exist; assume already abandoned.
948 #
949 return True
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800950
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700951 head = self.work_git.GetHead()
952 if head == rev:
953 # We can't destroy the branch while we are sitting
954 # on it. Switch to a detached HEAD.
955 #
956 head = all[head]
957
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700958 revid = self.GetRevisionId(all)
959 if head == revid:
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700960 _lwrite(os.path.join(self.worktree, '.git', HEAD),
961 '%s\n' % revid)
962 else:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700963 self._Checkout(revid, quiet=True)
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800964
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700965 return GitCommand(self,
966 ['branch', '-D', name],
967 capture_stdout = True,
968 capture_stderr = True).Wait() == 0
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800969
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700970 def PruneHeads(self):
971 """Prune any topic branches already merged into upstream.
972 """
973 cb = self.CurrentBranch
974 kill = []
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800975 left = self._allrefs
976 for name in left.keys():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700977 if name.startswith(R_HEADS):
978 name = name[len(R_HEADS):]
979 if cb is None or name != cb:
980 kill.append(name)
981
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700982 rev = self.GetRevisionId(left)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700983 if cb is not None \
984 and not self._revlist(HEAD + '...' + rev) \
985 and not self.IsDirty(consider_untracked = False):
986 self.work_git.DetachHead(HEAD)
987 kill.append(cb)
988
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700989 if kill:
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700990 old = self.bare_git.GetHead()
991 if old is None:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700992 old = 'refs/heads/please_never_use_this_as_a_branch_name'
993
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700994 try:
995 self.bare_git.DetachHead(rev)
996
997 b = ['branch', '-d']
998 b.extend(kill)
999 b = GitCommand(self, b, bare=True,
1000 capture_stdout=True,
1001 capture_stderr=True)
1002 b.Wait()
1003 finally:
1004 self.bare_git.SetHead(old)
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -08001005 left = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001006
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -08001007 for branch in kill:
1008 if (R_HEADS + branch) not in left:
1009 self.CleanPublishedCache()
1010 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001011
1012 if cb and cb not in kill:
1013 kill.append(cb)
Shawn O. Pearce7c6c64d2009-03-02 12:38:13 -08001014 kill.sort()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001015
1016 kept = []
1017 for branch in kill:
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -08001018 if (R_HEADS + branch) in left:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001019 branch = self.GetBranch(branch)
1020 base = branch.LocalMerge
1021 if not base:
1022 base = rev
1023 kept.append(ReviewableBranch(self, branch, base))
1024 return kept
1025
1026
1027## Direct Git Commands ##
1028
Shawn O. Pearce16614f82010-10-29 12:05:43 -07001029 def _RemoteFetch(self, name=None, tag=None,
1030 initial=False,
1031 quiet=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001032 if not name:
1033 name = self.remote.name
Shawn O. Pearcefb231612009-04-10 18:53:46 -07001034
1035 ssh_proxy = False
1036 if self.GetRemote(name).PreConnectFetch():
1037 ssh_proxy = True
1038
Shawn O. Pearce88443382010-10-08 10:02:09 +02001039 if initial:
1040 alt = os.path.join(self.gitdir, 'objects/info/alternates')
1041 try:
1042 fd = open(alt, 'rb')
1043 try:
1044 ref_dir = fd.readline()
1045 if ref_dir and ref_dir.endswith('\n'):
1046 ref_dir = ref_dir[:-1]
1047 finally:
1048 fd.close()
1049 except IOError, e:
1050 ref_dir = None
1051
1052 if ref_dir and 'objects' == os.path.basename(ref_dir):
1053 ref_dir = os.path.dirname(ref_dir)
1054 packed_refs = os.path.join(self.gitdir, 'packed-refs')
1055 remote = self.GetRemote(name)
1056
1057 all = self.bare_ref.all
1058 ids = set(all.values())
1059 tmp = set()
1060
1061 for r, id in GitRefs(ref_dir).all.iteritems():
1062 if r not in all:
1063 if r.startswith(R_TAGS) or remote.WritesTo(r):
1064 all[r] = id
1065 ids.add(id)
1066 continue
1067
1068 if id in ids:
1069 continue
1070
1071 r = 'refs/_alt/%s' % id
1072 all[r] = id
1073 ids.add(id)
1074 tmp.add(r)
1075
1076 ref_names = list(all.keys())
1077 ref_names.sort()
1078
1079 tmp_packed = ''
1080 old_packed = ''
1081
1082 for r in ref_names:
1083 line = '%s %s\n' % (all[r], r)
1084 tmp_packed += line
1085 if r not in tmp:
1086 old_packed += line
1087
1088 _lwrite(packed_refs, tmp_packed)
1089
1090 else:
1091 ref_dir = None
1092
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001093 cmd = ['fetch']
Shawn O. Pearce16614f82010-10-29 12:05:43 -07001094 if quiet:
1095 cmd.append('--quiet')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001096 if not self.worktree:
1097 cmd.append('--update-head-ok')
1098 cmd.append(name)
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +02001099 if tag is not None:
1100 cmd.append('tag')
1101 cmd.append(tag)
Shawn O. Pearce88443382010-10-08 10:02:09 +02001102
1103 ok = GitCommand(self,
1104 cmd,
1105 bare = True,
1106 ssh_proxy = ssh_proxy).Wait() == 0
1107
1108 if initial:
1109 if ref_dir:
1110 if old_packed != '':
1111 _lwrite(packed_refs, old_packed)
1112 else:
1113 os.remove(packed_refs)
1114 self.bare_git.pack_refs('--all', '--prune')
1115
1116 return ok
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001117
1118 def _Checkout(self, rev, quiet=False):
1119 cmd = ['checkout']
1120 if quiet:
1121 cmd.append('-q')
1122 cmd.append(rev)
1123 cmd.append('--')
1124 if GitCommand(self, cmd).Wait() != 0:
1125 if self._allrefs:
1126 raise GitError('%s checkout %s ' % (self.name, rev))
1127
1128 def _ResetHard(self, rev, quiet=True):
1129 cmd = ['reset', '--hard']
1130 if quiet:
1131 cmd.append('-q')
1132 cmd.append(rev)
1133 if GitCommand(self, cmd).Wait() != 0:
1134 raise GitError('%s reset --hard %s ' % (self.name, rev))
1135
1136 def _Rebase(self, upstream, onto = None):
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07001137 cmd = ['rebase']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001138 if onto is not None:
1139 cmd.extend(['--onto', onto])
1140 cmd.append(upstream)
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07001141 if GitCommand(self, cmd).Wait() != 0:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001142 raise GitError('%s rebase %s ' % (self.name, upstream))
1143
1144 def _FastForward(self, head):
1145 cmd = ['merge', head]
1146 if GitCommand(self, cmd).Wait() != 0:
1147 raise GitError('%s merge %s ' % (self.name, head))
1148
1149 def _InitGitDir(self):
1150 if not os.path.exists(self.gitdir):
1151 os.makedirs(self.gitdir)
1152 self.bare_git.init()
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -08001153
Shawn O. Pearce88443382010-10-08 10:02:09 +02001154 mp = self.manifest.manifestProject
1155 ref_dir = mp.config.GetString('repo.reference')
1156
1157 if ref_dir:
1158 mirror_git = os.path.join(ref_dir, self.name + '.git')
1159 repo_git = os.path.join(ref_dir, '.repo', 'projects',
1160 self.relpath + '.git')
1161
1162 if os.path.exists(mirror_git):
1163 ref_dir = mirror_git
1164
1165 elif os.path.exists(repo_git):
1166 ref_dir = repo_git
1167
1168 else:
1169 ref_dir = None
1170
1171 if ref_dir:
1172 _lwrite(os.path.join(self.gitdir, 'objects/info/alternates'),
1173 os.path.join(ref_dir, 'objects') + '\n')
1174
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -08001175 if self.manifest.IsMirror:
1176 self.config.SetString('core.bare', 'true')
1177 else:
1178 self.config.SetString('core.bare', None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001179
1180 hooks = self._gitdir_path('hooks')
Shawn O. Pearcede646812008-10-29 14:38:12 -07001181 try:
1182 to_rm = os.listdir(hooks)
1183 except OSError:
1184 to_rm = []
1185 for old_hook in to_rm:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001186 os.remove(os.path.join(hooks, old_hook))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001187 self._InitHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001188
1189 m = self.manifest.manifestProject.config
1190 for key in ['user.name', 'user.email']:
1191 if m.Has(key, include_defaults = False):
1192 self.config.SetString(key, m.GetString(key))
1193
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001194 def _InitHooks(self):
1195 hooks = self._gitdir_path('hooks')
1196 if not os.path.exists(hooks):
1197 os.makedirs(hooks)
1198 for stock_hook in repo_hooks():
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001199 name = os.path.basename(stock_hook)
1200
1201 if name in ('commit-msg') and not self.remote.review:
1202 # Don't install a Gerrit Code Review hook if this
1203 # project does not appear to use it for reviews.
1204 #
1205 continue
1206
1207 dst = os.path.join(hooks, name)
1208 if os.path.islink(dst):
1209 continue
1210 if os.path.exists(dst):
1211 if filecmp.cmp(stock_hook, dst, shallow=False):
1212 os.remove(dst)
1213 else:
1214 _error("%s: Not replacing %s hook", self.relpath, name)
1215 continue
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001216 try:
1217 os.symlink(relpath(stock_hook, dst), dst)
1218 except OSError, e:
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001219 if e.errno == errno.EPERM:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001220 raise GitError('filesystem must support symlinks')
1221 else:
1222 raise
1223
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001224 def _InitRemote(self):
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001225 if self.remote.url:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001226 remote = self.GetRemote(self.remote.name)
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001227 remote.url = self.remote.url
1228 remote.review = self.remote.review
1229 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001230
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001231 if self.worktree:
1232 remote.ResetFetch(mirror=False)
1233 else:
1234 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001235 remote.Save()
1236
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001237 def _InitMRef(self):
1238 if self.manifest.branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001239 self._InitAnyMRef(R_M + self.manifest.branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001240
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001241 def _InitMirrorHead(self):
Shawn O. Pearcefe200ee2009-06-01 15:28:21 -07001242 self._InitAnyMRef(HEAD)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001243
1244 def _InitAnyMRef(self, ref):
1245 cur = self.bare_ref.symref(ref)
1246
1247 if self.revisionId:
1248 if cur != '' or self.bare_ref.get(ref) != self.revisionId:
1249 msg = 'manifest set to %s' % self.revisionId
1250 dst = self.revisionId + '^0'
1251 self.bare_git.UpdateRef(ref, dst, message = msg, detach = True)
1252 else:
1253 remote = self.GetRemote(self.remote.name)
1254 dst = remote.ToLocal(self.revisionExpr)
1255 if cur != dst:
1256 msg = 'manifest set to %s' % self.revisionExpr
1257 self.bare_git.symbolic_ref('-m', msg, ref, dst)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001258
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001259 def _InitWorkTree(self):
1260 dotgit = os.path.join(self.worktree, '.git')
1261 if not os.path.exists(dotgit):
1262 os.makedirs(dotgit)
1263
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001264 for name in ['config',
1265 'description',
1266 'hooks',
1267 'info',
1268 'logs',
1269 'objects',
1270 'packed-refs',
1271 'refs',
1272 'rr-cache',
1273 'svn']:
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001274 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001275 src = os.path.join(self.gitdir, name)
1276 dst = os.path.join(dotgit, name)
Nico Sallembiend63060f2010-01-20 10:27:50 -08001277 if os.path.islink(dst) or not os.path.exists(dst):
1278 os.symlink(relpath(src, dst), dst)
1279 else:
1280 raise GitError('cannot overwrite a local work tree')
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001281 except OSError, e:
1282 if e.errno == errno.EPERM:
1283 raise GitError('filesystem must support symlinks')
1284 else:
1285 raise
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001286
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001287 _lwrite(os.path.join(dotgit, HEAD), '%s\n' % self.GetRevisionId())
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001288
1289 cmd = ['read-tree', '--reset', '-u']
1290 cmd.append('-v')
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001291 cmd.append(HEAD)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001292 if GitCommand(self, cmd).Wait() != 0:
1293 raise GitError("cannot initialize work tree")
Shawn O. Pearce93609662009-04-21 10:50:33 -07001294 self._CopyFiles()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001295
1296 def _gitdir_path(self, path):
1297 return os.path.join(self.gitdir, path)
1298
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001299 def _revlist(self, *args, **kw):
1300 a = []
1301 a.extend(args)
1302 a.append('--')
1303 return self.work_git.rev_list(*a, **kw)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001304
1305 @property
1306 def _allrefs(self):
Shawn O. Pearced237b692009-04-17 18:49:50 -07001307 return self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001308
1309 class _GitGetByExec(object):
1310 def __init__(self, project, bare):
1311 self._project = project
1312 self._bare = bare
1313
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001314 def LsOthers(self):
1315 p = GitCommand(self._project,
1316 ['ls-files',
1317 '-z',
1318 '--others',
1319 '--exclude-standard'],
1320 bare = False,
1321 capture_stdout = True,
1322 capture_stderr = True)
1323 if p.Wait() == 0:
1324 out = p.stdout
1325 if out:
1326 return out[:-1].split("\0")
1327 return []
1328
1329 def DiffZ(self, name, *args):
1330 cmd = [name]
1331 cmd.append('-z')
1332 cmd.extend(args)
1333 p = GitCommand(self._project,
1334 cmd,
1335 bare = False,
1336 capture_stdout = True,
1337 capture_stderr = True)
1338 try:
1339 out = p.process.stdout.read()
1340 r = {}
1341 if out:
1342 out = iter(out[:-1].split('\0'))
1343 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07001344 try:
1345 info = out.next()
1346 path = out.next()
1347 except StopIteration:
1348 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001349
1350 class _Info(object):
1351 def __init__(self, path, omode, nmode, oid, nid, state):
1352 self.path = path
1353 self.src_path = None
1354 self.old_mode = omode
1355 self.new_mode = nmode
1356 self.old_id = oid
1357 self.new_id = nid
1358
1359 if len(state) == 1:
1360 self.status = state
1361 self.level = None
1362 else:
1363 self.status = state[:1]
1364 self.level = state[1:]
1365 while self.level.startswith('0'):
1366 self.level = self.level[1:]
1367
1368 info = info[1:].split(' ')
1369 info =_Info(path, *info)
1370 if info.status in ('R', 'C'):
1371 info.src_path = info.path
1372 info.path = out.next()
1373 r[info.path] = info
1374 return r
1375 finally:
1376 p.Wait()
1377
1378 def GetHead(self):
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001379 if self._bare:
1380 path = os.path.join(self._project.gitdir, HEAD)
1381 else:
1382 path = os.path.join(self._project.worktree, '.git', HEAD)
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -07001383 fd = open(path, 'rb')
1384 try:
1385 line = fd.read()
1386 finally:
1387 fd.close()
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001388 if line.startswith('ref: '):
1389 return line[5:-1]
1390 return line[:-1]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001391
1392 def SetHead(self, ref, message=None):
1393 cmdv = []
1394 if message is not None:
1395 cmdv.extend(['-m', message])
1396 cmdv.append(HEAD)
1397 cmdv.append(ref)
1398 self.symbolic_ref(*cmdv)
1399
1400 def DetachHead(self, new, message=None):
1401 cmdv = ['--no-deref']
1402 if message is not None:
1403 cmdv.extend(['-m', message])
1404 cmdv.append(HEAD)
1405 cmdv.append(new)
1406 self.update_ref(*cmdv)
1407
1408 def UpdateRef(self, name, new, old=None,
1409 message=None,
1410 detach=False):
1411 cmdv = []
1412 if message is not None:
1413 cmdv.extend(['-m', message])
1414 if detach:
1415 cmdv.append('--no-deref')
1416 cmdv.append(name)
1417 cmdv.append(new)
1418 if old is not None:
1419 cmdv.append(old)
1420 self.update_ref(*cmdv)
1421
1422 def DeleteRef(self, name, old=None):
1423 if not old:
1424 old = self.rev_parse(name)
1425 self.update_ref('-d', name, old)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001426 self._project.bare_ref.deleted(name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001427
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001428 def rev_list(self, *args, **kw):
1429 if 'format' in kw:
1430 cmdv = ['log', '--pretty=format:%s' % kw['format']]
1431 else:
1432 cmdv = ['rev-list']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001433 cmdv.extend(args)
1434 p = GitCommand(self._project,
1435 cmdv,
1436 bare = self._bare,
1437 capture_stdout = True,
1438 capture_stderr = True)
1439 r = []
1440 for line in p.process.stdout:
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001441 if line[-1] == '\n':
1442 line = line[:-1]
1443 r.append(line)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001444 if p.Wait() != 0:
1445 raise GitError('%s rev-list %s: %s' % (
1446 self._project.name,
1447 str(args),
1448 p.stderr))
1449 return r
1450
1451 def __getattr__(self, name):
1452 name = name.replace('_', '-')
1453 def runner(*args):
1454 cmdv = [name]
1455 cmdv.extend(args)
1456 p = GitCommand(self._project,
1457 cmdv,
1458 bare = self._bare,
1459 capture_stdout = True,
1460 capture_stderr = True)
1461 if p.Wait() != 0:
1462 raise GitError('%s %s: %s' % (
1463 self._project.name,
1464 name,
1465 p.stderr))
1466 r = p.stdout
1467 if r.endswith('\n') and r.index('\n') == len(r) - 1:
1468 return r[:-1]
1469 return r
1470 return runner
1471
1472
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001473class _PriorSyncFailedError(Exception):
1474 def __str__(self):
1475 return 'prior sync failed; rebase still in progress'
1476
1477class _DirtyError(Exception):
1478 def __str__(self):
1479 return 'contains uncommitted changes'
1480
1481class _InfoMessage(object):
1482 def __init__(self, project, text):
1483 self.project = project
1484 self.text = text
1485
1486 def Print(self, syncbuf):
1487 syncbuf.out.info('%s/: %s', self.project.relpath, self.text)
1488 syncbuf.out.nl()
1489
1490class _Failure(object):
1491 def __init__(self, project, why):
1492 self.project = project
1493 self.why = why
1494
1495 def Print(self, syncbuf):
1496 syncbuf.out.fail('error: %s/: %s',
1497 self.project.relpath,
1498 str(self.why))
1499 syncbuf.out.nl()
1500
1501class _Later(object):
1502 def __init__(self, project, action):
1503 self.project = project
1504 self.action = action
1505
1506 def Run(self, syncbuf):
1507 out = syncbuf.out
1508 out.project('project %s/', self.project.relpath)
1509 out.nl()
1510 try:
1511 self.action()
1512 out.nl()
1513 return True
1514 except GitError, e:
1515 out.nl()
1516 return False
1517
1518class _SyncColoring(Coloring):
1519 def __init__(self, config):
1520 Coloring.__init__(self, config, 'reposync')
1521 self.project = self.printer('header', attr = 'bold')
1522 self.info = self.printer('info')
1523 self.fail = self.printer('fail', fg='red')
1524
1525class SyncBuffer(object):
1526 def __init__(self, config, detach_head=False):
1527 self._messages = []
1528 self._failures = []
1529 self._later_queue1 = []
1530 self._later_queue2 = []
1531
1532 self.out = _SyncColoring(config)
1533 self.out.redirect(sys.stderr)
1534
1535 self.detach_head = detach_head
1536 self.clean = True
1537
1538 def info(self, project, fmt, *args):
1539 self._messages.append(_InfoMessage(project, fmt % args))
1540
1541 def fail(self, project, err=None):
1542 self._failures.append(_Failure(project, err))
1543 self.clean = False
1544
1545 def later1(self, project, what):
1546 self._later_queue1.append(_Later(project, what))
1547
1548 def later2(self, project, what):
1549 self._later_queue2.append(_Later(project, what))
1550
1551 def Finish(self):
1552 self._PrintMessages()
1553 self._RunLater()
1554 self._PrintMessages()
1555 return self.clean
1556
1557 def _RunLater(self):
1558 for q in ['_later_queue1', '_later_queue2']:
1559 if not self._RunQueue(q):
1560 return
1561
1562 def _RunQueue(self, queue):
1563 for m in getattr(self, queue):
1564 if not m.Run(self):
1565 self.clean = False
1566 return False
1567 setattr(self, queue, [])
1568 return True
1569
1570 def _PrintMessages(self):
1571 for m in self._messages:
1572 m.Print(self)
1573 for m in self._failures:
1574 m.Print(self)
1575
1576 self._messages = []
1577 self._failures = []
1578
1579
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001580class MetaProject(Project):
1581 """A special project housed under .repo.
1582 """
1583 def __init__(self, manifest, name, gitdir, worktree):
1584 repodir = manifest.repodir
1585 Project.__init__(self,
1586 manifest = manifest,
1587 name = name,
1588 gitdir = gitdir,
1589 worktree = worktree,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001590 remote = RemoteSpec('origin'),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001591 relpath = '.repo/%s' % name,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001592 revisionExpr = 'refs/heads/master',
1593 revisionId = None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001594
1595 def PreSync(self):
1596 if self.Exists:
1597 cb = self.CurrentBranch
1598 if cb:
1599 base = self.GetBranch(cb).merge
1600 if base:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001601 self.revisionExpr = base
1602 self.revisionId = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001603
1604 @property
Shawn O. Pearcef6906872009-04-18 10:49:00 -07001605 def LastFetch(self):
1606 try:
1607 fh = os.path.join(self.gitdir, 'FETCH_HEAD')
1608 return os.path.getmtime(fh)
1609 except OSError:
1610 return 0
1611
1612 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001613 def HasChanges(self):
1614 """Has the remote received new commits not yet checked out?
1615 """
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001616 if not self.remote or not self.revisionExpr:
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001617 return False
1618
1619 all = self.bare_ref.all
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001620 revid = self.GetRevisionId(all)
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001621 head = self.work_git.GetHead()
1622 if head.startswith(R_HEADS):
1623 try:
1624 head = all[head]
1625 except KeyError:
1626 head = None
1627
1628 if revid == head:
1629 return False
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001630 elif self._revlist(not_rev(HEAD), revid):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001631 return True
1632 return False