From cf4658579326856b9b25cfe2efbfc8683af88814 Mon Sep 17 00:00:00 2001 From: Aravind Vasudevan Date: Wed, 29 Mar 2023 16:47:12 +0000 Subject: [PATCH] Reset alternates when the git_cache dir is updated When cache URL is updated, `gclient sync` appends the new cache path as an additional alternate object db instead of overriding the old alternate object db. This was done to support reusing cache while changing mirror path. However, when the cache is reset by LUCI, gclient appends the new mirror path instead of overwriting the existing one. This results in failed builds where `git fetch` looks for non-existent alternate object dbs. Bug: 1428312 Change-Id: Ib10e0b966ddef8793415cf8ef0eddbec13ba1a8d Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4379380 Reviewed-by: Joanna Wang Commit-Queue: Aravind Vasudevan --- gclient_scm.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/gclient_scm.py b/gclient_scm.py index 3b1b59f7a..8acc4250c 100644 --- a/gclient_scm.py +++ b/gclient_scm.py @@ -698,15 +698,24 @@ class GitWrapper(SCMWrapper): # Switch over to the new upstream self._Run(['remote', 'set-url', self.remote, url], options) if mirror: - # Because we use Git alternatives, our existing repository is not - # self-contained. It's possible that new git alternative doesn't have - # all necessary objects that the current repository needs. Instead of - # blindly hoping that new alternative contains all necessary objects, - # keep the old alternative and just append a new one on top of it. - with open(os.path.join( - self.checkout_path, '.git', 'objects', 'info', 'alternates'), - 'a') as fh: - fh.write("\n" + os.path.join(url, 'objects')) + if git_cache.Mirror.CacheDirToUrl( + current_url.rstrip('/')) == git_cache.Mirror.CacheDirToUrl( + url.rstrip('/')): + # Reset alternates when the cache dir is updated. + with open( + os.path.join(self.checkout_path, '.git', 'objects', 'info', + 'alternates'), 'w') as fh: + fh.write(os.path.join(url, 'objects')) + else: + # Because we use Git alternatives, our existing repository is not + # self-contained. It's possible that new git alternative doesn't have + # all necessary objects that the current repository needs. Instead of + # blindly hoping that new alternative contains all necessary objects, + # keep the old alternative and just append a new one on top of it. + with open( + os.path.join(self.checkout_path, '.git', 'objects', 'info', + 'alternates'), 'a') as fh: + fh.write("\n" + os.path.join(url, 'objects')) self._EnsureValidHeadObjectOrCheckout(revision, options, url) self._FetchAndReset(revision, file_list, options)