]> git.scottworley.com Git - git-cache/blobdiff - git_cache.py
Start on 1.4.0
[git-cache] / git_cache.py
index 594fb8a77cdad825a2783723ffc04bbd6c951b71..48430033e7afa805d307cf208b46c52a8e23bd0f 100644 (file)
@@ -12,6 +12,8 @@ import sys
 
 from typing import Tuple
 
+import backoff
+
 Path = str  # eg: "/home/user/.cache/git-cache/v1"
 Repo = str  # eg: "https://github.com/NixOS/nixpkgs.git"
 Ref = str   # eg: "master" or "v1.0.0"
@@ -29,24 +31,39 @@ def git_cachedir(repo: Repo) -> Path:
         hashlib.sha256(repo.encode()).hexdigest()))
 
 
-def verify_ancestry(repo: Repo, ref: Ref, rev: Rev) -> None:
+def is_ancestor(repo: Repo, ref: Ref, rev: Rev) -> bool:
     cachedir = git_cachedir(repo)
-    logging.debug('Verifying rev %s is an ancestor of ref "%s"', rev, ref)
-    subprocess.run(['git', '-C', cachedir, 'merge-base', '--is-ancestor',
-                    rev, ref], check=True)
+    logging.debug('Checking if rev %s is an ancestor of ref "%s"', rev, ref)
+    process = subprocess.run(
+        ['git', '-C', cachedir, 'merge-base', '--is-ancestor', rev, ref], check=False)
+    return process.returncode == 0
+
+
+def verify_ancestry(repo: Repo, ref: Ref, rev: Rev) -> None:
+    if not is_ancestor(repo, ref, rev):
+        raise Exception('Rev %s is not an ancestor of ref "%s"' % (rev, ref))
+
+
+@backoff.on_exception(
+    backoff.expo,
+    subprocess.CalledProcessError,
+    max_time=lambda: int(os.environ.get('BACKOFF_MAX_TIME', '30')))
+def _git_fetch(cachedir: Path, repo: Repo, ref: Ref) -> None:
+    # We don't use --force here because we want to abort and freak out if forced
+    # updates are happening.
+    subprocess.run(['git', '-C', cachedir, 'fetch', repo,
+                    '%s:%s' % (ref, ref)], check=True)
 
 
 def fetch(repo: Repo, ref: Ref) -> Tuple[Path, Rev]:
     cachedir = git_cachedir(repo)
     if not os.path.exists(cachedir):
         logging.debug("Initializing git repo")
-        subprocess.run(['git', 'init', '--bare', cachedir], check=True)
+        subprocess.run(['git', 'init', '--bare', cachedir],
+                       check=True, stdout=sys.stderr)
 
     logging.debug('Fetching ref "%s" from %s', ref, repo)
-    # We don't use --force here because we want to abort and freak out if forced
-    # updates are happening.
-    subprocess.run(['git', '-C', cachedir, 'fetch', repo,
-                    '%s:%s' % (ref, ref)], check=True)
+    _git_fetch(cachedir, repo, ref)
 
     with open(os.path.join(cachedir, 'refs', 'heads', ref)) as rev_file:
         rev = Rev(rev_file.read(999).strip())
@@ -57,18 +74,8 @@ def fetch(repo: Repo, ref: Ref) -> Tuple[Path, Rev]:
 
 def ensure_rev_available(repo: Repo, ref: Ref, rev: Rev) -> Path:
     cachedir = git_cachedir(repo)
-    if os.path.exists(cachedir):
-        logging.debug('Checking if we already have rev %s', rev)
-        process = subprocess.run(
-            ['git', '-C', cachedir, 'cat-file', '-e', rev], check=False)
-        if process.returncode == 0:
-            logging.debug('We already have rev %s', rev)
-            verify_ancestry(repo, ref, rev)
-            return cachedir
-        if process.returncode != 1:
-            raise Exception(
-                'Could not test for presence of rev %s.  Is cache dir "%s" messed up?' %
-                (rev, cachedir))
+    if os.path.exists(cachedir) and is_ancestor(repo, ref, rev):
+        return cachedir
 
     logging.debug(
         'We do not have rev %s.  We will fetch ref "%s" and hope it appears.',
@@ -76,6 +83,7 @@ def ensure_rev_available(repo: Repo, ref: Ref, rev: Rev) -> Path:
     fetch(repo, ref)
     logging.debug('Verifying that fetch retrieved rev %s', rev)
     subprocess.run(['git', '-C', cachedir, 'cat-file', '-e', rev], check=True)
+    verify_ancestry(repo, ref, rev)
 
     return cachedir