From 8281847e059fd9677943327f59786c42a139b9df Mon Sep 17 00:00:00 2001 From: Josip Sokcevic Date: Mon, 29 Apr 2024 15:39:31 +0000 Subject: [PATCH] [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 Reviewed-by: Gavin Mak --- scm.py | 4 ++++ tests/gclient_scm_test.py | 8 +++++++- tests/scm_unittest.py | 3 ++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/scm.py b/scm.py index 36bb69d15..4e8123056 100644 --- a/scm.py +++ b/scm.py @@ -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) diff --git a/tests/gclient_scm_test.py b/tests/gclient_scm_test.py index f4ed08567..5cee7364d 100755 --- a/tests/gclient_scm_test.py +++ b/tests/gclient_scm_test.py @@ -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']]) diff --git a/tests/scm_unittest.py b/tests/scm_unittest.py index b9f93a580..e298c5cdf 100755 --- a/tests/scm_unittest.py +++ b/tests/scm_unittest.py @@ -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):