From 0d009eb1f869e3a744a5f266a19dab2a6984f145 Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Sat, 4 Apr 2026 22:19:37 -0500 Subject: [PATCH] chore: pre-commit hook auto-fixes ruff violations before blocking Instead of failing and requiring manual fix + re-commit, the hook now runs ruff check --fix first, re-stages the fixed files, then checks for remaining unfixable issues. Co-Authored-By: Claude Opus 4.6 (1M context) --- .githooks/install-hooks.sh | 31 ++++++++++++++++++++++++++++++ .githooks/pre-commit | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100755 .githooks/install-hooks.sh create mode 100755 .githooks/pre-commit diff --git a/.githooks/install-hooks.sh b/.githooks/install-hooks.sh new file mode 100755 index 0000000..39da684 --- /dev/null +++ b/.githooks/install-hooks.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# +# Install git hooks for this repository +# + +REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null) + +if [ -z "$REPO_ROOT" ]; then + echo "Error: Not in a git repository" + exit 1 +fi + +HOOKS_DIR="$REPO_ROOT/.githooks" +GIT_HOOKS_DIR="$REPO_ROOT/.git/hooks" + +echo "Installing git hooks..." + +if [ -f "$HOOKS_DIR/pre-commit" ]; then + cp "$HOOKS_DIR/pre-commit" "$GIT_HOOKS_DIR/pre-commit" + chmod +x "$GIT_HOOKS_DIR/pre-commit" + echo "Installed pre-commit hook" +else + echo "pre-commit hook not found in $HOOKS_DIR" +fi + +echo "" +echo "The pre-commit hook will:" +echo " - Run ruff lint checks" +echo " - Block commits on syntax errors or lint failures" +echo "" +echo "To bypass in emergency: git commit --no-verify" diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..a40a8b3 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,39 @@ +#!/bin/bash +# +# Pre-commit hook: ruff lint check on staged Python files. +# Catches syntax errors, unused imports, and basic issues before commit. +# To bypass in emergency: git commit --no-verify +# + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' + +REPO_ROOT=$(git rev-parse --show-toplevel) +cd "$REPO_ROOT" + +STAGED_PY=$(git diff --cached --name-only --diff-filter=ACM -- '*.py') +if [ -z "$STAGED_PY" ]; then + exit 0 +fi + +echo "ruff check on staged files..." + +# Auto-fix what we can, then re-stage the fixed files +echo "$STAGED_PY" | xargs ruff check --fix --exit-zero +echo "$STAGED_PY" | xargs git add + +# Now check for remaining unfixable issues +echo "$STAGED_PY" | xargs ruff check +RUFF_EXIT=$? + +if [ $RUFF_EXIT -ne 0 ]; then + echo "" + echo -e "${RED}Pre-commit checks failed (unfixable issues). Commit blocked.${NC}" + echo -e "${YELLOW}To bypass (not recommended): git commit --no-verify${NC}" + exit 1 +fi + +echo -e "${GREEN}All checks passed.${NC}" +exit 0