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):