Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/codeguard
Original file line number Diff line number Diff line change
Expand Up @@ -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" "$@"
;;
*)
Expand Down
1 change: 1 addition & 0 deletions scripts/check_architecture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions scripts/guard_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:])
Expand Down
61 changes: 61 additions & 0 deletions scripts/hooks_cli.py
Original file line number Diff line number Diff line change
@@ -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:]))
Loading