From 60a375960ffaa0aa8675f676f2cbcf7fe27b7804 Mon Sep 17 00:00:00 2001 From: Jon Leopard Date: Sun, 16 Nov 2025 22:21:54 -0700 Subject: [PATCH] refactor: improve pre-commit hook to format only staged files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes: - Format only staged PHP files (not all dirty files) - Auto-stage only the files that were already staged - Prevents accidentally staging unstaged changes - Safer for partial staging workflows - Maintains full automation for normal commits This aligns with industry best practices from lint-staged and prevents security risks from staging unintended files. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .githooks/README.md | 5 +++-- .githooks/pre-commit | 43 +++++++++++++++++++++++++++++-------------- routes/web.php | 1 + 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/.githooks/README.md b/.githooks/README.md index e8fc7ffb..c318e584 100644 --- a/.githooks/README.md +++ b/.githooks/README.md @@ -23,10 +23,11 @@ chmod +x .githooks/* ### `pre-commit` - Laravel Pint Auto-formatter ✅ ENFORCED **What it does:** -- Runs Laravel Pint on staged files only (`--dirty`) +- Runs Laravel Pint on staged PHP files only (not unstaged files) - Auto-formats code to match team standards -- Automatically stages formatted files +- Automatically re-stages the formatted files - Fast feedback (runs in seconds) +- Safe: Won't format or stage files you haven't explicitly added **When it runs:** - Every time you run `git commit` diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 99a168f3..dd3e0a4c 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,22 +1,37 @@ #!/bin/sh # Laravel Pint Pre-commit Hook -# Automatically format code before committing +# Automatically format staged PHP files before committing echo "🎨 Running Laravel Pint..." -# Run Pint on staged files only -./vendor/bin/pint --dirty +# Get only staged PHP files +STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.php$') -# Check if Pint made changes -if ! git diff --quiet; then - echo "✅ Code formatted! Files have been updated." - echo " Changes have been staged automatically." - - # Stage the formatted files - git add -u - - exit 0 -else - echo "✅ Code style looks good!" +# Exit early if no PHP files are staged +if [ -z "$STAGED_FILES" ]; then + echo "✅ No PHP files staged" exit 0 fi + +# Run Pint only on staged files +echo "$STAGED_FILES" | xargs ./vendor/bin/pint + +# Check if Pint made changes to any of the staged files +CHANGED=false +for file in $STAGED_FILES; do + if ! git diff --quiet "$file" 2>/dev/null; then + CHANGED=true + break + fi +done + +# Re-stage the formatted files (only the ones that were already staged) +if [ "$CHANGED" = true ]; then + echo "✅ Code formatted! Files have been updated." + echo " Changes have been staged automatically." + echo "$STAGED_FILES" | xargs git add +else + echo "✅ Code style looks good!" +fi + +exit 0 diff --git a/routes/web.php b/routes/web.php index 553c14ec..239063b0 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,6 @@