|
1 | 1 | #!/bin/bash |
2 | 2 | # Stop hook: batch-format PHP files touched this turn, run test suite, report issues. |
3 | | -# Tests are fast (~2s) so we include them every turn - failures are reported as context. |
| 3 | +# Tests are fast (~2s) so we include them when files were edited. |
| 4 | +# Failures are reported as context, not blocking. |
4 | 5 | cd "$CLAUDE_PROJECT_DIR" || exit 0 |
5 | 6 |
|
6 | | -TRACKER="/tmp/claude-edited-php-files-${CLAUDE_HOOK_SESSION_ID:-default}" |
| 7 | +TRACKER="${TMPDIR:-/tmp}/claude-edited-php-files-${CLAUDE_HOOK_SESSION_ID:-default}" |
| 8 | + |
| 9 | +# Skip entirely if no files were edited this turn |
| 10 | +[[ -f "$TRACKER" ]] || exit 0 |
| 11 | + |
| 12 | +files=$(cat "$TRACKER") |
| 13 | +rm -f "$TRACKER" |
| 14 | +[[ -n "$files" ]] || exit 0 |
7 | 15 |
|
8 | 16 | ctx="" |
9 | 17 |
|
10 | | -# Batch php-cs-fixer autocorrect on tracked files (if any were edited) |
11 | | -if [[ -f "$TRACKER" ]]; then |
12 | | - files=$(cat "$TRACKER") |
13 | | - rm -f "$TRACKER" |
14 | | - |
15 | | - if [[ -n "$files" ]]; then |
16 | | - if docker image inspect chartmogulphp82 &>/dev/null 2>&1; then |
17 | | - file_args=$(echo "$files" | tr '\n' ' ') |
18 | | - docker run --rm -w /src -v "$(pwd):/src" -v "$HOME/.composer/cache:/root/.composer/cache" chartmogulphp82 \ |
19 | | - bash -c "vendor/bin/php-cs-fixer fix --no-interaction --quiet $file_args" 2>/dev/null || true |
20 | | - lint_out=$(docker run --rm -w /src -v "$(pwd):/src" -v "$HOME/.composer/cache:/root/.composer/cache" chartmogulphp82 \ |
21 | | - bash -c "vendor/bin/php-cs-fixer fix --dry-run --no-interaction $file_args 2>&1") |
22 | | - elif [[ -x vendor/bin/php-cs-fixer ]]; then |
23 | | - echo "$files" | xargs vendor/bin/php-cs-fixer fix --no-interaction --quiet 2>/dev/null || true |
24 | | - lint_out=$(echo "$files" | xargs vendor/bin/php-cs-fixer fix --dry-run --no-interaction 2>&1) |
25 | | - fi |
26 | | - offenses=$(echo "$lint_out" | grep -E "^\s+\d+\)" | head -20) |
27 | | - if [[ -n "$offenses" ]]; then |
28 | | - ctx+="php-cs-fixer offenses remaining after autocorrect:\n$offenses\n" |
29 | | - fi |
30 | | - fi |
| 18 | +# Batch php-cs-fixer autocorrect on tracked files |
| 19 | +if docker image inspect chartmogulphp82 &>/dev/null 2>&1; then |
| 20 | + file_args=$(echo "$files" | tr '\n' ' ') |
| 21 | + docker run --rm -w /src -v "$(pwd):/src" -v "$HOME/.composer/cache:/root/.composer/cache" chartmogulphp82 \ |
| 22 | + bash -c "vendor/bin/php-cs-fixer fix --no-interaction --quiet $file_args" 2>/dev/null || true |
| 23 | + lint_out=$(docker run --rm -w /src -v "$(pwd):/src" -v "$HOME/.composer/cache:/root/.composer/cache" chartmogulphp82 \ |
| 24 | + bash -c "vendor/bin/php-cs-fixer fix --dry-run --no-interaction $file_args 2>&1") |
| 25 | +elif [[ -x vendor/bin/php-cs-fixer ]]; then |
| 26 | + echo "$files" | xargs vendor/bin/php-cs-fixer fix --no-interaction --quiet 2>/dev/null || true |
| 27 | + lint_out=$(echo "$files" | xargs vendor/bin/php-cs-fixer fix --dry-run --no-interaction 2>&1) |
| 28 | +fi |
| 29 | +offenses=$(echo "$lint_out" | grep -E "^\s+\d+\)" | head -20) |
| 30 | +if [[ -n "$offenses" ]]; then |
| 31 | + ctx+="php-cs-fixer offenses remaining after autocorrect:\n$offenses\n" |
31 | 32 | fi |
32 | 33 |
|
33 | | -# Always run tests - edits to tests or src both matter (~2s) |
| 34 | +# Run tests - only when files were edited this turn (~2s) |
34 | 35 | if docker image inspect chartmogulphp82 &>/dev/null 2>&1; then |
35 | 36 | test_out=$(docker run --rm -w /src -v "$(pwd):/src" -v "$HOME/.composer/cache:/root/.composer/cache" chartmogulphp82 \ |
36 | 37 | phpunit 2>&1) |
|
0 commit comments