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

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: Fixed nit 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 _DisableHooks(self): 309 def _DisableHooks(self):
312 hook_dir = os.path.join(self.checkout_path, '.git', 'hooks') 310 hook_dir = os.path.join(self.checkout_path, '.git', 'hooks')
313 if not os.path.isdir(hook_dir): 311 if not os.path.isdir(hook_dir):
314 return 312 return
315 for f in os.listdir(hook_dir): 313 for f in os.listdir(hook_dir):
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
715 elif rev.isdigit() and len(rev) < 7: 713 elif rev.isdigit() and len(rev) < 7:
716 # Handles an SVN rev. As an optimization, only verify an SVN revision as 714 # Handles an SVN rev. As an optimization, only verify an SVN revision as
717 # [0-9]{1,6} for now to avoid making a network request. 715 # [0-9]{1,6} for now to avoid making a network request.
718 if scm.GIT.IsGitSvn(cwd=self.checkout_path): 716 if scm.GIT.IsGitSvn(cwd=self.checkout_path):
719 local_head = scm.GIT.GetGitSvnHeadRev(cwd=self.checkout_path) 717 local_head = scm.GIT.GetGitSvnHeadRev(cwd=self.checkout_path)
720 if not local_head or local_head < int(rev): 718 if not local_head or local_head < int(rev):
721 try: 719 try:
722 logging.debug('Looking for git-svn configuration optimizations.') 720 logging.debug('Looking for git-svn configuration optimizations.')
723 if scm.GIT.Capture(['config', '--get', 'svn-remote.svn.fetch'], 721 if scm.GIT.Capture(['config', '--get', 'svn-remote.svn.fetch'],
724 cwd=self.checkout_path): 722 cwd=self.checkout_path):
725 scm.GIT.Capture(['fetch'], cwd=self.checkout_path) 723 self._Fetch(options)
726 except subprocess2.CalledProcessError: 724 except subprocess2.CalledProcessError:
727 logging.debug('git config --get svn-remote.svn.fetch failed, ' 725 logging.debug('git config --get svn-remote.svn.fetch failed, '
728 'ignoring possible optimization.') 726 'ignoring possible optimization.')
729 if options.verbose: 727 if options.verbose:
730 self.Print('Running git svn fetch. This might take a while.\n') 728 self.Print('Running git svn fetch. This might take a while.\n')
731 scm.GIT.Capture(['svn', 'fetch'], cwd=self.checkout_path) 729 scm.GIT.Capture(['svn', 'fetch'], cwd=self.checkout_path)
732 try: 730 try:
733 sha1 = scm.GIT.GetBlessedSha1ForSvnRev( 731 sha1 = scm.GIT.GetBlessedSha1ForSvnRev(
734 cwd=self.checkout_path, rev=rev) 732 cwd=self.checkout_path, rev=rev)
735 except gclient_utils.Error, e: 733 except gclient_utils.Error, e:
736 sha1 = e.message 734 sha1 = e.message
737 self.Print('Warning: Could not find a git revision with accurate\n' 735 self.Print('Warning: Could not find a git revision with accurate\n'
738 '.DEPS.git that maps to SVN revision %s. Sync-ing to\n' 736 '.DEPS.git that maps to SVN revision %s. Sync-ing to\n'
739 'the closest sane git revision, which is:\n' 737 'the closest sane git revision, which is:\n'
740 ' %s\n' % (rev, e.message)) 738 ' %s\n' % (rev, e.message))
741 if not sha1: 739 if not sha1:
742 raise gclient_utils.Error( 740 raise gclient_utils.Error(
743 ( 'It appears that either your git-svn remote is incorrectly\n' 741 ( 'It appears that either your git-svn remote is incorrectly\n'
744 'configured or the revision in your safesync_url is\n' 742 'configured or the revision in your safesync_url is\n'
745 'higher than git-svn remote\'s HEAD as we couldn\'t find a\n' 743 'higher than git-svn remote\'s HEAD as we couldn\'t find a\n'
746 'corresponding git hash for SVN rev %s.' ) % rev) 744 'corresponding git hash for SVN rev %s.' ) % rev)
747 else: 745 else:
748 if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev): 746 if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev):
749 sha1 = rev 747 sha1 = rev
750 else: 748 else:
751 # May exist in origin, but we don't have it yet, so fetch and look 749 # May exist in origin, but we don't have it yet, so fetch and look
752 # again. 750 # again.
753 scm.GIT.Capture(['fetch', self.remote], cwd=self.checkout_path) 751 self._Fetch(options)
754 if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev): 752 if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev):
755 sha1 = rev 753 sha1 = rev
756 754
757 if not sha1: 755 if not sha1:
758 raise gclient_utils.Error( 756 raise gclient_utils.Error(
759 ( 'We could not find a valid hash for safesync_url response "%s".\n' 757 ( 'We could not find a valid hash for safesync_url response "%s".\n'
760 'Safesync URLs with a git checkout currently require a git-svn\n' 758 'Safesync URLs with a git checkout currently require a git-svn\n'
761 'remote or a safesync_url that provides git sha1s. Please add a\n' 759 'remote or a safesync_url that provides git sha1s. Please add a\n'
762 'git-svn remote or change your safesync_url. For more info, see:\n' 760 'git-svn remote or change your safesync_url. For more info, see:\n'
763 'http://code.google.com/p/chromium/wiki/UsingNewGit' 761 'http://code.google.com/p/chromium/wiki/UsingNewGit'
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
1030 if quiet is None: 1028 if quiet is None:
1031 quiet = (not options.verbose) 1029 quiet = (not options.verbose)
1032 checkout_args = ['checkout'] 1030 checkout_args = ['checkout']
1033 if force: 1031 if force:
1034 checkout_args.append('--force') 1032 checkout_args.append('--force')
1035 if quiet: 1033 if quiet:
1036 checkout_args.append('--quiet') 1034 checkout_args.append('--quiet')
1037 checkout_args.append(ref) 1035 checkout_args.append(ref)
1038 return self._Capture(checkout_args) 1036 return self._Capture(checkout_args)
1039 1037
1038 def _Fetch(self, options, remote=None, prune=False, quiet=False):
1039 cfg = gclient_utils.DefaultIndexPackConfig(self.url)
1040 fetch_cmd = cfg + [
1041 'fetch',
1042 remote or self.remote,
1043 ]
1044
1045 if prune:
1046 fetch_cmd.append('--prune')
1047 if options.verbose:
1048 fetch_cmd.append('--verbose')
1049 elif quiet:
1050 fetch_cmd.append('--quiet')
1051 self._Run(fetch_cmd, options, show_header=options.verbose, retry=True)
1052
1053 # Return the revision that was fetched; this will be stored in 'FETCH_HEAD'
1054 return self._Capture(['rev-parse', '--verify', 'FETCH_HEAD'])
1055
1040 def _UpdateBranchHeads(self, options, fetch=False): 1056 def _UpdateBranchHeads(self, options, fetch=False):
1041 """Adds, and optionally fetches, "branch-heads" refspecs if requested.""" 1057 """Adds, and optionally fetches, "branch-heads" refspecs if requested."""
1042 if hasattr(options, 'with_branch_heads') and options.with_branch_heads: 1058 if hasattr(options, 'with_branch_heads') and options.with_branch_heads:
1043 config_cmd = ['config', 'remote.%s.fetch' % self.remote, 1059 config_cmd = ['config', 'remote.%s.fetch' % self.remote,
1044 '+refs/branch-heads/*:refs/remotes/branch-heads/*', 1060 '+refs/branch-heads/*:refs/remotes/branch-heads/*',
1045 '^\\+refs/branch-heads/\\*:.*$'] 1061 '^\\+refs/branch-heads/\\*:.*$']
1046 self._Run(config_cmd, options) 1062 self._Run(config_cmd, options)
1047 if fetch: 1063 if fetch:
1048 cfg = gclient_utils.DefaultIndexPackConfig(self.url) 1064 self._Fetch(options)
1049 fetch_cmd = cfg + ['fetch', self.remote]
1050 if options.verbose:
1051 fetch_cmd.append('--verbose')
1052 self._Run(fetch_cmd, options, retry=True)
1053 1065
1054 def _Run(self, args, options, **kwargs): 1066 def _Run(self, args, options, show_header=True, **kwargs):
1067 # Disable 'unused options' warning | pylint: disable=W0613
1055 cwd = kwargs.setdefault('cwd', self.checkout_path) 1068 cwd = kwargs.setdefault('cwd', self.checkout_path)
1056 kwargs.setdefault('stdout', self.out_fh) 1069 kwargs.setdefault('stdout', self.out_fh)
1057 kwargs['filter_fn'] = self.filter 1070 kwargs['filter_fn'] = self.filter
1058 kwargs.setdefault('print_stdout', False) 1071 kwargs.setdefault('print_stdout', False)
1059 env = scm.GIT.ApplyEnvVars(kwargs) 1072 env = scm.GIT.ApplyEnvVars(kwargs)
1060 cmd = ['git'] + args 1073 cmd = ['git'] + args
1061 header = "running '%s' in '%s'" % (' '.join(cmd), cwd) 1074 if show_header:
1062 self.filter(header) 1075 header = "running '%s' in '%s'" % (' '.join(cmd), cwd)
1076 self.filter(header)
1063 return gclient_utils.CheckCallAndFilter(cmd, env=env, **kwargs) 1077 return gclient_utils.CheckCallAndFilter(cmd, env=env, **kwargs)
1064 1078
1065 1079
1066 class SVNWrapper(SCMWrapper): 1080 class SVNWrapper(SCMWrapper):
1067 """ Wrapper for SVN """ 1081 """ Wrapper for SVN """
1068 name = 'svn' 1082 name = 'svn'
1069 1083
1070 @staticmethod 1084 @staticmethod
1071 def BinaryExists(): 1085 def BinaryExists():
1072 """Returns true if the command exists.""" 1086 """Returns true if the command exists."""
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after
1525 new_command.append('--force') 1539 new_command.append('--force')
1526 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: 1540 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]:
1527 new_command.extend(('--accept', 'theirs-conflict')) 1541 new_command.extend(('--accept', 'theirs-conflict'))
1528 elif options.manually_grab_svn_rev: 1542 elif options.manually_grab_svn_rev:
1529 new_command.append('--force') 1543 new_command.append('--force')
1530 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: 1544 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]:
1531 new_command.extend(('--accept', 'postpone')) 1545 new_command.extend(('--accept', 'postpone'))
1532 elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: 1546 elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]:
1533 new_command.extend(('--accept', 'postpone')) 1547 new_command.extend(('--accept', 'postpone'))
1534 return new_command 1548 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