-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·80 lines (70 loc) · 2.61 KB
/
Copy pathpre-commit
File metadata and controls
executable file
·80 lines (70 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
# Git pre-commit hook — auto-fix formatting + block on real errors
# Auto-fixes Pint/ESLint formatting, re-stages fixed files, blocks on syntax errors.
# Install: git config core.hooksPath scripts/hooks
set -euo pipefail
# Ensure dev tools are on PATH across macOS, Linux, and Windows (Git Bash).
# On macOS, Homebrew typically installs to /opt/homebrew or /usr/local.
# On Windows, winget links and Composer's setup directory must be added
# explicitly because git's hook environment doesn't always inherit them.
export PATH="/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:$PATH"
if [ -d "/c/Users/${USERNAME:-$USER}/AppData/Local/Microsoft/WinGet/Links" ]; then
export PATH="/c/Users/${USERNAME:-$USER}/AppData/Local/Microsoft/WinGet/Links:$PATH"
fi
# winget PHP installs land in Packages/PHP.PHP.X.Y_*; pick the highest version.
WINGET_PHP_DIR=$(ls -d /c/Users/"${USERNAME:-$USER}"/AppData/Local/Microsoft/WinGet/Packages/PHP.PHP.*_*/ 2>/dev/null | sort -V | tail -1)
if [ -n "$WINGET_PHP_DIR" ] && [ -x "${WINGET_PHP_DIR}php.exe" ]; then
export PATH="${WINGET_PHP_DIR%/}:$PATH"
fi
if [ -d "/c/ProgramData/ComposerSetup/bin" ]; then
export PATH="/c/ProgramData/ComposerSetup/bin:$PATH"
fi
# Get staged files (only added/modified, not deleted)
STAGED_PHP=$(git diff --cached --name-only --diff-filter=d -- '*.php' || true)
STAGED_JS=$(git diff --cached --name-only --diff-filter=d -- '*.js' '*.vue' || true)
ERRORS=0
# --- PHP: syntax check + auto-format ---
if [ -n "$STAGED_PHP" ]; then
# Syntax check first
for file in $STAGED_PHP; do
if [ -f "$file" ]; then
if ! php -l "$file" 2>&1 | grep -q "No syntax errors"; then
echo " PHP syntax error: $file"
php -l "$file" 2>&1
ERRORS=1
fi
fi
done
# Auto-format with Pint (only staged files)
if [ -f "./vendor/bin/pint" ]; then
for file in $STAGED_PHP; do
if [ -f "$file" ]; then
./vendor/bin/pint "$file" --quiet 2>/dev/null || true
git add "$file"
fi
done
fi
fi
# --- JS/Vue: auto-fix + check for remaining errors ---
if [ -n "$STAGED_JS" ]; then
if command -v npx &>/dev/null; then
for file in $STAGED_JS; do
if [ -f "$file" ]; then
# Auto-fix what we can
npx eslint --fix "$file" 2>/dev/null || true
git add "$file"
# Check if unfixable errors remain
if ! npx eslint "$file" 2>/dev/null; then
echo " ESLint error (unfixable): $file"
ERRORS=1
fi
fi
done
fi
fi
if [ "$ERRORS" -ne 0 ]; then
echo ""
echo "Pre-commit hook blocked the commit. Fix the errors above and try again."
exit 1
fi
exit 0