Commit a4b36024 authored by Josip Sokcevic's avatar Josip Sokcevic Committed by LUCI CQ

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/+/3696906Reviewed-by: 's avatarGavin Mak <gavinmak@google.com>
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
parent eb8db115
...@@ -399,10 +399,6 @@ class Mirror(object): ...@@ -399,10 +399,6 @@ class Mirror(object):
if depth and os.path.exists(os.path.join(self.mirror_path, 'shallow')): if depth and os.path.exists(os.path.join(self.mirror_path, 'shallow')):
logging.warning( logging.warning(
'Shallow fetch requested, but repo cache already exists.') '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 return
if not self.exists(): if not self.exists():
...@@ -426,10 +422,15 @@ class Mirror(object): ...@@ -426,10 +422,15 @@ class Mirror(object):
# 2. Project doesn't have a bootstrap folder. # 2. Project doesn't have a bootstrap folder.
# Start with a bare git dir. # Start with a bare git dir.
self.RunGit(['init', '--bare'], cwd=self.mirror_path) self.RunGit(['init', '--bare'], cwd=self.mirror_path)
# Set HEAD to main. -b is introduced in 2.28 and may not be available # Set appropriate symbolic-ref
# everywhere. remote_info = subprocess.check_output(
self.RunGit(['symbolic-ref', 'HEAD', 'refs/heads/main'], [self.git_exe, 'remote', 'show', self.url],
cwd=self.mirror_path) 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: else:
# Bootstrap failed, previous cache exists; warn and continue. # Bootstrap failed, previous cache exists; warn and continue.
logging.warning( logging.warning(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment