Files
knowledge-base-framework/scripts/install-hooks.py

44 lines
1.7 KiB
Python

#!/usr/bin/env python3
"""Install a vault-local hook that invokes this framework without copying its tools."""
import argparse
import shlex
import subprocess
from pathlib import Path
from vault_root import FRAMEWORK_ROOT, parse_vault_args
def main():
parser = argparse.ArgumentParser(description=__doc__)
root = parse_vault_args(parser).vault_root
repo = subprocess.run(["git", "-C", str(root), "rev-parse", "--show-toplevel"],
capture_output=True, text=True)
if repo.returncode or repo.stdout.strip() != str(root):
parser.error("vault root must be the Git repository root to install hooks")
result = subprocess.run(
["git", "-C", str(root), "rev-parse", "--path-format=absolute", "--git-path", "hooks"],
capture_output=True, text=True, check=True,
)
hook = Path(result.stdout.strip()) / "pre-commit"
marker = "# knowledge-base framework hook"
if hook.exists() or hook.is_symlink():
legacy = hook.is_symlink() and hook.resolve() in {
root / "scripts/pre-commit", FRAMEWORK_ROOT / "scripts/pre-commit",
}
managed = not hook.is_symlink() and marker in hook.read_text()
if not legacy and not managed:
parser.error(f"refusing to replace unmanaged hook: {hook}")
script = FRAMEWORK_ROOT / "scripts/pre-commit"
content = f"#!/bin/sh\n{marker}\nexec bash {shlex.quote(str(script))} {shlex.quote(str(root))}\n"
hook.parent.mkdir(parents=True, exist_ok=True)
if hook.is_symlink():
hook.unlink()
hook.write_text(content)
hook.chmod(0o755)
print(f"Installed {hook} -> {script} (vault: {root})")
if __name__ == "__main__":
main()