]> git.scottworley.com Git - git-cache/blobdiff - git_cache.py
_setenv()
[git-cache] / git_cache.py
index aab5b070fdb15509f4fd74cd182db5ecfc541280..077406c63275d97d701e1e9498e1efca989045c1 100644 (file)
@@ -10,7 +10,7 @@ import os
 import subprocess
 import sys
 
-from typing import Tuple
+from typing import Tuple, Union
 
 import backoff
 
@@ -18,6 +18,7 @@ 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"
 Rev = str   # eg: "53a27350551844e1ed1a9257690294767389ef0d"
+RefOrRev = Union[Ref, Rev]
 
 
 def git_cachedir(repo: Repo) -> Path:
@@ -31,11 +32,26 @@ 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, descendant: RefOrRev, ancestor: RefOrRev) -> 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 %s is an ancestor of %s', ancestor, descendant)
+    process = subprocess.run(['git',
+                              '-C',
+                              cachedir,
+                              'merge-base',
+                              '--is-ancestor',
+                              ancestor,
+                              descendant],
+                             check=False)
+    return process.returncode == 0
+
+
+def verify_ancestry(
+        repo: Repo,
+        descendant: RefOrRev,
+        ancestor: RefOrRev) -> None:
+    if not is_ancestor(repo, descendant, ancestor):
+        raise Exception('%s is not an ancestor of %s' % (ancestor, descendant))
 
 
 @backoff.on_exception(
@@ -68,18 +84,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.',
@@ -87,6 +93,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