Explorar o código

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 <sokcevic@google.com>
Commit-Queue: Gavin Mak <gavinmak@google.com>
Reviewed-by: Gavin Mak <gavinmak@google.com>
Josip Sokcevic %!s(int64=4) %!d(string=hai) anos
pai
achega
e1482c5548
Modificáronse 2 ficheiros con 18 adicións e 0 borrados
  1. 6 0
      scm.py
  2. 12 0
      tests/scm_unittest.py

+ 6 - 0
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

+ 12 - 0
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):