|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Pre-commit hygiene: |
| 3 | +# - Laravel Pint: checks only changed files and auto-fixes |
| 4 | +# - Composer Normalize: formats composer.json deterministically |
| 5 | +# - Test inventory doc: refreshes docs/tests.md |
| 6 | + |
| 7 | +set -euo pipefail |
| 8 | + |
| 9 | +# Ensure we're at repo root even if hook runs from .git/hooks |
| 10 | +cd "$(git rev-parse --show-toplevel)" |
| 11 | + |
| 12 | +# Only run if composer.json exists and Pint is installed |
| 13 | +if [[ ! -f composer.json ]]; then |
| 14 | + exit 0 |
| 15 | +fi |
| 16 | + |
| 17 | +if [[ ! -x vendor/bin/pint ]]; then |
| 18 | + # Try running via Composer bin proxy if available |
| 19 | + if ! command -v composer >/dev/null 2>&1; then |
| 20 | + echo "composer not found; skipping Pint pre-commit" >&2 |
| 21 | + exit 0 |
| 22 | + fi |
| 23 | +fi |
| 24 | + |
| 25 | +# First, operate only on staged PHP files to avoid touching unstaged work. |
| 26 | +# - Collect staged PHP files (Added/Copied/Modified) |
| 27 | +# - Run Pint only on that list (use --test first), then fix and re-add only those files |
| 28 | +_STAGED_PHP=() |
| 29 | +# Portable collection of staged PHP files (avoids mapfile/readarray so it works on macOS' default Bash) |
| 30 | +while IFS= read -r _f; do |
| 31 | + _STAGED_PHP+=("${_f}") |
| 32 | +done < <(git diff --name-only --cached --diff-filter=ACM -- '*.php' || true) |
| 33 | +if [[ ${#_STAGED_PHP[@]} -gt 0 ]]; then |
| 34 | + # Ensure Pint binary exists (we checked earlier) and run tests against staged files |
| 35 | + if ! vendor/bin/pint --test "${_STAGED_PHP[@]}" >/dev/null 2>&1; then |
| 36 | + echo "Pint found style issues in staged files. Applying fixes..." >&2 |
| 37 | + # Run Pint to apply fixes to the same staged files only |
| 38 | + vendor/bin/pint "${_STAGED_PHP[@]}" || true |
| 39 | + # Re-add only the files we targeted (don't use a blanket add) |
| 40 | + git add -- "${_STAGED_PHP[@]}" |
| 41 | + fi |
| 42 | +else |
| 43 | + # No staged PHP files to lint/format |
| 44 | + : |
| 45 | +fi |
| 46 | + |
| 47 | +# Composer Normalize (if available) |
| 48 | +if command -v composer >/dev/null 2>&1; then |
| 49 | + if composer normalize --dry-run --no-interaction >/dev/null 2>&1; then |
| 50 | + : # already normalized |
| 51 | + else |
| 52 | + echo "Normalizing composer.json..." >&2 |
| 53 | + composer normalize --no-interaction || true |
| 54 | + if [[ -f composer.json ]]; then |
| 55 | + git add composer.json || true |
| 56 | + fi |
| 57 | + if [[ -f composer.lock ]]; then |
| 58 | + git add composer.lock || true |
| 59 | + fi |
| 60 | + fi |
| 61 | +fi |
| 62 | + |
| 63 | +# Generate/refresh test inventory docs to keep docs/tests.md current |
| 64 | +if command -v composer >/dev/null 2>&1; then |
| 65 | + if composer run -q docs:tests >/dev/null 2>&1; then |
| 66 | + if ! git diff --quiet -- docs/tests.md; then |
| 67 | + git add docs/tests.md |
| 68 | + echo "Updated docs/tests.md (test inventory)." >&2 |
| 69 | + fi |
| 70 | + fi |
| 71 | +fi |
| 72 | + |
| 73 | +exit 0 |
0 commit comments