diff --git a/bin/codeguard b/bin/codeguard index 71ebe25..bcb498c 100755 --- a/bin/codeguard +++ b/bin/codeguard @@ -43,7 +43,7 @@ case "$cmd" in engine) exec python3 "$ROOT/scripts/engine_report.py" "$@" ;; - init|version|discover|doctor|status|report) + init|version|discover|doctor|status|report|hooks) exec python3 "$ROOT/scripts/guard_cli.py" "$cmd" "$@" ;; *) diff --git a/scripts/check_architecture.py b/scripts/check_architecture.py index d14faa0..e11e685 100644 --- a/scripts/check_architecture.py +++ b/scripts/check_architecture.py @@ -21,6 +21,7 @@ "detect_lang.py": "entry", "dockerfile_security.py": "entry", "guard_cli.py": "entry", + "hooks_cli.py": "entry", "engine_report.py": "entry", "fix.py": "entry", "gen_language_docs.py": "entry", diff --git a/scripts/guard_cli.py b/scripts/guard_cli.py index f480754..ccd2fcc 100644 --- a/scripts/guard_cli.py +++ b/scripts/guard_cli.py @@ -98,6 +98,9 @@ def cmd_init(args): COMMANDS={"version":cmd_version,"discover":cmd_discover,"doctor":cmd_doctor,"status":cmd_status,"report":cmd_report,"init":cmd_init} def main(argv): + if argv and argv[0] == "hooks": + from hooks_cli import main as hooks_main + return hooks_main(argv[1:]) if not argv or argv[0] not in COMMANDS: print("usage: codeguard version|discover|doctor|status|report|init [--json] [path]",file=sys.stderr);return 2 return COMMANDS[argv[0]](argv[1:]) diff --git a/scripts/hooks_cli.py b/scripts/hooks_cli.py new file mode 100644 index 0000000..f820a69 --- /dev/null +++ b/scripts/hooks_cli.py @@ -0,0 +1,61 @@ +"""Install and inspect the repository pre-commit hook.""" +from __future__ import annotations + +import json +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MARKER = "# codeguard-managed" + + +def git_dir(root: Path): + out = subprocess.run(["git", "-C", str(root), "rev-parse", "--git-dir"], capture_output=True, text=True, check=False) + if out.returncode: + return None + return (root / out.stdout.strip()).resolve() + + +def hook_path(root: Path): + directory = git_dir(root) + return None if directory is None else directory / "hooks" / "pre-commit" + + +def status(root: Path): + path = hook_path(root) + if path is None: + return {"git": False, "installed": False, "managed": False, "executable": False, "path": None} + text = path.read_text(errors="replace") if path.is_file() else "" + return {"git": True, "installed": path.is_file(), "managed": MARKER in text, + "executable": bool(path.is_file() and path.stat().st_mode & 0o111), "path": str(path)} + + +def main(argv): + action = argv[0] if argv else "status" + root = Path.cwd().resolve() + if action == "status": + data = status(root) + print(json.dumps({"tool": "codeguard", "command": "codeguard.hooks.status", "hooks": data}, ensure_ascii=False, indent=2)) + return 0 + if action != "install": + print("usage: codeguard hooks install|status [--force]", file=sys.stderr) + return 2 + path = hook_path(root) + if path is None: + print(json.dumps({"decision": "deny", "error": "not_a_git_repository"}, ensure_ascii=False)) + return 1 + if path.exists(): + text = path.read_text(errors="replace") + if MARKER not in text and "--force" not in argv: + print(json.dumps({"decision": "deny", "error": "existing_hook_not_managed", "path": str(path)}, ensure_ascii=False)) + return 1 + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(f'#!/bin/sh\n{MARKER}\nexec "{ROOT}/bin/codeguard" check --quiet .\n') + path.chmod(0o755) + print(json.dumps({"decision": "allow", "installed": True, "path": str(path)}, ensure_ascii=False)) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main(sys.argv[1:]))