blob: 125fb48c631acc711f51f4f791a72232fb797995 [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
Doug Anderson8ced8642011-01-10 14:16:30 -080057_project_hook_list = None
58def _ProjectHooks():
59 """List the hooks present in the 'hooks' directory.
60
61 These hooks are project hooks and are copied to the '.git/hooks' directory
62 of all subprojects.
63
64 This function caches the list of hooks (based on the contents of the
65 'repo/hooks' directory) on the first call.
66
67 Returns:
68 A list of absolute paths to all of the files in the hooks directory.
69 """
70 global _project_hook_list
71 if _project_hook_list is None:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080072 d = os.path.abspath(os.path.dirname(__file__))
73 d = os.path.join(d , 'hooks')
Doug Anderson8ced8642011-01-10 14:16:30 -080074 _project_hook_list = map(lambda x: os.path.join(d, x), os.listdir(d))
75 return _project_hook_list
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080076
77def relpath(dst, src):
78 src = os.path.dirname(src)
79 top = os.path.commonprefix([dst, src])
80 if top.endswith('/'):
81 top = top[:-1]
82 else:
83 top = os.path.dirname(top)
84
85 tmp = src
86 rel = ''
87 while top != tmp:
88 rel += '../'
89 tmp = os.path.dirname(tmp)
90 return rel + dst[len(top) + 1:]
91
92
Shawn O. Pearce632768b2008-10-23 11:58:52 -070093class DownloadedChange(object):
94 _commit_cache = None
95
96 def __init__(self, project, base, change_id, ps_id, commit):
97 self.project = project
98 self.base = base
99 self.change_id = change_id
100 self.ps_id = ps_id
101 self.commit = commit
102
103 @property
104 def commits(self):
105 if self._commit_cache is None:
106 self._commit_cache = self.project.bare_git.rev_list(
107 '--abbrev=8',
108 '--abbrev-commit',
109 '--pretty=oneline',
110 '--reverse',
111 '--date-order',
112 not_rev(self.base),
113 self.commit,
114 '--')
115 return self._commit_cache
116
117
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700118class ReviewableBranch(object):
119 _commit_cache = None
120
121 def __init__(self, project, branch, base):
122 self.project = project
123 self.branch = branch
124 self.base = base
125
126 @property
127 def name(self):
128 return self.branch.name
129
130 @property
131 def commits(self):
132 if self._commit_cache is None:
133 self._commit_cache = self.project.bare_git.rev_list(
134 '--abbrev=8',
135 '--abbrev-commit',
136 '--pretty=oneline',
137 '--reverse',
138 '--date-order',
139 not_rev(self.base),
140 R_HEADS + self.name,
141 '--')
142 return self._commit_cache
143
144 @property
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800145 def unabbrev_commits(self):
146 r = dict()
147 for commit in self.project.bare_git.rev_list(
148 not_rev(self.base),
149 R_HEADS + self.name,
150 '--'):
151 r[commit[0:8]] = commit
152 return r
153
154 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700155 def date(self):
156 return self.project.bare_git.log(
157 '--pretty=format:%cd',
158 '-n', '1',
159 R_HEADS + self.name,
160 '--')
161
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700162 def UploadForReview(self, people, auto_topic=False):
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800163 self.project.UploadForReview(self.name,
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700164 people,
165 auto_topic=auto_topic)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700166
Ficus Kirkpatrickbc7ef672009-05-04 12:45:11 -0700167 def GetPublishedRefs(self):
168 refs = {}
169 output = self.project.bare_git.ls_remote(
170 self.branch.remote.SshReviewUrl(self.project.UserEmail),
171 'refs/changes/*')
172 for line in output.split('\n'):
173 try:
174 (sha, ref) = line.split()
175 refs[sha] = ref
176 except ValueError:
177 pass
178
179 return refs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700180
181class StatusColoring(Coloring):
182 def __init__(self, config):
183 Coloring.__init__(self, config, 'status')
184 self.project = self.printer('header', attr = 'bold')
185 self.branch = self.printer('header', attr = 'bold')
186 self.nobranch = self.printer('nobranch', fg = 'red')
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700187 self.important = self.printer('important', fg = 'red')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700188
189 self.added = self.printer('added', fg = 'green')
190 self.changed = self.printer('changed', fg = 'red')
191 self.untracked = self.printer('untracked', fg = 'red')
192
193
194class DiffColoring(Coloring):
195 def __init__(self, config):
196 Coloring.__init__(self, config, 'diff')
197 self.project = self.printer('header', attr = 'bold')
198
199
200class _CopyFile:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800201 def __init__(self, src, dest, abssrc, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700202 self.src = src
203 self.dest = dest
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800204 self.abs_src = abssrc
205 self.abs_dest = absdest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700206
207 def _Copy(self):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800208 src = self.abs_src
209 dest = self.abs_dest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700210 # copy file if it does not exist or is out of date
211 if not os.path.exists(dest) or not filecmp.cmp(src, dest):
212 try:
213 # remove existing file first, since it might be read-only
214 if os.path.exists(dest):
215 os.remove(dest)
Matthew Buckett2daf6672009-07-11 09:43:47 -0400216 else:
217 dir = os.path.dirname(dest)
218 if not os.path.isdir(dir):
219 os.makedirs(dir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700220 shutil.copy(src, dest)
221 # make the file read-only
222 mode = os.stat(dest)[stat.ST_MODE]
223 mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
224 os.chmod(dest, mode)
225 except IOError:
Shawn O. Pearce48244782009-04-16 08:25:57 -0700226 _error('Cannot copy file %s to %s', src, dest)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700227
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700228class RemoteSpec(object):
229 def __init__(self,
230 name,
231 url = None,
232 review = None):
233 self.name = name
234 self.url = url
235 self.review = review
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700236
237class Project(object):
238 def __init__(self,
239 manifest,
240 name,
241 remote,
242 gitdir,
243 worktree,
244 relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700245 revisionExpr,
246 revisionId):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700247 self.manifest = manifest
248 self.name = name
249 self.remote = remote
Anthony Newnamdf14a702011-01-09 17:31:57 -0800250 self.gitdir = gitdir.replace('\\', '/')
Shawn O. Pearce0ce6ca92011-01-10 13:26:01 -0800251 if worktree:
252 self.worktree = worktree.replace('\\', '/')
253 else:
254 self.worktree = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700255 self.relpath = relpath
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700256 self.revisionExpr = revisionExpr
257
258 if revisionId is None \
259 and revisionExpr \
260 and IsId(revisionExpr):
261 self.revisionId = revisionExpr
262 else:
263 self.revisionId = revisionId
264
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700265 self.snapshots = {}
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700266 self.copyfiles = []
267 self.config = GitConfig.ForRepository(
268 gitdir = self.gitdir,
269 defaults = self.manifest.globalConfig)
270
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800271 if self.worktree:
272 self.work_git = self._GitGetByExec(self, bare=False)
273 else:
274 self.work_git = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700275 self.bare_git = self._GitGetByExec(self, bare=True)
Shawn O. Pearced237b692009-04-17 18:49:50 -0700276 self.bare_ref = GitRefs(gitdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700277
278 @property
279 def Exists(self):
280 return os.path.isdir(self.gitdir)
281
282 @property
283 def CurrentBranch(self):
284 """Obtain the name of the currently checked out branch.
285 The branch name omits the 'refs/heads/' prefix.
286 None is returned if the project is on a detached HEAD.
287 """
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700288 b = self.work_git.GetHead()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700289 if b.startswith(R_HEADS):
290 return b[len(R_HEADS):]
291 return None
292
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700293 def IsRebaseInProgress(self):
294 w = self.worktree
295 g = os.path.join(w, '.git')
296 return os.path.exists(os.path.join(g, 'rebase-apply')) \
297 or os.path.exists(os.path.join(g, 'rebase-merge')) \
298 or os.path.exists(os.path.join(w, '.dotest'))
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200299
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700300 def IsDirty(self, consider_untracked=True):
301 """Is the working directory modified in some way?
302 """
303 self.work_git.update_index('-q',
304 '--unmerged',
305 '--ignore-missing',
306 '--refresh')
307 if self.work_git.DiffZ('diff-index','-M','--cached',HEAD):
308 return True
309 if self.work_git.DiffZ('diff-files'):
310 return True
311 if consider_untracked and self.work_git.LsOthers():
312 return True
313 return False
314
315 _userident_name = None
316 _userident_email = None
317
318 @property
319 def UserName(self):
320 """Obtain the user's personal name.
321 """
322 if self._userident_name is None:
323 self._LoadUserIdentity()
324 return self._userident_name
325
326 @property
327 def UserEmail(self):
328 """Obtain the user's email address. This is very likely
329 to be their Gerrit login.
330 """
331 if self._userident_email is None:
332 self._LoadUserIdentity()
333 return self._userident_email
334
335 def _LoadUserIdentity(self):
336 u = self.bare_git.var('GIT_COMMITTER_IDENT')
337 m = re.compile("^(.*) <([^>]*)> ").match(u)
338 if m:
339 self._userident_name = m.group(1)
340 self._userident_email = m.group(2)
341 else:
342 self._userident_name = ''
343 self._userident_email = ''
344
345 def GetRemote(self, name):
346 """Get the configuration for a single remote.
347 """
348 return self.config.GetRemote(name)
349
350 def GetBranch(self, name):
351 """Get the configuration for a single branch.
352 """
353 return self.config.GetBranch(name)
354
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700355 def GetBranches(self):
356 """Get all existing local branches.
357 """
358 current = self.CurrentBranch
Shawn O. Pearced237b692009-04-17 18:49:50 -0700359 all = self._allrefs
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700360 heads = {}
361 pubd = {}
362
363 for name, id in all.iteritems():
364 if name.startswith(R_HEADS):
365 name = name[len(R_HEADS):]
366 b = self.GetBranch(name)
367 b.current = name == current
368 b.published = None
369 b.revision = id
370 heads[name] = b
371
372 for name, id in all.iteritems():
373 if name.startswith(R_PUB):
374 name = name[len(R_PUB):]
375 b = heads.get(name)
376 if b:
377 b.published = id
378
379 return heads
380
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700381
382## Status Display ##
383
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500384 def HasChanges(self):
385 """Returns true if there are uncommitted changes.
386 """
387 self.work_git.update_index('-q',
388 '--unmerged',
389 '--ignore-missing',
390 '--refresh')
391 if self.IsRebaseInProgress():
392 return True
393
394 if self.work_git.DiffZ('diff-index', '--cached', HEAD):
395 return True
396
397 if self.work_git.DiffZ('diff-files'):
398 return True
399
400 if self.work_git.LsOthers():
401 return True
402
403 return False
404
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700405 def PrintWorkTreeStatus(self):
406 """Prints the status of the repository to stdout.
407 """
408 if not os.path.isdir(self.worktree):
409 print ''
410 print 'project %s/' % self.relpath
411 print ' missing (run "repo sync")'
412 return
413
414 self.work_git.update_index('-q',
415 '--unmerged',
416 '--ignore-missing',
417 '--refresh')
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700418 rb = self.IsRebaseInProgress()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700419 di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD)
420 df = self.work_git.DiffZ('diff-files')
421 do = self.work_git.LsOthers()
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700422 if not rb and not di and not df and not do:
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700423 return 'CLEAN'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700424
425 out = StatusColoring(self.config)
426 out.project('project %-40s', self.relpath + '/')
427
428 branch = self.CurrentBranch
429 if branch is None:
430 out.nobranch('(*** NO BRANCH ***)')
431 else:
432 out.branch('branch %s', branch)
433 out.nl()
434
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700435 if rb:
436 out.important('prior sync failed; rebase still in progress')
437 out.nl()
438
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700439 paths = list()
440 paths.extend(di.keys())
441 paths.extend(df.keys())
442 paths.extend(do)
443
444 paths = list(set(paths))
445 paths.sort()
446
447 for p in paths:
448 try: i = di[p]
449 except KeyError: i = None
450
451 try: f = df[p]
452 except KeyError: f = None
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200453
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700454 if i: i_status = i.status.upper()
455 else: i_status = '-'
456
457 if f: f_status = f.status.lower()
458 else: f_status = '-'
459
460 if i and i.src_path:
Shawn O. Pearcefe086752009-03-03 13:49:48 -0800461 line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700462 i.src_path, p, i.level)
463 else:
464 line = ' %s%s\t%s' % (i_status, f_status, p)
465
466 if i and not f:
467 out.added('%s', line)
468 elif (i and f) or (not i and f):
469 out.changed('%s', line)
470 elif not i and not f:
471 out.untracked('%s', line)
472 else:
473 out.write('%s', line)
474 out.nl()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700475 return 'DIRTY'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700476
477 def PrintWorkTreeDiff(self):
478 """Prints the status of the repository to stdout.
479 """
480 out = DiffColoring(self.config)
481 cmd = ['diff']
482 if out.is_on:
483 cmd.append('--color')
484 cmd.append(HEAD)
485 cmd.append('--')
486 p = GitCommand(self,
487 cmd,
488 capture_stdout = True,
489 capture_stderr = True)
490 has_diff = False
491 for line in p.process.stdout:
492 if not has_diff:
493 out.nl()
494 out.project('project %s/' % self.relpath)
495 out.nl()
496 has_diff = True
497 print line[:-1]
498 p.Wait()
499
500
501## Publish / Upload ##
502
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700503 def WasPublished(self, branch, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700504 """Was the branch published (uploaded) for code review?
505 If so, returns the SHA-1 hash of the last published
506 state for the branch.
507 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700508 key = R_PUB + branch
509 if all is None:
510 try:
511 return self.bare_git.rev_parse(key)
512 except GitError:
513 return None
514 else:
515 try:
516 return all[key]
517 except KeyError:
518 return None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700519
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700520 def CleanPublishedCache(self, all=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700521 """Prunes any stale published refs.
522 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700523 if all is None:
524 all = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700525 heads = set()
526 canrm = {}
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700527 for name, id in all.iteritems():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700528 if name.startswith(R_HEADS):
529 heads.add(name)
530 elif name.startswith(R_PUB):
531 canrm[name] = id
532
533 for name, id in canrm.iteritems():
534 n = name[len(R_PUB):]
535 if R_HEADS + n not in heads:
536 self.bare_git.DeleteRef(name, id)
537
538 def GetUploadableBranches(self):
539 """List any branches which can be uploaded for review.
540 """
541 heads = {}
542 pubed = {}
543
544 for name, id in self._allrefs.iteritems():
545 if name.startswith(R_HEADS):
546 heads[name[len(R_HEADS):]] = id
547 elif name.startswith(R_PUB):
548 pubed[name[len(R_PUB):]] = id
549
550 ready = []
551 for branch, id in heads.iteritems():
552 if branch in pubed and pubed[branch] == id:
553 continue
554
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800555 rb = self.GetUploadableBranch(branch)
556 if rb:
557 ready.append(rb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700558 return ready
559
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800560 def GetUploadableBranch(self, branch_name):
561 """Get a single uploadable branch, or None.
562 """
563 branch = self.GetBranch(branch_name)
564 base = branch.LocalMerge
565 if branch.LocalMerge:
566 rb = ReviewableBranch(self, branch, base)
567 if rb.commits:
568 return rb
569 return None
570
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700571 def UploadForReview(self, branch=None,
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700572 people=([],[]),
573 auto_topic=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700574 """Uploads the named branch for code review.
575 """
576 if branch is None:
577 branch = self.CurrentBranch
578 if branch is None:
579 raise GitError('not currently on a branch')
580
581 branch = self.GetBranch(branch)
582 if not branch.LocalMerge:
583 raise GitError('branch %s does not track a remote' % branch.name)
584 if not branch.remote.review:
585 raise GitError('remote %s has no review url' % branch.remote.name)
586
587 dest_branch = branch.merge
588 if not dest_branch.startswith(R_HEADS):
589 dest_branch = R_HEADS + dest_branch
590
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800591 if not branch.remote.projectname:
592 branch.remote.projectname = self.name
593 branch.remote.Save()
594
Shawn O. Pearce370e3fa2009-01-26 10:55:39 -0800595 if branch.remote.ReviewProtocol == 'ssh':
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800596 if dest_branch.startswith(R_HEADS):
597 dest_branch = dest_branch[len(R_HEADS):]
598
599 rp = ['gerrit receive-pack']
600 for e in people[0]:
601 rp.append('--reviewer=%s' % sq(e))
602 for e in people[1]:
603 rp.append('--cc=%s' % sq(e))
604
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700605 ref_spec = '%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch)
606 if auto_topic:
607 ref_spec = ref_spec + '/' + branch.name
608
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800609 cmd = ['push']
610 cmd.append('--receive-pack=%s' % " ".join(rp))
611 cmd.append(branch.remote.SshReviewUrl(self.UserEmail))
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700612 cmd.append(ref_spec)
613
Shawn O. Pearceb54a3922009-01-05 16:18:58 -0800614 if GitCommand(self, cmd, bare = True).Wait() != 0:
615 raise UploadError('Upload failed')
616
617 else:
618 raise UploadError('Unsupported protocol %s' \
619 % branch.remote.review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700620
621 msg = "posted to %s for %s" % (branch.remote.review, dest_branch)
622 self.bare_git.UpdateRef(R_PUB + branch.name,
623 R_HEADS + branch.name,
624 message = msg)
625
626
627## Sync ##
628
Shawn O. Pearce16614f82010-10-29 12:05:43 -0700629 def Sync_NetworkHalf(self, quiet=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700630 """Perform only the network IO portion of the sync process.
631 Local working directory/branch state is not affected.
632 """
Shawn O. Pearce88443382010-10-08 10:02:09 +0200633 is_new = not self.Exists
634 if is_new:
Shawn O. Pearce16614f82010-10-29 12:05:43 -0700635 if not quiet:
636 print >>sys.stderr
637 print >>sys.stderr, 'Initializing project %s ...' % self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700638 self._InitGitDir()
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800639
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700640 self._InitRemote()
Shawn O. Pearce16614f82010-10-29 12:05:43 -0700641 if not self._RemoteFetch(initial=is_new, quiet=quiet):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700642 return False
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800643
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200644 #Check that the requested ref was found after fetch
645 #
646 try:
647 self.GetRevisionId()
648 except ManifestInvalidRevisionError:
649 # if the ref is a tag. We can try fetching
650 # the tag manually as a last resort
651 #
652 rev = self.revisionExpr
653 if rev.startswith(R_TAGS):
Shawn O. Pearce16614f82010-10-29 12:05:43 -0700654 self._RemoteFetch(None, rev[len(R_TAGS):], quiet=quiet)
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200655
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800656 if self.worktree:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800657 self._InitMRef()
658 else:
659 self._InitMirrorHead()
660 try:
661 os.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
662 except OSError:
663 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700664 return True
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800665
666 def PostRepoUpgrade(self):
667 self._InitHooks()
668
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700669 def _CopyFiles(self):
670 for file in self.copyfiles:
671 file._Copy()
672
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700673 def GetRevisionId(self, all=None):
674 if self.revisionId:
675 return self.revisionId
676
677 rem = self.GetRemote(self.remote.name)
678 rev = rem.ToLocal(self.revisionExpr)
679
680 if all is not None and rev in all:
681 return all[rev]
682
683 try:
684 return self.bare_git.rev_parse('--verify', '%s^0' % rev)
685 except GitError:
686 raise ManifestInvalidRevisionError(
687 'revision %s in %s not found' % (self.revisionExpr,
688 self.name))
689
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700690 def Sync_LocalHalf(self, syncbuf):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700691 """Perform only the local IO portion of the sync process.
692 Network access is not required.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700693 """
694 self._InitWorkTree()
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700695 all = self.bare_ref.all
696 self.CleanPublishedCache(all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700697
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700698 revid = self.GetRevisionId(all)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700699 head = self.work_git.GetHead()
700 if head.startswith(R_HEADS):
701 branch = head[len(R_HEADS):]
702 try:
703 head = all[head]
704 except KeyError:
705 head = None
706 else:
707 branch = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700708
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700709 if branch is None or syncbuf.detach_head:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700710 # Currently on a detached HEAD. The user is assumed to
711 # not have any local modifications worth worrying about.
712 #
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700713 if self.IsRebaseInProgress():
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700714 syncbuf.fail(self, _PriorSyncFailedError())
715 return
716
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700717 if head == revid:
718 # No changes; don't do anything further.
719 #
720 return
721
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700722 lost = self._revlist(not_rev(revid), HEAD)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700723 if lost:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700724 syncbuf.info(self, "discarding %d commits", len(lost))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700725 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700726 self._Checkout(revid, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700727 except GitError, e:
728 syncbuf.fail(self, e)
729 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700730 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700731 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700732
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700733 if head == revid:
734 # No changes; don't do anything further.
735 #
736 return
737
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700738 branch = self.GetBranch(branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700739
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700740 if not branch.LocalMerge:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700741 # The current branch has no tracking configuration.
742 # Jump off it to a deatched HEAD.
743 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700744 syncbuf.info(self,
745 "leaving %s; does not track upstream",
746 branch.name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700747 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700748 self._Checkout(revid, quiet=True)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700749 except GitError, e:
750 syncbuf.fail(self, e)
751 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700752 self._CopyFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700753 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700754
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700755 upstream_gain = self._revlist(not_rev(HEAD), revid)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700756 pub = self.WasPublished(branch.name, all)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700757 if pub:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700758 not_merged = self._revlist(not_rev(revid), pub)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700759 if not_merged:
760 if upstream_gain:
761 # The user has published this branch and some of those
762 # commits are not yet merged upstream. We do not want
763 # to rewrite the published commits so we punt.
764 #
Daniel Sandler4c50dee2010-03-02 15:38:03 -0500765 syncbuf.fail(self,
766 "branch %s is published (but not merged) and is now %d commits behind"
767 % (branch.name, len(upstream_gain)))
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700768 return
Shawn O. Pearce05f66b62009-04-21 08:26:32 -0700769 elif pub == head:
770 # All published commits are merged, and thus we are a
771 # strict subset. We can fast-forward safely.
Shawn O. Pearcea54c5272008-10-30 11:03:00 -0700772 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700773 def _doff():
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700774 self._FastForward(revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700775 self._CopyFiles()
776 syncbuf.later1(self, _doff)
777 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700778
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700779 # Examine the local commits not in the remote. Find the
780 # last one attributed to this user, if any.
781 #
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700782 local_changes = self._revlist(not_rev(revid), HEAD, format='%H %ce')
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700783 last_mine = None
784 cnt_mine = 0
785 for commit in local_changes:
Shawn O. Pearceaa4982e2009-12-30 18:38:27 -0800786 commit_id, committer_email = commit.split(' ', 1)
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700787 if committer_email == self.UserEmail:
788 last_mine = commit_id
789 cnt_mine += 1
790
Shawn O. Pearceda88ff42009-06-03 11:09:12 -0700791 if not upstream_gain and cnt_mine == len(local_changes):
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700792 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700793
794 if self.IsDirty(consider_untracked=False):
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700795 syncbuf.fail(self, _DirtyError())
796 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700797
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700798 # If the upstream switched on us, warn the user.
799 #
800 if branch.merge != self.revisionExpr:
801 if branch.merge and self.revisionExpr:
802 syncbuf.info(self,
803 'manifest switched %s...%s',
804 branch.merge,
805 self.revisionExpr)
806 elif branch.merge:
807 syncbuf.info(self,
808 'manifest no longer tracks %s',
809 branch.merge)
810
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700811 if cnt_mine < len(local_changes):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700812 # Upstream rebased. Not everything in HEAD
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700813 # was created by this user.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700814 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700815 syncbuf.info(self,
816 "discarding %d commits removed from upstream",
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700817 len(local_changes) - cnt_mine)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700818
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700819 branch.remote = self.GetRemote(self.remote.name)
820 branch.merge = self.revisionExpr
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700821 branch.Save()
822
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700823 if cnt_mine > 0:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700824 def _dorebase():
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700825 self._Rebase(upstream = '%s^1' % last_mine, onto = revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700826 self._CopyFiles()
827 syncbuf.later2(self, _dorebase)
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -0700828 elif local_changes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700829 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700830 self._ResetHard(revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700831 self._CopyFiles()
832 except GitError, e:
833 syncbuf.fail(self, e)
834 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700835 else:
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700836 def _doff():
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700837 self._FastForward(revid)
Shawn O. Pearce350cde42009-04-16 11:21:18 -0700838 self._CopyFiles()
839 syncbuf.later1(self, _doff)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700840
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800841 def AddCopyFile(self, src, dest, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700842 # dest should already be an absolute path, but src is project relative
843 # make src an absolute path
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800844 abssrc = os.path.join(self.worktree, src)
845 self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700846
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700847 def DownloadPatchSet(self, change_id, patch_id):
848 """Download a single patch set of a single change to FETCH_HEAD.
849 """
850 remote = self.GetRemote(self.remote.name)
851
852 cmd = ['fetch', remote.name]
853 cmd.append('refs/changes/%2.2d/%d/%d' \
854 % (change_id % 100, change_id, patch_id))
855 cmd.extend(map(lambda x: str(x), remote.fetch))
856 if GitCommand(self, cmd, bare=True).Wait() != 0:
857 return None
858 return DownloadedChange(self,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700859 self.GetRevisionId(),
Shawn O. Pearce632768b2008-10-23 11:58:52 -0700860 change_id,
861 patch_id,
862 self.bare_git.rev_parse('FETCH_HEAD'))
863
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700864
865## Branch Management ##
866
867 def StartBranch(self, name):
868 """Create a new branch off the manifest's revision.
869 """
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700870 head = self.work_git.GetHead()
871 if head == (R_HEADS + name):
872 return True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700873
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700874 all = self.bare_ref.all
875 if (R_HEADS + name) in all:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700876 return GitCommand(self,
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700877 ['checkout', name, '--'],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700878 capture_stdout = True,
879 capture_stderr = True).Wait() == 0
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700880
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700881 branch = self.GetBranch(name)
882 branch.remote = self.GetRemote(self.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700883 branch.merge = self.revisionExpr
884 revid = self.GetRevisionId(all)
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700885
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700886 if head.startswith(R_HEADS):
887 try:
888 head = all[head]
889 except KeyError:
890 head = None
891
892 if revid and head and revid == head:
893 ref = os.path.join(self.gitdir, R_HEADS + name)
894 try:
895 os.makedirs(os.path.dirname(ref))
896 except OSError:
897 pass
898 _lwrite(ref, '%s\n' % revid)
899 _lwrite(os.path.join(self.worktree, '.git', HEAD),
900 'ref: %s%s\n' % (R_HEADS, name))
901 branch.Save()
902 return True
903
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700904 if GitCommand(self,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700905 ['checkout', '-b', branch.name, revid],
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700906 capture_stdout = True,
907 capture_stderr = True).Wait() == 0:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -0700908 branch.Save()
909 return True
910 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700911
Wink Saville02d79452009-04-10 13:01:24 -0700912 def CheckoutBranch(self, name):
913 """Checkout a local topic branch.
914 """
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700915 rev = R_HEADS + name
916 head = self.work_git.GetHead()
917 if head == rev:
918 # Already on the branch
919 #
920 return True
Wink Saville02d79452009-04-10 13:01:24 -0700921
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700922 all = self.bare_ref.all
Wink Saville02d79452009-04-10 13:01:24 -0700923 try:
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700924 revid = all[rev]
925 except KeyError:
926 # Branch does not exist in this project
927 #
928 return False
929
930 if head.startswith(R_HEADS):
931 try:
932 head = all[head]
933 except KeyError:
934 head = None
935
936 if head == revid:
937 # Same revision; just update HEAD to point to the new
938 # target branch, but otherwise take no other action.
939 #
940 _lwrite(os.path.join(self.worktree, '.git', HEAD),
941 'ref: %s%s\n' % (R_HEADS, name))
942 return True
Wink Saville02d79452009-04-10 13:01:24 -0700943
Shawn O. Pearce89e717d2009-04-18 15:04:41 -0700944 return GitCommand(self,
945 ['checkout', name, '--'],
946 capture_stdout = True,
947 capture_stderr = True).Wait() == 0
Wink Saville02d79452009-04-10 13:01:24 -0700948
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800949 def AbandonBranch(self, name):
950 """Destroy a local topic branch.
951 """
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700952 rev = R_HEADS + name
953 all = self.bare_ref.all
954 if rev not in all:
955 # Doesn't exist; assume already abandoned.
956 #
957 return True
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800958
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700959 head = self.work_git.GetHead()
960 if head == rev:
961 # We can't destroy the branch while we are sitting
962 # on it. Switch to a detached HEAD.
963 #
964 head = all[head]
965
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700966 revid = self.GetRevisionId(all)
967 if head == revid:
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700968 _lwrite(os.path.join(self.worktree, '.git', HEAD),
969 '%s\n' % revid)
970 else:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700971 self._Checkout(revid, quiet=True)
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800972
Shawn O. Pearce552ac892009-04-18 15:15:24 -0700973 return GitCommand(self,
974 ['branch', '-D', name],
975 capture_stdout = True,
976 capture_stderr = True).Wait() == 0
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -0800977
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700978 def PruneHeads(self):
979 """Prune any topic branches already merged into upstream.
980 """
981 cb = self.CurrentBranch
982 kill = []
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -0800983 left = self._allrefs
984 for name in left.keys():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700985 if name.startswith(R_HEADS):
986 name = name[len(R_HEADS):]
987 if cb is None or name != cb:
988 kill.append(name)
989
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700990 rev = self.GetRevisionId(left)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700991 if cb is not None \
992 and not self._revlist(HEAD + '...' + rev) \
993 and not self.IsDirty(consider_untracked = False):
994 self.work_git.DetachHead(HEAD)
995 kill.append(cb)
996
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700997 if kill:
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700998 old = self.bare_git.GetHead()
999 if old is None:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001000 old = 'refs/heads/please_never_use_this_as_a_branch_name'
1001
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001002 try:
1003 self.bare_git.DetachHead(rev)
1004
1005 b = ['branch', '-d']
1006 b.extend(kill)
1007 b = GitCommand(self, b, bare=True,
1008 capture_stdout=True,
1009 capture_stderr=True)
1010 b.Wait()
1011 finally:
1012 self.bare_git.SetHead(old)
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -08001013 left = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001014
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -08001015 for branch in kill:
1016 if (R_HEADS + branch) not in left:
1017 self.CleanPublishedCache()
1018 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001019
1020 if cb and cb not in kill:
1021 kill.append(cb)
Shawn O. Pearce7c6c64d2009-03-02 12:38:13 -08001022 kill.sort()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001023
1024 kept = []
1025 for branch in kill:
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -08001026 if (R_HEADS + branch) in left:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001027 branch = self.GetBranch(branch)
1028 base = branch.LocalMerge
1029 if not base:
1030 base = rev
1031 kept.append(ReviewableBranch(self, branch, base))
1032 return kept
1033
1034
1035## Direct Git Commands ##
1036
Shawn O. Pearce16614f82010-10-29 12:05:43 -07001037 def _RemoteFetch(self, name=None, tag=None,
1038 initial=False,
1039 quiet=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001040 if not name:
1041 name = self.remote.name
Shawn O. Pearcefb231612009-04-10 18:53:46 -07001042
1043 ssh_proxy = False
1044 if self.GetRemote(name).PreConnectFetch():
1045 ssh_proxy = True
1046
Shawn O. Pearce88443382010-10-08 10:02:09 +02001047 if initial:
1048 alt = os.path.join(self.gitdir, 'objects/info/alternates')
1049 try:
1050 fd = open(alt, 'rb')
1051 try:
1052 ref_dir = fd.readline()
1053 if ref_dir and ref_dir.endswith('\n'):
1054 ref_dir = ref_dir[:-1]
1055 finally:
1056 fd.close()
1057 except IOError, e:
1058 ref_dir = None
1059
1060 if ref_dir and 'objects' == os.path.basename(ref_dir):
1061 ref_dir = os.path.dirname(ref_dir)
1062 packed_refs = os.path.join(self.gitdir, 'packed-refs')
1063 remote = self.GetRemote(name)
1064
1065 all = self.bare_ref.all
1066 ids = set(all.values())
1067 tmp = set()
1068
1069 for r, id in GitRefs(ref_dir).all.iteritems():
1070 if r not in all:
1071 if r.startswith(R_TAGS) or remote.WritesTo(r):
1072 all[r] = id
1073 ids.add(id)
1074 continue
1075
1076 if id in ids:
1077 continue
1078
1079 r = 'refs/_alt/%s' % id
1080 all[r] = id
1081 ids.add(id)
1082 tmp.add(r)
1083
1084 ref_names = list(all.keys())
1085 ref_names.sort()
1086
1087 tmp_packed = ''
1088 old_packed = ''
1089
1090 for r in ref_names:
1091 line = '%s %s\n' % (all[r], r)
1092 tmp_packed += line
1093 if r not in tmp:
1094 old_packed += line
1095
1096 _lwrite(packed_refs, tmp_packed)
1097
1098 else:
1099 ref_dir = None
1100
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001101 cmd = ['fetch']
Shawn O. Pearce16614f82010-10-29 12:05:43 -07001102 if quiet:
1103 cmd.append('--quiet')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001104 if not self.worktree:
1105 cmd.append('--update-head-ok')
1106 cmd.append(name)
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +02001107 if tag is not None:
1108 cmd.append('tag')
1109 cmd.append(tag)
Shawn O. Pearce88443382010-10-08 10:02:09 +02001110
1111 ok = GitCommand(self,
1112 cmd,
1113 bare = True,
1114 ssh_proxy = ssh_proxy).Wait() == 0
1115
1116 if initial:
1117 if ref_dir:
1118 if old_packed != '':
1119 _lwrite(packed_refs, old_packed)
1120 else:
1121 os.remove(packed_refs)
1122 self.bare_git.pack_refs('--all', '--prune')
1123
1124 return ok
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001125
1126 def _Checkout(self, rev, quiet=False):
1127 cmd = ['checkout']
1128 if quiet:
1129 cmd.append('-q')
1130 cmd.append(rev)
1131 cmd.append('--')
1132 if GitCommand(self, cmd).Wait() != 0:
1133 if self._allrefs:
1134 raise GitError('%s checkout %s ' % (self.name, rev))
1135
1136 def _ResetHard(self, rev, quiet=True):
1137 cmd = ['reset', '--hard']
1138 if quiet:
1139 cmd.append('-q')
1140 cmd.append(rev)
1141 if GitCommand(self, cmd).Wait() != 0:
1142 raise GitError('%s reset --hard %s ' % (self.name, rev))
1143
1144 def _Rebase(self, upstream, onto = None):
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07001145 cmd = ['rebase']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001146 if onto is not None:
1147 cmd.extend(['--onto', onto])
1148 cmd.append(upstream)
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07001149 if GitCommand(self, cmd).Wait() != 0:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001150 raise GitError('%s rebase %s ' % (self.name, upstream))
1151
1152 def _FastForward(self, head):
1153 cmd = ['merge', head]
1154 if GitCommand(self, cmd).Wait() != 0:
1155 raise GitError('%s merge %s ' % (self.name, head))
1156
1157 def _InitGitDir(self):
1158 if not os.path.exists(self.gitdir):
1159 os.makedirs(self.gitdir)
1160 self.bare_git.init()
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -08001161
Shawn O. Pearce88443382010-10-08 10:02:09 +02001162 mp = self.manifest.manifestProject
1163 ref_dir = mp.config.GetString('repo.reference')
1164
1165 if ref_dir:
1166 mirror_git = os.path.join(ref_dir, self.name + '.git')
1167 repo_git = os.path.join(ref_dir, '.repo', 'projects',
1168 self.relpath + '.git')
1169
1170 if os.path.exists(mirror_git):
1171 ref_dir = mirror_git
1172
1173 elif os.path.exists(repo_git):
1174 ref_dir = repo_git
1175
1176 else:
1177 ref_dir = None
1178
1179 if ref_dir:
1180 _lwrite(os.path.join(self.gitdir, 'objects/info/alternates'),
1181 os.path.join(ref_dir, 'objects') + '\n')
1182
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -08001183 if self.manifest.IsMirror:
1184 self.config.SetString('core.bare', 'true')
1185 else:
1186 self.config.SetString('core.bare', None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001187
1188 hooks = self._gitdir_path('hooks')
Shawn O. Pearcede646812008-10-29 14:38:12 -07001189 try:
1190 to_rm = os.listdir(hooks)
1191 except OSError:
1192 to_rm = []
1193 for old_hook in to_rm:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001194 os.remove(os.path.join(hooks, old_hook))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001195 self._InitHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001196
1197 m = self.manifest.manifestProject.config
1198 for key in ['user.name', 'user.email']:
1199 if m.Has(key, include_defaults = False):
1200 self.config.SetString(key, m.GetString(key))
1201
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001202 def _InitHooks(self):
1203 hooks = self._gitdir_path('hooks')
1204 if not os.path.exists(hooks):
1205 os.makedirs(hooks)
Doug Anderson8ced8642011-01-10 14:16:30 -08001206 for stock_hook in _ProjectHooks():
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001207 name = os.path.basename(stock_hook)
1208
Doug Anderson2536f802011-01-10 12:38:37 -08001209 if name in ('commit-msg',) and not self.remote.review:
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001210 # Don't install a Gerrit Code Review hook if this
1211 # project does not appear to use it for reviews.
1212 #
1213 continue
1214
1215 dst = os.path.join(hooks, name)
1216 if os.path.islink(dst):
1217 continue
1218 if os.path.exists(dst):
1219 if filecmp.cmp(stock_hook, dst, shallow=False):
1220 os.remove(dst)
1221 else:
1222 _error("%s: Not replacing %s hook", self.relpath, name)
1223 continue
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001224 try:
1225 os.symlink(relpath(stock_hook, dst), dst)
1226 except OSError, e:
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001227 if e.errno == errno.EPERM:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001228 raise GitError('filesystem must support symlinks')
1229 else:
1230 raise
1231
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001232 def _InitRemote(self):
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001233 if self.remote.url:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001234 remote = self.GetRemote(self.remote.name)
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001235 remote.url = self.remote.url
1236 remote.review = self.remote.review
1237 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001238
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001239 if self.worktree:
1240 remote.ResetFetch(mirror=False)
1241 else:
1242 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001243 remote.Save()
1244
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001245 def _InitMRef(self):
1246 if self.manifest.branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001247 self._InitAnyMRef(R_M + self.manifest.branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001248
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001249 def _InitMirrorHead(self):
Shawn O. Pearcefe200ee2009-06-01 15:28:21 -07001250 self._InitAnyMRef(HEAD)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001251
1252 def _InitAnyMRef(self, ref):
1253 cur = self.bare_ref.symref(ref)
1254
1255 if self.revisionId:
1256 if cur != '' or self.bare_ref.get(ref) != self.revisionId:
1257 msg = 'manifest set to %s' % self.revisionId
1258 dst = self.revisionId + '^0'
1259 self.bare_git.UpdateRef(ref, dst, message = msg, detach = True)
1260 else:
1261 remote = self.GetRemote(self.remote.name)
1262 dst = remote.ToLocal(self.revisionExpr)
1263 if cur != dst:
1264 msg = 'manifest set to %s' % self.revisionExpr
1265 self.bare_git.symbolic_ref('-m', msg, ref, dst)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001266
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001267 def _InitWorkTree(self):
1268 dotgit = os.path.join(self.worktree, '.git')
1269 if not os.path.exists(dotgit):
1270 os.makedirs(dotgit)
1271
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001272 for name in ['config',
1273 'description',
1274 'hooks',
1275 'info',
1276 'logs',
1277 'objects',
1278 'packed-refs',
1279 'refs',
1280 'rr-cache',
1281 'svn']:
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001282 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001283 src = os.path.join(self.gitdir, name)
1284 dst = os.path.join(dotgit, name)
Nico Sallembiend63060f2010-01-20 10:27:50 -08001285 if os.path.islink(dst) or not os.path.exists(dst):
1286 os.symlink(relpath(src, dst), dst)
1287 else:
1288 raise GitError('cannot overwrite a local work tree')
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -08001289 except OSError, e:
1290 if e.errno == errno.EPERM:
1291 raise GitError('filesystem must support symlinks')
1292 else:
1293 raise
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001294
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001295 _lwrite(os.path.join(dotgit, HEAD), '%s\n' % self.GetRevisionId())
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001296
1297 cmd = ['read-tree', '--reset', '-u']
1298 cmd.append('-v')
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001299 cmd.append(HEAD)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001300 if GitCommand(self, cmd).Wait() != 0:
1301 raise GitError("cannot initialize work tree")
Shawn O. Pearce93609662009-04-21 10:50:33 -07001302 self._CopyFiles()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001303
1304 def _gitdir_path(self, path):
1305 return os.path.join(self.gitdir, path)
1306
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001307 def _revlist(self, *args, **kw):
1308 a = []
1309 a.extend(args)
1310 a.append('--')
1311 return self.work_git.rev_list(*a, **kw)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001312
1313 @property
1314 def _allrefs(self):
Shawn O. Pearced237b692009-04-17 18:49:50 -07001315 return self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001316
1317 class _GitGetByExec(object):
1318 def __init__(self, project, bare):
1319 self._project = project
1320 self._bare = bare
1321
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001322 def LsOthers(self):
1323 p = GitCommand(self._project,
1324 ['ls-files',
1325 '-z',
1326 '--others',
1327 '--exclude-standard'],
1328 bare = False,
1329 capture_stdout = True,
1330 capture_stderr = True)
1331 if p.Wait() == 0:
1332 out = p.stdout
1333 if out:
1334 return out[:-1].split("\0")
1335 return []
1336
1337 def DiffZ(self, name, *args):
1338 cmd = [name]
1339 cmd.append('-z')
1340 cmd.extend(args)
1341 p = GitCommand(self._project,
1342 cmd,
1343 bare = False,
1344 capture_stdout = True,
1345 capture_stderr = True)
1346 try:
1347 out = p.process.stdout.read()
1348 r = {}
1349 if out:
1350 out = iter(out[:-1].split('\0'))
1351 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07001352 try:
1353 info = out.next()
1354 path = out.next()
1355 except StopIteration:
1356 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001357
1358 class _Info(object):
1359 def __init__(self, path, omode, nmode, oid, nid, state):
1360 self.path = path
1361 self.src_path = None
1362 self.old_mode = omode
1363 self.new_mode = nmode
1364 self.old_id = oid
1365 self.new_id = nid
1366
1367 if len(state) == 1:
1368 self.status = state
1369 self.level = None
1370 else:
1371 self.status = state[:1]
1372 self.level = state[1:]
1373 while self.level.startswith('0'):
1374 self.level = self.level[1:]
1375
1376 info = info[1:].split(' ')
1377 info =_Info(path, *info)
1378 if info.status in ('R', 'C'):
1379 info.src_path = info.path
1380 info.path = out.next()
1381 r[info.path] = info
1382 return r
1383 finally:
1384 p.Wait()
1385
1386 def GetHead(self):
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001387 if self._bare:
1388 path = os.path.join(self._project.gitdir, HEAD)
1389 else:
1390 path = os.path.join(self._project.worktree, '.git', HEAD)
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -07001391 fd = open(path, 'rb')
1392 try:
1393 line = fd.read()
1394 finally:
1395 fd.close()
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001396 if line.startswith('ref: '):
1397 return line[5:-1]
1398 return line[:-1]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001399
1400 def SetHead(self, ref, message=None):
1401 cmdv = []
1402 if message is not None:
1403 cmdv.extend(['-m', message])
1404 cmdv.append(HEAD)
1405 cmdv.append(ref)
1406 self.symbolic_ref(*cmdv)
1407
1408 def DetachHead(self, new, message=None):
1409 cmdv = ['--no-deref']
1410 if message is not None:
1411 cmdv.extend(['-m', message])
1412 cmdv.append(HEAD)
1413 cmdv.append(new)
1414 self.update_ref(*cmdv)
1415
1416 def UpdateRef(self, name, new, old=None,
1417 message=None,
1418 detach=False):
1419 cmdv = []
1420 if message is not None:
1421 cmdv.extend(['-m', message])
1422 if detach:
1423 cmdv.append('--no-deref')
1424 cmdv.append(name)
1425 cmdv.append(new)
1426 if old is not None:
1427 cmdv.append(old)
1428 self.update_ref(*cmdv)
1429
1430 def DeleteRef(self, name, old=None):
1431 if not old:
1432 old = self.rev_parse(name)
1433 self.update_ref('-d', name, old)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001434 self._project.bare_ref.deleted(name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001435
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001436 def rev_list(self, *args, **kw):
1437 if 'format' in kw:
1438 cmdv = ['log', '--pretty=format:%s' % kw['format']]
1439 else:
1440 cmdv = ['rev-list']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001441 cmdv.extend(args)
1442 p = GitCommand(self._project,
1443 cmdv,
1444 bare = self._bare,
1445 capture_stdout = True,
1446 capture_stderr = True)
1447 r = []
1448 for line in p.process.stdout:
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001449 if line[-1] == '\n':
1450 line = line[:-1]
1451 r.append(line)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001452 if p.Wait() != 0:
1453 raise GitError('%s rev-list %s: %s' % (
1454 self._project.name,
1455 str(args),
1456 p.stderr))
1457 return r
1458
1459 def __getattr__(self, name):
1460 name = name.replace('_', '-')
1461 def runner(*args):
1462 cmdv = [name]
1463 cmdv.extend(args)
1464 p = GitCommand(self._project,
1465 cmdv,
1466 bare = self._bare,
1467 capture_stdout = True,
1468 capture_stderr = True)
1469 if p.Wait() != 0:
1470 raise GitError('%s %s: %s' % (
1471 self._project.name,
1472 name,
1473 p.stderr))
1474 r = p.stdout
1475 if r.endswith('\n') and r.index('\n') == len(r) - 1:
1476 return r[:-1]
1477 return r
1478 return runner
1479
1480
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001481class _PriorSyncFailedError(Exception):
1482 def __str__(self):
1483 return 'prior sync failed; rebase still in progress'
1484
1485class _DirtyError(Exception):
1486 def __str__(self):
1487 return 'contains uncommitted changes'
1488
1489class _InfoMessage(object):
1490 def __init__(self, project, text):
1491 self.project = project
1492 self.text = text
1493
1494 def Print(self, syncbuf):
1495 syncbuf.out.info('%s/: %s', self.project.relpath, self.text)
1496 syncbuf.out.nl()
1497
1498class _Failure(object):
1499 def __init__(self, project, why):
1500 self.project = project
1501 self.why = why
1502
1503 def Print(self, syncbuf):
1504 syncbuf.out.fail('error: %s/: %s',
1505 self.project.relpath,
1506 str(self.why))
1507 syncbuf.out.nl()
1508
1509class _Later(object):
1510 def __init__(self, project, action):
1511 self.project = project
1512 self.action = action
1513
1514 def Run(self, syncbuf):
1515 out = syncbuf.out
1516 out.project('project %s/', self.project.relpath)
1517 out.nl()
1518 try:
1519 self.action()
1520 out.nl()
1521 return True
1522 except GitError, e:
1523 out.nl()
1524 return False
1525
1526class _SyncColoring(Coloring):
1527 def __init__(self, config):
1528 Coloring.__init__(self, config, 'reposync')
1529 self.project = self.printer('header', attr = 'bold')
1530 self.info = self.printer('info')
1531 self.fail = self.printer('fail', fg='red')
1532
1533class SyncBuffer(object):
1534 def __init__(self, config, detach_head=False):
1535 self._messages = []
1536 self._failures = []
1537 self._later_queue1 = []
1538 self._later_queue2 = []
1539
1540 self.out = _SyncColoring(config)
1541 self.out.redirect(sys.stderr)
1542
1543 self.detach_head = detach_head
1544 self.clean = True
1545
1546 def info(self, project, fmt, *args):
1547 self._messages.append(_InfoMessage(project, fmt % args))
1548
1549 def fail(self, project, err=None):
1550 self._failures.append(_Failure(project, err))
1551 self.clean = False
1552
1553 def later1(self, project, what):
1554 self._later_queue1.append(_Later(project, what))
1555
1556 def later2(self, project, what):
1557 self._later_queue2.append(_Later(project, what))
1558
1559 def Finish(self):
1560 self._PrintMessages()
1561 self._RunLater()
1562 self._PrintMessages()
1563 return self.clean
1564
1565 def _RunLater(self):
1566 for q in ['_later_queue1', '_later_queue2']:
1567 if not self._RunQueue(q):
1568 return
1569
1570 def _RunQueue(self, queue):
1571 for m in getattr(self, queue):
1572 if not m.Run(self):
1573 self.clean = False
1574 return False
1575 setattr(self, queue, [])
1576 return True
1577
1578 def _PrintMessages(self):
1579 for m in self._messages:
1580 m.Print(self)
1581 for m in self._failures:
1582 m.Print(self)
1583
1584 self._messages = []
1585 self._failures = []
1586
1587
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001588class MetaProject(Project):
1589 """A special project housed under .repo.
1590 """
1591 def __init__(self, manifest, name, gitdir, worktree):
1592 repodir = manifest.repodir
1593 Project.__init__(self,
1594 manifest = manifest,
1595 name = name,
1596 gitdir = gitdir,
1597 worktree = worktree,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07001598 remote = RemoteSpec('origin'),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001599 relpath = '.repo/%s' % name,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001600 revisionExpr = 'refs/heads/master',
1601 revisionId = None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001602
1603 def PreSync(self):
1604 if self.Exists:
1605 cb = self.CurrentBranch
1606 if cb:
1607 base = self.GetBranch(cb).merge
1608 if base:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001609 self.revisionExpr = base
1610 self.revisionId = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001611
1612 @property
Shawn O. Pearcef6906872009-04-18 10:49:00 -07001613 def LastFetch(self):
1614 try:
1615 fh = os.path.join(self.gitdir, 'FETCH_HEAD')
1616 return os.path.getmtime(fh)
1617 except OSError:
1618 return 0
1619
1620 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001621 def HasChanges(self):
1622 """Has the remote received new commits not yet checked out?
1623 """
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001624 if not self.remote or not self.revisionExpr:
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001625 return False
1626
1627 all = self.bare_ref.all
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001628 revid = self.GetRevisionId(all)
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07001629 head = self.work_git.GetHead()
1630 if head.startswith(R_HEADS):
1631 try:
1632 head = all[head]
1633 except KeyError:
1634 head = None
1635
1636 if revid == head:
1637 return False
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001638 elif self._revlist(not_rev(HEAD), revid):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001639 return True
1640 return False