diff --git a/git_cache.py b/git_cache.py index b7ed76705..5184c5b59 100755 --- a/git_cache.py +++ b/git_cache.py @@ -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() diff --git a/tests/git_cache_test.py b/tests/git_cache_test.py index 539ab2629..f3b5a2329 100755 --- a/tests/git_cache_test.py +++ b/tests/git_cache_test.py @@ -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: