From e5c5b2664cc1c4f54608d08c47cb02b9b14e55e1 Mon Sep 17 00:00:00 2001 From: Josip Sokcevic Date: Tue, 23 Jan 2024 18:02:47 +0000 Subject: [PATCH] [git_cache] Check if HEAD still points to existing ref It's possible that HEAD points to a different branch, and old one was deleted. This ensures that cache still has the correct HEAD information. Change-Id: Iab0b6badfd1f3e2c0e09fe542ee603b78d4a0041 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5218257 Commit-Queue: Josip Sokcevic Reviewed-by: Gavin Mak --- git_cache.py | 37 ++++++++++++++++++------------- recipes/trigger_recipe_roller.txt | 3 +-- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/git_cache.py b/git_cache.py index 0c7c92487..02f36a031 100755 --- a/git_cache.py +++ b/git_cache.py @@ -248,7 +248,7 @@ class Mirror(object): env.setdefault('GIT_ASKPASS', 'true') env.setdefault('SSH_ASKPASS', 'true') self.print('running "git %s" in "%s"' % (' '.join(cmd), cwd)) - gclient_utils.CheckCallAndFilter([self.git_exe] + cmd, **kwargs) + return gclient_utils.CheckCallAndFilter([self.git_exe] + cmd, **kwargs) def config(self, reset_fetch_config=False): if reset_fetch_config: @@ -467,20 +467,7 @@ class Mirror(object): with open(self._init_sentient_file, 'w'): # Create sentient file pass - # Set appropriate symbolic-ref - remote_info = exponential_backoff_retry( - lambda: subprocess.check_output( - [ - self.git_exe, '--git-dir', - os.path.abspath(self.mirror_path), '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]]) + self._set_symbolic_ref() else: # Bootstrap failed, previous cache exists; warn and continue. logging.warning( @@ -488,6 +475,19 @@ class Mirror(object): 're-bootstrap but failed. Continuing with non-optimized ' 'repository.' % len(pack_files)) + def _set_symbolic_ref(self): + remote_info = exponential_backoff_retry(lambda: subprocess.check_output( + [ + self.git_exe, '--git-dir', + os.path.abspath(self.mirror_path), '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]]) + + def _fetch(self, verbose, depth, @@ -534,6 +534,13 @@ class Mirror(object): if os.path.isfile(self._init_sentient_file): os.remove(self._init_sentient_file) + # Since --prune is used, it's possible that HEAD no longer exists (e.g. + # a repo uses new HEAD and old is removed). This ensures that HEAD still + # points to a valid commit, otherwise gets a new HEAD. + out = self.RunGit(['rev-parse', 'HEAD'], print_stdout=False) + if out.startswith(b'HEAD'): + self._set_symbolic_ref() + def populate(self, depth=None, no_fetch_tags=False, diff --git a/recipes/trigger_recipe_roller.txt b/recipes/trigger_recipe_roller.txt index 12dfaaa60..1b9ec3997 100644 --- a/recipes/trigger_recipe_roller.txt +++ b/recipes/trigger_recipe_roller.txt @@ -9,5 +9,4 @@ As the CI needs of the browser grew, Batty, the Build and Test Yeti, got a new friend, but this information was marked RVG, so it was months before any details were revealed. -The End! - +The End! \ No newline at end of file