Coverage for scripts/install_hooks.py: 100%
35 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
1"""Installs the repository's pre-commit hook into this clone's `.git/hooks` (V6.6.4).
3The hook (`.githooks/pre-commit`) runs `aq secrets-check`, the library gate, the formal inventory, the Scorecard pin check, ruff on the
4staged files and the coverage of the changed modules; `aq hooks install` calls `install()` and `aq hooks status` reads `installed()`.
5It is installed as a small wrapper in `.git/hooks/pre-commit`, not through `core.hooksPath`,
6because pointing `core.hooksPath` at `.githooks` would switch off the Git LFS hooks that already live in `.git/hooks`. An existing `pre-commit` that is not this wrapper is left alone and reported.
8 python scripts/install_hooks.py # install
9 python scripts/install_hooks.py --check # exit 1 if it is not installed
10"""
12from __future__ import annotations
14import subprocess
15import sys
16from pathlib import Path
18ROOT = Path(__file__).resolve().parents[1]
19MARKER = "# aether-quant pre-commit wrapper"
20WRAPPER = f"""#!/bin/sh
21{MARKER}: runs .githooks/pre-commit (secrets, library gate, formal inventory, pins, ruff, coverage). Reinstall with `aq hooks install`.
22repo_root=$(git rev-parse --show-toplevel)
23[ -f "$repo_root/.githooks/pre-commit" ] || exit 0 # a worktree of a data-only branch (forward-ledger) has no script to run
24exec sh "$repo_root/.githooks/pre-commit" "$@"
25"""
28def hooks_directory(root: Path = ROOT) -> Path:
29 result = subprocess.run(["git", "rev-parse", "--git-path", "hooks"], cwd=root, capture_output=True, text=True, check=True)
30 path = Path(result.stdout.strip())
31 return path if path.is_absolute() else root / path
34def installed(root: Path = ROOT) -> bool:
35 target = hooks_directory(root) / "pre-commit"
36 return target.exists() and MARKER in target.read_text(encoding="utf-8", errors="replace")
39def install(root: Path = ROOT) -> str:
40 target = hooks_directory(root) / "pre-commit"
41 if target.exists() and MARKER not in target.read_text(encoding="utf-8", errors="replace"):
42 return f"{target} exists and is not this wrapper: left alone"
43 target.parent.mkdir(parents=True, exist_ok=True)
44 target.write_text(WRAPPER, encoding="utf-8", newline="\n")
45 try:
46 target.chmod(0o755)
47 except OSError:
48 pass
49 return f"installed {target}"
52def main(argv: list[str] | None = None) -> int:
53 args = list(sys.argv[1:] if argv is None else argv)
54 if "--check" in args:
55 print("installed" if installed() else "not installed")
56 return 0 if installed() else 1
57 message = install()
58 print(message)
59 return 0 if installed() else 1
62if __name__ == "__main__":
63 raise SystemExit(main())