Commit 680f2171 authored by dnj@chromium.org's avatar dnj@chromium.org

Added common git 'fetch' function

Replaced multiple various invocations of 'git fetch' with calls to a common
fetch function.

BUG=373504
TEST=localtest

Review URL: https://codereview.chromium.org/327803006

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@279560 0039d316-1c4b-4281-b951-d872f2087c98
parent 16f10f7f
......@@ -300,9 +300,7 @@ class GitWrapper(SCMWrapper):
quiet = ['--quiet']
self._UpdateBranchHeads(options, fetch=False)
cfg = gclient_utils.DefaultIndexPackConfig(self.url)
fetch_cmd = cfg + ['fetch', self.remote, '--prune']
self._Run(fetch_cmd + quiet, options, retry=True)
self._Fetch(options, prune=True, quiet=options.verbose)
self._Run(['reset', '--hard', revision] + quiet, options)
if file_list is not None:
files = self._Capture(['ls-files']).splitlines()
......@@ -722,7 +720,7 @@ class GitWrapper(SCMWrapper):
logging.debug('Looking for git-svn configuration optimizations.')
if scm.GIT.Capture(['config', '--get', 'svn-remote.svn.fetch'],
cwd=self.checkout_path):
scm.GIT.Capture(['fetch'], cwd=self.checkout_path)
self._Fetch(options)
except subprocess2.CalledProcessError:
logging.debug('git config --get svn-remote.svn.fetch failed, '
'ignoring possible optimization.')
......@@ -750,7 +748,7 @@ class GitWrapper(SCMWrapper):
else:
# May exist in origin, but we don't have it yet, so fetch and look
# again.
scm.GIT.Capture(['fetch', self.remote], cwd=self.checkout_path)
self._Fetch(options)
if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev):
sha1 = rev
......@@ -1037,6 +1035,24 @@ class GitWrapper(SCMWrapper):
checkout_args.append(ref)
return self._Capture(checkout_args)
def _Fetch(self, options, remote=None, prune=False, quiet=False):
cfg = gclient_utils.DefaultIndexPackConfig(self.url)
fetch_cmd = cfg + [
'fetch',
remote or self.remote,
]
if prune:
fetch_cmd.append('--prune')
if options.verbose:
fetch_cmd.append('--verbose')
elif quiet:
fetch_cmd.append('--quiet')
self._Run(fetch_cmd, options, show_header=options.verbose, retry=True)
# Return the revision that was fetched; this will be stored in 'FETCH_HEAD'
return self._Capture(['rev-parse', '--verify', 'FETCH_HEAD'])
def _UpdateBranchHeads(self, options, fetch=False):
"""Adds, and optionally fetches, "branch-heads" refspecs if requested."""
if hasattr(options, 'with_branch_heads') and options.with_branch_heads:
......@@ -1045,21 +1061,19 @@ class GitWrapper(SCMWrapper):
'^\\+refs/branch-heads/\\*:.*$']
self._Run(config_cmd, options)
if fetch:
cfg = gclient_utils.DefaultIndexPackConfig(self.url)
fetch_cmd = cfg + ['fetch', self.remote]
if options.verbose:
fetch_cmd.append('--verbose')
self._Run(fetch_cmd, options, retry=True)
self._Fetch(options)
def _Run(self, args, options, **kwargs):
def _Run(self, args, options, show_header=True, **kwargs):
# Disable 'unused options' warning | pylint: disable=W0613
cwd = kwargs.setdefault('cwd', self.checkout_path)
kwargs.setdefault('stdout', self.out_fh)
kwargs['filter_fn'] = self.filter
kwargs.setdefault('print_stdout', False)
env = scm.GIT.ApplyEnvVars(kwargs)
cmd = ['git'] + args
header = "running '%s' in '%s'" % (' '.join(cmd), cwd)
self.filter(header)
if show_header:
header = "running '%s' in '%s'" % (' '.join(cmd), cwd)
self.filter(header)
return gclient_utils.CheckCallAndFilter(cmd, env=env, **kwargs)
......
......@@ -1267,16 +1267,18 @@ class ManagedGitWrapperTestCaseMox(BaseTestCase):
).MultipleTimes().AndReturn(self.fake_hash_2)
# Ensure that we call git svn fetch if our LKGR is > the git-svn HEAD rev.
self.mox.StubOutWithMock(gclient_scm.GitWrapper, '_Fetch', True)
self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'Capture', True)
gclient_scm.scm.GIT.Capture(['config', '--get', 'svn-remote.svn.fetch'],
cwd=self.base_path).AndReturn('blah')
gclient_scm.scm.GIT.Capture(['fetch'], cwd=self.base_path)
# pylint: disable=E1120
gclient_scm.scm.GIT.Capture(['svn', 'fetch'], cwd=self.base_path)
error = subprocess2.CalledProcessError(1, 'cmd', '/cwd', 'stdout', 'stderr')
gclient_scm.scm.GIT.Capture(['config', '--get', 'svn-remote.svn.fetch'],
cwd=self.base_path).AndRaise(error)
gclient_scm.GitWrapper._Fetch(options)
gclient_scm.scm.GIT.Capture(['svn', 'fetch'], cwd=self.base_path)
gclient_scm.scm.GIT.Capture(['fetch', 'origin'], cwd=self.base_path)
gclient_scm.GitWrapper._Fetch(options)
self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'IsGitSvn', True)
gclient_scm.scm.GIT.IsGitSvn(cwd=self.base_path).MultipleTimes(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment