From e1482c55484acb20a6383bd9e458a0e1574d0a10 Mon Sep 17 00:00:00 2001 From: Josip Sokcevic Date: Thu, 9 Sep 2021 23:03:47 +0000 Subject: [PATCH] Refresh remote HEAD if matches legacy default If remote HEAD returns legacy default branch, refresh HEAD by running set-head to check if there are any changes. R=gavinmak@google.com Change-Id: I5891a899b512e81fccfb086a056d497c906df4b2 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3152593 Auto-Submit: Josip Sokcevic Commit-Queue: Gavin Mak Reviewed-by: Gavin Mak --- scm.py | 6 ++++++ tests/scm_unittest.py | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/scm.py b/scm.py index 5275d434d..39f8c58ec 100644 --- a/scm.py +++ b/scm.py @@ -201,6 +201,12 @@ class GIT(object): try: # Try using local git copy first ref = 'refs/remotes/%s/HEAD' % remote + ref = GIT.Capture(['symbolic-ref', ref], cwd=cwd) + if not ref.endswith('master'): + return ref + # Check if there are changes in the default branch for this particular + # repository. + GIT.Capture(['remote', 'set-head', '-a', remote], cwd=cwd) return GIT.Capture(['symbolic-ref', ref], cwd=cwd) except subprocess2.CalledProcessError: pass diff --git a/tests/scm_unittest.py b/tests/scm_unittest.py index 8f0b7825d..0eb5b675e 100755 --- a/tests/scm_unittest.py +++ b/tests/scm_unittest.py @@ -107,6 +107,18 @@ class GitWrapperTestCase(unittest.TestCase): scm.GIT.GetRemoteHeadRef('foo', 'proto://url', 'origin')) self.assertEqual(mockCapture.call_count, 1) + @mock.patch('scm.GIT.Capture') + @mock.patch('os.path.exists', lambda _: True) + def testGetRemoteHeadRefLocalUpdateHead(self, mockCapture): + mockCapture.side_effect = [ + 'refs/remotes/origin/master', # first symbolic-ref call + 'foo', # set-head call + 'refs/remotes/origin/main', # second symbolic-ref call + ] + self.assertEqual('refs/remotes/origin/main', + scm.GIT.GetRemoteHeadRef('foo', 'proto://url', 'origin')) + self.assertEqual(mockCapture.call_count, 3) + @mock.patch('scm.GIT.Capture') @mock.patch('os.path.exists', lambda _:True) def testGetRemoteHeadRefRemote(self, mockCapture):