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/+/2120281
Reviewed-by: Josip Sokcevic <sokcevic@google.com>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
diff --git a/git_cache.py b/git_cache.py
index b7ed767..5184c5b 100755
--- a/git_cache.py
+++ b/git_cache.py
@@ -481,17 +481,19 @@
                    '%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 @@
             '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 @@
       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()