|
| 1 | +""" |
| 2 | +Git Pre-Commit Hook Installer for CodeSlim. |
| 3 | +
|
| 4 | +Installs a local Git `pre-commit` hook into `.git/hooks/pre-commit` that |
| 5 | +intercepts `git commit` commands, identifies staged Python files, runs Node 2.5 |
| 6 | +LibCST deterministic dead import removal in < 50ms, and updates the commit stage. |
| 7 | +""" |
| 8 | + |
| 9 | +import stat |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +from codeslim.utils.logger import get_logger |
| 13 | + |
| 14 | +log = get_logger("codeslim.hooks") |
| 15 | + |
| 16 | +PRE_COMMIT_SCRIPT_CONTENT = """#!/bin/sh |
| 17 | +# CodeSlim Pre-Commit Guardrail Hook |
| 18 | +# Automatically strips dead imports and verifies code quality before commit. |
| 19 | +
|
| 20 | +echo "🔍 [CodeSlim] Running pre-commit static bloat audit..." |
| 21 | +
|
| 22 | +# Find staged Python files |
| 23 | +STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\\.py$') |
| 24 | +
|
| 25 | +if [ -z "$STAGED_FILES" ]; then |
| 26 | + echo "✨ [CodeSlim] No staged Python files detected." |
| 27 | + exit 0 |
| 28 | +fi |
| 29 | +
|
| 30 | +for FILE in $STAGED_FILES; do |
| 31 | + if [ -f "$FILE" ]; then |
| 32 | + python -m codeslim.cli optimize "$FILE" --apply --no-llm > /dev/null 2>&1 |
| 33 | + git add "$FILE" |
| 34 | + fi |
| 35 | +done |
| 36 | +
|
| 37 | +echo "✅ [CodeSlim] Clean code contract verified & staged!" |
| 38 | +exit 0 |
| 39 | +""" |
| 40 | + |
| 41 | + |
| 42 | +def generate_pre_commit_script() -> str: |
| 43 | + """ |
| 44 | + Generate shell script content for .git/hooks/pre-commit. |
| 45 | +
|
| 46 | + Returns: |
| 47 | + String containing pre-commit bash script. |
| 48 | + """ |
| 49 | + return PRE_COMMIT_SCRIPT_CONTENT.strip() + "\n" |
| 50 | + |
| 51 | + |
| 52 | +def install_git_pre_commit_hook(repo_dir: Path | str = ".") -> bool: |
| 53 | + """ |
| 54 | + Install CodeSlim pre-commit hook into target repository's .git/hooks/ directory. |
| 55 | +
|
| 56 | + Args: |
| 57 | + repo_dir: Target repository root directory. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + True if hook installed successfully, False if .git directory not found. |
| 61 | + """ |
| 62 | + repo_path = Path(repo_dir).resolve() |
| 63 | + git_dir = repo_path / ".git" |
| 64 | + |
| 65 | + if not git_dir.is_dir(): |
| 66 | + log.error("git_directory_not_found", path=str(repo_path)) |
| 67 | + return False |
| 68 | + |
| 69 | + hooks_dir = git_dir / "hooks" |
| 70 | + hooks_dir.mkdir(parents=True, exist_ok=True) |
| 71 | + |
| 72 | + hook_file = hooks_dir / "pre-commit" |
| 73 | + script_content = generate_pre_commit_script() |
| 74 | + |
| 75 | + hook_file.write_text(script_content, encoding="utf-8") |
| 76 | + |
| 77 | + # Make hook file executable (chmod +x) |
| 78 | + try: |
| 79 | + current_permissions = hook_file.stat().st_mode |
| 80 | + hook_file.chmod(current_permissions | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
| 81 | + except Exception as exc: |
| 82 | + log.warning("could_not_set_chmod_x", file=str(hook_file), error=str(exc)) |
| 83 | + |
| 84 | + log.info("pre_commit_hook_installed", path=str(hook_file)) |
| 85 | + return True |
0 commit comments