blob: 25347daf78805e8dedf1097610cdef04a1d13fe5 [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
114
115 @property
116 def name(self):
117 return self.branch.name
118
119 @property
120 def commits(self):
121 if self._commit_cache is None:
122 self._commit_cache = self.project.bare_git.rev_list(
123 '--abbrev=8',
124 '--abbrev-commit',
125 '--pretty=oneline',
126 '--reverse',
127 '--date-order',
128 not_rev(self.base),
129 R_HEADS + self.name,
130 '--')
131 return self._commit_cache
132
133 @property
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800134 def unabbrev_commits(self):
135 r = dict()
136 for commit in self.project.bare_git.rev_list(
137 not_rev(self.base),
138 R_HEADS + self.name,
139 '--'):
140 r[commit[0:8]] = commit
141 return r
142
143 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700144 def date(self):
145 return self.project.bare_git.log(
146 '--pretty=format:%cd',
147 '-n', '1',
148 R_HEADS + self.name,
149 '--')
150
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700151 def UploadForReview(self, people, auto_topic=False):
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800152 self.project.UploadForReview(self.name,
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700153 people,
154 auto_topic=auto_topic)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700155
Ficus Kirkpatrickbc7ef672009-05-04 12:45:11 -0700156 def GetPublishedRefs(self):
157 refs = {}
158 output = self.project.bare_git.ls_remote(
159 self.branch.remote.SshReviewUrl(self.project.UserEmail),
160 'refs/changes/*')
161 for line in output.split('\n'):
162 try:
163 (sha, ref) = line.split()
164 refs[sha] = ref
165 except ValueError:
166 pass
167
168 return refs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700169
170class StatusColoring(Coloring):
171 def __init__(self, config):
172 Coloring.__init__(self, config, 'status')
173 self.project = self.printer('header', attr = 'bold')
174 self.branch = self.printer('header', attr = 'bold')
175 self.nobranch = self.printer('nobranch', fg = 'red')
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700176 self.important = self.printer('important', fg = 'red')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700177
178 self.added = self.printer('added', fg = 'green')
179 self.changed = self.printer('changed', fg = 'red')
180 self.untracked = self.printer('untracked', fg = 'red')
181
182
183class DiffColoring(Coloring):
184 def __init__(self, config):
185 Coloring.__init__(self, config, 'diff')
186 self.project = self.printer('header', attr = 'bold')
187
188
189class _CopyFile:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800190 def __init__(self, src, dest, abssrc, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700191 self.src = src
192 self.dest = dest
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800193 self.abs_src = abssrc
194 self.abs_dest = absdest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700195
196 def _Copy(self):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800197 src = self.abs_src
198 dest = self.abs_dest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700199 # copy file if it does not exist or is out of date
200 if not os.path.exists(dest) or not filecmp.cmp(src, dest):
201 try:
202 # remove existing file first, since it might be read-only
203 if os.path.exists(dest):
204 os.remove(dest)
Matthew Buckett2daf6672009-07-11 09:43:47 -0400205 else:
206 dir = os.path.dirname(dest)
207 if not os.path.isdir(dir):
208 os.makedirs(dir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700209 shutil.copy(src, dest)
210 # make the file read-only
211 mode = os.stat(dest)[stat.ST_MODE]
212 mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
213 os.chmod(dest, mode)
214 except IOError:
Shawn O. Pearce48244782009-04-16 08:25:57 -0700215 _error('Cannot copy file %s to %s', src, dest)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700216
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700217class RemoteSpec(object):
218 def __init__(self,
219 name,
220 url = None,
221 review = None):
222 self.name = name
223 self.url = url
224 self.review = review
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700225
226class Project(object):
227 def __init__(self,
228 manifest,
229 name,
230 remote,
231 gitdir,
232 worktree,
233 relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700234 revisionExpr,
235 revisionId):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700236 self.manifest = manifest
237 self.name = name
238 self.remote = remote
Anthony Newnamdf14a702011-01-09 17:31:57 -0800239 self.gitdir = gitdir.replace('\\', '/')
240 self.worktree = worktree.replace('\\', '/')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700241 self.relpath = relpath
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700242 self.revisionExpr = revisionExpr
243
244 if revisionId is None \
245 and revisionExpr \
246 and IsId(revisionExpr):
247 self.revisionId = revisionExpr
248 else:
249 self.revisionId = revisionId
250
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700251 self.snapshots = {}
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700252 self.copyfiles = []
253 self.config = GitConfig.ForRepository(
254 gitdir = self.gitdir,
255 defaults = self.manifest.globalConfig)
256
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800257 if self.worktree:
258 self.work_git = self._GitGetByExec(self, bare=False)
259 else:
260 self.work_git = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700261 self.bare_git = self._GitGetByExec(self, bare=True)
Shawn O. Pearced237b692009-04-17 18:49:50 -0700262 self.bare_ref = GitRefs(gitdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700263
264 @property
265 def Exists(self):
266 return os.path.isdir(self.gitdir)
267
268 @property
269 def CurrentBranch(self):
270 """Obtain the name of the currently checked out branch.
271 The branch name omits the 'refs/heads/' prefix.
272 None is returned if the project is on a detached HEAD.
273 """
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700274 b = self.work_git.GetHead()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700275 if b.startswith(R_HEADS):
276 return b[len(R_HEADS):]
277 return None
278
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700279 def IsRebaseInProgress(self):
280 w = self.worktree
281 g = os.path.join(w, '.git')
282 return os.path.exists(os.path.join(g, 'rebase-apply')) \
283 or os.path.exists(os.path.join(g, 'rebase-merge')) \
284 or os.path.exists(os.path.join(w, '.dotest'))
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200285
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700286 def IsDirty(self, consider_untracked=True):
287 """Is the working directory modified in some way?
288 """
289 self.work_git.update_index('-q',
290 '--unmerged',
291 '--ignore-missing',
292 '--refresh')
293 if self.work_git.DiffZ('diff-index','-M','--cached',HEAD):
294 return True
295 if self.work_git.DiffZ('diff-files'):
296 return True
297 if consider_untracked and self.work_git.LsOthers():
298 return True
299 return False
300
301 _userident_name = None
302 _userident_email = None
303
304 @property
305 def UserName(self):
306 """Obtain the user's personal name.
307 """
308 if self._userident_name is None:
309 self._LoadUserIdentity()
310 return self._userident_name
311
312 @property
313 def UserEmail(self):
314 """Obtain the user's email address. This is very likely
315 to be their Gerrit login.
316 """
317 if self._userident_email is None:
318 self._LoadUserIdentity()
319 return self._userident_email
320
321 def _LoadUserIdentity(self):
322 u = self.bare_git.var('GIT_COMMITTER_IDENT')
323 m = re.compile("^(.*) <([^>]*)> ").match(u)
324 if m:
325 self._userident_name = m.group(1)
326 self._userident_email = m.group(2)
327 else:
328 self._userident_name = ''
329 self._userident_email = ''
330
331 def GetRemote(self, name):
332 """Get the configuration for a single remote.
333 """
334 return self.config.GetRemote(name)
335
336 def GetBranch(self, name):
337 """Get the configuration for a single branch.
338 """
339 return self.config.GetBranch(name)
340
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700341 def GetBranches(self):
342 """Get all existing local branches.
343 """
344 current = self.CurrentBranch
Shawn O. Pearced237b692009-04-17 18:49:50 -0700345 all = self._allrefs
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700346 heads = {}
347 pubd = {}
348
349 for name, id in all.iteritems():
350 if name.startswith(R_HEADS):
351 name = name[len(R_HEADS):]
352 b = self.GetBranch(name)
353 b.current = name == current
354 b.published = None
355 b.revision = id
356 heads[name] = b
357
358 for name, id in all.iteritems():
359 if name.startswith(R_PUB):
360 name = name[len(R_PUB):]
361 b = heads.get(name)
362 if b:
363 b.published = id
364
365 return heads
366
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700367
368## Status Display ##
369
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500370 def HasChanges(self):
371 """Returns true if there are uncommitted changes.
372 """
373 self.work_git.update_index('-q',
374 '--unmerged',
375 '--ignore-missing',
376 '--refresh')
377 if self.IsRebaseInProgress():
378 return True
379
380 if self.work_git.DiffZ('diff-index', '--cached', HEAD):
381 return True
382
383 if self.work_git.DiffZ('diff-files'):
384 return True
385
386 if self.work_git.LsOthers():
387 return True
388
389 return False
390
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700391 def PrintWorkTreeStatus(self):
392 """Prints the status of the repository to stdout.
393 """
394 if not os.path.isdir(self.worktree):
395 print ''
396 print 'project %s/' % self.relpath
397 print ' missing (run "repo sync")'
398 return
399
400 self.work_git.update_index('-q',
401 '--unmerged',
402 '--ignore-missing',
403 '--refresh')
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700404 rb = self.IsRebaseInProgress()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700405 di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD)
406 df = self.work_git.DiffZ('diff-files')
407 do = self.work_git.LsOthers()
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700408 if not rb and not di and not df and not do:
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700409 return 'CLEAN'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700410
411 out = StatusColoring(self.config)
412 out.project('project %-40s', self.relpath + '/')
413
414 branch = self.CurrentBranch
415 if branch is None:
416 out.nobranch('(*** NO BRANCH ***)')
417 else:
418 out.branch('branch %s', branch)
419 out.nl()
420
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700421 if rb:
422 out.important('prior sync failed; rebase still in progress')
423 out.nl()
424
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700425 paths = list()
426 paths.extend(di.keys())
427 paths.extend(df.keys())
428 paths.extend(do)
429
430 paths = list(set(paths))
431 paths.sort()
432
433 for p in paths:
434 try: i = di[p]
435 except KeyError: i = None
436
437 try: f = df[p]
438 except KeyError: f = None
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200439
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700440 if i: i_status = i.status.upper()
441 else: i_status = '-'
442
443 if f: f_status = f.status.lower()
444 else: f_status = '-'
445
446 if i and i.src_path:
Shawn O. Pearcefe086752009-03-03 13:49:48 -0800447 line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700448 i.src_path, p, i.level)
449 else:
450 line = ' %s%s\t%s' % (i_status, f_status, p)
451
452 if i and not f:
453 out.added('%s', line)
454 elif (i and f) or (not i and f):
455 out.changed('%s', line)
456 elif not i and not f:
457 out.untracked('%s', line)
458 else:
459 out.write('%s', line)
460 out.nl()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700461 return 'DIRTY'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700462
463 def PrintWorkTreeDiff(self):
464 """Prints the status of the repository to stdout.
465 """
466 out = DiffColoring(self.config)
467 cmd = ['diff']
468 if out.is_on:
469 cmd.append('--color')
470 cmd.append(HEAD)
471 cmd.append('--')
472 p = GitCommand(self,
473 cmd,
474 capture_stdout = True,
475 capture_stderr = True)
476 has_diff = False
477 for line in p.process.stdout:
478 if not has_diff:
479 out.nl()
480 out.project('project %s/' % self.relpath)
481 out.nl()
482 has_diff = True
483 print line[:-1]
484 p.Wait()
485
486
487## Publish / Upload ##
488
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700489 def WasPublished(self, branch, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700490 """Was the branch published (uploaded) for code review?
491 If so, returns the SHA-1 hash of the last published
492 state for the branch.
493 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700494 key = R_PUB + branch
495 if all is None:
496 try:
497 return self.bare_git.rev_parse(key)
498 except GitError:
499 return None
500 else:
501 try:
502 return all[key]
503 except KeyError:
504 return None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700505
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700506 def CleanPublishedCache(self, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700507 """Prunes any stale published refs.
508 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700509 if all is None:
510 all = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700511 heads = set()
512 canrm = {}
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700513 for name, id in all.iteritems():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700514 if name.startswith(R_HEADS):
515 heads.add(name)
516 elif name.startswith(R_PUB):
517 canrm[name] = id
518
519 for name, id in canrm.iteritems():
520 n = name[len(R_PUB):]
521 if R_HEADS + n not in heads:
522 self.bare_git.DeleteRef(name, id)
523
524 def GetUploadableBranches(self):
525 """List any branches which can be uploaded for review.
526 """
527 heads = {}
528 pubed = {}
529
530 for name, id in self._allrefs.iteritems():
531 if name.startswith(R_HEADS):
532 heads[name[len(R_HEADS):]] = id
533 elif name.startswith(R_PUB):
534 pubed[name[len(R_PUB):]] = id
535
536 ready = []
537 for branch, id in heads.iteritems():
538 if branch in pubed and pubed[branch] == id:
539 continue
540
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800541 rb = self.GetUploadableBranch(branch)
542 if rb:
543 ready.append(rb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700544 return ready
545
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800546 def GetUploadableBranch(self, branch_name):
547 """Get a single uploadable branch, or None.
548 """
549 branch = self.GetBranch(branch_name)
550 base = branch.LocalMerge
551 if branch.LocalMerge:
552 rb = ReviewableBranch(self, branch, base)
553 if rb.commits:
554 return rb
555 return None
556
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700557 def UploadForReview(self, branch=None,
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700558 people=([],[]),
559 auto_topic=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700560 """Uploads the named branch for code review.
561 """
562 if branch is None:
563 branch = self.CurrentBranch
564 if branch is None:
565 raise GitError('not currently on a branch')
566
567 branch = self.GetBranch(branch)
568 if not branch.LocalMerge:
569 raise GitError('branch %s does not track a remote' % branch.name)
570 if not branch.remote.review:
571 raise GitError('remote %s has no review url' % branch.remote.name)
572
573 dest_branch = branch.merge
574 if not dest_branch.startswith(R_HEADS):
575 dest_branch = R_HEADS + dest_branch
576
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800577 if not branch.remote.projectname:
578 branch.remote.projectname = self.name
579 branch.remote.Save()
580
Shawn O. Pearce370e3fa2009-01-26 10:55:39 -0800581 if branch.remote.ReviewProtocol == 'ssh':
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800582 if dest_branch.startswith(R_HEADS):
583 dest_branch = dest_branch[len(R_HEADS):]
584
585 rp = ['gerrit receive-pack']
586 for e in people[0]:
587 rp.append('--reviewer=%s' % sq(e))
588 for e in people[1]:
589 rp.append('--cc=%s' % sq(e))
590
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700591 ref_spec = '%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch)
592 if auto_topic:
593 ref_spec = ref_spec + '/' + branch.name
594
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800595 cmd = ['push']
596 cmd.append('--receive-pack=%s' % " ".join(rp))
597 cmd.append(branch.remote.SshReviewUrl(self.UserEmail))
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700598 cmd.append(ref_spec)
599
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800600 if GitCommand(self, cmd, bare = True).Wait() != 0:
601 raise UploadError('Upload failed')
602
603 else:
604 raise UploadError('Unsupported protocol %s' \
605 % branch.remote.review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700606
607 msg = "posted to %s for %s" % (branch.remote.review, dest_branch)
608 self.bare_git.UpdateRef(R_PUB + branch.name,
609 R_HEADS + branch.name,
610 message = msg)
611
612
613## Sync ##
614
Shawn O. Pearce16614f82010-10-29 12:05:43 -0700615 def Sync_NetworkHalf(self, quiet=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700616 """Perform only the network IO portion of the sync process.
617 Local working directory/branch state is not affected.
618 """
Shawn O. Pearce88443382010-10-08 10:02:09 +0200619 is_new = not self.Exists
620 if is_new:
Shawn O. Pearce16614f82010-10-29 12:05:43 -0700621 if not quiet:
622 print >>sys.stderr
623 print >>sys.stderr, 'Initializing project %s ...' % self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700624 self._InitGitDir()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800625
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700626 self._InitRemote()
Shawn O. Pearce16614f82010-10-29 12:05:43 -0700627 if not self._RemoteFetch(initial=is_new, quiet=quiet):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700628 return False
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800629
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200630 #Check that the requested ref was found after fetch
631 #
632 try:
633 self.GetRevisionId()
634 except ManifestInvalidRevisionError:
635 # if the ref is a tag. We can try fetching
636 # the tag manually as a last resort
637 #
638 rev = self.revisionExpr
639 if rev.startswith(R_TAGS):
Shawn O. Pearce16614f82010-10-29 12:05:43 -0700640 self._RemoteFetch(None, rev[len(R_TAGS):], quiet=quiet)
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200641
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800642 if self.worktree:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800643 self._InitMRef()
644 else:
645 self._InitMirrorHead()
646 try:
647 os.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
648 except OSError:
649 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700650 return True
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800651
652 def PostRepoUpgrade(self):
653 self._InitHooks()
654
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700655 def _CopyFiles(self):
656 for file in self.copyfiles:
657 file._Copy()
658
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700659 def GetRevisionId(self, all=None):
660 if self.revisionId:
661 return self.revisionId
662
663 rem = self.GetRemote(self.remote.name)
664 rev = rem.ToLocal(self.revisionExpr)
665
666 if all is not None and rev in all:
667 return all[rev]
668
669 try:
670 return self.bare_git.rev_parse('--verify', '%s^0' % rev)
671 except GitError:
672 raise ManifestInvalidRevisionError(
673 'revision %s in %s not found' % (self.revisionExpr,
674 self.name))
675
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700676 def Sync_LocalHalf(self, syncbuf):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700677 """Perform only the local IO portion of the sync process.
678 Network access is not required.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700679 """
680 self._InitWorkTree()
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700681 all = self.bare_ref.all
682 self.CleanPublishedCache(all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700683
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700684 revid = self.GetRevisionId(all)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700685 head = self.work_git.GetHead()
686 if head.startswith(R_HEADS):
687 branch = head[len(R_HEADS):]
688 try:
689 head = all[head]
690 except KeyError:
691 head = None
692 else:
693 branch = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700694
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700695 if branch is None or syncbuf.detach_head:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700696 # Currently on a detached HEAD. The user is assumed to
697 # not have any local modifications worth worrying about.
698 #
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700699 if self.IsRebaseInProgress():
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700700 syncbuf.fail(self, _PriorSyncFailedError())
701 return
702
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700703 if head == revid:
704 # No changes; don't do anything further.
705 #
706 return
707
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700708 lost = self._revlist(not_rev(revid), HEAD)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700709 if lost:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700710 syncbuf.info(self, "discarding %d commits", len(lost))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700711 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700712 self._Checkout(revid, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700713 except GitError, e:
714 syncbuf.fail(self, e)
715 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700716 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700717 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700718
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700719 if head == revid:
720 # No changes; don't do anything further.
721 #
722 return
723
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700724 branch = self.GetBranch(branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700725
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700726 if not branch.LocalMerge:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700727 # The current branch has no tracking configuration.
728 # Jump off it to a deatched HEAD.
729 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700730 syncbuf.info(self,
731 "leaving %s; does not track upstream",
732 branch.name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700733 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700734 self._Checkout(revid, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700735 except GitError, e:
736 syncbuf.fail(self, e)
737 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700738 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700739 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700740
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700741 upstream_gain = self._revlist(not_rev(HEAD), revid)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700742 pub = self.WasPublished(branch.name, all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700743 if pub:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700744 not_merged = self._revlist(not_rev(revid), pub)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700745 if not_merged:
746 if upstream_gain:
747 # The user has published this branch and some of those
748 # commits are not yet merged upstream. We do not want
749 # to rewrite the published commits so we punt.
750 #
Daniel Sandler4c50dee2010-03-02 15:38:03 -0500751 syncbuf.fail(self,
752 "branch %s is published (but not merged) and is now %d commits behind"
753 % (branch.name, len(upstream_gain)))
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700754 return
Shawn O. Pearce05f66b62009-04-21 08:26:32 -0700755 elif pub == head:
756 # All published commits are merged, and thus we are a
757 # strict subset. We can fast-forward safely.
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700758 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700759 def _doff():
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700760 self._FastForward(revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700761 self._CopyFiles()
762 syncbuf.later1(self, _doff)
763 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700764
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700765 # Examine the local commits not in the remote. Find the
766 # last one attributed to this user, if any.
767 #
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700768 local_changes = self._revlist(not_rev(revid), HEAD, format='%H %ce')
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700769 last_mine = None
770 cnt_mine = 0
771 for commit in local_changes:
Shawn O. Pearceaa4982e2009-12-30 18:38:27 -0800772 commit_id, committer_email = commit.split(' ', 1)
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700773 if committer_email == self.UserEmail:
774 last_mine = commit_id
775 cnt_mine += 1
776
Shawn O. Pearceda88ff42009-06-03 11:09:12 -0700777 if not upstream_gain and cnt_mine == len(local_changes):
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700778 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700779
780 if self.IsDirty(consider_untracked=False):
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700781 syncbuf.fail(self, _DirtyError())
782 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700783
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700784 # If the upstream switched on us, warn the user.
785 #
786 if branch.merge != self.revisionExpr:
787 if branch.merge and self.revisionExpr:
788 syncbuf.info(self,
789 'manifest switched %s...%s',
790 branch.merge,
791 self.revisionExpr)
792 elif branch.merge:
793 syncbuf.info(self,
794 'manifest no longer tracks %s',
795 branch.merge)
796
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700797 if cnt_mine < len(local_changes):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700798 # Upstream rebased. Not everything in HEAD
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700799 # was created by this user.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700800 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700801 syncbuf.info(self,
802 "discarding %d commits removed from upstream",
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700803 len(local_changes) - cnt_mine)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700804
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700805 branch.remote = self.GetRemote(self.remote.name)
806 branch.merge = self.revisionExpr
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700807 branch.Save()
808
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700809 if cnt_mine > 0:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700810 def _dorebase():
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700811 self._Rebase(upstream = '%s^1' % last_mine, onto = revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700812 self._CopyFiles()
813 syncbuf.later2(self, _dorebase)
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700814 elif local_changes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700815 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700816 self._ResetHard(revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700817 self._CopyFiles()
818 except GitError, e:
819 syncbuf.fail(self, e)
820 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700821 else:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700822 def _doff():
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700823 self._FastForward(revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700824 self._CopyFiles()
825 syncbuf.later1(self, _doff)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700826
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800827 def AddCopyFile(self, src, dest, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700828 # dest should already be an absolute path, but src is project relative
829 # make src an absolute path
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800830 abssrc = os.path.join(self.worktree, src)
831 self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700832
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700833 def DownloadPatchSet(self, change_id, patch_id):
834 """Download a single patch set of a single change to FETCH_HEAD.
835 """
836 remote = self.GetRemote(self.remote.name)
837
838 cmd = ['fetch', remote.name]
839 cmd.append('refs/changes/%2.2d/%d/%d' \
840 % (change_id % 100, change_id, patch_id))
841 cmd.extend(map(lambda x: str(x), remote.fetch))
842 if GitCommand(self, cmd, bare=True).Wait() != 0:
843 return None
844 return DownloadedChange(self,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700845 self.GetRevisionId(),
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700846 change_id,
847 patch_id,
848 self.bare_git.rev_parse('FETCH_HEAD'))
849
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700850
851## Branch Management ##
852
853 def StartBranch(self, name):
854 """Create a new branch off the manifest's revision.
855 """
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700856 head = self.work_git.GetHead()
857 if head == (R_HEADS + name):
858 return True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700859
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700860 all = self.bare_ref.all
861 if (R_HEADS + name) in all:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700862 return GitCommand(self,
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700863 ['checkout', name, '--'],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700864 capture_stdout = True,
865 capture_stderr = True).Wait() == 0
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700866
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700867 branch = self.GetBranch(name)
868 branch.remote = self.GetRemote(self.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700869 branch.merge = self.revisionExpr
870 revid = self.GetRevisionId(all)
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700871
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700872 if head.startswith(R_HEADS):
873 try:
874 head = all[head]
875 except KeyError:
876 head = None
877
878 if revid and head and revid == head:
879 ref = os.path.join(self.gitdir, R_HEADS + name)
880 try:
881 os.makedirs(os.path.dirname(ref))
882 except OSError:
883 pass
884 _lwrite(ref, '%s\n' % revid)
885 _lwrite(os.path.join(self.worktree, '.git', HEAD),
886 'ref: %s%s\n' % (R_HEADS, name))
887 branch.Save()
888 return True
889
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700890 if GitCommand(self,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700891 ['checkout', '-b', branch.name, revid],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700892 capture_stdout = True,
893 capture_stderr = True).Wait() == 0:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700894 branch.Save()
895 return True
896 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700897
Wink Saville02d79452009-04-10 13:01:24 -0700898 def CheckoutBranch(self, name):
899 """Checkout a local topic branch.
900 """
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700901 rev = R_HEADS + name
902 head = self.work_git.GetHead()
903 if head == rev:
904 # Already on the branch
905 #
906 return True
Wink Saville02d79452009-04-10 13:01:24 -0700907
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700908 all = self.bare_ref.all
Wink Saville02d79452009-04-10 13:01:24 -0700909 try:
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700910 revid = all[rev]
911 except KeyError:
912 # Branch does not exist in this project
913 #
914 return False
Wink Saville02d79452009-04-10 13:01:24 -0700915
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700916 if head.startswith(R_HEADS):
917 try:
918 head = all[head]
919 except KeyError:
920 head = None
921
922 if head == revid:
923 # Same revision; just update HEAD to point to the new
924 # target branch, but otherwise take no other action.
925 #
926 _lwrite(os.path.join(self.worktree, '.git', HEAD),
927 'ref: %s%s\n' % (R_HEADS, name))
928 return True
929
930 return GitCommand(self,
931 ['checkout', name, '--'],
932 capture_stdout = True,
933 capture_stderr = True).Wait() == 0
Wink Saville02d79452009-04-10 13:01:24 -0700934
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800935 def AbandonBranch(self, name):
936 """Destroy a local topic branch.
937 """
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700938 rev = R_HEADS + name
939 all = self.bare_ref.all
940 if rev not in all:
941 # Doesn't exist; assume already abandoned.
942 #
943 return True
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800944
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700945 head = self.work_git.GetHead()
946 if head == rev:
947 # We can't destroy the branch while we are sitting
948 # on it. Switch to a detached HEAD.
949 #
950 head = all[head]
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800951
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700952 revid = self.GetRevisionId(all)
953 if head == revid:
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700954 _lwrite(os.path.join(self.worktree, '.git', HEAD),
955 '%s\n' % revid)
956 else:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700957 self._Checkout(revid, quiet=True)
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700958
959 return GitCommand(self,
960 ['branch', '-D', name],
961 capture_stdout = True,
962 capture_stderr = True).Wait() == 0
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800963
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700964 def PruneHeads(self):
965 """Prune any topic branches already merged into upstream.
966 """
967 cb = self.CurrentBranch
968 kill = []
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800969 left = self._allrefs
970 for name in left.keys():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700971 if name.startswith(R_HEADS):
972 name = name[len(R_HEADS):]
973 if cb is None or name != cb:
974 kill.append(name)
975
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700976 rev = self.GetRevisionId(left)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700977 if cb is not None \
978 and not self._revlist(HEAD + '...' + rev) \
979 and not self.IsDirty(consider_untracked = False):
980 self.work_git.DetachHead(HEAD)
981 kill.append(cb)
982
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700983 if kill:
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700984 old = self.bare_git.GetHead()
985 if old is None:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700986 old = 'refs/heads/please_never_use_this_as_a_branch_name'
987
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700988 try:
989 self.bare_git.DetachHead(rev)
990
991 b = ['branch', '-d']
992 b.extend(kill)
993 b = GitCommand(self, b, bare=True,
994 capture_stdout=True,
995 capture_stderr=True)
996 b.Wait()
997 finally:
998 self.bare_git.SetHead(old)
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800999 left = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001000
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -08001001 for branch in kill:
1002 if (R_HEADS + branch) not in left:
1003 self.CleanPublishedCache()
1004 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001005
1006 if cb and cb not in kill:
1007 kill.append(cb)
Shawn O. Pearce7c6c64d2009-03-02 12:38:13 -08001008 kill.sort()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001009
1010 kept = []
1011 for branch in kill:
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -08001012 if (R_HEADS + branch) in left:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001013 branch = self.GetBranch(branch)
1014 base = branch.LocalMerge
1015 if not base:
1016 base = rev
1017 kept.append(ReviewableBranch(self, branch, base))
1018 return kept
1019
1020
1021## Direct Git Commands ##
1022
Shawn O. Pearce16614f82010-10-29 12:05:43 -07001023 def _RemoteFetch(self, name=None, tag=None,
1024 initial=False,
1025 quiet=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001026 if not name:
1027 name = self.remote.name
Shawn O. Pearcefb231612009-04-10 18:53:46 -07001028
1029 ssh_proxy = False
1030 if self.GetRemote(name).PreConnectFetch():
1031 ssh_proxy = True
1032
Shawn O. Pearce88443382010-10-08 10:02:09 +02001033 if initial:
1034 alt = os.path.join(self.gitdir, 'objects/info/alternates')
1035 try:
1036 fd = open(alt, 'rb')
1037 try:
1038 ref_dir = fd.readline()
1039 if ref_dir and ref_dir.endswith('\n'):
1040 ref_dir = ref_dir[:-1]
1041 finally:
1042 fd.close()
1043 except IOError, e:
1044 ref_dir = None
1045
1046 if ref_dir and 'objects' == os.path.basename(ref_dir):
1047 ref_dir = os.path.dirname(ref_dir)
1048 packed_refs = os.path.join(self.gitdir, 'packed-refs')
1049 remote = self.GetRemote(name)
1050
1051 all = self.bare_ref.all
1052 ids = set(all.values())
1053 tmp = set()
1054
1055 for r, id in GitRefs(ref_dir).all.iteritems():
1056 if r not in all:
1057 if r.startswith(R_TAGS) or remote.WritesTo(r):
1058 all[r] = id
1059 ids.add(id)
1060 continue
1061
1062 if id in ids:
1063 continue
1064
1065 r = 'refs/_alt/%s' % id
1066 all[r] = id
1067 ids.add(id)
1068 tmp.add(r)
1069
1070 ref_names = list(all.keys())
1071 ref_names.sort()
1072
1073 tmp_packed = ''
1074 old_packed = ''
1075
1076 for r in ref_names:
1077 line = '%s %s\n' % (all[r], r)
1078 tmp_packed += line
1079 if r not in tmp:
1080 old_packed += line
1081
1082 _lwrite(packed_refs, tmp_packed)
1083
1084 else:
1085 ref_dir = None
1086
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001087 cmd = ['fetch']
Shawn O. Pearce16614f82010-10-29 12:05:43 -07001088 if quiet:
1089 cmd.append('--quiet')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001090 if not self.worktree:
1091 cmd.append('--update-head-ok')
1092 cmd.append(name)
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +02001093 if tag is not None:
1094 cmd.append('tag')
1095 cmd.append(tag)
Shawn O. Pearce88443382010-10-08 10:02:09 +02001096
1097 ok = GitCommand(self,
1098 cmd,
1099 bare = True,
1100 ssh_proxy = ssh_proxy).Wait() == 0
1101
1102 if initial:
1103 if ref_dir:
1104 if old_packed != '':
1105 _lwrite(packed_refs, old_packed)
1106 else:
1107 os.remove(packed_refs)
1108 self.bare_git.pack_refs('--all', '--prune')
1109
1110 return ok
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001111
1112 def _Checkout(self, rev, quiet=False):
1113 cmd = ['checkout']
1114 if quiet:
1115 cmd.append('-q')
1116 cmd.append(rev)
1117 cmd.append('--')
1118 if GitCommand(self, cmd).Wait() != 0:
1119 if self._allrefs:
1120 raise GitError('%s checkout %s ' % (self.name, rev))
1121
1122 def _ResetHard(self, rev, quiet=True):
1123 cmd = ['reset', '--hard']
1124 if quiet:
1125 cmd.append('-q')
1126 cmd.append(rev)
1127 if GitCommand(self, cmd).Wait() != 0:
1128 raise GitError('%s reset --hard %s ' % (self.name, rev))
1129
1130 def _Rebase(self, upstream, onto = None):
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07001131 cmd = ['rebase']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001132 if onto is not None:
1133 cmd.extend(['--onto', onto])
1134 cmd.append(upstream)
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07001135 if GitCommand(self, cmd).Wait() != 0:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001136 raise GitError('%s rebase %s ' % (self.name, upstream))
1137
1138 def _FastForward(self, head):
1139 cmd = ['merge', head]
1140 if GitCommand(self, cmd).Wait() != 0:
1141 raise GitError('%s merge %s ' % (self.name, head))
1142
1143 def _InitGitDir(self):
1144 if not os.path.exists(self.gitdir):
1145 os.makedirs(self.gitdir)
1146 self.bare_git.init()
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -08001147
Shawn O. Pearce88443382010-10-08 10:02:09 +02001148 mp = self.manifest.manifestProject
1149 ref_dir = mp.config.GetString('repo.reference')
1150
1151 if ref_dir:
1152 mirror_git = os.path.join(ref_dir, self.name + '.git')
1153 repo_git = os.path.join(ref_dir, '.repo', 'projects',
1154 self.relpath + '.git')
1155
1156 if os.path.exists(mirror_git):
1157 ref_dir = mirror_git
1158
1159 elif os.path.exists(repo_git):
1160 ref_dir = repo_git
1161
1162 else:
1163 ref_dir = None
1164
1165 if ref_dir:
1166 _lwrite(os.path.join(self.gitdir, 'objects/info/alternates'),
1167 os.path.join(ref_dir, 'objects') + '\n')
1168
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -08001169 if self.manifest.IsMirror:
1170 self.config.SetString('core.bare', 'true')
1171 else:
1172 self.config.SetString('core.bare', None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001173
1174 hooks = self._gitdir_path('hooks')
Shawn O. Pearcede646812008-10-29 14:38:12 -07001175 try:
1176 to_rm = os.listdir(hooks)
1177 except OSError:
1178 to_rm = []
1179 for old_hook in to_rm:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001180 os.remove(os.path.join(hooks, old_hook))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001181 self._InitHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001182
1183 m = self.manifest.manifestProject.config
1184 for key in ['user.name', 'user.email']:
1185 if m.Has(key, include_defaults = False):
1186 self.config.SetString(key, m.GetString(key))
1187
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001188 def _InitHooks(self):
1189 hooks = self._gitdir_path('hooks')
1190 if not os.path.exists(hooks):
1191 os.makedirs(hooks)
1192 for stock_hook in repo_hooks():
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001193 name = os.path.basename(stock_hook)
1194
1195 if name in ('commit-msg') and not self.remote.review:
1196 # Don't install a Gerrit Code Review hook if this
1197 # project does not appear to use it for reviews.
1198 #
1199 continue
1200
1201 dst = os.path.join(hooks, name)
1202 if os.path.islink(dst):
1203 continue
1204 if os.path.exists(dst):
1205 if filecmp.cmp(stock_hook, dst, shallow=False):
1206 os.remove(dst)
1207 else:
1208 _error("%s: Not replacing %s hook", self.relpath, name)
1209 continue
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001210 try:
1211 os.symlink(relpath(stock_hook, dst), dst)
1212 except OSError, e:
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001213 if e.errno == errno.EPERM:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001214 raise GitError('filesystem must support symlinks')
1215 else:
1216 raise
1217
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001218 def _InitRemote(self):
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001219 if self.remote.url:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001220 remote = self.GetRemote(self.remote.name)
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001221 remote.url = self.remote.url
1222 remote.review = self.remote.review
1223 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001224
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001225 if self.worktree:
1226 remote.ResetFetch(mirror=False)
1227 else:
1228 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001229 remote.Save()
1230
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001231 def _InitMRef(self):
1232 if self.manifest.branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001233 self._InitAnyMRef(R_M + self.manifest.branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001234
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001235 def _InitMirrorHead(self):
Shawn O. Pearcefe200ee2009-06-01 15:28:21 -07001236 self._InitAnyMRef(HEAD)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001237
1238 def _InitAnyMRef(self, ref):
1239 cur = self.bare_ref.symref(ref)
1240
1241 if self.revisionId:
1242 if cur != '' or self.bare_ref.get(ref) != self.revisionId:
1243 msg = 'manifest set to %s' % self.revisionId
1244 dst = self.revisionId + '^0'
1245 self.bare_git.UpdateRef(ref, dst, message = msg, detach = True)
1246 else:
1247 remote = self.GetRemote(self.remote.name)
1248 dst = remote.ToLocal(self.revisionExpr)
1249 if cur != dst:
1250 msg = 'manifest set to %s' % self.revisionExpr
1251 self.bare_git.symbolic_ref('-m', msg, ref, dst)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001252
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001253 def _InitWorkTree(self):
1254 dotgit = os.path.join(self.worktree, '.git')
1255 if not os.path.exists(dotgit):
1256 os.makedirs(dotgit)
1257
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001258 for name in ['config',
1259 'description',
1260 'hooks',
1261 'info',
1262 'logs',
1263 'objects',
1264 'packed-refs',
1265 'refs',
1266 'rr-cache',
1267 'svn']:
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001268 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001269 src = os.path.join(self.gitdir, name)
1270 dst = os.path.join(dotgit, name)
Nico Sallembiend63060f2010-01-20 10:27:50 -08001271 if os.path.islink(dst) or not os.path.exists(dst):
1272 os.symlink(relpath(src, dst), dst)
1273 else:
1274 raise GitError('cannot overwrite a local work tree')
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001275 except OSError, e:
1276 if e.errno == errno.EPERM:
1277 raise GitError('filesystem must support symlinks')
1278 else:
1279 raise
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001280
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001281 _lwrite(os.path.join(dotgit, HEAD), '%s\n' % self.GetRevisionId())
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001282
1283 cmd = ['read-tree', '--reset', '-u']
1284 cmd.append('-v')
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001285 cmd.append(HEAD)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001286 if GitCommand(self, cmd).Wait() != 0:
1287 raise GitError("cannot initialize work tree")
Shawn O. Pearce93609662009-04-21 10:50:33 -07001288 self._CopyFiles()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001289
1290 def _gitdir_path(self, path):
1291 return os.path.join(self.gitdir, path)
1292
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001293 def _revlist(self, *args, **kw):
1294 a = []
1295 a.extend(args)
1296 a.append('--')
1297 return self.work_git.rev_list(*a, **kw)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001298
1299 @property
1300 def _allrefs(self):
Shawn O. Pearced237b692009-04-17 18:49:50 -07001301 return self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001302
1303 class _GitGetByExec(object):
1304 def __init__(self, project, bare):
1305 self._project = project
1306 self._bare = bare
1307
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001308 def LsOthers(self):
1309 p = GitCommand(self._project,
1310 ['ls-files',
1311 '-z',
1312 '--others',
1313 '--exclude-standard'],
1314 bare = False,
1315 capture_stdout = True,
1316 capture_stderr = True)
1317 if p.Wait() == 0:
1318 out = p.stdout
1319 if out:
1320 return out[:-1].split("\0")
1321 return []
1322
1323 def DiffZ(self, name, *args):
1324 cmd = [name]
1325 cmd.append('-z')
1326 cmd.extend(args)
1327 p = GitCommand(self._project,
1328 cmd,
1329 bare = False,
1330 capture_stdout = True,
1331 capture_stderr = True)
1332 try:
1333 out = p.process.stdout.read()
1334 r = {}
1335 if out:
1336 out = iter(out[:-1].split('\0'))
1337 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07001338 try:
1339 info = out.next()
1340 path = out.next()
1341 except StopIteration:
1342 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001343
1344 class _Info(object):
1345 def __init__(self, path, omode, nmode, oid, nid, state):
1346 self.path = path
1347 self.src_path = None
1348 self.old_mode = omode
1349 self.new_mode = nmode
1350 self.old_id = oid
1351 self.new_id = nid
1352
1353 if len(state) == 1:
1354 self.status = state
1355 self.level = None
1356 else:
1357 self.status = state[:1]
1358 self.level = state[1:]
1359 while self.level.startswith('0'):
1360 self.level = self.level[1:]
1361
1362 info = info[1:].split(' ')
1363 info =_Info(path, *info)
1364 if info.status in ('R', 'C'):
1365 info.src_path = info.path
1366 info.path = out.next()
1367 r[info.path] = info
1368 return r
1369 finally:
1370 p.Wait()
1371
1372 def GetHead(self):
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001373 if self._bare:
1374 path = os.path.join(self._project.gitdir, HEAD)
1375 else:
1376 path = os.path.join(self._project.worktree, '.git', HEAD)
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -07001377 fd = open(path, 'rb')
1378 try:
1379 line = fd.read()
1380 finally:
1381 fd.close()
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001382 if line.startswith('ref: '):
1383 return line[5:-1]
1384 return line[:-1]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001385
1386 def SetHead(self, ref, message=None):
1387 cmdv = []
1388 if message is not None:
1389 cmdv.extend(['-m', message])
1390 cmdv.append(HEAD)
1391 cmdv.append(ref)
1392 self.symbolic_ref(*cmdv)
1393
1394 def DetachHead(self, new, message=None):
1395 cmdv = ['--no-deref']
1396 if message is not None:
1397 cmdv.extend(['-m', message])
1398 cmdv.append(HEAD)
1399 cmdv.append(new)
1400 self.update_ref(*cmdv)
1401
1402 def UpdateRef(self, name, new, old=None,
1403 message=None,
1404 detach=False):
1405 cmdv = []
1406 if message is not None:
1407 cmdv.extend(['-m', message])
1408 if detach:
1409 cmdv.append('--no-deref')
1410 cmdv.append(name)
1411 cmdv.append(new)
1412 if old is not None:
1413 cmdv.append(old)
1414 self.update_ref(*cmdv)
1415
1416 def DeleteRef(self, name, old=None):
1417 if not old:
1418 old = self.rev_parse(name)
1419 self.update_ref('-d', name, old)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001420 self._project.bare_ref.deleted(name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001421
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001422 def rev_list(self, *args, **kw):
1423 if 'format' in kw:
1424 cmdv = ['log', '--pretty=format:%s' % kw['format']]
1425 else:
1426 cmdv = ['rev-list']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001427 cmdv.extend(args)
1428 p = GitCommand(self._project,
1429 cmdv,
1430 bare = self._bare,
1431 capture_stdout = True,
1432 capture_stderr = True)
1433 r = []
1434 for line in p.process.stdout:
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001435 if line[-1] == '\n':
1436 line = line[:-1]
1437 r.append(line)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001438 if p.Wait() != 0:
1439 raise GitError('%s rev-list %s: %s' % (
1440 self._project.name,
1441 str(args),
1442 p.stderr))
1443 return r
1444
1445 def __getattr__(self, name):
1446 name = name.replace('_', '-')
1447 def runner(*args):
1448 cmdv = [name]
1449 cmdv.extend(args)
1450 p = GitCommand(self._project,
1451 cmdv,
1452 bare = self._bare,
1453 capture_stdout = True,
1454 capture_stderr = True)
1455 if p.Wait() != 0:
1456 raise GitError('%s %s: %s' % (
1457 self._project.name,
1458 name,
1459 p.stderr))
1460 r = p.stdout
1461 if r.endswith('\n') and r.index('\n') == len(r) - 1:
1462 return r[:-1]
1463 return r
1464 return runner
1465
1466
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001467class _PriorSyncFailedError(Exception):
1468 def __str__(self):
1469 return 'prior sync failed; rebase still in progress'
1470
1471class _DirtyError(Exception):
1472 def __str__(self):
1473 return 'contains uncommitted changes'
1474
1475class _InfoMessage(object):
1476 def __init__(self, project, text):
1477 self.project = project
1478 self.text = text
1479
1480 def Print(self, syncbuf):
1481 syncbuf.out.info('%s/: %s', self.project.relpath, self.text)
1482 syncbuf.out.nl()
1483
1484class _Failure(object):
1485 def __init__(self, project, why):
1486 self.project = project
1487 self.why = why
1488
1489 def Print(self, syncbuf):
1490 syncbuf.out.fail('error: %s/: %s',
1491 self.project.relpath,
1492 str(self.why))
1493 syncbuf.out.nl()
1494
1495class _Later(object):
1496 def __init__(self, project, action):
1497 self.project = project
1498 self.action = action
1499
1500 def Run(self, syncbuf):
1501 out = syncbuf.out
1502 out.project('project %s/', self.project.relpath)
1503 out.nl()
1504 try:
1505 self.action()
1506 out.nl()
1507 return True
1508 except GitError, e:
1509 out.nl()
1510 return False
1511
1512class _SyncColoring(Coloring):
1513 def __init__(self, config):
1514 Coloring.__init__(self, config, 'reposync')
1515 self.project = self.printer('header', attr = 'bold')
1516 self.info = self.printer('info')
1517 self.fail = self.printer('fail', fg='red')
1518
1519class SyncBuffer(object):
1520 def __init__(self, config, detach_head=False):
1521 self._messages = []
1522 self._failures = []
1523 self._later_queue1 = []
1524 self._later_queue2 = []
1525
1526 self.out = _SyncColoring(config)
1527 self.out.redirect(sys.stderr)
1528
1529 self.detach_head = detach_head
1530 self.clean = True
1531
1532 def info(self, project, fmt, *args):
1533 self._messages.append(_InfoMessage(project, fmt % args))
1534
1535 def fail(self, project, err=None):
1536 self._failures.append(_Failure(project, err))
1537 self.clean = False
1538
1539 def later1(self, project, what):
1540 self._later_queue1.append(_Later(project, what))
1541
1542 def later2(self, project, what):
1543 self._later_queue2.append(_Later(project, what))
1544
1545 def Finish(self):
1546 self._PrintMessages()
1547 self._RunLater()
1548 self._PrintMessages()
1549 return self.clean
1550
1551 def _RunLater(self):
1552 for q in ['_later_queue1', '_later_queue2']:
1553 if not self._RunQueue(q):
1554 return
1555
1556 def _RunQueue(self, queue):
1557 for m in getattr(self, queue):
1558 if not m.Run(self):
1559 self.clean = False
1560 return False
1561 setattr(self, queue, [])
1562 return True
1563
1564 def _PrintMessages(self):
1565 for m in self._messages:
1566 m.Print(self)
1567 for m in self._failures:
1568 m.Print(self)
1569
1570 self._messages = []
1571 self._failures = []
1572
1573
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001574class MetaProject(Project):
1575 """A special project housed under .repo.
1576 """
1577 def __init__(self, manifest, name, gitdir, worktree):
1578 repodir = manifest.repodir
1579 Project.__init__(self,
1580 manifest = manifest,
1581 name = name,
1582 gitdir = gitdir,
1583 worktree = worktree,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001584 remote = RemoteSpec('origin'),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001585 relpath = '.repo/%s' % name,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001586 revisionExpr = 'refs/heads/master',
1587 revisionId = None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001588
1589 def PreSync(self):
1590 if self.Exists:
1591 cb = self.CurrentBranch
1592 if cb:
1593 base = self.GetBranch(cb).merge
1594 if base:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001595 self.revisionExpr = base
1596 self.revisionId = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001597
1598 @property
Shawn O. Pearcef6906872009-04-18 10:49:00 -07001599 def LastFetch(self):
1600 try:
1601 fh = os.path.join(self.gitdir, 'FETCH_HEAD')
1602 return os.path.getmtime(fh)
1603 except OSError:
1604 return 0
1605
1606 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001607 def HasChanges(self):
1608 """Has the remote received new commits not yet checked out?
1609 """
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001610 if not self.remote or not self.revisionExpr:
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001611 return False
1612
1613 all = self.bare_ref.all
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001614 revid = self.GetRevisionId(all)
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001615 head = self.work_git.GetHead()
1616 if head.startswith(R_HEADS):
1617 try:
1618 head = all[head]
1619 except KeyError:
1620 head = None
1621
1622 if revid == head:
1623 return False
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001624 elif self._revlist(not_rev(HEAD), revid):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001625 return True
1626 return False