From e7e9a96b152acd8694c258a6220ddfebaf359992 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 5 Feb 2026 06:34:38 +0000 Subject: [PATCH] Initial upload: Local AI Commit Reviewer CLI with CI/CD workflow --- src/hooks/hooks.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/hooks/hooks.py diff --git a/src/hooks/hooks.py b/src/hooks/hooks.py new file mode 100644 index 0000000..c14311e --- /dev/null +++ b/src/hooks/hooks.py @@ -0,0 +1,69 @@ +from pathlib import Path + + +def get_hook_script() -> str: + return """#!/bin/bash +# Local AI Commit Reviewer - Pre-commit Hook +# Automatically reviews staged changes before committing + +set -e + +# Allow bypass with --no-verify +if [ "$1" = "--no-verify" ]; then + exit 0 +fi + +# Get the directory where this script is located +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# Change to repository root +cd "$SCRIPT_DIR/../.." + +# Run the AI commit reviewer +python -m aicr review --hook --strictness balanced || exit 1 +""" + + +def install_pre_commit_hook( + repo_path: Path, + content: str | None = None, + force: bool = False +) -> bool: + hooks_dir = repo_path / ".git" / "hooks" + hooks_dir.mkdir(parents=True, exist_ok=True) + + hook_path = hooks_dir / "pre-commit" + + if hook_path.exists() and not force: + return False + + if content is None: + content = get_hook_script() + + try: + hook_path.write_text(content) + hook_path.chmod(0o755) + return True + except OSError: + return False + + +def check_hook_installed(repo_path: Path) -> bool: + hook_path = repo_path / ".git" / "hooks" / "pre-commit" + if not hook_path.exists(): + return False + + content = hook_path.read_text() + return "aicr" in content or "local-ai-commit-reviewer" in content + + +def uninstall_hook(repo_path: Path) -> bool: + hook_path = repo_path / ".git" / "hooks" / "pre-commit" + if not hook_path.exists(): + return True + + try: + hook_path.unlink() + return True + except OSError: + return False