Use real default branch in gclient
Currently, gclient sync assumes the default branch is master, and
it doesn't work at all if such branch doesn't exist. This change queries
local git copy to get remote HEAD. If local git version is not
available, it queries remote git server using ls-remote.
This change requires git version 2.28 (depot_tools comes with 2.29).
R=ehmaldonado@chromium.org
Bug: 1156318
Change-Id: Id348e0f1004093f395139e8f4d62adb66b94ca9c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2628359
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
diff --git a/tests/gclient_scm_test.py b/tests/gclient_scm_test.py
index d5fafed..6fa3bd5 100755
--- a/tests/gclient_scm_test.py
+++ b/tests/gclient_scm_test.py
@@ -196,6 +196,11 @@
stderr=STDOUT, cwd=path).communicate()
Popen([GIT, 'config', 'user.name', 'Some User'], stdout=PIPE,
stderr=STDOUT, cwd=path).communicate()
+ # Set HEAD back to master
+ Popen([GIT, 'checkout', 'master', '-q'],
+ stdout=PIPE,
+ stderr=STDOUT,
+ cwd=path).communicate()
return True
def _GetAskForDataCallback(self, expected_prompt, return_value):
@@ -640,7 +645,7 @@
self, mockCheckOutput, mockExists, mockIsdir, mockClone):
mockIsdir.side_effect = lambda path: path == self.base_path
mockExists.side_effect = lambda path: path == self.base_path
- mockCheckOutput.return_value = b''
+ mockCheckOutput.side_effect = [b'refs/remotes/origin/main', b'', b'']
options = self.Options()
scm = gclient_scm.GitWrapper(
@@ -648,18 +653,21 @@
scm.update(options, None, [])
env = gclient_scm.scm.GIT.ApplyEnvVars({})
- self.assertEqual(
- mockCheckOutput.mock_calls,
- [
- mock.call(
- ['git', '-c', 'core.quotePath=false', 'ls-files'],
- cwd=self.base_path, env=env, stderr=-1),
- mock.call(
- ['git', 'rev-parse', '--verify', 'HEAD'],
- cwd=self.base_path, env=env, stderr=-1),
- ])
- mockClone.assert_called_with(
- 'refs/remotes/origin/master', self.url, options)
+ self.assertEqual(mockCheckOutput.mock_calls, [
+ mock.call(['git', 'symbolic-ref', 'refs/remotes/origin/HEAD'],
+ cwd=self.base_path,
+ env=env,
+ stderr=-1),
+ mock.call(['git', '-c', 'core.quotePath=false', 'ls-files'],
+ cwd=self.base_path,
+ env=env,
+ stderr=-1),
+ mock.call(['git', 'rev-parse', '--verify', 'HEAD'],
+ cwd=self.base_path,
+ env=env,
+ stderr=-1),
+ ])
+ mockClone.assert_called_with('refs/remotes/origin/main', self.url, options)
self.checkstdout('\n')
@mock.patch('gclient_scm.GitWrapper._Clone')
@@ -670,7 +678,7 @@
self, mockCheckOutput, mockExists, mockIsdir, mockClone):
mockIsdir.side_effect = lambda path: path == self.base_path
mockExists.side_effect = lambda path: path == self.base_path
- mockCheckOutput.return_value = b''
+ mockCheckOutput.side_effect = [b'refs/remotes/origin/main', b'', b'']
mockClone.side_effect = [
gclient_scm.subprocess2.CalledProcessError(
None, None, None, None, None),
@@ -683,18 +691,21 @@
scm.update(options, None, [])
env = gclient_scm.scm.GIT.ApplyEnvVars({})
- self.assertEqual(
- mockCheckOutput.mock_calls,
- [
- mock.call(
- ['git', '-c', 'core.quotePath=false', 'ls-files'],
- cwd=self.base_path, env=env, stderr=-1),
- mock.call(
- ['git', 'rev-parse', '--verify', 'HEAD'],
- cwd=self.base_path, env=env, stderr=-1),
- ])
- mockClone.assert_called_with(
- 'refs/remotes/origin/master', self.url, options)
+ self.assertEqual(mockCheckOutput.mock_calls, [
+ mock.call(['git', 'symbolic-ref', 'refs/remotes/origin/HEAD'],
+ cwd=self.base_path,
+ env=env,
+ stderr=-1),
+ mock.call(['git', '-c', 'core.quotePath=false', 'ls-files'],
+ cwd=self.base_path,
+ env=env,
+ stderr=-1),
+ mock.call(['git', 'rev-parse', '--verify', 'HEAD'],
+ cwd=self.base_path,
+ env=env,
+ stderr=-1),
+ ])
+ mockClone.assert_called_with('refs/remotes/origin/main', self.url, options)
self.checkstdout('\n')