소스 검색

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>
Edward Lesmes 5 년 전
부모
커밋
34f71abca1
2개의 변경된 파일28개의 추가작업 그리고 13개의 파일을 삭제
  1. 16 13
      git_cache.py
  2. 12 0
      tests/git_cache_test.py

+ 16 - 13
git_cache.py

@@ -481,17 +481,19 @@ class Mirror(object):
                    '%s and "git cache fetch" again.'
                    '%s and "git cache fetch" again.'
                    % os.path.join(self.mirror_path, 'config'))
                    % 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_dir = os.path.join(self.mirror_path, 'objects', 'pack')
     pack_files = []
     pack_files = []
     if os.path.isdir(pack_dir):
     if os.path.isdir(pack_dir):
       pack_files = [f for f in os.listdir(pack_dir) if f.endswith('.pack')]
       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))
                 (self.mirror_path, len(pack_files), GC_AUTOPACKLIMIT))
 
 
     should_bootstrap = (force or
     should_bootstrap = (force or
                         not self.exists() 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 not should_bootstrap:
       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')):
@@ -499,16 +501,16 @@ class Mirror(object):
             'Shallow fetch requested, but repo cache already exists.')
             'Shallow fetch requested, but repo cache already exists.')
       return
       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 os.path.exists(self.mirror_path):
         # If the mirror path exists but self.exists() returns false, we're
         # If the mirror path exists but self.exists() returns false, we're
         # in an unexpected state. Nuke the previous mirror directory and
         # in an unexpected state. Nuke the previous mirror directory and
         # start fresh.
         # start fresh.
         gclient_utils.rmtree(self.mirror_path)
         gclient_utils.rmtree(self.mirror_path)
       os.mkdir(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
     bootstrapped = (not depth and bootstrap and
                     self.bootstrap_repo(self.mirror_path))
                     self.bootstrap_repo(self.mirror_path))
@@ -571,16 +573,17 @@ class Mirror(object):
       lockfile.lock()
       lockfile.lock()
 
 
     try:
     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:
     except ClobberNeeded:
       # This is a major failure, we need to clean and force a bootstrap.
       # This is a major failure, we need to clean and force a bootstrap.
       gclient_utils.rmtree(self.mirror_path)
       gclient_utils.rmtree(self.mirror_path)
       self.print(GIT_CACHE_CORRUPT_MESSAGE)
       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:
     finally:
       if not ignore_lock:
       if not ignore_lock:
         lockfile.unlock()
         lockfile.unlock()

+ 12 - 0
tests/git_cache_test.py

@@ -84,6 +84,18 @@ class GitCacheTest(unittest.TestCase):
 
 
     mirror.populate(reset_fetch_config=True)
     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):
   def _makeGitRepoWithTag(self):
     self.git(['init', '-q'])
     self.git(['init', '-q'])
     with open(os.path.join(self.origin_dir, 'foo'), 'w') as f:
     with open(os.path.join(self.origin_dir, 'foo'), 'w') as f: