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