From: Scott Worley Date: Mon, 21 Jun 2021 19:35:51 +0000 (-0700) Subject: Allow ancestry checks beween two revs X-Git-Tag: v1.4.0~5 X-Git-Url: http://git.scottworley.com/git-cache/commitdiff_plain/a5d42d8d29f3f7327176f33001bd016fc2c867cf?ds=sidebyside;hp=866f291808c96e89c86adaa63634d89a58526ec7 Allow ancestry checks beween two revs --- diff --git a/git_cache.py b/git_cache.py index 4843003..077406c 100644 --- a/git_cache.py +++ b/git_cache.py @@ -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,17 +32,26 @@ def git_cachedir(repo: Repo) -> Path: hashlib.sha256(repo.encode()).hexdigest())) -def is_ancestor(repo: Repo, ref: Ref, rev: Rev) -> bool: +def is_ancestor(repo: Repo, descendant: RefOrRev, ancestor: RefOrRev) -> bool: cachedir = git_cachedir(repo) - 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) + 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, 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)) +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(