Use remote information about default branch
When initializing git_cache repository, we can't assume the default
branch is main. This results in HEAD pointing to invalid ref for repos
that don't have main branch.
Instead, fetch information from remote git repository.
R=gavinmak@google.com
Fixed: 1334889
Change-Id: I84e6d8e09c81bdae7e5e31af88973534431824e3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3696906
Reviewed-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
diff --git a/git_cache.py b/git_cache.py
index 03f7413..21a7095 100755
--- a/git_cache.py
+++ b/git_cache.py
@@ -399,10 +399,6 @@
if depth and os.path.exists(os.path.join(self.mirror_path, 'shallow')):
logging.warning(
'Shallow fetch requested, but repo cache already exists.')
- # Old boostraps may have old default HEAD, so this ensures main is always
- # used.
- self.RunGit(['symbolic-ref', 'HEAD', 'refs/heads/main'],
- cwd=self.mirror_path)
return
if not self.exists():
@@ -426,10 +422,15 @@
# 2. Project doesn't have a bootstrap folder.
# Start with a bare git dir.
self.RunGit(['init', '--bare'], cwd=self.mirror_path)
- # Set HEAD to main. -b is introduced in 2.28 and may not be available
- # everywhere.
- self.RunGit(['symbolic-ref', 'HEAD', 'refs/heads/main'],
- cwd=self.mirror_path)
+ # Set appropriate symbolic-ref
+ remote_info = subprocess.check_output(
+ [self.git_exe, 'remote', 'show', self.url],
+ cwd=self.mirror_path).decode('utf-8', 'ignore').strip()
+ default_branch_regexp = re.compile(r'HEAD branch: (.*)$')
+ m = default_branch_regexp.search(remote_info, re.MULTILINE)
+ if m:
+ self.RunGit(['symbolic-ref', 'HEAD', 'refs/heads/' + m.groups()[0]],
+ cwd=self.mirror_path)
else:
# Bootstrap failed, previous cache exists; warn and continue.
logging.warning(