Commit 34f71abc authored by Edward Lesmes's avatar Edward Lesmes Committed by LUCI CQ

git-cache: Also bootstrap in the case that there are 0 pack files

This can happen if the cache repo was init'd, but has no pack files;
previously it would try to fetch from scratch.

Change-Id: I71689e30bdede392588c69e118e9297d86a134a3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2120281Reviewed-by: 's avatarJosip Sokcevic <sokcevic@google.com>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
parent 7d1655b8
......@@ -481,17 +481,19 @@ class Mirror(object):
'%s and "git cache fetch" again.'
% os.path.join(self.mirror_path, 'config'))
def _ensure_bootstrapped(self, depth, bootstrap, force=False):
def _ensure_bootstrapped(
self, depth, bootstrap, reset_fetch_config, force=False):
pack_dir = os.path.join(self.mirror_path, 'objects', 'pack')
pack_files = []
if os.path.isdir(pack_dir):
pack_files = [f for f in os.listdir(pack_dir) if f.endswith('.pack')]
self.print('%s has %d .pack files, re-bootstrapping if >%d' %
self.print('%s has %d .pack files, re-bootstrapping if >%d or ==0' %
(self.mirror_path, len(pack_files), GC_AUTOPACKLIMIT))
should_bootstrap = (force or
not self.exists() or
len(pack_files) > GC_AUTOPACKLIMIT)
len(pack_files) > GC_AUTOPACKLIMIT or
len(pack_files) == 0)
if not should_bootstrap:
if depth and os.path.exists(os.path.join(self.mirror_path, 'shallow')):
......@@ -499,16 +501,16 @@ class Mirror(object):
'Shallow fetch requested, but repo cache already exists.')
return
if self.exists():
# Re-bootstrapping an existing mirror; preserve existing fetch spec.
self._preserve_fetchspec()
else:
if not self.exists():
if os.path.exists(self.mirror_path):
# If the mirror path exists but self.exists() returns false, we're
# in an unexpected state. Nuke the previous mirror directory and
# start fresh.
gclient_utils.rmtree(self.mirror_path)
os.mkdir(self.mirror_path)
elif not reset_fetch_config:
# Re-bootstrapping an existing mirror; preserve existing fetch spec.
self._preserve_fetchspec()
bootstrapped = (not depth and bootstrap and
self.bootstrap_repo(self.mirror_path))
......@@ -571,16 +573,17 @@ class Mirror(object):
lockfile.lock()
try:
self._ensure_bootstrapped(depth, bootstrap)
self._fetch(self.mirror_path, verbose, depth, no_fetch_tags,
reset_fetch_config)
self._ensure_bootstrapped(depth, bootstrap, reset_fetch_config)
self._fetch(
self.mirror_path, verbose, depth, no_fetch_tags, reset_fetch_config)
except ClobberNeeded:
# This is a major failure, we need to clean and force a bootstrap.
gclient_utils.rmtree(self.mirror_path)
self.print(GIT_CACHE_CORRUPT_MESSAGE)
self._ensure_bootstrapped(depth, bootstrap, force=True)
self._fetch(self.mirror_path, verbose, depth, no_fetch_tags,
reset_fetch_config)
self._ensure_bootstrapped(
depth, bootstrap, reset_fetch_config, force=True)
self._fetch(
self.mirror_path, verbose, depth, no_fetch_tags, reset_fetch_config)
finally:
if not ignore_lock:
lockfile.unlock()
......
......@@ -84,6 +84,18 @@ class GitCacheTest(unittest.TestCase):
mirror.populate(reset_fetch_config=True)
def testPopulateTwice(self):
self.git(['init', '-q'])
with open(os.path.join(self.origin_dir, 'foo'), 'w') as f:
f.write('touched\n')
self.git(['add', 'foo'])
self.git(['commit', '-m', 'foo'])
mirror = git_cache.Mirror(self.origin_dir)
mirror.populate()
mirror.populate()
def _makeGitRepoWithTag(self):
self.git(['init', '-q'])
with open(os.path.join(self.origin_dir, 'foo'), 'w') as f:
......
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