23 lines
882 B
Python
23 lines
882 B
Python
import pathlib
|
|
|
|
from failure import Failure
|
|
import git_repo
|
|
|
|
|
|
def git(directory: str, *args: str, timeout: float | None = None) -> str | Failure:
|
|
"""What git printed in directory, stripped, run guarded: the hook runs git in a checkout the call has not yet been allowed to touch."""
|
|
ran = git_repo.git(*args, cwd=directory, timeout=timeout, guarded=True)
|
|
return ran if isinstance(ran, Failure) else ran.strip()
|
|
|
|
|
|
def local_default_branch(directory: str) -> str | None:
|
|
"""The default branch as the checkout last saw it, with no fetch, which the hook must not run."""
|
|
return next((name for name in ("origin/HEAD", "main", "master") if not isinstance(git(directory, "rev-parse", "--verify", "--quiet", name), Failure)), None)
|
|
|
|
|
|
def existing(directory: str) -> str:
|
|
path = pathlib.Path(directory or ".").absolute()
|
|
while not path.is_dir():
|
|
path = path.parent
|
|
return str(path)
|