[scm] Run remote set-head if symbolic-ref fails

If symbolic-ref remote HEAD is not available, we skip setting it and
query remote Git using ls-remote. Such information is not stored, and
gclient will need to repeat it on the next invocation.

Instead, we can call set-head on symbolic-ref failure. While that's
slower operation than ls-remote, it saved in internal Git database and
can be reused on next gclient invocation.

We may be okay with hardcoding 'main' as default remote branch today,
but it's possible that some projects still use old default.

R=gavinmak@google.com

Change-Id: Ic4c826b888d96e367039bfc4b9bd2ba0d8b58b52
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5492789
Commit-Queue: Josip Sokcevic <sokcevic@chromium.org>
Reviewed-by: Gavin Mak <gavinmak@google.com>
changes/89/5492789/2
Josip Sokcevic 1 year ago committed by LUCI CQ
parent 4246ba023a
commit 8281847e05

@ -253,6 +253,10 @@ class GIT(object):
ref = GIT.Capture(['symbolic-ref', ref], cwd=cwd)
if not ref.endswith('master'):
return ref
except subprocess2.CalledProcessError:
pass
try:
# Check if there are changes in the default branch for this
# particular repository.
GIT.Capture(['remote', 'set-head', '-a', remote], cwd=cwd)

@ -411,6 +411,9 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
options = self.Options()
options.merge = True
scm = gclient_scm.GitWrapper(self.url, self.root_dir, self.relpath)
# This sets correct remote HEAD
scm.update(options, (), [])
scm._Run(['checkout', '-q', 'feature'], options)
rev = scm.revinfo(options, (), None)
file_list = []
@ -432,12 +435,15 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
return
options = self.Options()
scm = gclient_scm.GitWrapper(self.url, self.root_dir, self.relpath)
# This sets correct remote HEAD
scm.update(options, (), [])
scm._Run(['checkout', '-q', 'feature'], options)
file_list = []
# Fake a 'y' key press.
scm._AskForData = self._GetAskForDataCallback(
'Cannot fast-forward merge, attempt to rebase? '
'(y)es / (q)uit / (s)kip : ', 'y')
file_list = []
scm.update(options, (), file_list)
self.assertEqual(file_list,
[join(self.base_path, x) for x in ['a', 'b', 'c']])

@ -101,6 +101,7 @@ class GitWrapperTestCase(unittest.TestCase):
@mock.patch('os.path.exists', lambda _: True)
def testGetRemoteHeadRefRemote(self, mockCapture):
mockCapture.side_effect = [
subprocess2.CalledProcessError(1, '', '', '', ''),
subprocess2.CalledProcessError(1, '', '', '', ''),
'ref: refs/heads/main\tHEAD\n' +
'0000000000000000000000000000000000000000\tHEAD',
@ -108,7 +109,7 @@ class GitWrapperTestCase(unittest.TestCase):
self.assertEqual(
'refs/remotes/origin/main',
scm.GIT.GetRemoteHeadRef('foo', 'proto://url', 'origin'))
self.assertEqual(mockCapture.call_count, 2)
self.assertEqual(mockCapture.call_count, 3)
@mock.patch('scm.GIT.Capture')
def testIsVersioned(self, mockCapture):

Loading…
Cancel
Save