Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: gclient_scm.py

Issue 327803006: Added common git 'fetch' function (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/gclient_scm_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Gclient-specific SCM-specific operations.""" 5 """Gclient-specific SCM-specific operations."""
6 6
7 from __future__ import print_function 7 from __future__ import print_function
8 8
9 import errno 9 import errno
10 import logging 10 import logging
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 cwd=self.checkout_path, 293 cwd=self.checkout_path,
294 filter_fn=GitDiffFilterer(self.relpath).Filter, print_func=self.Print) 294 filter_fn=GitDiffFilterer(self.relpath).Filter, print_func=self.Print)
295 295
296 def _FetchAndReset(self, revision, file_list, options): 296 def _FetchAndReset(self, revision, file_list, options):
297 """Equivalent to git fetch; git reset.""" 297 """Equivalent to git fetch; git reset."""
298 quiet = [] 298 quiet = []
299 if not options.verbose: 299 if not options.verbose:
300 quiet = ['--quiet'] 300 quiet = ['--quiet']
301 self._UpdateBranchHeads(options, fetch=False) 301 self._UpdateBranchHeads(options, fetch=False)
302 302
303 cfg = gclient_utils.DefaultIndexPackConfig(self.url) 303 self._Fetch(options, prune=True, quiet=options.verbose)
304 fetch_cmd = cfg + ['fetch', self.remote, '--prune']
305 self._Run(fetch_cmd + quiet, options, retry=True)
306 self._Run(['reset', '--hard', revision] + quiet, options) 304 self._Run(['reset', '--hard', revision] + quiet, options)
307 if file_list is not None: 305 if file_list is not None:
308 files = self._Capture(['ls-files']).splitlines() 306 files = self._Capture(['ls-files']).splitlines()
309 file_list.extend([os.path.join(self.checkout_path, f) for f in files]) 307 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
310 308
311 def update(self, options, args, file_list): 309 def update(self, options, args, file_list):
312 """Runs git to update or transparently checkout the working copy. 310 """Runs git to update or transparently checkout the working copy.
313 311
314 All updated files will be appended to file_list. 312 All updated files will be appended to file_list.
315 313
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 elif rev.isdigit() and len(rev) < 7: 687 elif rev.isdigit() and len(rev) < 7:
690 # Handles an SVN rev. As an optimization, only verify an SVN revision as 688 # Handles an SVN rev. As an optimization, only verify an SVN revision as
691 # [0-9]{1,6} for now to avoid making a network request. 689 # [0-9]{1,6} for now to avoid making a network request.
692 if scm.GIT.IsGitSvn(cwd=self.checkout_path): 690 if scm.GIT.IsGitSvn(cwd=self.checkout_path):
693 local_head = scm.GIT.GetGitSvnHeadRev(cwd=self.checkout_path) 691 local_head = scm.GIT.GetGitSvnHeadRev(cwd=self.checkout_path)
694 if not local_head or local_head < int(rev): 692 if not local_head or local_head < int(rev):
695 try: 693 try:
696 logging.debug('Looking for git-svn configuration optimizations.') 694 logging.debug('Looking for git-svn configuration optimizations.')
697 if scm.GIT.Capture(['config', '--get', 'svn-remote.svn.fetch'], 695 if scm.GIT.Capture(['config', '--get', 'svn-remote.svn.fetch'],
698 cwd=self.checkout_path): 696 cwd=self.checkout_path):
699 scm.GIT.Capture(['fetch'], cwd=self.checkout_path) 697 self._Fetch(options)
700 except subprocess2.CalledProcessError: 698 except subprocess2.CalledProcessError:
701 logging.debug('git config --get svn-remote.svn.fetch failed, ' 699 logging.debug('git config --get svn-remote.svn.fetch failed, '
702 'ignoring possible optimization.') 700 'ignoring possible optimization.')
703 if options.verbose: 701 if options.verbose:
704 self.Print('Running git svn fetch. This might take a while.\n') 702 self.Print('Running git svn fetch. This might take a while.\n')
705 scm.GIT.Capture(['svn', 'fetch'], cwd=self.checkout_path) 703 scm.GIT.Capture(['svn', 'fetch'], cwd=self.checkout_path)
706 try: 704 try:
707 sha1 = scm.GIT.GetBlessedSha1ForSvnRev( 705 sha1 = scm.GIT.GetBlessedSha1ForSvnRev(
708 cwd=self.checkout_path, rev=rev) 706 cwd=self.checkout_path, rev=rev)
709 except gclient_utils.Error, e: 707 except gclient_utils.Error, e:
710 sha1 = e.message 708 sha1 = e.message
711 self.Print('Warning: Could not find a git revision with accurate\n' 709 self.Print('Warning: Could not find a git revision with accurate\n'
712 '.DEPS.git that maps to SVN revision %s. Sync-ing to\n' 710 '.DEPS.git that maps to SVN revision %s. Sync-ing to\n'
713 'the closest sane git revision, which is:\n' 711 'the closest sane git revision, which is:\n'
714 ' %s\n' % (rev, e.message)) 712 ' %s\n' % (rev, e.message))
715 if not sha1: 713 if not sha1:
716 raise gclient_utils.Error( 714 raise gclient_utils.Error(
717 ( 'It appears that either your git-svn remote is incorrectly\n' 715 ( 'It appears that either your git-svn remote is incorrectly\n'
718 'configured or the revision in your safesync_url is\n' 716 'configured or the revision in your safesync_url is\n'
719 'higher than git-svn remote\'s HEAD as we couldn\'t find a\n' 717 'higher than git-svn remote\'s HEAD as we couldn\'t find a\n'
720 'corresponding git hash for SVN rev %s.' ) % rev) 718 'corresponding git hash for SVN rev %s.' ) % rev)
721 else: 719 else:
722 if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev): 720 if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev):
723 sha1 = rev 721 sha1 = rev
724 else: 722 else:
725 # May exist in origin, but we don't have it yet, so fetch and look 723 # May exist in origin, but we don't have it yet, so fetch and look
726 # again. 724 # again.
727 scm.GIT.Capture(['fetch', self.remote], cwd=self.checkout_path) 725 self._Fetch(options)
728 if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev): 726 if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev):
729 sha1 = rev 727 sha1 = rev
730 728
731 if not sha1: 729 if not sha1:
732 raise gclient_utils.Error( 730 raise gclient_utils.Error(
733 ( 'We could not find a valid hash for safesync_url response "%s".\n' 731 ( 'We could not find a valid hash for safesync_url response "%s".\n'
734 'Safesync URLs with a git checkout currently require a git-svn\n' 732 'Safesync URLs with a git checkout currently require a git-svn\n'
735 'remote or a safesync_url that provides git sha1s. Please add a\n' 733 'remote or a safesync_url that provides git sha1s. Please add a\n'
736 'git-svn remote or change your safesync_url. For more info, see:\n' 734 'git-svn remote or change your safesync_url. For more info, see:\n'
737 'http://code.google.com/p/chromium/wiki/UsingNewGit' 735 'http://code.google.com/p/chromium/wiki/UsingNewGit'
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
1005 Returns: (str) The output of the checkout operation 1003 Returns: (str) The output of the checkout operation
1006 """ 1004 """
1007 if quiet is None: 1005 if quiet is None:
1008 quiet = (not options.verbose) 1006 quiet = (not options.verbose)
1009 checkout_args = ['checkout'] 1007 checkout_args = ['checkout']
1010 if quiet: 1008 if quiet:
1011 checkout_args.append('--quiet') 1009 checkout_args.append('--quiet')
1012 checkout_args.append(ref) 1010 checkout_args.append(ref)
1013 return self._Capture(checkout_args) 1011 return self._Capture(checkout_args)
1014 1012
1013 def _Fetch(self, options, remote=None, prune=False, quiet=False):
1014 if remote is None:
1015 remote = self.remote
1016 cfg = gclient_utils.DefaultIndexPackConfig(self.url)
1017 fetch_cmd = cfg + ['fetch', remote]
1018
1019 if prune:
1020 fetch_cmd.append('--prune')
1021 if options.verbose:
1022 fetch_cmd.append('--verbose')
1023 elif quiet:
1024 fetch_cmd.append('--quiet')
1025 self._Run(fetch_cmd, options, _show_header=options.verbose, retry=True)
1026
1027 # Return the revision that was fetched; this will be stored in 'FETCH_HEAD'
1028 return self._Capture(['rev-parse', '--verify', 'FETCH_HEAD'])
1029
1015 def _UpdateBranchHeads(self, options, fetch=False): 1030 def _UpdateBranchHeads(self, options, fetch=False):
1016 """Adds, and optionally fetches, "branch-heads" refspecs if requested.""" 1031 """Adds, and optionally fetches, "branch-heads" refspecs if requested."""
1017 if hasattr(options, 'with_branch_heads') and options.with_branch_heads: 1032 if hasattr(options, 'with_branch_heads') and options.with_branch_heads:
1018 config_cmd = ['config', 'remote.%s.fetch' % self.remote, 1033 config_cmd = ['config', 'remote.%s.fetch' % self.remote,
1019 '+refs/branch-heads/*:refs/remotes/branch-heads/*', 1034 '+refs/branch-heads/*:refs/remotes/branch-heads/*',
1020 '^\\+refs/branch-heads/\\*:.*$'] 1035 '^\\+refs/branch-heads/\\*:.*$']
1021 self._Run(config_cmd, options) 1036 self._Run(config_cmd, options)
1022 if fetch: 1037 if fetch:
1023 cfg = gclient_utils.DefaultIndexPackConfig(self.url) 1038 self._Fetch(options)
1024 fetch_cmd = cfg + ['fetch', self.remote]
1025 if options.verbose:
1026 fetch_cmd.append('--verbose')
1027 self._Run(fetch_cmd, options, retry=True)
1028 1039
1029 def _Run(self, args, options, **kwargs): 1040 def _Run(self, args, options, _show_header=True, **kwargs):
M-A Ruel 2014/06/24 21:11:52 Why an underscore prefix?
dnj 2014/06/24 21:45:23 The thought is that since this is opaquely wrappin
1041 # Disable 'unused options' warning | pylint: disable=W0613
1030 cwd = kwargs.setdefault('cwd', self.checkout_path) 1042 cwd = kwargs.setdefault('cwd', self.checkout_path)
1031 kwargs.setdefault('stdout', self.out_fh) 1043 kwargs.setdefault('stdout', self.out_fh)
1032 kwargs['filter_fn'] = self.filter 1044 kwargs['filter_fn'] = self.filter
1033 kwargs.setdefault('print_stdout', False) 1045 kwargs.setdefault('print_stdout', False)
1034 env = scm.GIT.ApplyEnvVars(kwargs) 1046 env = scm.GIT.ApplyEnvVars(kwargs)
1035 cmd = ['git'] + args 1047 cmd = ['git'] + args
1036 header = "running '%s' in '%s'" % (' '.join(cmd), cwd) 1048 if _show_header:
1037 self.filter(header) 1049 header = "running '%s' in '%s'" % (' '.join(cmd), cwd)
1050 self.filter(header)
1038 return gclient_utils.CheckCallAndFilter(cmd, env=env, **kwargs) 1051 return gclient_utils.CheckCallAndFilter(cmd, env=env, **kwargs)
1039 1052
1040 1053
1041 class SVNWrapper(SCMWrapper): 1054 class SVNWrapper(SCMWrapper):
1042 """ Wrapper for SVN """ 1055 """ Wrapper for SVN """
1043 name = 'svn' 1056 name = 'svn'
1044 1057
1045 @staticmethod 1058 @staticmethod
1046 def BinaryExists(): 1059 def BinaryExists():
1047 """Returns true if the command exists.""" 1060 """Returns true if the command exists."""
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after
1500 new_command.append('--force') 1513 new_command.append('--force')
1501 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: 1514 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]:
1502 new_command.extend(('--accept', 'theirs-conflict')) 1515 new_command.extend(('--accept', 'theirs-conflict'))
1503 elif options.manually_grab_svn_rev: 1516 elif options.manually_grab_svn_rev:
1504 new_command.append('--force') 1517 new_command.append('--force')
1505 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: 1518 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]:
1506 new_command.extend(('--accept', 'postpone')) 1519 new_command.extend(('--accept', 'postpone'))
1507 elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: 1520 elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]:
1508 new_command.extend(('--accept', 'postpone')) 1521 new_command.extend(('--accept', 'postpone'))
1509 return new_command 1522 return new_command
OLDNEW
« no previous file with comments | « no previous file | tests/gclient_scm_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698