blob: d11a3c94c02e596fea50bd7b45ff67a7652bfc81 [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
Sarah Owenscecd1d82012-11-01 22:59:27 -070015from __future__ import print_function
Dave Borowitz137d0132015-01-02 11:12:54 -080016import contextlib
Shawn O. Pearce438ee1c2008-11-03 09:59:36 -080017import errno
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018import filecmp
19import os
Shawn O. Pearcec325dc32011-10-03 08:30:24 -070020import random
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070021import re
22import shutil
23import stat
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -070024import subprocess
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070025import sys
Julien Campergue335f5ef2013-10-16 11:02:35 +020026import tarfile
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +080027import tempfile
Shawn O. Pearcec325dc32011-10-03 08:30:24 -070028import time
Dave Borowitz137d0132015-01-02 11:12:54 -080029import traceback
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -070030
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070031from color import Coloring
Dave Borowitzb42b4742012-10-31 12:27:27 -070032from git_command import GitCommand, git_require
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -070033from git_config import GitConfig, IsId, GetSchemeFromUrl, ID_RE
David Pursehousee15c65a2012-08-22 10:46:11 +090034from error import GitError, HookError, UploadError
Shawn O. Pearce559b8462009-03-02 12:56:08 -080035from error import ManifestInvalidRevisionError
Conley Owens75ee0572012-11-15 17:33:11 -080036from error import NoManifestException
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -070037from trace import IsTrace, Trace
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070038
Shawn O. Pearced237b692009-04-17 18:49:50 -070039from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040
David Pursehouse59bbb582013-05-17 10:49:33 +090041from pyversion import is_python3
42if not is_python3():
43 # pylint:disable=W0622
Chirayu Desai217ea7d2013-03-01 19:14:38 +053044 input = raw_input
David Pursehouse59bbb582013-05-17 10:49:33 +090045 # pylint:enable=W0622
Chirayu Desai217ea7d2013-03-01 19:14:38 +053046
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -070047def _lwrite(path, content):
48 lock = '%s.lock' % path
49
Chirayu Desai303a82f2014-08-19 22:57:17 +053050 fd = open(lock, 'w')
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -070051 try:
52 fd.write(content)
53 finally:
54 fd.close()
55
56 try:
57 os.rename(lock, path)
58 except OSError:
59 os.remove(lock)
60 raise
61
Shawn O. Pearce48244782009-04-16 08:25:57 -070062def _error(fmt, *args):
63 msg = fmt % args
Sarah Owenscecd1d82012-11-01 22:59:27 -070064 print('error: %s' % msg, file=sys.stderr)
Shawn O. Pearce48244782009-04-16 08:25:57 -070065
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070066def not_rev(r):
67 return '^' + r
68
Shawn O. Pearceb54a3922009-01-05 16:18:58 -080069def sq(r):
70 return "'" + r.replace("'", "'\''") + "'"
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -080071
Jonathan Nieder93719792015-03-17 11:29:58 -070072_project_hook_list = None
73def _ProjectHooks():
74 """List the hooks present in the 'hooks' directory.
75
76 These hooks are project hooks and are copied to the '.git/hooks' directory
77 of all subprojects.
78
79 This function caches the list of hooks (based on the contents of the
80 'repo/hooks' directory) on the first call.
81
82 Returns:
83 A list of absolute paths to all of the files in the hooks directory.
84 """
85 global _project_hook_list
86 if _project_hook_list is None:
87 d = os.path.realpath(os.path.abspath(os.path.dirname(__file__)))
88 d = os.path.join(d, 'hooks')
89 _project_hook_list = [os.path.join(d, x) for x in os.listdir(d)]
90 return _project_hook_list
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
Bryan Jacobsf609f912013-05-06 13:36:24 -0400162 def UploadForReview(self, people, auto_topic=False, draft=False, dest_branch=None):
Shawn O. Pearcec99883f2008-11-11 17:12:43 -0800163 self.project.UploadForReview(self.name,
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700164 people,
Brian Harring435370c2012-07-28 15:37:04 -0700165 auto_topic=auto_topic,
Bryan Jacobsf609f912013-05-06 13:36:24 -0400166 draft=draft,
167 dest_branch=dest_branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700168
Ficus Kirkpatrickbc7ef672009-05-04 12:45:11 -0700169 def GetPublishedRefs(self):
170 refs = {}
171 output = self.project.bare_git.ls_remote(
172 self.branch.remote.SshReviewUrl(self.project.UserEmail),
173 'refs/changes/*')
174 for line in output.split('\n'):
175 try:
176 (sha, ref) = line.split()
177 refs[sha] = ref
178 except ValueError:
179 pass
180
181 return refs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700182
183class StatusColoring(Coloring):
184 def __init__(self, config):
185 Coloring.__init__(self, config, 'status')
Anthony King7bdac712014-07-16 12:56:40 +0100186 self.project = self.printer('header', attr='bold')
187 self.branch = self.printer('header', attr='bold')
188 self.nobranch = self.printer('nobranch', fg='red')
189 self.important = self.printer('important', fg='red')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700190
Anthony King7bdac712014-07-16 12:56:40 +0100191 self.added = self.printer('added', fg='green')
192 self.changed = self.printer('changed', fg='red')
193 self.untracked = self.printer('untracked', fg='red')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700194
195
196class DiffColoring(Coloring):
197 def __init__(self, config):
198 Coloring.__init__(self, config, 'diff')
Anthony King7bdac712014-07-16 12:56:40 +0100199 self.project = self.printer('header', attr='bold')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700200
Anthony King7bdac712014-07-16 12:56:40 +0100201class _Annotation(object):
James W. Mills24c13082012-04-12 15:04:13 -0500202 def __init__(self, name, value, keep):
203 self.name = name
204 self.value = value
205 self.keep = keep
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700206
Anthony King7bdac712014-07-16 12:56:40 +0100207class _CopyFile(object):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800208 def __init__(self, src, dest, abssrc, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700209 self.src = src
210 self.dest = dest
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800211 self.abs_src = abssrc
212 self.abs_dest = absdest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700213
214 def _Copy(self):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800215 src = self.abs_src
216 dest = self.abs_dest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700217 # copy file if it does not exist or is out of date
218 if not os.path.exists(dest) or not filecmp.cmp(src, dest):
219 try:
220 # remove existing file first, since it might be read-only
221 if os.path.exists(dest):
222 os.remove(dest)
Matthew Buckett2daf6672009-07-11 09:43:47 -0400223 else:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200224 dest_dir = os.path.dirname(dest)
225 if not os.path.isdir(dest_dir):
226 os.makedirs(dest_dir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700227 shutil.copy(src, dest)
228 # make the file read-only
229 mode = os.stat(dest)[stat.ST_MODE]
230 mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
231 os.chmod(dest, mode)
232 except IOError:
Shawn O. Pearce48244782009-04-16 08:25:57 -0700233 _error('Cannot copy file %s to %s', src, dest)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700234
Anthony King7bdac712014-07-16 12:56:40 +0100235class _LinkFile(object):
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500236 def __init__(self, src, dest, abssrc, absdest):
237 self.src = src
238 self.dest = dest
239 self.abs_src = abssrc
240 self.abs_dest = absdest
241
242 def _Link(self):
243 src = self.abs_src
244 dest = self.abs_dest
245 # link file if it does not exist or is out of date
246 if not os.path.islink(dest) or os.readlink(dest) != src:
247 try:
248 # remove existing file first, since it might be read-only
249 if os.path.exists(dest):
250 os.remove(dest)
251 else:
252 dest_dir = os.path.dirname(dest)
253 if not os.path.isdir(dest_dir):
254 os.makedirs(dest_dir)
255 os.symlink(src, dest)
256 except IOError:
257 _error('Cannot link file %s to %s', src, dest)
258
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700259class RemoteSpec(object):
260 def __init__(self,
261 name,
Anthony King7bdac712014-07-16 12:56:40 +0100262 url=None,
263 review=None,
264 revision=None):
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700265 self.name = name
266 self.url = url
267 self.review = review
Anthony King36ea2fb2014-05-06 11:54:01 +0100268 self.revision = revision
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700269
Doug Anderson37282b42011-03-04 11:54:18 -0800270class RepoHook(object):
271 """A RepoHook contains information about a script to run as a hook.
272
273 Hooks are used to run a python script before running an upload (for instance,
274 to run presubmit checks). Eventually, we may have hooks for other actions.
275
276 This shouldn't be confused with files in the 'repo/hooks' directory. Those
277 files are copied into each '.git/hooks' folder for each project. Repo-level
278 hooks are associated instead with repo actions.
279
280 Hooks are always python. When a hook is run, we will load the hook into the
281 interpreter and execute its main() function.
282 """
283 def __init__(self,
284 hook_type,
285 hooks_project,
286 topdir,
287 abort_if_user_denies=False):
288 """RepoHook constructor.
289
290 Params:
291 hook_type: A string representing the type of hook. This is also used
292 to figure out the name of the file containing the hook. For
293 example: 'pre-upload'.
294 hooks_project: The project containing the repo hooks. If you have a
295 manifest, this is manifest.repo_hooks_project. OK if this is None,
296 which will make the hook a no-op.
297 topdir: Repo's top directory (the one containing the .repo directory).
298 Scripts will run with CWD as this directory. If you have a manifest,
299 this is manifest.topdir
300 abort_if_user_denies: If True, we'll throw a HookError() if the user
301 doesn't allow us to run the hook.
302 """
303 self._hook_type = hook_type
304 self._hooks_project = hooks_project
305 self._topdir = topdir
306 self._abort_if_user_denies = abort_if_user_denies
307
308 # Store the full path to the script for convenience.
309 if self._hooks_project:
310 self._script_fullpath = os.path.join(self._hooks_project.worktree,
311 self._hook_type + '.py')
312 else:
313 self._script_fullpath = None
314
315 def _GetHash(self):
316 """Return a hash of the contents of the hooks directory.
317
318 We'll just use git to do this. This hash has the property that if anything
319 changes in the directory we will return a different has.
320
321 SECURITY CONSIDERATION:
322 This hash only represents the contents of files in the hook directory, not
323 any other files imported or called by hooks. Changes to imported files
324 can change the script behavior without affecting the hash.
325
326 Returns:
327 A string representing the hash. This will always be ASCII so that it can
328 be printed to the user easily.
329 """
330 assert self._hooks_project, "Must have hooks to calculate their hash."
331
332 # We will use the work_git object rather than just calling GetRevisionId().
333 # That gives us a hash of the latest checked in version of the files that
334 # the user will actually be executing. Specifically, GetRevisionId()
335 # doesn't appear to change even if a user checks out a different version
336 # of the hooks repo (via git checkout) nor if a user commits their own revs.
337 #
338 # NOTE: Local (non-committed) changes will not be factored into this hash.
339 # I think this is OK, since we're really only worried about warning the user
340 # about upstream changes.
341 return self._hooks_project.work_git.rev_parse('HEAD')
342
343 def _GetMustVerb(self):
344 """Return 'must' if the hook is required; 'should' if not."""
345 if self._abort_if_user_denies:
346 return 'must'
347 else:
348 return 'should'
349
350 def _CheckForHookApproval(self):
351 """Check to see whether this hook has been approved.
352
353 We'll look at the hash of all of the hooks. If this matches the hash that
354 the user last approved, we're done. If it doesn't, we'll ask the user
355 about approval.
356
357 Note that we ask permission for each individual hook even though we use
358 the hash of all hooks when detecting changes. We'd like the user to be
359 able to approve / deny each hook individually. We only use the hash of all
360 hooks because there is no other easy way to detect changes to local imports.
361
362 Returns:
363 True if this hook is approved to run; False otherwise.
364
365 Raises:
366 HookError: Raised if the user doesn't approve and abort_if_user_denies
367 was passed to the consturctor.
368 """
Doug Anderson37282b42011-03-04 11:54:18 -0800369 hooks_config = self._hooks_project.config
370 git_approval_key = 'repo.hooks.%s.approvedhash' % self._hook_type
371
372 # Get the last hash that the user approved for this hook; may be None.
373 old_hash = hooks_config.GetString(git_approval_key)
374
375 # Get the current hash so we can tell if scripts changed since approval.
376 new_hash = self._GetHash()
377
378 if old_hash is not None:
379 # User previously approved hook and asked not to be prompted again.
380 if new_hash == old_hash:
381 # Approval matched. We're done.
382 return True
383 else:
384 # Give the user a reason why we're prompting, since they last told
385 # us to "never ask again".
386 prompt = 'WARNING: Scripts have changed since %s was allowed.\n\n' % (
387 self._hook_type)
388 else:
389 prompt = ''
390
391 # Prompt the user if we're not on a tty; on a tty we'll assume "no".
392 if sys.stdout.isatty():
393 prompt += ('Repo %s run the script:\n'
394 ' %s\n'
395 '\n'
396 'Do you want to allow this script to run '
397 '(yes/yes-never-ask-again/NO)? ') % (
398 self._GetMustVerb(), self._script_fullpath)
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530399 response = input(prompt).lower()
David Pursehouse98ffba12012-11-14 11:18:00 +0900400 print()
Doug Anderson37282b42011-03-04 11:54:18 -0800401
402 # User is doing a one-time approval.
403 if response in ('y', 'yes'):
404 return True
405 elif response == 'yes-never-ask-again':
406 hooks_config.SetString(git_approval_key, new_hash)
407 return True
408
409 # For anything else, we'll assume no approval.
410 if self._abort_if_user_denies:
411 raise HookError('You must allow the %s hook or use --no-verify.' %
412 self._hook_type)
413
414 return False
415
416 def _ExecuteHook(self, **kwargs):
417 """Actually execute the given hook.
418
419 This will run the hook's 'main' function in our python interpreter.
420
421 Args:
422 kwargs: Keyword arguments to pass to the hook. These are often specific
423 to the hook type. For instance, pre-upload hooks will contain
424 a project_list.
425 """
426 # Keep sys.path and CWD stashed away so that we can always restore them
427 # upon function exit.
428 orig_path = os.getcwd()
429 orig_syspath = sys.path
430
431 try:
432 # Always run hooks with CWD as topdir.
433 os.chdir(self._topdir)
434
435 # Put the hook dir as the first item of sys.path so hooks can do
436 # relative imports. We want to replace the repo dir as [0] so
437 # hooks can't import repo files.
438 sys.path = [os.path.dirname(self._script_fullpath)] + sys.path[1:]
439
440 # Exec, storing global context in the context dict. We catch exceptions
441 # and convert to a HookError w/ just the failing traceback.
442 context = {}
443 try:
Anthony King70f68902014-05-05 21:15:34 +0100444 exec(compile(open(self._script_fullpath).read(),
445 self._script_fullpath, 'exec'), context)
Doug Anderson37282b42011-03-04 11:54:18 -0800446 except Exception:
447 raise HookError('%s\nFailed to import %s hook; see traceback above.' % (
448 traceback.format_exc(), self._hook_type))
449
450 # Running the script should have defined a main() function.
451 if 'main' not in context:
452 raise HookError('Missing main() in: "%s"' % self._script_fullpath)
453
454
455 # Add 'hook_should_take_kwargs' to the arguments to be passed to main.
456 # We don't actually want hooks to define their main with this argument--
457 # it's there to remind them that their hook should always take **kwargs.
458 # For instance, a pre-upload hook should be defined like:
459 # def main(project_list, **kwargs):
460 #
461 # This allows us to later expand the API without breaking old hooks.
462 kwargs = kwargs.copy()
463 kwargs['hook_should_take_kwargs'] = True
464
465 # Call the main function in the hook. If the hook should cause the
466 # build to fail, it will raise an Exception. We'll catch that convert
467 # to a HookError w/ just the failing traceback.
468 try:
469 context['main'](**kwargs)
470 except Exception:
471 raise HookError('%s\nFailed to run main() for %s hook; see traceback '
472 'above.' % (
473 traceback.format_exc(), self._hook_type))
474 finally:
475 # Restore sys.path and CWD.
476 sys.path = orig_syspath
477 os.chdir(orig_path)
478
479 def Run(self, user_allows_all_hooks, **kwargs):
480 """Run the hook.
481
482 If the hook doesn't exist (because there is no hooks project or because
483 this particular hook is not enabled), this is a no-op.
484
485 Args:
486 user_allows_all_hooks: If True, we will never prompt about running the
487 hook--we'll just assume it's OK to run it.
488 kwargs: Keyword arguments to pass to the hook. These are often specific
489 to the hook type. For instance, pre-upload hooks will contain
490 a project_list.
491
492 Raises:
493 HookError: If there was a problem finding the hook or the user declined
494 to run a required hook (from _CheckForHookApproval).
495 """
496 # No-op if there is no hooks project or if hook is disabled.
497 if ((not self._hooks_project) or
498 (self._hook_type not in self._hooks_project.enabled_repo_hooks)):
499 return
500
501 # Bail with a nice error if we can't find the hook.
502 if not os.path.isfile(self._script_fullpath):
503 raise HookError('Couldn\'t find repo hook: "%s"' % self._script_fullpath)
504
505 # Make sure the user is OK with running the hook.
506 if (not user_allows_all_hooks) and (not self._CheckForHookApproval()):
507 return
508
509 # Run the hook with the same version of python we're using.
510 self._ExecuteHook(**kwargs)
511
512
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700513class Project(object):
514 def __init__(self,
515 manifest,
516 name,
517 remote,
518 gitdir,
David James8d201162013-10-11 17:03:19 -0700519 objdir,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700520 worktree,
521 relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700522 revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800523 revisionId,
Anthony King7bdac712014-07-16 12:56:40 +0100524 rebase=True,
525 groups=None,
526 sync_c=False,
527 sync_s=False,
528 clone_depth=None,
529 upstream=None,
530 parent=None,
531 is_derived=False,
532 dest_branch=None):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800533 """Init a Project object.
534
535 Args:
536 manifest: The XmlManifest object.
537 name: The `name` attribute of manifest.xml's project element.
538 remote: RemoteSpec object specifying its remote's properties.
539 gitdir: Absolute path of git directory.
David James8d201162013-10-11 17:03:19 -0700540 objdir: Absolute path of directory to store git objects.
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800541 worktree: Absolute path of git working tree.
542 relpath: Relative path of git working tree to repo's top directory.
543 revisionExpr: The `revision` attribute of manifest.xml's project element.
544 revisionId: git commit id for checking out.
545 rebase: The `rebase` attribute of manifest.xml's project element.
546 groups: The `groups` attribute of manifest.xml's project element.
547 sync_c: The `sync-c` attribute of manifest.xml's project element.
548 sync_s: The `sync-s` attribute of manifest.xml's project element.
549 upstream: The `upstream` attribute of manifest.xml's project element.
550 parent: The parent Project object.
551 is_derived: False if the project was explicitly defined in the manifest;
552 True if the project is a discovered submodule.
Bryan Jacobsf609f912013-05-06 13:36:24 -0400553 dest_branch: The branch to which to push changes for review by default.
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800554 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700555 self.manifest = manifest
556 self.name = name
557 self.remote = remote
Anthony Newnamdf14a702011-01-09 17:31:57 -0800558 self.gitdir = gitdir.replace('\\', '/')
David James8d201162013-10-11 17:03:19 -0700559 self.objdir = objdir.replace('\\', '/')
Shawn O. Pearce0ce6ca92011-01-10 13:26:01 -0800560 if worktree:
561 self.worktree = worktree.replace('\\', '/')
562 else:
563 self.worktree = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700564 self.relpath = relpath
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700565 self.revisionExpr = revisionExpr
566
567 if revisionId is None \
568 and revisionExpr \
569 and IsId(revisionExpr):
570 self.revisionId = revisionExpr
571 else:
572 self.revisionId = revisionId
573
Mike Pontillod3153822012-02-28 11:53:24 -0800574 self.rebase = rebase
Colin Cross5acde752012-03-28 20:15:45 -0700575 self.groups = groups
Anatol Pomazau79770d22012-04-20 14:41:59 -0700576 self.sync_c = sync_c
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800577 self.sync_s = sync_s
David Pursehouseede7f122012-11-27 22:25:30 +0900578 self.clone_depth = clone_depth
Brian Harring14a66742012-09-28 20:21:57 -0700579 self.upstream = upstream
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800580 self.parent = parent
581 self.is_derived = is_derived
582 self.subprojects = []
Mike Pontillod3153822012-02-28 11:53:24 -0800583
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700584 self.snapshots = {}
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700585 self.copyfiles = []
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500586 self.linkfiles = []
James W. Mills24c13082012-04-12 15:04:13 -0500587 self.annotations = []
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700588 self.config = GitConfig.ForRepository(
Anthony King7bdac712014-07-16 12:56:40 +0100589 gitdir=self.gitdir,
590 defaults=self.manifest.globalConfig)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700591
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800592 if self.worktree:
David James8d201162013-10-11 17:03:19 -0700593 self.work_git = self._GitGetByExec(self, bare=False, gitdir=gitdir)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800594 else:
595 self.work_git = None
David James8d201162013-10-11 17:03:19 -0700596 self.bare_git = self._GitGetByExec(self, bare=True, gitdir=gitdir)
Shawn O. Pearced237b692009-04-17 18:49:50 -0700597 self.bare_ref = GitRefs(gitdir)
David James8d201162013-10-11 17:03:19 -0700598 self.bare_objdir = self._GitGetByExec(self, bare=True, gitdir=objdir)
Bryan Jacobsf609f912013-05-06 13:36:24 -0400599 self.dest_branch = dest_branch
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700600
Doug Anderson37282b42011-03-04 11:54:18 -0800601 # This will be filled in if a project is later identified to be the
602 # project containing repo hooks.
603 self.enabled_repo_hooks = []
604
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700605 @property
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800606 def Derived(self):
607 return self.is_derived
608
609 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700610 def Exists(self):
611 return os.path.isdir(self.gitdir)
612
613 @property
614 def CurrentBranch(self):
615 """Obtain the name of the currently checked out branch.
616 The branch name omits the 'refs/heads/' prefix.
617 None is returned if the project is on a detached HEAD.
618 """
Shawn O. Pearce5b23f242009-04-17 18:43:33 -0700619 b = self.work_git.GetHead()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700620 if b.startswith(R_HEADS):
621 return b[len(R_HEADS):]
622 return None
623
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700624 def IsRebaseInProgress(self):
625 w = self.worktree
626 g = os.path.join(w, '.git')
627 return os.path.exists(os.path.join(g, 'rebase-apply')) \
628 or os.path.exists(os.path.join(g, 'rebase-merge')) \
629 or os.path.exists(os.path.join(w, '.dotest'))
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200630
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700631 def IsDirty(self, consider_untracked=True):
632 """Is the working directory modified in some way?
633 """
634 self.work_git.update_index('-q',
635 '--unmerged',
636 '--ignore-missing',
637 '--refresh')
David Pursehouse8f62fb72012-11-14 12:09:38 +0900638 if self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700639 return True
640 if self.work_git.DiffZ('diff-files'):
641 return True
642 if consider_untracked and self.work_git.LsOthers():
643 return True
644 return False
645
646 _userident_name = None
647 _userident_email = None
648
649 @property
650 def UserName(self):
651 """Obtain the user's personal name.
652 """
653 if self._userident_name is None:
654 self._LoadUserIdentity()
655 return self._userident_name
656
657 @property
658 def UserEmail(self):
659 """Obtain the user's email address. This is very likely
660 to be their Gerrit login.
661 """
662 if self._userident_email is None:
663 self._LoadUserIdentity()
664 return self._userident_email
665
666 def _LoadUserIdentity(self):
David Pursehousec1b86a22012-11-14 11:36:51 +0900667 u = self.bare_git.var('GIT_COMMITTER_IDENT')
668 m = re.compile("^(.*) <([^>]*)> ").match(u)
669 if m:
670 self._userident_name = m.group(1)
671 self._userident_email = m.group(2)
672 else:
673 self._userident_name = ''
674 self._userident_email = ''
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700675
676 def GetRemote(self, name):
677 """Get the configuration for a single remote.
678 """
679 return self.config.GetRemote(name)
680
681 def GetBranch(self, name):
682 """Get the configuration for a single branch.
683 """
684 return self.config.GetBranch(name)
685
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700686 def GetBranches(self):
687 """Get all existing local branches.
688 """
689 current = self.CurrentBranch
David Pursehouse8a68ff92012-09-24 12:15:13 +0900690 all_refs = self._allrefs
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700691 heads = {}
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700692
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530693 for name, ref_id in all_refs.items():
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700694 if name.startswith(R_HEADS):
695 name = name[len(R_HEADS):]
696 b = self.GetBranch(name)
697 b.current = name == current
698 b.published = None
David Pursehouse8a68ff92012-09-24 12:15:13 +0900699 b.revision = ref_id
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700700 heads[name] = b
701
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530702 for name, ref_id in all_refs.items():
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700703 if name.startswith(R_PUB):
704 name = name[len(R_PUB):]
705 b = heads.get(name)
706 if b:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900707 b.published = ref_id
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700708
709 return heads
710
Colin Cross5acde752012-03-28 20:15:45 -0700711 def MatchesGroups(self, manifest_groups):
712 """Returns true if the manifest groups specified at init should cause
713 this project to be synced.
714 Prefixing a manifest group with "-" inverts the meaning of a group.
Conley Owensbb1b5f52012-08-13 13:11:18 -0700715 All projects are implicitly labelled with "all".
Colin Cross5acde752012-03-28 20:15:45 -0700716
Conley Owens971de8e2012-04-16 10:36:08 -0700717 labels are resolved in order. In the example case of
Conley Owensbb1b5f52012-08-13 13:11:18 -0700718 project_groups: "all,group1,group2"
Conley Owens971de8e2012-04-16 10:36:08 -0700719 manifest_groups: "-group1,group2"
720 the project will be matched.
David Holmer0a1c6a12012-11-14 19:19:00 -0500721
722 The special manifest group "default" will match any project that
723 does not have the special project group "notdefault"
Conley Owens971de8e2012-04-16 10:36:08 -0700724 """
David Holmer0a1c6a12012-11-14 19:19:00 -0500725 expanded_manifest_groups = manifest_groups or ['default']
Conley Owensbb1b5f52012-08-13 13:11:18 -0700726 expanded_project_groups = ['all'] + (self.groups or [])
David Holmer0a1c6a12012-11-14 19:19:00 -0500727 if not 'notdefault' in expanded_project_groups:
728 expanded_project_groups += ['default']
Conley Owensbb1b5f52012-08-13 13:11:18 -0700729
Conley Owens971de8e2012-04-16 10:36:08 -0700730 matched = False
Conley Owensbb1b5f52012-08-13 13:11:18 -0700731 for group in expanded_manifest_groups:
732 if group.startswith('-') and group[1:] in expanded_project_groups:
Conley Owens971de8e2012-04-16 10:36:08 -0700733 matched = False
Conley Owensbb1b5f52012-08-13 13:11:18 -0700734 elif group in expanded_project_groups:
Conley Owens971de8e2012-04-16 10:36:08 -0700735 matched = True
Colin Cross5acde752012-03-28 20:15:45 -0700736
Conley Owens971de8e2012-04-16 10:36:08 -0700737 return matched
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700738
739## Status Display ##
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700740 def UncommitedFiles(self, get_all=True):
741 """Returns a list of strings, uncommitted files in the git tree.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700742
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700743 Args:
744 get_all: a boolean, if True - get information about all different
745 uncommitted files. If False - return as soon as any kind of
746 uncommitted files is detected.
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500747 """
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700748 details = []
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500749 self.work_git.update_index('-q',
750 '--unmerged',
751 '--ignore-missing',
752 '--refresh')
753 if self.IsRebaseInProgress():
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700754 details.append("rebase in progress")
755 if not get_all:
756 return details
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500757
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700758 changes = self.work_git.DiffZ('diff-index', '--cached', HEAD).keys()
759 if changes:
760 details.extend(changes)
761 if not get_all:
762 return details
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500763
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700764 changes = self.work_git.DiffZ('diff-files').keys()
765 if changes:
766 details.extend(changes)
767 if not get_all:
768 return details
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500769
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700770 changes = self.work_git.LsOthers()
771 if changes:
772 details.extend(changes)
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500773
Vadim Bendebury14e134d2014-10-05 15:40:30 -0700774 return details
775
776 def HasChanges(self):
777 """Returns true if there are uncommitted changes.
778 """
779 if self.UncommitedFiles(get_all=False):
780 return True
781 else:
782 return False
Anthony Newnamcc50bac2010-04-08 10:28:59 -0500783
Terence Haddock4655e812011-03-31 12:33:34 +0200784 def PrintWorkTreeStatus(self, output_redir=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700785 """Prints the status of the repository to stdout.
Terence Haddock4655e812011-03-31 12:33:34 +0200786
787 Args:
788 output: If specified, redirect the output to this object.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700789 """
790 if not os.path.isdir(self.worktree):
Terence Haddock4655e812011-03-31 12:33:34 +0200791 if output_redir == None:
792 output_redir = sys.stdout
Sarah Owenscecd1d82012-11-01 22:59:27 -0700793 print(file=output_redir)
794 print('project %s/' % self.relpath, file=output_redir)
795 print(' missing (run "repo sync")', file=output_redir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700796 return
797
798 self.work_git.update_index('-q',
799 '--unmerged',
800 '--ignore-missing',
801 '--refresh')
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700802 rb = self.IsRebaseInProgress()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700803 di = self.work_git.DiffZ('diff-index', '-M', '--cached', HEAD)
804 df = self.work_git.DiffZ('diff-files')
805 do = self.work_git.LsOthers()
Ali Utku Selen76abcc12012-01-25 10:51:12 +0100806 if not rb and not di and not df and not do and not self.CurrentBranch:
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700807 return 'CLEAN'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700808
809 out = StatusColoring(self.config)
Terence Haddock4655e812011-03-31 12:33:34 +0200810 if not output_redir == None:
811 out.redirect(output_redir)
Jakub Vrana0402cd82014-09-09 15:39:15 -0700812 out.project('project %-40s', self.relpath + '/ ')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700813
814 branch = self.CurrentBranch
815 if branch is None:
816 out.nobranch('(*** NO BRANCH ***)')
817 else:
818 out.branch('branch %s', branch)
819 out.nl()
820
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -0700821 if rb:
822 out.important('prior sync failed; rebase still in progress')
823 out.nl()
824
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700825 paths = list()
826 paths.extend(di.keys())
827 paths.extend(df.keys())
828 paths.extend(do)
829
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530830 for p in sorted(set(paths)):
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900831 try:
832 i = di[p]
833 except KeyError:
834 i = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700835
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900836 try:
837 f = df[p]
838 except KeyError:
839 f = None
Julius Gustavsson0cb1b3f2010-06-17 17:55:02 +0200840
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900841 if i:
842 i_status = i.status.upper()
843 else:
844 i_status = '-'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700845
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900846 if f:
847 f_status = f.status.lower()
848 else:
849 f_status = '-'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700850
851 if i and i.src_path:
Shawn O. Pearcefe086752009-03-03 13:49:48 -0800852 line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700853 i.src_path, p, i.level)
854 else:
855 line = ' %s%s\t%s' % (i_status, f_status, p)
856
857 if i and not f:
858 out.added('%s', line)
859 elif (i and f) or (not i and f):
860 out.changed('%s', line)
861 elif not i and not f:
862 out.untracked('%s', line)
863 else:
864 out.write('%s', line)
865 out.nl()
Terence Haddock4655e812011-03-31 12:33:34 +0200866
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700867 return 'DIRTY'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700868
pelyad67872d2012-03-28 14:49:58 +0300869 def PrintWorkTreeDiff(self, absolute_paths=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700870 """Prints the status of the repository to stdout.
871 """
872 out = DiffColoring(self.config)
873 cmd = ['diff']
874 if out.is_on:
875 cmd.append('--color')
876 cmd.append(HEAD)
pelyad67872d2012-03-28 14:49:58 +0300877 if absolute_paths:
878 cmd.append('--src-prefix=a/%s/' % self.relpath)
879 cmd.append('--dst-prefix=b/%s/' % self.relpath)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700880 cmd.append('--')
881 p = GitCommand(self,
882 cmd,
Anthony King7bdac712014-07-16 12:56:40 +0100883 capture_stdout=True,
884 capture_stderr=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700885 has_diff = False
886 for line in p.process.stdout:
887 if not has_diff:
888 out.nl()
889 out.project('project %s/' % self.relpath)
890 out.nl()
891 has_diff = True
Sarah Owenscecd1d82012-11-01 22:59:27 -0700892 print(line[:-1])
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700893 p.Wait()
894
895
896## Publish / Upload ##
897
David Pursehouse8a68ff92012-09-24 12:15:13 +0900898 def WasPublished(self, branch, all_refs=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700899 """Was the branch published (uploaded) for code review?
900 If so, returns the SHA-1 hash of the last published
901 state for the branch.
902 """
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700903 key = R_PUB + branch
David Pursehouse8a68ff92012-09-24 12:15:13 +0900904 if all_refs is None:
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700905 try:
906 return self.bare_git.rev_parse(key)
907 except GitError:
908 return None
909 else:
910 try:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900911 return all_refs[key]
Shawn O. Pearcefbcde472009-04-17 20:58:02 -0700912 except KeyError:
913 return None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700914
David Pursehouse8a68ff92012-09-24 12:15:13 +0900915 def CleanPublishedCache(self, all_refs=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700916 """Prunes any stale published refs.
917 """
David Pursehouse8a68ff92012-09-24 12:15:13 +0900918 if all_refs is None:
919 all_refs = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700920 heads = set()
921 canrm = {}
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530922 for name, ref_id in all_refs.items():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700923 if name.startswith(R_HEADS):
924 heads.add(name)
925 elif name.startswith(R_PUB):
David Pursehouse8a68ff92012-09-24 12:15:13 +0900926 canrm[name] = ref_id
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700927
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530928 for name, ref_id in canrm.items():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700929 n = name[len(R_PUB):]
930 if R_HEADS + n not in heads:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900931 self.bare_git.DeleteRef(name, ref_id)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700932
Mandeep Singh Bainesd6c93a22011-05-26 10:34:11 -0700933 def GetUploadableBranches(self, selected_branch=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700934 """List any branches which can be uploaded for review.
935 """
936 heads = {}
937 pubed = {}
938
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530939 for name, ref_id in self._allrefs.items():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700940 if name.startswith(R_HEADS):
David Pursehouse8a68ff92012-09-24 12:15:13 +0900941 heads[name[len(R_HEADS):]] = ref_id
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700942 elif name.startswith(R_PUB):
David Pursehouse8a68ff92012-09-24 12:15:13 +0900943 pubed[name[len(R_PUB):]] = ref_id
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700944
945 ready = []
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530946 for branch, ref_id in heads.items():
David Pursehouse8a68ff92012-09-24 12:15:13 +0900947 if branch in pubed and pubed[branch] == ref_id:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700948 continue
Mandeep Singh Bainesd6c93a22011-05-26 10:34:11 -0700949 if selected_branch and branch != selected_branch:
950 continue
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700951
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800952 rb = self.GetUploadableBranch(branch)
953 if rb:
954 ready.append(rb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700955 return ready
956
Shawn O. Pearce35f25962008-11-11 17:03:13 -0800957 def GetUploadableBranch(self, branch_name):
958 """Get a single uploadable branch, or None.
959 """
960 branch = self.GetBranch(branch_name)
961 base = branch.LocalMerge
962 if branch.LocalMerge:
963 rb = ReviewableBranch(self, branch, base)
964 if rb.commits:
965 return rb
966 return None
967
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -0700968 def UploadForReview(self, branch=None,
Anthony King7bdac712014-07-16 12:56:40 +0100969 people=([], []),
Brian Harring435370c2012-07-28 15:37:04 -0700970 auto_topic=False,
Bryan Jacobsf609f912013-05-06 13:36:24 -0400971 draft=False,
972 dest_branch=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700973 """Uploads the named branch for code review.
974 """
975 if branch is None:
976 branch = self.CurrentBranch
977 if branch is None:
978 raise GitError('not currently on a branch')
979
980 branch = self.GetBranch(branch)
981 if not branch.LocalMerge:
982 raise GitError('branch %s does not track a remote' % branch.name)
983 if not branch.remote.review:
984 raise GitError('remote %s has no review url' % branch.remote.name)
985
Bryan Jacobsf609f912013-05-06 13:36:24 -0400986 if dest_branch is None:
987 dest_branch = self.dest_branch
988 if dest_branch is None:
989 dest_branch = branch.merge
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700990 if not dest_branch.startswith(R_HEADS):
991 dest_branch = R_HEADS + dest_branch
992
Shawn O. Pearce339ba9f2008-11-06 09:52:51 -0800993 if not branch.remote.projectname:
994 branch.remote.projectname = self.name
995 branch.remote.Save()
996
Shawn O. Pearcec9571422012-01-11 14:58:54 -0800997 url = branch.remote.ReviewUrl(self.UserEmail)
998 if url is None:
999 raise UploadError('review not configured')
1000 cmd = ['push']
Shawn O. Pearceb54a3922009-01-05 16:18:58 -08001001
Shawn O. Pearcec9571422012-01-11 14:58:54 -08001002 if url.startswith('ssh://'):
Shawn O. Pearceb54a3922009-01-05 16:18:58 -08001003 rp = ['gerrit receive-pack']
1004 for e in people[0]:
1005 rp.append('--reviewer=%s' % sq(e))
1006 for e in people[1]:
1007 rp.append('--cc=%s' % sq(e))
Shawn O. Pearceb54a3922009-01-05 16:18:58 -08001008 cmd.append('--receive-pack=%s' % " ".join(rp))
Shawn O. Pearcea5ece0e2010-07-15 16:52:42 -07001009
Shawn O. Pearcec9571422012-01-11 14:58:54 -08001010 cmd.append(url)
1011
1012 if dest_branch.startswith(R_HEADS):
1013 dest_branch = dest_branch[len(R_HEADS):]
Brian Harring435370c2012-07-28 15:37:04 -07001014
1015 upload_type = 'for'
1016 if draft:
1017 upload_type = 'drafts'
1018
1019 ref_spec = '%s:refs/%s/%s' % (R_HEADS + branch.name, upload_type,
1020 dest_branch)
Shawn O. Pearcec9571422012-01-11 14:58:54 -08001021 if auto_topic:
1022 ref_spec = ref_spec + '/' + branch.name
Shawn Pearce45d21682013-02-28 00:35:51 -08001023 if not url.startswith('ssh://'):
1024 rp = ['r=%s' % p for p in people[0]] + \
1025 ['cc=%s' % p for p in people[1]]
1026 if rp:
1027 ref_spec = ref_spec + '%' + ','.join(rp)
Shawn O. Pearcec9571422012-01-11 14:58:54 -08001028 cmd.append(ref_spec)
Shawn O. Pearceb54a3922009-01-05 16:18:58 -08001029
Anthony King7bdac712014-07-16 12:56:40 +01001030 if GitCommand(self, cmd, bare=True).Wait() != 0:
Shawn O. Pearcec9571422012-01-11 14:58:54 -08001031 raise UploadError('Upload failed')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001032
1033 msg = "posted to %s for %s" % (branch.remote.review, dest_branch)
1034 self.bare_git.UpdateRef(R_PUB + branch.name,
1035 R_HEADS + branch.name,
Anthony King7bdac712014-07-16 12:56:40 +01001036 message=msg)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001037
1038
1039## Sync ##
1040
Julien Campergue335f5ef2013-10-16 11:02:35 +02001041 def _ExtractArchive(self, tarpath, path=None):
1042 """Extract the given tar on its current location
1043
1044 Args:
1045 - tarpath: The path to the actual tar file
1046
1047 """
1048 try:
1049 with tarfile.open(tarpath, 'r') as tar:
1050 tar.extractall(path=path)
1051 return True
1052 except (IOError, tarfile.TarError) as e:
1053 print("error: Cannot extract archive %s: "
1054 "%s" % (tarpath, str(e)), file=sys.stderr)
1055 return False
1056
Shawn O. Pearcee02ac0a2012-03-14 15:36:59 -07001057 def Sync_NetworkHalf(self,
1058 quiet=False,
1059 is_new=None,
1060 current_branch_only=False,
Mitchel Humpherys597868b2012-10-29 10:18:34 -07001061 clone_bundle=True,
Julien Campergue335f5ef2013-10-16 11:02:35 +02001062 no_tags=False,
1063 archive=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001064 """Perform only the network IO portion of the sync process.
1065 Local working directory/branch state is not affected.
1066 """
Julien Campergue335f5ef2013-10-16 11:02:35 +02001067 if archive and not isinstance(self, MetaProject):
1068 if self.remote.url.startswith(('http://', 'https://')):
1069 print("error: %s: Cannot fetch archives from http/https "
1070 "remotes." % self.name, file=sys.stderr)
1071 return False
1072
1073 name = self.relpath.replace('\\', '/')
1074 name = name.replace('/', '_')
1075 tarpath = '%s.tar' % name
1076 topdir = self.manifest.topdir
1077
1078 try:
1079 self._FetchArchive(tarpath, cwd=topdir)
1080 except GitError as e:
1081 print('error: %s' % str(e), file=sys.stderr)
1082 return False
1083
1084 # From now on, we only need absolute tarpath
1085 tarpath = os.path.join(topdir, tarpath)
1086
1087 if not self._ExtractArchive(tarpath, path=topdir):
1088 return False
1089 try:
1090 os.remove(tarpath)
1091 except OSError as e:
1092 print("warn: Cannot remove archive %s: "
1093 "%s" % (tarpath, str(e)), file=sys.stderr)
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001094 self._CopyAndLinkFiles()
Julien Campergue335f5ef2013-10-16 11:02:35 +02001095 return True
1096
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -07001097 if is_new is None:
1098 is_new = not self.Exists
Shawn O. Pearce88443382010-10-08 10:02:09 +02001099 if is_new:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001100 self._InitGitDir()
Jimmie Westera0444582012-10-24 13:44:42 +02001101 else:
1102 self._UpdateHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001103 self._InitRemote()
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001104
1105 if is_new:
1106 alt = os.path.join(self.gitdir, 'objects/info/alternates')
1107 try:
1108 fd = open(alt, 'rb')
1109 try:
1110 alt_dir = fd.readline().rstrip()
1111 finally:
1112 fd.close()
1113 except IOError:
1114 alt_dir = None
1115 else:
1116 alt_dir = None
1117
Shawn O. Pearcee02ac0a2012-03-14 15:36:59 -07001118 if clone_bundle \
1119 and alt_dir is None \
1120 and self._ApplyCloneBundle(initial=is_new, quiet=quiet):
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001121 is_new = False
1122
Shawn O. Pearce6ba6ba02012-05-24 09:46:50 -07001123 if not current_branch_only:
1124 if self.sync_c:
1125 current_branch_only = True
1126 elif not self.manifest._loaded:
1127 # Manifest cannot check defaults until it syncs.
1128 current_branch_only = False
1129 elif self.manifest.default.sync_c:
1130 current_branch_only = True
1131
Conley Owens666d5342014-05-01 13:09:57 -07001132 has_sha1 = ID_RE.match(self.revisionExpr) and self._CheckForSha1()
1133 if (not has_sha1 #Need to fetch since we don't already have this revision
1134 and not self._RemoteFetch(initial=is_new, quiet=quiet, alt_dir=alt_dir,
1135 current_branch_only=current_branch_only,
1136 no_tags=no_tags)):
Anthony King7bdac712014-07-16 12:56:40 +01001137 return False
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001138
1139 if self.worktree:
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001140 self._InitMRef()
1141 else:
1142 self._InitMirrorHead()
1143 try:
1144 os.remove(os.path.join(self.gitdir, 'FETCH_HEAD'))
1145 except OSError:
1146 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001147 return True
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08001148
1149 def PostRepoUpgrade(self):
1150 self._InitHooks()
1151
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001152 def _CopyAndLinkFiles(self):
David Pursehouse8a68ff92012-09-24 12:15:13 +09001153 for copyfile in self.copyfiles:
1154 copyfile._Copy()
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001155 for linkfile in self.linkfiles:
1156 linkfile._Link()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001157
Julien Camperguedd654222014-01-09 16:21:37 +01001158 def GetCommitRevisionId(self):
1159 """Get revisionId of a commit.
1160
1161 Use this method instead of GetRevisionId to get the id of the commit rather
1162 than the id of the current git object (for example, a tag)
1163
1164 """
1165 if not self.revisionExpr.startswith(R_TAGS):
1166 return self.GetRevisionId(self._allrefs)
1167
1168 try:
1169 return self.bare_git.rev_list(self.revisionExpr, '-1')[0]
1170 except GitError:
1171 raise ManifestInvalidRevisionError(
1172 'revision %s in %s not found' % (self.revisionExpr,
1173 self.name))
1174
David Pursehouse8a68ff92012-09-24 12:15:13 +09001175 def GetRevisionId(self, all_refs=None):
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001176 if self.revisionId:
1177 return self.revisionId
1178
1179 rem = self.GetRemote(self.remote.name)
1180 rev = rem.ToLocal(self.revisionExpr)
1181
David Pursehouse8a68ff92012-09-24 12:15:13 +09001182 if all_refs is not None and rev in all_refs:
1183 return all_refs[rev]
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001184
1185 try:
1186 return self.bare_git.rev_parse('--verify', '%s^0' % rev)
1187 except GitError:
1188 raise ManifestInvalidRevisionError(
1189 'revision %s in %s not found' % (self.revisionExpr,
1190 self.name))
1191
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001192 def Sync_LocalHalf(self, syncbuf):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001193 """Perform only the local IO portion of the sync process.
1194 Network access is not required.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001195 """
David James8d201162013-10-11 17:03:19 -07001196 self._InitWorkTree()
David Pursehouse8a68ff92012-09-24 12:15:13 +09001197 all_refs = self.bare_ref.all
1198 self.CleanPublishedCache(all_refs)
1199 revid = self.GetRevisionId(all_refs)
Skyler Kaufman835cd682011-03-08 12:14:41 -08001200
David Pursehouse1d947b32012-10-25 12:23:11 +09001201 def _doff():
1202 self._FastForward(revid)
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001203 self._CopyAndLinkFiles()
David Pursehouse1d947b32012-10-25 12:23:11 +09001204
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001205 head = self.work_git.GetHead()
1206 if head.startswith(R_HEADS):
1207 branch = head[len(R_HEADS):]
1208 try:
David Pursehouse8a68ff92012-09-24 12:15:13 +09001209 head = all_refs[head]
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001210 except KeyError:
1211 head = None
1212 else:
1213 branch = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001214
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001215 if branch is None or syncbuf.detach_head:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001216 # Currently on a detached HEAD. The user is assumed to
1217 # not have any local modifications worth worrying about.
1218 #
Shawn O. Pearce3d2cdd02009-04-18 15:26:10 -07001219 if self.IsRebaseInProgress():
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001220 syncbuf.fail(self, _PriorSyncFailedError())
1221 return
1222
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001223 if head == revid:
1224 # No changes; don't do anything further.
Florian Vallee7cf1b362012-06-07 17:11:42 +02001225 # Except if the head needs to be detached
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001226 #
Florian Vallee7cf1b362012-06-07 17:11:42 +02001227 if not syncbuf.detach_head:
1228 return
1229 else:
1230 lost = self._revlist(not_rev(revid), HEAD)
1231 if lost:
1232 syncbuf.info(self, "discarding %d commits", len(lost))
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001233
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001234 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001235 self._Checkout(revid, quiet=True)
Sarah Owensa5be53f2012-09-09 15:37:57 -07001236 except GitError as e:
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001237 syncbuf.fail(self, e)
1238 return
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001239 self._CopyAndLinkFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001240 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001241
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07001242 if head == revid:
1243 # No changes; don't do anything further.
1244 #
1245 return
1246
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001247 branch = self.GetBranch(branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001248
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001249 if not branch.LocalMerge:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001250 # The current branch has no tracking configuration.
Anatol Pomazau2a32f6a2011-08-30 10:52:33 -07001251 # Jump off it to a detached HEAD.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001252 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001253 syncbuf.info(self,
1254 "leaving %s; does not track upstream",
1255 branch.name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001256 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001257 self._Checkout(revid, quiet=True)
Sarah Owensa5be53f2012-09-09 15:37:57 -07001258 except GitError as e:
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001259 syncbuf.fail(self, e)
1260 return
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001261 self._CopyAndLinkFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001262 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001263
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001264 upstream_gain = self._revlist(not_rev(HEAD), revid)
David Pursehouse8a68ff92012-09-24 12:15:13 +09001265 pub = self.WasPublished(branch.name, all_refs)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001266 if pub:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001267 not_merged = self._revlist(not_rev(revid), pub)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001268 if not_merged:
1269 if upstream_gain:
1270 # The user has published this branch and some of those
1271 # commits are not yet merged upstream. We do not want
1272 # to rewrite the published commits so we punt.
1273 #
Daniel Sandler4c50dee2010-03-02 15:38:03 -05001274 syncbuf.fail(self,
1275 "branch %s is published (but not merged) and is now %d commits behind"
1276 % (branch.name, len(upstream_gain)))
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001277 return
Shawn O. Pearce05f66b62009-04-21 08:26:32 -07001278 elif pub == head:
1279 # All published commits are merged, and thus we are a
1280 # strict subset. We can fast-forward safely.
Shawn O. Pearcea54c5272008-10-30 11:03:00 -07001281 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001282 syncbuf.later1(self, _doff)
1283 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001284
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001285 # Examine the local commits not in the remote. Find the
1286 # last one attributed to this user, if any.
1287 #
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001288 local_changes = self._revlist(not_rev(revid), HEAD, format='%H %ce')
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001289 last_mine = None
1290 cnt_mine = 0
1291 for commit in local_changes:
Chirayu Desai0eb35cb2013-11-19 18:46:29 +05301292 commit_id, committer_email = commit.decode('utf-8').split(' ', 1)
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001293 if committer_email == self.UserEmail:
1294 last_mine = commit_id
1295 cnt_mine += 1
1296
Shawn O. Pearceda88ff42009-06-03 11:09:12 -07001297 if not upstream_gain and cnt_mine == len(local_changes):
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001298 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001299
1300 if self.IsDirty(consider_untracked=False):
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001301 syncbuf.fail(self, _DirtyError())
1302 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001303
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001304 # If the upstream switched on us, warn the user.
1305 #
1306 if branch.merge != self.revisionExpr:
1307 if branch.merge and self.revisionExpr:
1308 syncbuf.info(self,
1309 'manifest switched %s...%s',
1310 branch.merge,
1311 self.revisionExpr)
1312 elif branch.merge:
1313 syncbuf.info(self,
1314 'manifest no longer tracks %s',
1315 branch.merge)
1316
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001317 if cnt_mine < len(local_changes):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001318 # Upstream rebased. Not everything in HEAD
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001319 # was created by this user.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001320 #
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001321 syncbuf.info(self,
1322 "discarding %d commits removed from upstream",
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001323 len(local_changes) - cnt_mine)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001324
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001325 branch.remote = self.GetRemote(self.remote.name)
Anatol Pomazaucd7c5de2012-03-20 13:45:00 -07001326 if not ID_RE.match(self.revisionExpr):
1327 # in case of manifest sync the revisionExpr might be a SHA1
1328 branch.merge = self.revisionExpr
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001329 branch.Save()
1330
Mike Pontillod3153822012-02-28 11:53:24 -08001331 if cnt_mine > 0 and self.rebase:
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001332 def _dorebase():
Anthony King7bdac712014-07-16 12:56:40 +01001333 self._Rebase(upstream='%s^1' % last_mine, onto=revid)
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001334 self._CopyAndLinkFiles()
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001335 syncbuf.later2(self, _dorebase)
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07001336 elif local_changes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001337 try:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001338 self._ResetHard(revid)
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001339 self._CopyAndLinkFiles()
Sarah Owensa5be53f2012-09-09 15:37:57 -07001340 except GitError as e:
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001341 syncbuf.fail(self, e)
1342 return
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001343 else:
Shawn O. Pearce350cde42009-04-16 11:21:18 -07001344 syncbuf.later1(self, _doff)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001345
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -08001346 def AddCopyFile(self, src, dest, absdest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001347 # dest should already be an absolute path, but src is project relative
1348 # make src an absolute path
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -08001349 abssrc = os.path.join(self.worktree, src)
1350 self.copyfiles.append(_CopyFile(src, dest, abssrc, absdest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001351
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001352 def AddLinkFile(self, src, dest, absdest):
1353 # dest should already be an absolute path, but src is project relative
1354 # make src an absolute path
1355 abssrc = os.path.join(self.worktree, src)
1356 self.linkfiles.append(_LinkFile(src, dest, abssrc, absdest))
1357
James W. Mills24c13082012-04-12 15:04:13 -05001358 def AddAnnotation(self, name, value, keep):
1359 self.annotations.append(_Annotation(name, value, keep))
1360
Shawn O. Pearce632768b2008-10-23 11:58:52 -07001361 def DownloadPatchSet(self, change_id, patch_id):
1362 """Download a single patch set of a single change to FETCH_HEAD.
1363 """
1364 remote = self.GetRemote(self.remote.name)
1365
1366 cmd = ['fetch', remote.name]
1367 cmd.append('refs/changes/%2.2d/%d/%d' \
1368 % (change_id % 100, change_id, patch_id))
Shawn O. Pearce632768b2008-10-23 11:58:52 -07001369 if GitCommand(self, cmd, bare=True).Wait() != 0:
1370 return None
1371 return DownloadedChange(self,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001372 self.GetRevisionId(),
Shawn O. Pearce632768b2008-10-23 11:58:52 -07001373 change_id,
1374 patch_id,
1375 self.bare_git.rev_parse('FETCH_HEAD'))
1376
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001377
1378## Branch Management ##
1379
1380 def StartBranch(self, name):
1381 """Create a new branch off the manifest's revision.
1382 """
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001383 head = self.work_git.GetHead()
1384 if head == (R_HEADS + name):
1385 return True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001386
David Pursehouse8a68ff92012-09-24 12:15:13 +09001387 all_refs = self.bare_ref.all
Anthony King7bdac712014-07-16 12:56:40 +01001388 if R_HEADS + name in all_refs:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001389 return GitCommand(self,
Shawn O. Pearce89e717d2009-04-18 15:04:41 -07001390 ['checkout', name, '--'],
Anthony King7bdac712014-07-16 12:56:40 +01001391 capture_stdout=True,
1392 capture_stderr=True).Wait() == 0
Shawn O. Pearce0a389e92009-04-10 16:21:18 -07001393
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001394 branch = self.GetBranch(name)
1395 branch.remote = self.GetRemote(self.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001396 branch.merge = self.revisionExpr
David Pursehouse8a68ff92012-09-24 12:15:13 +09001397 revid = self.GetRevisionId(all_refs)
Shawn O. Pearce0a389e92009-04-10 16:21:18 -07001398
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001399 if head.startswith(R_HEADS):
1400 try:
David Pursehouse8a68ff92012-09-24 12:15:13 +09001401 head = all_refs[head]
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001402 except KeyError:
1403 head = None
1404
1405 if revid and head and revid == head:
1406 ref = os.path.join(self.gitdir, R_HEADS + name)
1407 try:
1408 os.makedirs(os.path.dirname(ref))
1409 except OSError:
1410 pass
1411 _lwrite(ref, '%s\n' % revid)
1412 _lwrite(os.path.join(self.worktree, '.git', HEAD),
1413 'ref: %s%s\n' % (R_HEADS, name))
1414 branch.Save()
1415 return True
1416
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001417 if GitCommand(self,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001418 ['checkout', '-b', branch.name, revid],
Anthony King7bdac712014-07-16 12:56:40 +01001419 capture_stdout=True,
1420 capture_stderr=True).Wait() == 0:
Shawn O. Pearceaccc56d2009-04-18 14:45:51 -07001421 branch.Save()
1422 return True
1423 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001424
Wink Saville02d79452009-04-10 13:01:24 -07001425 def CheckoutBranch(self, name):
1426 """Checkout a local topic branch.
Doug Anderson3ba5f952011-04-07 12:51:04 -07001427
1428 Args:
1429 name: The name of the branch to checkout.
1430
1431 Returns:
1432 True if the checkout succeeded; False if it didn't; None if the branch
1433 didn't exist.
Wink Saville02d79452009-04-10 13:01:24 -07001434 """
Shawn O. Pearce89e717d2009-04-18 15:04:41 -07001435 rev = R_HEADS + name
1436 head = self.work_git.GetHead()
1437 if head == rev:
1438 # Already on the branch
1439 #
1440 return True
Wink Saville02d79452009-04-10 13:01:24 -07001441
David Pursehouse8a68ff92012-09-24 12:15:13 +09001442 all_refs = self.bare_ref.all
Wink Saville02d79452009-04-10 13:01:24 -07001443 try:
David Pursehouse8a68ff92012-09-24 12:15:13 +09001444 revid = all_refs[rev]
Shawn O. Pearce89e717d2009-04-18 15:04:41 -07001445 except KeyError:
1446 # Branch does not exist in this project
1447 #
Doug Anderson3ba5f952011-04-07 12:51:04 -07001448 return None
Shawn O. Pearce89e717d2009-04-18 15:04:41 -07001449
1450 if head.startswith(R_HEADS):
1451 try:
David Pursehouse8a68ff92012-09-24 12:15:13 +09001452 head = all_refs[head]
Shawn O. Pearce89e717d2009-04-18 15:04:41 -07001453 except KeyError:
1454 head = None
1455
1456 if head == revid:
1457 # Same revision; just update HEAD to point to the new
1458 # target branch, but otherwise take no other action.
1459 #
1460 _lwrite(os.path.join(self.worktree, '.git', HEAD),
1461 'ref: %s%s\n' % (R_HEADS, name))
1462 return True
Wink Saville02d79452009-04-10 13:01:24 -07001463
Shawn O. Pearce89e717d2009-04-18 15:04:41 -07001464 return GitCommand(self,
1465 ['checkout', name, '--'],
Anthony King7bdac712014-07-16 12:56:40 +01001466 capture_stdout=True,
1467 capture_stderr=True).Wait() == 0
Wink Saville02d79452009-04-10 13:01:24 -07001468
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -08001469 def AbandonBranch(self, name):
1470 """Destroy a local topic branch.
Doug Andersondafb1d62011-04-07 11:46:59 -07001471
1472 Args:
1473 name: The name of the branch to abandon.
1474
1475 Returns:
1476 True if the abandon succeeded; False if it didn't; None if the branch
1477 didn't exist.
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -08001478 """
Shawn O. Pearce552ac892009-04-18 15:15:24 -07001479 rev = R_HEADS + name
David Pursehouse8a68ff92012-09-24 12:15:13 +09001480 all_refs = self.bare_ref.all
1481 if rev not in all_refs:
Doug Andersondafb1d62011-04-07 11:46:59 -07001482 # Doesn't exist
1483 return None
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -08001484
Shawn O. Pearce552ac892009-04-18 15:15:24 -07001485 head = self.work_git.GetHead()
1486 if head == rev:
1487 # We can't destroy the branch while we are sitting
1488 # on it. Switch to a detached HEAD.
1489 #
David Pursehouse8a68ff92012-09-24 12:15:13 +09001490 head = all_refs[head]
Shawn O. Pearce552ac892009-04-18 15:15:24 -07001491
David Pursehouse8a68ff92012-09-24 12:15:13 +09001492 revid = self.GetRevisionId(all_refs)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001493 if head == revid:
Shawn O. Pearce552ac892009-04-18 15:15:24 -07001494 _lwrite(os.path.join(self.worktree, '.git', HEAD),
1495 '%s\n' % revid)
1496 else:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001497 self._Checkout(revid, quiet=True)
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -08001498
Shawn O. Pearce552ac892009-04-18 15:15:24 -07001499 return GitCommand(self,
1500 ['branch', '-D', name],
Anthony King7bdac712014-07-16 12:56:40 +01001501 capture_stdout=True,
1502 capture_stderr=True).Wait() == 0
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -08001503
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001504 def PruneHeads(self):
1505 """Prune any topic branches already merged into upstream.
1506 """
1507 cb = self.CurrentBranch
1508 kill = []
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -08001509 left = self._allrefs
1510 for name in left.keys():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001511 if name.startswith(R_HEADS):
1512 name = name[len(R_HEADS):]
1513 if cb is None or name != cb:
1514 kill.append(name)
1515
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001516 rev = self.GetRevisionId(left)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001517 if cb is not None \
1518 and not self._revlist(HEAD + '...' + rev) \
Anthony King7bdac712014-07-16 12:56:40 +01001519 and not self.IsDirty(consider_untracked=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001520 self.work_git.DetachHead(HEAD)
1521 kill.append(cb)
1522
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001523 if kill:
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07001524 old = self.bare_git.GetHead()
1525 if old is None:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001526 old = 'refs/heads/please_never_use_this_as_a_branch_name'
1527
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001528 try:
1529 self.bare_git.DetachHead(rev)
1530
1531 b = ['branch', '-d']
1532 b.extend(kill)
1533 b = GitCommand(self, b, bare=True,
1534 capture_stdout=True,
1535 capture_stderr=True)
1536 b.Wait()
1537 finally:
1538 self.bare_git.SetHead(old)
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -08001539 left = self._allrefs
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001540
Shawn O. Pearce3778f9d2009-03-02 12:30:50 -08001541 for branch in kill:
1542 if (R_HEADS + branch) not in left:
1543 self.CleanPublishedCache()
1544 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001545
1546 if cb and cb not in kill:
1547 kill.append(cb)
Shawn O. Pearce7c6c64d2009-03-02 12:38:13 -08001548 kill.sort()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001549
1550 kept = []
1551 for branch in kill:
Anthony King7bdac712014-07-16 12:56:40 +01001552 if R_HEADS + branch in left:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001553 branch = self.GetBranch(branch)
1554 base = branch.LocalMerge
1555 if not base:
1556 base = rev
1557 kept.append(ReviewableBranch(self, branch, base))
1558 return kept
1559
1560
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001561## Submodule Management ##
1562
1563 def GetRegisteredSubprojects(self):
1564 result = []
1565 def rec(subprojects):
1566 if not subprojects:
1567 return
1568 result.extend(subprojects)
1569 for p in subprojects:
1570 rec(p.subprojects)
1571 rec(self.subprojects)
1572 return result
1573
1574 def _GetSubmodules(self):
1575 # Unfortunately we cannot call `git submodule status --recursive` here
1576 # because the working tree might not exist yet, and it cannot be used
1577 # without a working tree in its current implementation.
1578
1579 def get_submodules(gitdir, rev):
1580 # Parse .gitmodules for submodule sub_paths and sub_urls
1581 sub_paths, sub_urls = parse_gitmodules(gitdir, rev)
1582 if not sub_paths:
1583 return []
1584 # Run `git ls-tree` to read SHAs of submodule object, which happen to be
1585 # revision of submodule repository
1586 sub_revs = git_ls_tree(gitdir, rev, sub_paths)
1587 submodules = []
1588 for sub_path, sub_url in zip(sub_paths, sub_urls):
1589 try:
1590 sub_rev = sub_revs[sub_path]
1591 except KeyError:
1592 # Ignore non-exist submodules
1593 continue
1594 submodules.append((sub_rev, sub_path, sub_url))
1595 return submodules
1596
1597 re_path = re.compile(r'^submodule\.([^.]+)\.path=(.*)$')
1598 re_url = re.compile(r'^submodule\.([^.]+)\.url=(.*)$')
1599 def parse_gitmodules(gitdir, rev):
1600 cmd = ['cat-file', 'blob', '%s:.gitmodules' % rev]
1601 try:
Anthony King7bdac712014-07-16 12:56:40 +01001602 p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True,
1603 bare=True, gitdir=gitdir)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001604 except GitError:
1605 return [], []
1606 if p.Wait() != 0:
1607 return [], []
1608
1609 gitmodules_lines = []
1610 fd, temp_gitmodules_path = tempfile.mkstemp()
1611 try:
1612 os.write(fd, p.stdout)
1613 os.close(fd)
1614 cmd = ['config', '--file', temp_gitmodules_path, '--list']
Anthony King7bdac712014-07-16 12:56:40 +01001615 p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True,
1616 bare=True, gitdir=gitdir)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001617 if p.Wait() != 0:
1618 return [], []
1619 gitmodules_lines = p.stdout.split('\n')
1620 except GitError:
1621 return [], []
1622 finally:
1623 os.remove(temp_gitmodules_path)
1624
1625 names = set()
1626 paths = {}
1627 urls = {}
1628 for line in gitmodules_lines:
1629 if not line:
1630 continue
1631 m = re_path.match(line)
1632 if m:
1633 names.add(m.group(1))
1634 paths[m.group(1)] = m.group(2)
1635 continue
1636 m = re_url.match(line)
1637 if m:
1638 names.add(m.group(1))
1639 urls[m.group(1)] = m.group(2)
1640 continue
1641 names = sorted(names)
1642 return ([paths.get(name, '') for name in names],
1643 [urls.get(name, '') for name in names])
1644
1645 def git_ls_tree(gitdir, rev, paths):
1646 cmd = ['ls-tree', rev, '--']
1647 cmd.extend(paths)
1648 try:
Anthony King7bdac712014-07-16 12:56:40 +01001649 p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True,
1650 bare=True, gitdir=gitdir)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001651 except GitError:
1652 return []
1653 if p.Wait() != 0:
1654 return []
1655 objects = {}
1656 for line in p.stdout.split('\n'):
1657 if not line.strip():
1658 continue
1659 object_rev, object_path = line.split()[2:4]
1660 objects[object_path] = object_rev
1661 return objects
1662
1663 try:
1664 rev = self.GetRevisionId()
1665 except GitError:
1666 return []
1667 return get_submodules(self.gitdir, rev)
1668
1669 def GetDerivedSubprojects(self):
1670 result = []
1671 if not self.Exists:
1672 # If git repo does not exist yet, querying its submodules will
1673 # mess up its states; so return here.
1674 return result
1675 for rev, path, url in self._GetSubmodules():
1676 name = self.manifest.GetSubprojectName(self, path)
David James8d201162013-10-11 17:03:19 -07001677 relpath, worktree, gitdir, objdir = \
1678 self.manifest.GetSubprojectPaths(self, name, path)
1679 project = self.manifest.paths.get(relpath)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001680 if project:
1681 result.extend(project.GetDerivedSubprojects())
1682 continue
David James8d201162013-10-11 17:03:19 -07001683
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001684 remote = RemoteSpec(self.remote.name,
Anthony King7bdac712014-07-16 12:56:40 +01001685 url=url,
1686 review=self.remote.review,
1687 revision=self.remote.revision)
1688 subproject = Project(manifest=self.manifest,
1689 name=name,
1690 remote=remote,
1691 gitdir=gitdir,
1692 objdir=objdir,
1693 worktree=worktree,
1694 relpath=relpath,
1695 revisionExpr=self.revisionExpr,
1696 revisionId=rev,
1697 rebase=self.rebase,
1698 groups=self.groups,
1699 sync_c=self.sync_c,
1700 sync_s=self.sync_s,
1701 parent=self,
1702 is_derived=True)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001703 result.append(subproject)
1704 result.extend(subproject.GetDerivedSubprojects())
1705 return result
1706
1707
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001708## Direct Git Commands ##
Chris AtLee2fb64662014-01-16 21:32:33 -05001709 def _CheckForSha1(self):
1710 try:
1711 # if revision (sha or tag) is not present then following function
1712 # throws an error.
1713 self.bare_git.rev_parse('--verify', '%s^0' % self.revisionExpr)
1714 return True
1715 except GitError:
1716 # There is no such persistent revision. We have to fetch it.
1717 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001718
Julien Campergue335f5ef2013-10-16 11:02:35 +02001719 def _FetchArchive(self, tarpath, cwd=None):
1720 cmd = ['archive', '-v', '-o', tarpath]
1721 cmd.append('--remote=%s' % self.remote.url)
1722 cmd.append('--prefix=%s/' % self.relpath)
1723 cmd.append(self.revisionExpr)
1724
1725 command = GitCommand(self, cmd, cwd=cwd,
1726 capture_stdout=True,
1727 capture_stderr=True)
1728
1729 if command.Wait() != 0:
1730 raise GitError('git archive %s: %s' % (self.name, command.stderr))
1731
Conley Owens80b87fe2014-05-09 17:13:44 -07001732
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07001733 def _RemoteFetch(self, name=None,
1734 current_branch_only=False,
Shawn O. Pearce16614f82010-10-29 12:05:43 -07001735 initial=False,
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001736 quiet=False,
Mitchel Humpherys597868b2012-10-29 10:18:34 -07001737 alt_dir=None,
1738 no_tags=False):
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07001739
1740 is_sha1 = False
1741 tag_name = None
David Pursehouse9bc422f2014-04-15 10:28:56 +09001742 depth = None
1743
1744 # The depth should not be used when fetching to a mirror because
1745 # it will result in a shallow repository that cannot be cloned or
1746 # fetched from.
1747 if not self.manifest.IsMirror:
1748 if self.clone_depth:
1749 depth = self.clone_depth
1750 else:
1751 depth = self.manifest.manifestProject.config.GetString('repo.depth')
Conley Owense4978cf2015-02-03 18:06:16 -08001752 # The repo project should never be synced with partial depth
1753 if self.relpath == '.repo/repo':
1754 depth = None
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07001755
Shawn Pearce69e04d82014-01-29 12:48:54 -08001756 if depth:
1757 current_branch_only = True
1758
Nasser Grainawi909d58b2014-09-19 12:13:04 -06001759 if ID_RE.match(self.revisionExpr) is not None:
1760 is_sha1 = True
1761
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07001762 if current_branch_only:
Nasser Grainawi909d58b2014-09-19 12:13:04 -06001763 if self.revisionExpr.startswith(R_TAGS):
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07001764 # this is a tag and its sha1 value should never change
1765 tag_name = self.revisionExpr[len(R_TAGS):]
1766
1767 if is_sha1 or tag_name is not None:
Chris AtLee2fb64662014-01-16 21:32:33 -05001768 if self._CheckForSha1():
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07001769 return True
Bertrand SIMONNET3000cda2014-11-25 16:19:29 -08001770 if is_sha1 and not depth:
1771 # When syncing a specific commit and --depth is not set:
1772 # * if upstream is explicitly specified and is not a sha1, fetch only
1773 # upstream as users expect only upstream to be fetch.
1774 # Note: The commit might not be in upstream in which case the sync
1775 # will fail.
1776 # * otherwise, fetch all branches to make sure we end up with the
1777 # specific commit.
1778 current_branch_only = self.upstream and not ID_RE.match(self.upstream)
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07001779
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001780 if not name:
1781 name = self.remote.name
Shawn O. Pearcefb231612009-04-10 18:53:46 -07001782
1783 ssh_proxy = False
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -07001784 remote = self.GetRemote(name)
1785 if remote.PreConnectFetch():
Shawn O. Pearcefb231612009-04-10 18:53:46 -07001786 ssh_proxy = True
1787
Shawn O. Pearce88443382010-10-08 10:02:09 +02001788 if initial:
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001789 if alt_dir and 'objects' == os.path.basename(alt_dir):
1790 ref_dir = os.path.dirname(alt_dir)
Shawn O. Pearce88443382010-10-08 10:02:09 +02001791 packed_refs = os.path.join(self.gitdir, 'packed-refs')
1792 remote = self.GetRemote(name)
1793
David Pursehouse8a68ff92012-09-24 12:15:13 +09001794 all_refs = self.bare_ref.all
1795 ids = set(all_refs.values())
Shawn O. Pearce88443382010-10-08 10:02:09 +02001796 tmp = set()
1797
Chirayu Desai217ea7d2013-03-01 19:14:38 +05301798 for r, ref_id in GitRefs(ref_dir).all.items():
David Pursehouse8a68ff92012-09-24 12:15:13 +09001799 if r not in all_refs:
Shawn O. Pearce88443382010-10-08 10:02:09 +02001800 if r.startswith(R_TAGS) or remote.WritesTo(r):
David Pursehouse8a68ff92012-09-24 12:15:13 +09001801 all_refs[r] = ref_id
1802 ids.add(ref_id)
Shawn O. Pearce88443382010-10-08 10:02:09 +02001803 continue
1804
David Pursehouse8a68ff92012-09-24 12:15:13 +09001805 if ref_id in ids:
Shawn O. Pearce88443382010-10-08 10:02:09 +02001806 continue
1807
David Pursehouse8a68ff92012-09-24 12:15:13 +09001808 r = 'refs/_alt/%s' % ref_id
1809 all_refs[r] = ref_id
1810 ids.add(ref_id)
Shawn O. Pearce88443382010-10-08 10:02:09 +02001811 tmp.add(r)
1812
Shawn O. Pearce88443382010-10-08 10:02:09 +02001813 tmp_packed = ''
1814 old_packed = ''
1815
Chirayu Desai217ea7d2013-03-01 19:14:38 +05301816 for r in sorted(all_refs):
David Pursehouse8a68ff92012-09-24 12:15:13 +09001817 line = '%s %s\n' % (all_refs[r], r)
Shawn O. Pearce88443382010-10-08 10:02:09 +02001818 tmp_packed += line
1819 if r not in tmp:
1820 old_packed += line
1821
1822 _lwrite(packed_refs, tmp_packed)
Shawn O. Pearce88443382010-10-08 10:02:09 +02001823 else:
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001824 alt_dir = None
Shawn O. Pearce88443382010-10-08 10:02:09 +02001825
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001826 cmd = ['fetch']
Doug Anderson30d45292011-05-04 15:01:04 -07001827
Conley Owensf97e8382015-01-21 11:12:46 -08001828 if depth:
Doug Anderson30d45292011-05-04 15:01:04 -07001829 cmd.append('--depth=%s' % depth)
1830
Shawn O. Pearce16614f82010-10-29 12:05:43 -07001831 if quiet:
1832 cmd.append('--quiet')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001833 if not self.worktree:
1834 cmd.append('--update-head-ok')
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001835 cmd.append(name)
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07001836
Mitchel Humpherys26c45a72014-03-10 14:21:59 -07001837 # If using depth then we should not get all the tags since they may
1838 # be outside of the depth.
1839 if no_tags or depth:
1840 cmd.append('--no-tags')
1841 else:
1842 cmd.append('--tags')
1843
Conley Owens80b87fe2014-05-09 17:13:44 -07001844 spec = []
Brian Harring14a66742012-09-28 20:21:57 -07001845 if not current_branch_only:
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07001846 # Fetch whole repo
Conley Owens80b87fe2014-05-09 17:13:44 -07001847 spec.append(str((u'+refs/heads/*:') + remote.ToLocal('refs/heads/*')))
Anatol Pomazau53d6f4d2011-08-25 17:21:47 -07001848 elif tag_name is not None:
Conley Owens80b87fe2014-05-09 17:13:44 -07001849 spec.append('tag')
1850 spec.append(tag_name)
Nasser Grainawi04e52d62014-09-30 13:34:52 -06001851
1852 branch = self.revisionExpr
Bertrand SIMONNET3000cda2014-11-25 16:19:29 -08001853 if is_sha1 and depth:
1854 # Shallow checkout of a specific commit, fetch from that commit and not
1855 # the heads only as the commit might be deeper in the history.
1856 spec.append(branch)
1857 else:
1858 if is_sha1:
1859 branch = self.upstream
1860 if branch is not None and branch.strip():
1861 if not branch.startswith('refs/'):
1862 branch = R_HEADS + branch
1863 spec.append(str((u'+%s:' % branch) + remote.ToLocal(branch)))
Conley Owens80b87fe2014-05-09 17:13:44 -07001864 cmd.extend(spec)
1865
1866 shallowfetch = self.config.GetString('repo.shallowfetch')
1867 if shallowfetch and shallowfetch != ' '.join(spec):
Anthony King23ff7df2015-03-28 19:42:39 +00001868 GitCommand(self, ['fetch', '--depth=2147483647', name]
1869 + shallowfetch.split(),
Conley Owens80b87fe2014-05-09 17:13:44 -07001870 bare=True, ssh_proxy=ssh_proxy).Wait()
1871 if depth:
Anthony King7bdac712014-07-16 12:56:40 +01001872 self.config.SetString('repo.shallowfetch', ' '.join(spec))
Conley Owens80b87fe2014-05-09 17:13:44 -07001873 else:
Anthony King7bdac712014-07-16 12:56:40 +01001874 self.config.SetString('repo.shallowfetch', None)
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -07001875
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001876 ok = False
David Pursehouse8a68ff92012-09-24 12:15:13 +09001877 for _i in range(2):
John L. Villalovos9c76f672015-03-16 20:49:10 -07001878 gitcmd = GitCommand(self, cmd, bare=True, ssh_proxy=ssh_proxy)
John L. Villalovos126e2982015-01-29 21:58:12 -08001879 ret = gitcmd.Wait()
Brian Harring14a66742012-09-28 20:21:57 -07001880 if ret == 0:
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001881 ok = True
1882 break
John L. Villalovos126e2982015-01-29 21:58:12 -08001883 # If needed, run the 'git remote prune' the first time through the loop
1884 elif (not _i and
1885 "error:" in gitcmd.stderr and
1886 "git remote prune" in gitcmd.stderr):
1887 prunecmd = GitCommand(self, ['remote', 'prune', name], bare=True,
John L. Villalovos9c76f672015-03-16 20:49:10 -07001888 ssh_proxy=ssh_proxy)
John L. Villalovose30f46b2015-02-25 14:27:02 -08001889 ret = prunecmd.Wait()
John L. Villalovose30f46b2015-02-25 14:27:02 -08001890 if ret:
John L. Villalovos126e2982015-01-29 21:58:12 -08001891 break
1892 continue
Brian Harring14a66742012-09-28 20:21:57 -07001893 elif current_branch_only and is_sha1 and ret == 128:
1894 # Exit code 128 means "couldn't find the ref you asked for"; if we're in sha1
1895 # mode, we just tried sync'ing from the upstream field; it doesn't exist, thus
1896 # abort the optimization attempt and do a full sync.
1897 break
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001898 time.sleep(random.randint(30, 45))
Shawn O. Pearce88443382010-10-08 10:02:09 +02001899
1900 if initial:
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001901 if alt_dir:
Shawn O. Pearce88443382010-10-08 10:02:09 +02001902 if old_packed != '':
1903 _lwrite(packed_refs, old_packed)
1904 else:
1905 os.remove(packed_refs)
1906 self.bare_git.pack_refs('--all', '--prune')
Brian Harring14a66742012-09-28 20:21:57 -07001907
1908 if is_sha1 and current_branch_only and self.upstream:
1909 # We just synced the upstream given branch; verify we
1910 # got what we wanted, else trigger a second run of all
1911 # refs.
Chris AtLee2fb64662014-01-16 21:32:33 -05001912 if not self._CheckForSha1():
Brian Harring14a66742012-09-28 20:21:57 -07001913 return self._RemoteFetch(name=name, current_branch_only=False,
1914 initial=False, quiet=quiet, alt_dir=alt_dir)
1915
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001916 return ok
1917
1918 def _ApplyCloneBundle(self, initial=False, quiet=False):
David Pursehouseede7f122012-11-27 22:25:30 +09001919 if initial and (self.manifest.manifestProject.config.GetString('repo.depth') or self.clone_depth):
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001920 return False
1921
1922 remote = self.GetRemote(self.remote.name)
1923 bundle_url = remote.url + '/clone.bundle'
1924 bundle_url = GitConfig.ForUser().UrlInsteadOf(bundle_url)
Dave Borowitz74c1f3d2013-06-03 15:05:07 -07001925 if GetSchemeFromUrl(bundle_url) not in (
1926 'http', 'https', 'persistent-http', 'persistent-https'):
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001927 return False
1928
1929 bundle_dst = os.path.join(self.gitdir, 'clone.bundle')
1930 bundle_tmp = os.path.join(self.gitdir, 'clone.bundle.tmp')
Shawn O. Pearce88443382010-10-08 10:02:09 +02001931
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001932 exist_dst = os.path.exists(bundle_dst)
1933 exist_tmp = os.path.exists(bundle_tmp)
1934
1935 if not initial and not exist_dst and not exist_tmp:
1936 return False
1937
1938 if not exist_dst:
1939 exist_dst = self._FetchBundle(bundle_url, bundle_tmp, bundle_dst, quiet)
1940 if not exist_dst:
1941 return False
1942
1943 cmd = ['fetch']
1944 if quiet:
1945 cmd.append('--quiet')
1946 if not self.worktree:
1947 cmd.append('--update-head-ok')
1948 cmd.append(bundle_dst)
1949 for f in remote.fetch:
1950 cmd.append(str(f))
1951 cmd.append('refs/tags/*:refs/tags/*')
1952
1953 ok = GitCommand(self, cmd, bare=True).Wait() == 0
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -07001954 if os.path.exists(bundle_dst):
1955 os.remove(bundle_dst)
1956 if os.path.exists(bundle_tmp):
1957 os.remove(bundle_tmp)
Shawn O. Pearce88443382010-10-08 10:02:09 +02001958 return ok
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001959
Shawn O. Pearcec325dc32011-10-03 08:30:24 -07001960 def _FetchBundle(self, srcUrl, tmpPath, dstPath, quiet):
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -07001961 if os.path.exists(dstPath):
1962 os.remove(dstPath)
Shawn O. Pearce29472462011-10-11 09:24:07 -07001963
Matt Gumbel2dc810c2012-08-30 09:39:36 -07001964 cmd = ['curl', '--fail', '--output', tmpPath, '--netrc', '--location']
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -07001965 if quiet:
1966 cmd += ['--silent']
1967 if os.path.exists(tmpPath):
1968 size = os.stat(tmpPath).st_size
1969 if size >= 1024:
1970 cmd += ['--continue-at', '%d' % (size,)]
1971 else:
1972 os.remove(tmpPath)
1973 if 'http_proxy' in os.environ and 'darwin' == sys.platform:
1974 cmd += ['--proxy', os.environ['http_proxy']]
Dave Borowitz497bde42015-01-02 13:58:05 -08001975 with self._GetBundleCookieFile(srcUrl, quiet) as cookiefile:
Dave Borowitz137d0132015-01-02 11:12:54 -08001976 if cookiefile:
Dave Borowitz4abf8e62015-01-02 11:39:04 -08001977 cmd += ['--cookie', cookiefile, '--cookie-jar', cookiefile]
Dave Borowitz137d0132015-01-02 11:12:54 -08001978 if srcUrl.startswith('persistent-'):
1979 srcUrl = srcUrl[len('persistent-'):]
1980 cmd += [srcUrl]
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -07001981
Dave Borowitz137d0132015-01-02 11:12:54 -08001982 if IsTrace():
1983 Trace('%s', ' '.join(cmd))
1984 try:
1985 proc = subprocess.Popen(cmd)
1986 except OSError:
1987 return False
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -07001988
Dave Borowitz137d0132015-01-02 11:12:54 -08001989 curlret = proc.wait()
Matt Gumbel2dc810c2012-08-30 09:39:36 -07001990
Dave Borowitz137d0132015-01-02 11:12:54 -08001991 if curlret == 22:
1992 # From curl man page:
1993 # 22: HTTP page not retrieved. The requested url was not found or
1994 # returned another error with the HTTP error code being 400 or above.
1995 # This return code only appears if -f, --fail is used.
1996 if not quiet:
1997 print("Server does not provide clone.bundle; ignoring.",
1998 file=sys.stderr)
1999 return False
Matt Gumbel2dc810c2012-08-30 09:39:36 -07002000
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -07002001 if os.path.exists(tmpPath):
Kris Giesingc8d882a2014-12-23 13:02:32 -08002002 if curlret == 0 and self._IsValidBundle(tmpPath, quiet):
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -07002003 os.rename(tmpPath, dstPath)
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -07002004 return True
2005 else:
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -07002006 os.remove(tmpPath)
Shawn O. Pearce5e7127d2012-08-02 14:57:37 -07002007 return False
2008 else:
2009 return False
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -07002010
Kris Giesingc8d882a2014-12-23 13:02:32 -08002011 def _IsValidBundle(self, path, quiet):
Dave Borowitz91f3ba52013-06-03 12:15:23 -07002012 try:
2013 with open(path) as f:
2014 if f.read(16) == '# v2 git bundle\n':
2015 return True
2016 else:
Kris Giesingc8d882a2014-12-23 13:02:32 -08002017 if not quiet:
2018 print("Invalid clone.bundle file; ignoring.", file=sys.stderr)
Dave Borowitz91f3ba52013-06-03 12:15:23 -07002019 return False
2020 except OSError:
2021 return False
2022
Dave Borowitz137d0132015-01-02 11:12:54 -08002023 @contextlib.contextmanager
Dave Borowitz497bde42015-01-02 13:58:05 -08002024 def _GetBundleCookieFile(self, url, quiet):
Dave Borowitz74c1f3d2013-06-03 15:05:07 -07002025 if url.startswith('persistent-'):
2026 try:
2027 p = subprocess.Popen(
2028 ['git-remote-persistent-https', '-print_config', url],
2029 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2030 stderr=subprocess.PIPE)
Dave Borowitz137d0132015-01-02 11:12:54 -08002031 try:
2032 prefix = 'http.cookiefile='
2033 cookiefile = None
2034 for line in p.stdout:
2035 line = line.strip()
2036 if line.startswith(prefix):
2037 cookiefile = line[len(prefix):]
2038 break
2039 # Leave subprocess open, as cookie file may be transient.
2040 if cookiefile:
2041 yield cookiefile
2042 return
2043 finally:
2044 p.stdin.close()
2045 if p.wait():
2046 err_msg = p.stderr.read()
2047 if ' -print_config' in err_msg:
2048 pass # Persistent proxy doesn't support -print_config.
Dave Borowitz497bde42015-01-02 13:58:05 -08002049 elif not quiet:
Dave Borowitz137d0132015-01-02 11:12:54 -08002050 print(err_msg, file=sys.stderr)
Dave Borowitz74c1f3d2013-06-03 15:05:07 -07002051 except OSError as e:
2052 if e.errno == errno.ENOENT:
2053 pass # No persistent proxy.
2054 raise
Dave Borowitz137d0132015-01-02 11:12:54 -08002055 yield GitConfig.ForUser().GetString('http.cookiefile')
Dave Borowitz74c1f3d2013-06-03 15:05:07 -07002056
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002057 def _Checkout(self, rev, quiet=False):
2058 cmd = ['checkout']
2059 if quiet:
2060 cmd.append('-q')
2061 cmd.append(rev)
2062 cmd.append('--')
2063 if GitCommand(self, cmd).Wait() != 0:
2064 if self._allrefs:
2065 raise GitError('%s checkout %s ' % (self.name, rev))
2066
Anthony King7bdac712014-07-16 12:56:40 +01002067 def _CherryPick(self, rev):
Pierre Tardye5a21222011-03-24 16:28:18 +01002068 cmd = ['cherry-pick']
2069 cmd.append(rev)
2070 cmd.append('--')
2071 if GitCommand(self, cmd).Wait() != 0:
2072 if self._allrefs:
2073 raise GitError('%s cherry-pick %s ' % (self.name, rev))
2074
Anthony King7bdac712014-07-16 12:56:40 +01002075 def _Revert(self, rev):
Erwan Mahea94f1622011-08-19 13:56:09 +02002076 cmd = ['revert']
2077 cmd.append('--no-edit')
2078 cmd.append(rev)
2079 cmd.append('--')
2080 if GitCommand(self, cmd).Wait() != 0:
2081 if self._allrefs:
2082 raise GitError('%s revert %s ' % (self.name, rev))
2083
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002084 def _ResetHard(self, rev, quiet=True):
2085 cmd = ['reset', '--hard']
2086 if quiet:
2087 cmd.append('-q')
2088 cmd.append(rev)
2089 if GitCommand(self, cmd).Wait() != 0:
2090 raise GitError('%s reset --hard %s ' % (self.name, rev))
2091
Anthony King7bdac712014-07-16 12:56:40 +01002092 def _Rebase(self, upstream, onto=None):
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07002093 cmd = ['rebase']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002094 if onto is not None:
2095 cmd.extend(['--onto', onto])
2096 cmd.append(upstream)
Shawn O. Pearce19a83d82009-04-16 08:14:26 -07002097 if GitCommand(self, cmd).Wait() != 0:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002098 raise GitError('%s rebase %s ' % (self.name, upstream))
2099
Pierre Tardy3d125942012-05-04 12:18:12 +02002100 def _FastForward(self, head, ffonly=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002101 cmd = ['merge', head]
Pierre Tardy3d125942012-05-04 12:18:12 +02002102 if ffonly:
2103 cmd.append("--ff-only")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002104 if GitCommand(self, cmd).Wait() != 0:
2105 raise GitError('%s merge %s ' % (self.name, head))
2106
Jonathan Nieder93719792015-03-17 11:29:58 -07002107 def _InitGitDir(self, mirror_git=None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002108 if not os.path.exists(self.gitdir):
David James8d201162013-10-11 17:03:19 -07002109
2110 # Initialize the bare repository, which contains all of the objects.
2111 if not os.path.exists(self.objdir):
2112 os.makedirs(self.objdir)
2113 self.bare_objdir.init()
2114
2115 # If we have a separate directory to hold refs, initialize it as well.
2116 if self.objdir != self.gitdir:
2117 os.makedirs(self.gitdir)
2118 self._ReferenceGitDir(self.objdir, self.gitdir, share_refs=False,
2119 copy_all=True)
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -08002120
Shawn O. Pearce88443382010-10-08 10:02:09 +02002121 mp = self.manifest.manifestProject
Victor Boivie2b30e3a2012-10-05 12:37:58 +02002122 ref_dir = mp.config.GetString('repo.reference') or ''
Shawn O. Pearce88443382010-10-08 10:02:09 +02002123
Victor Boivie2b30e3a2012-10-05 12:37:58 +02002124 if ref_dir or mirror_git:
2125 if not mirror_git:
2126 mirror_git = os.path.join(ref_dir, self.name + '.git')
Shawn O. Pearce88443382010-10-08 10:02:09 +02002127 repo_git = os.path.join(ref_dir, '.repo', 'projects',
2128 self.relpath + '.git')
2129
2130 if os.path.exists(mirror_git):
2131 ref_dir = mirror_git
2132
2133 elif os.path.exists(repo_git):
2134 ref_dir = repo_git
2135
2136 else:
2137 ref_dir = None
2138
2139 if ref_dir:
2140 _lwrite(os.path.join(self.gitdir, 'objects/info/alternates'),
2141 os.path.join(ref_dir, 'objects') + '\n')
2142
Jimmie Westera0444582012-10-24 13:44:42 +02002143 self._UpdateHooks()
2144
2145 m = self.manifest.manifestProject.config
2146 for key in ['user.name', 'user.email']:
Anthony King7bdac712014-07-16 12:56:40 +01002147 if m.Has(key, include_defaults=False):
Jimmie Westera0444582012-10-24 13:44:42 +02002148 self.config.SetString(key, m.GetString(key))
Jonathan Nieder93719792015-03-17 11:29:58 -07002149 if self.manifest.IsMirror:
Shawn O. Pearce2816d4f2009-03-03 17:53:18 -08002150 self.config.SetString('core.bare', 'true')
2151 else:
2152 self.config.SetString('core.bare', None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002153
Jimmie Westera0444582012-10-24 13:44:42 +02002154 def _UpdateHooks(self):
2155 if os.path.exists(self.gitdir):
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08002156 self._InitHooks()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002157
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08002158 def _InitHooks(self):
Jesse Hall672cc492013-11-27 11:17:13 -08002159 hooks = os.path.realpath(self._gitdir_path('hooks'))
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08002160 if not os.path.exists(hooks):
2161 os.makedirs(hooks)
Jonathan Nieder93719792015-03-17 11:29:58 -07002162 for stock_hook in _ProjectHooks():
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07002163 name = os.path.basename(stock_hook)
2164
Victor Boivie65e0f352011-04-18 11:23:29 +02002165 if name in ('commit-msg',) and not self.remote.review \
2166 and not self is self.manifest.manifestProject:
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07002167 # Don't install a Gerrit Code Review hook if this
2168 # project does not appear to use it for reviews.
2169 #
Victor Boivie65e0f352011-04-18 11:23:29 +02002170 # Since the manifest project is one of those, but also
2171 # managed through gerrit, it's excluded
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07002172 continue
2173
2174 dst = os.path.join(hooks, name)
2175 if os.path.islink(dst):
2176 continue
2177 if os.path.exists(dst):
2178 if filecmp.cmp(stock_hook, dst, shallow=False):
2179 os.remove(dst)
2180 else:
2181 _error("%s: Not replacing %s hook", self.relpath, name)
2182 continue
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08002183 try:
Mickaël Salaünb9477bc2012-08-05 13:39:26 +02002184 os.symlink(os.path.relpath(stock_hook, os.path.dirname(dst)), dst)
Sarah Owensa5be53f2012-09-09 15:37:57 -07002185 except OSError as e:
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07002186 if e.errno == errno.EPERM:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -08002187 raise GitError('filesystem must support symlinks')
2188 else:
2189 raise
2190
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002191 def _InitRemote(self):
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07002192 if self.remote.url:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002193 remote = self.GetRemote(self.remote.name)
Shawn O. Pearced1f70d92009-05-19 14:58:02 -07002194 remote.url = self.remote.url
2195 remote.review = self.remote.review
2196 remote.projectname = self.name
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002197
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08002198 if self.worktree:
2199 remote.ResetFetch(mirror=False)
2200 else:
2201 remote.ResetFetch(mirror=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002202 remote.Save()
2203
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002204 def _InitMRef(self):
2205 if self.manifest.branch:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07002206 self._InitAnyMRef(R_M + self.manifest.branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002207
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08002208 def _InitMirrorHead(self):
Shawn O. Pearcefe200ee2009-06-01 15:28:21 -07002209 self._InitAnyMRef(HEAD)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07002210
2211 def _InitAnyMRef(self, ref):
2212 cur = self.bare_ref.symref(ref)
2213
2214 if self.revisionId:
2215 if cur != '' or self.bare_ref.get(ref) != self.revisionId:
2216 msg = 'manifest set to %s' % self.revisionId
2217 dst = self.revisionId + '^0'
Anthony King7bdac712014-07-16 12:56:40 +01002218 self.bare_git.UpdateRef(ref, dst, message=msg, detach=True)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07002219 else:
2220 remote = self.GetRemote(self.remote.name)
2221 dst = remote.ToLocal(self.revisionExpr)
2222 if cur != dst:
2223 msg = 'manifest set to %s' % self.revisionExpr
2224 self.bare_git.symbolic_ref('-m', msg, ref, dst)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08002225
David James8d201162013-10-11 17:03:19 -07002226 def _ReferenceGitDir(self, gitdir, dotgit, share_refs, copy_all):
2227 """Update |dotgit| to reference |gitdir|, using symlinks where possible.
2228
2229 Args:
2230 gitdir: The bare git repository. Must already be initialized.
2231 dotgit: The repository you would like to initialize.
2232 share_refs: If true, |dotgit| will store its refs under |gitdir|.
2233 Only one work tree can store refs under a given |gitdir|.
2234 copy_all: If true, copy all remaining files from |gitdir| -> |dotgit|.
2235 This saves you the effort of initializing |dotgit| yourself.
2236 """
2237 # These objects can be shared between several working trees.
2238 symlink_files = ['description', 'info']
2239 symlink_dirs = ['hooks', 'objects', 'rr-cache', 'svn']
2240 if share_refs:
2241 # These objects can only be used by a single working tree.
Conley Owensf2af7562014-04-30 11:31:01 -07002242 symlink_files += ['config', 'packed-refs', 'shallow']
David James8d201162013-10-11 17:03:19 -07002243 symlink_dirs += ['logs', 'refs']
2244 to_symlink = symlink_files + symlink_dirs
2245
2246 to_copy = []
2247 if copy_all:
2248 to_copy = os.listdir(gitdir)
2249
2250 for name in set(to_copy).union(to_symlink):
2251 try:
2252 src = os.path.realpath(os.path.join(gitdir, name))
2253 dst = os.path.realpath(os.path.join(dotgit, name))
2254
2255 if os.path.lexists(dst) and not os.path.islink(dst):
2256 raise GitError('cannot overwrite a local work tree')
2257
2258 # If the source dir doesn't exist, create an empty dir.
2259 if name in symlink_dirs and not os.path.lexists(src):
2260 os.makedirs(src)
2261
Conley Owens80b87fe2014-05-09 17:13:44 -07002262 # If the source file doesn't exist, ensure the destination
2263 # file doesn't either.
2264 if name in symlink_files and not os.path.lexists(src):
2265 try:
2266 os.remove(dst)
2267 except OSError:
2268 pass
2269
David James8d201162013-10-11 17:03:19 -07002270 if name in to_symlink:
2271 os.symlink(os.path.relpath(src, os.path.dirname(dst)), dst)
2272 elif copy_all and not os.path.islink(dst):
2273 if os.path.isdir(src):
2274 shutil.copytree(src, dst)
2275 elif os.path.isfile(src):
2276 shutil.copy(src, dst)
2277 except OSError as e:
2278 if e.errno == errno.EPERM:
2279 raise GitError('filesystem must support symlinks')
2280 else:
2281 raise
2282
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002283 def _InitWorkTree(self):
2284 dotgit = os.path.join(self.worktree, '.git')
2285 if not os.path.exists(dotgit):
2286 os.makedirs(dotgit)
David James8d201162013-10-11 17:03:19 -07002287 self._ReferenceGitDir(self.gitdir, dotgit, share_refs=True,
2288 copy_all=False)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002289
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07002290 _lwrite(os.path.join(dotgit, HEAD), '%s\n' % self.GetRevisionId())
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002291
2292 cmd = ['read-tree', '--reset', '-u']
2293 cmd.append('-v')
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07002294 cmd.append(HEAD)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002295 if GitCommand(self, cmd).Wait() != 0:
2296 raise GitError("cannot initialize work tree")
Victor Boivie0960b5b2010-11-26 13:42:13 +01002297
Jeff Hamiltone0df2322014-04-21 17:10:59 -05002298 self._CopyAndLinkFiles()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002299
2300 def _gitdir_path(self, path):
David James8d201162013-10-11 17:03:19 -07002301 return os.path.realpath(os.path.join(self.gitdir, path))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002302
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07002303 def _revlist(self, *args, **kw):
2304 a = []
2305 a.extend(args)
2306 a.append('--')
2307 return self.work_git.rev_list(*a, **kw)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002308
2309 @property
2310 def _allrefs(self):
Shawn O. Pearced237b692009-04-17 18:49:50 -07002311 return self.bare_ref.all
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002312
Julien Camperguedd654222014-01-09 16:21:37 +01002313 def _getLogs(self, rev1, rev2, oneline=False, color=True):
2314 """Get logs between two revisions of this project."""
2315 comp = '..'
2316 if rev1:
2317 revs = [rev1]
2318 if rev2:
2319 revs.extend([comp, rev2])
2320 cmd = ['log', ''.join(revs)]
2321 out = DiffColoring(self.config)
2322 if out.is_on and color:
2323 cmd.append('--color')
2324 if oneline:
2325 cmd.append('--oneline')
2326
2327 try:
2328 log = GitCommand(self, cmd, capture_stdout=True, capture_stderr=True)
2329 if log.Wait() == 0:
2330 return log.stdout
2331 except GitError:
2332 # worktree may not exist if groups changed for example. In that case,
2333 # try in gitdir instead.
2334 if not os.path.exists(self.worktree):
2335 return self.bare_git.log(*cmd[1:])
2336 else:
2337 raise
2338 return None
2339
2340 def getAddedAndRemovedLogs(self, toProject, oneline=False, color=True):
2341 """Get the list of logs from this revision to given revisionId"""
2342 logs = {}
2343 selfId = self.GetRevisionId(self._allrefs)
2344 toId = toProject.GetRevisionId(toProject._allrefs)
2345
2346 logs['added'] = self._getLogs(selfId, toId, oneline=oneline, color=color)
2347 logs['removed'] = self._getLogs(toId, selfId, oneline=oneline, color=color)
2348 return logs
2349
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002350 class _GitGetByExec(object):
David James8d201162013-10-11 17:03:19 -07002351 def __init__(self, project, bare, gitdir):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002352 self._project = project
2353 self._bare = bare
David James8d201162013-10-11 17:03:19 -07002354 self._gitdir = gitdir
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002355
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002356 def LsOthers(self):
2357 p = GitCommand(self._project,
2358 ['ls-files',
2359 '-z',
2360 '--others',
2361 '--exclude-standard'],
Anthony King7bdac712014-07-16 12:56:40 +01002362 bare=False,
David James8d201162013-10-11 17:03:19 -07002363 gitdir=self._gitdir,
Anthony King7bdac712014-07-16 12:56:40 +01002364 capture_stdout=True,
2365 capture_stderr=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002366 if p.Wait() == 0:
2367 out = p.stdout
2368 if out:
David Pursehouse1d947b32012-10-25 12:23:11 +09002369 return out[:-1].split('\0') # pylint: disable=W1401
2370 # Backslash is not anomalous
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002371 return []
2372
2373 def DiffZ(self, name, *args):
2374 cmd = [name]
2375 cmd.append('-z')
2376 cmd.extend(args)
2377 p = GitCommand(self._project,
2378 cmd,
David James8d201162013-10-11 17:03:19 -07002379 gitdir=self._gitdir,
Anthony King7bdac712014-07-16 12:56:40 +01002380 bare=False,
2381 capture_stdout=True,
2382 capture_stderr=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002383 try:
2384 out = p.process.stdout.read()
2385 r = {}
2386 if out:
David Pursehouse1d947b32012-10-25 12:23:11 +09002387 out = iter(out[:-1].split('\0')) # pylint: disable=W1401
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002388 while out:
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07002389 try:
Anthony King2cd1f042014-05-05 21:24:05 +01002390 info = next(out)
2391 path = next(out)
Shawn O. Pearce02dbb6d2008-10-21 13:59:08 -07002392 except StopIteration:
2393 break
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002394
2395 class _Info(object):
2396 def __init__(self, path, omode, nmode, oid, nid, state):
2397 self.path = path
2398 self.src_path = None
2399 self.old_mode = omode
2400 self.new_mode = nmode
2401 self.old_id = oid
2402 self.new_id = nid
2403
2404 if len(state) == 1:
2405 self.status = state
2406 self.level = None
2407 else:
2408 self.status = state[:1]
2409 self.level = state[1:]
2410 while self.level.startswith('0'):
2411 self.level = self.level[1:]
2412
2413 info = info[1:].split(' ')
David Pursehouse8f62fb72012-11-14 12:09:38 +09002414 info = _Info(path, *info)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002415 if info.status in ('R', 'C'):
2416 info.src_path = info.path
Anthony King2cd1f042014-05-05 21:24:05 +01002417 info.path = next(out)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002418 r[info.path] = info
2419 return r
2420 finally:
2421 p.Wait()
2422
2423 def GetHead(self):
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07002424 if self._bare:
2425 path = os.path.join(self._project.gitdir, HEAD)
2426 else:
2427 path = os.path.join(self._project.worktree, '.git', HEAD)
Conley Owens75ee0572012-11-15 17:33:11 -08002428 try:
2429 fd = open(path, 'rb')
Dan Sandler53e902a2014-03-09 13:20:02 -04002430 except IOError as e:
2431 raise NoManifestException(path, str(e))
Shawn O. Pearce76ca9f82009-04-18 14:48:03 -07002432 try:
2433 line = fd.read()
2434 finally:
2435 fd.close()
Chirayu Desai217ea7d2013-03-01 19:14:38 +05302436 try:
2437 line = line.decode()
2438 except AttributeError:
2439 pass
Shawn O. Pearce5b23f242009-04-17 18:43:33 -07002440 if line.startswith('ref: '):
2441 return line[5:-1]
2442 return line[:-1]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002443
2444 def SetHead(self, ref, message=None):
2445 cmdv = []
2446 if message is not None:
2447 cmdv.extend(['-m', message])
2448 cmdv.append(HEAD)
2449 cmdv.append(ref)
2450 self.symbolic_ref(*cmdv)
2451
2452 def DetachHead(self, new, message=None):
2453 cmdv = ['--no-deref']
2454 if message is not None:
2455 cmdv.extend(['-m', message])
2456 cmdv.append(HEAD)
2457 cmdv.append(new)
2458 self.update_ref(*cmdv)
2459
2460 def UpdateRef(self, name, new, old=None,
2461 message=None,
2462 detach=False):
2463 cmdv = []
2464 if message is not None:
2465 cmdv.extend(['-m', message])
2466 if detach:
2467 cmdv.append('--no-deref')
2468 cmdv.append(name)
2469 cmdv.append(new)
2470 if old is not None:
2471 cmdv.append(old)
2472 self.update_ref(*cmdv)
2473
2474 def DeleteRef(self, name, old=None):
2475 if not old:
2476 old = self.rev_parse(name)
2477 self.update_ref('-d', name, old)
Shawn O. Pearcefbcde472009-04-17 20:58:02 -07002478 self._project.bare_ref.deleted(name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002479
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07002480 def rev_list(self, *args, **kw):
2481 if 'format' in kw:
2482 cmdv = ['log', '--pretty=format:%s' % kw['format']]
2483 else:
2484 cmdv = ['rev-list']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002485 cmdv.extend(args)
2486 p = GitCommand(self._project,
2487 cmdv,
Anthony King7bdac712014-07-16 12:56:40 +01002488 bare=self._bare,
David James8d201162013-10-11 17:03:19 -07002489 gitdir=self._gitdir,
Anthony King7bdac712014-07-16 12:56:40 +01002490 capture_stdout=True,
2491 capture_stderr=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002492 r = []
2493 for line in p.process.stdout:
Shawn O. Pearce8ad8a0e2009-05-29 18:28:25 -07002494 if line[-1] == '\n':
2495 line = line[:-1]
2496 r.append(line)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002497 if p.Wait() != 0:
2498 raise GitError('%s rev-list %s: %s' % (
2499 self._project.name,
2500 str(args),
2501 p.stderr))
2502 return r
2503
2504 def __getattr__(self, name):
Doug Anderson37282b42011-03-04 11:54:18 -08002505 """Allow arbitrary git commands using pythonic syntax.
2506
2507 This allows you to do things like:
2508 git_obj.rev_parse('HEAD')
2509
2510 Since we don't have a 'rev_parse' method defined, the __getattr__ will
2511 run. We'll replace the '_' with a '-' and try to run a git command.
Dave Borowitz091f8932012-10-23 17:01:04 -07002512 Any other positional arguments will be passed to the git command, and the
2513 following keyword arguments are supported:
2514 config: An optional dict of git config options to be passed with '-c'.
Doug Anderson37282b42011-03-04 11:54:18 -08002515
2516 Args:
2517 name: The name of the git command to call. Any '_' characters will
2518 be replaced with '-'.
2519
2520 Returns:
2521 A callable object that will try to call git with the named command.
2522 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002523 name = name.replace('_', '-')
Dave Borowitz091f8932012-10-23 17:01:04 -07002524 def runner(*args, **kwargs):
2525 cmdv = []
2526 config = kwargs.pop('config', None)
2527 for k in kwargs:
2528 raise TypeError('%s() got an unexpected keyword argument %r'
2529 % (name, k))
2530 if config is not None:
Dave Borowitzb42b4742012-10-31 12:27:27 -07002531 if not git_require((1, 7, 2)):
2532 raise ValueError('cannot set config on command line for %s()'
2533 % name)
Chirayu Desai217ea7d2013-03-01 19:14:38 +05302534 for k, v in config.items():
Dave Borowitz091f8932012-10-23 17:01:04 -07002535 cmdv.append('-c')
2536 cmdv.append('%s=%s' % (k, v))
2537 cmdv.append(name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002538 cmdv.extend(args)
2539 p = GitCommand(self._project,
2540 cmdv,
Anthony King7bdac712014-07-16 12:56:40 +01002541 bare=self._bare,
David James8d201162013-10-11 17:03:19 -07002542 gitdir=self._gitdir,
Anthony King7bdac712014-07-16 12:56:40 +01002543 capture_stdout=True,
2544 capture_stderr=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002545 if p.Wait() != 0:
2546 raise GitError('%s %s: %s' % (
2547 self._project.name,
2548 name,
2549 p.stderr))
2550 r = p.stdout
Chirayu Desai217ea7d2013-03-01 19:14:38 +05302551 try:
Conley Owensedd01512013-09-26 12:59:58 -07002552 r = r.decode('utf-8')
Chirayu Desai217ea7d2013-03-01 19:14:38 +05302553 except AttributeError:
2554 pass
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002555 if r.endswith('\n') and r.index('\n') == len(r) - 1:
2556 return r[:-1]
2557 return r
2558 return runner
2559
2560
Shawn O. Pearce350cde42009-04-16 11:21:18 -07002561class _PriorSyncFailedError(Exception):
2562 def __str__(self):
2563 return 'prior sync failed; rebase still in progress'
2564
2565class _DirtyError(Exception):
2566 def __str__(self):
2567 return 'contains uncommitted changes'
2568
2569class _InfoMessage(object):
2570 def __init__(self, project, text):
2571 self.project = project
2572 self.text = text
2573
2574 def Print(self, syncbuf):
2575 syncbuf.out.info('%s/: %s', self.project.relpath, self.text)
2576 syncbuf.out.nl()
2577
2578class _Failure(object):
2579 def __init__(self, project, why):
2580 self.project = project
2581 self.why = why
2582
2583 def Print(self, syncbuf):
2584 syncbuf.out.fail('error: %s/: %s',
2585 self.project.relpath,
2586 str(self.why))
2587 syncbuf.out.nl()
2588
2589class _Later(object):
2590 def __init__(self, project, action):
2591 self.project = project
2592 self.action = action
2593
2594 def Run(self, syncbuf):
2595 out = syncbuf.out
2596 out.project('project %s/', self.project.relpath)
2597 out.nl()
2598 try:
2599 self.action()
2600 out.nl()
2601 return True
David Pursehouse8a68ff92012-09-24 12:15:13 +09002602 except GitError:
Shawn O. Pearce350cde42009-04-16 11:21:18 -07002603 out.nl()
2604 return False
2605
2606class _SyncColoring(Coloring):
2607 def __init__(self, config):
2608 Coloring.__init__(self, config, 'reposync')
Anthony King7bdac712014-07-16 12:56:40 +01002609 self.project = self.printer('header', attr='bold')
2610 self.info = self.printer('info')
2611 self.fail = self.printer('fail', fg='red')
Shawn O. Pearce350cde42009-04-16 11:21:18 -07002612
2613class SyncBuffer(object):
2614 def __init__(self, config, detach_head=False):
2615 self._messages = []
2616 self._failures = []
2617 self._later_queue1 = []
2618 self._later_queue2 = []
2619
2620 self.out = _SyncColoring(config)
2621 self.out.redirect(sys.stderr)
2622
2623 self.detach_head = detach_head
2624 self.clean = True
2625
2626 def info(self, project, fmt, *args):
2627 self._messages.append(_InfoMessage(project, fmt % args))
2628
2629 def fail(self, project, err=None):
2630 self._failures.append(_Failure(project, err))
2631 self.clean = False
2632
2633 def later1(self, project, what):
2634 self._later_queue1.append(_Later(project, what))
2635
2636 def later2(self, project, what):
2637 self._later_queue2.append(_Later(project, what))
2638
2639 def Finish(self):
2640 self._PrintMessages()
2641 self._RunLater()
2642 self._PrintMessages()
2643 return self.clean
2644
2645 def _RunLater(self):
2646 for q in ['_later_queue1', '_later_queue2']:
2647 if not self._RunQueue(q):
2648 return
2649
2650 def _RunQueue(self, queue):
2651 for m in getattr(self, queue):
2652 if not m.Run(self):
2653 self.clean = False
2654 return False
2655 setattr(self, queue, [])
2656 return True
2657
2658 def _PrintMessages(self):
2659 for m in self._messages:
2660 m.Print(self)
2661 for m in self._failures:
2662 m.Print(self)
2663
2664 self._messages = []
2665 self._failures = []
2666
2667
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002668class MetaProject(Project):
2669 """A special project housed under .repo.
2670 """
2671 def __init__(self, manifest, name, gitdir, worktree):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002672 Project.__init__(self,
Anthony King7bdac712014-07-16 12:56:40 +01002673 manifest=manifest,
2674 name=name,
2675 gitdir=gitdir,
2676 objdir=gitdir,
2677 worktree=worktree,
2678 remote=RemoteSpec('origin'),
2679 relpath='.repo/%s' % name,
2680 revisionExpr='refs/heads/master',
2681 revisionId=None,
2682 groups=None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002683
2684 def PreSync(self):
2685 if self.Exists:
2686 cb = self.CurrentBranch
2687 if cb:
2688 base = self.GetBranch(cb).merge
2689 if base:
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07002690 self.revisionExpr = base
2691 self.revisionId = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002692
Anthony King7bdac712014-07-16 12:56:40 +01002693 def MetaBranchSwitch(self):
Florian Vallee5d016502012-06-07 17:19:26 +02002694 """ Prepare MetaProject for manifest branch switch
2695 """
2696
2697 # detach and delete manifest branch, allowing a new
2698 # branch to take over
Anthony King7bdac712014-07-16 12:56:40 +01002699 syncbuf = SyncBuffer(self.config, detach_head=True)
Florian Vallee5d016502012-06-07 17:19:26 +02002700 self.Sync_LocalHalf(syncbuf)
2701 syncbuf.Finish()
2702
2703 return GitCommand(self,
Torne (Richard Coles)e8f75fa2012-07-20 15:32:19 +01002704 ['update-ref', '-d', 'refs/heads/default'],
Anthony King7bdac712014-07-16 12:56:40 +01002705 capture_stdout=True,
2706 capture_stderr=True).Wait() == 0
Florian Vallee5d016502012-06-07 17:19:26 +02002707
2708
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002709 @property
Shawn O. Pearcef6906872009-04-18 10:49:00 -07002710 def LastFetch(self):
2711 try:
2712 fh = os.path.join(self.gitdir, 'FETCH_HEAD')
2713 return os.path.getmtime(fh)
2714 except OSError:
2715 return 0
2716
2717 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002718 def HasChanges(self):
2719 """Has the remote received new commits not yet checked out?
2720 """
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07002721 if not self.remote or not self.revisionExpr:
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07002722 return False
2723
David Pursehouse8a68ff92012-09-24 12:15:13 +09002724 all_refs = self.bare_ref.all
2725 revid = self.GetRevisionId(all_refs)
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07002726 head = self.work_git.GetHead()
2727 if head.startswith(R_HEADS):
2728 try:
David Pursehouse8a68ff92012-09-24 12:15:13 +09002729 head = all_refs[head]
Shawn O. Pearce336f7bd2009-04-18 10:39:28 -07002730 except KeyError:
2731 head = None
2732
2733 if revid == head:
2734 return False
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07002735 elif self._revlist(not_rev(HEAD), revid):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002736 return True
2737 return False