11#! /bin/bash
22
33# Pre-commit hook for Claude Code plugin
4- # Automatically formats Lua files with StyLua before commit
4+ # 1. Formats Lua files with StyLua
5+ # 2. Runs linting with luacheck
6+ # 3. Runs tests to ensure code quality
57
8+ # ======== Style Formatting ========
69# Check if stylua is installed
710if ! command -v stylua & > /dev/null; then
811 echo " Error: stylua is not installed. Please install it to format Lua code."
912 echo " You can install it from: https://github.com/JohnnyMorganz/StyLua"
1013 exit 1
1114fi
1215
16+ # Check for luacheck
17+ if ! command -v luacheck & > /dev/null; then
18+ echo " Warning: luacheck is not installed. Skipping lint checks."
19+ echo " You can install it using luarocks: luarocks install luacheck"
20+ HAS_LUACHECK=0
21+ else
22+ HAS_LUACHECK=1
23+ fi
24+
1325# Get all staged Lua files
1426STAGED_FILES=$( git diff --cached --name-only --diff-filter=ACM | grep -E ' \.lua$' )
1527
@@ -23,6 +35,75 @@ if [ -n "$STAGED_FILES" ]; then
2335 done
2436
2537 echo " Lua files have been formatted and staged."
38+
39+ # Run luacheck if available
40+ if [ " $HAS_LUACHECK " -eq 1 ]; then
41+ echo " Running luacheck on staged Lua files..."
42+ LINT_ISSUES=0
43+ FIXABLE_ISSUES=0
44+ FIXED_FILES=" "
45+
46+ # First check for issues that we can automatically fix
47+ for FILE in $STAGED_FILES ; do
48+ # Auto-fix trailing whitespace
49+ if grep -q " [[:space:]]$" " $FILE " ; then
50+ sed -i ' s/[[:space:]]*$//' " $FILE "
51+ FIXABLE_ISSUES=1
52+ FIXED_FILES=" $FIXED_FILES $FILE "
53+ fi
54+
55+ # Auto-fix line endings (ensure LF)
56+ if file " $FILE " | grep -q " CRLF" ; then
57+ dos2unix " $FILE " 2> /dev/null
58+ FIXABLE_ISSUES=1
59+ FIXED_FILES=" $FIXED_FILES $FILE "
60+ fi
61+ done
62+
63+ # If we fixed any issues, add them back to staging
64+ if [ " $FIXABLE_ISSUES " -eq 1 ]; then
65+ echo " Fixed some linting issues automatically in:$FIXED_FILES "
66+ for FILE in $FIXED_FILES ; do
67+ git add " $FILE "
68+ done
69+ fi
70+
71+ # Now run the full luacheck to see if there are remaining issues
72+ for FILE in $STAGED_FILES ; do
73+ luacheck " $FILE "
74+ if [ $? -ne 0 ]; then
75+ LINT_ISSUES=1
76+ fi
77+ done
78+
79+ if [ " $LINT_ISSUES " -eq 1 ]; then
80+ echo " Error: Lua lint issues found that couldn't be fixed automatically."
81+ echo " Please fix the remaining issues before committing."
82+ echo " You can bypass this check with git commit --no-verify"
83+ exit 1
84+ fi
85+ fi
86+
87+ # ======== Run Tests ========
88+ echo " Running tests to ensure code quality..."
89+
90+ # Find the project root directory (where .git is)
91+ PROJECT_ROOT=$( git rev-parse --show-toplevel)
92+
93+ # Change to the project root
94+ cd " $PROJECT_ROOT " || exit 1
95+
96+ # Run the tests directly with full path
97+ bash " ${PROJECT_ROOT} /scripts/test.sh"
98+
99+ # Check if tests passed
100+ if [ $? -ne 0 ]; then
101+ echo " ❌ Tests failed! Commit aborted."
102+ echo " Please fix the test failures before committing."
103+ exit 1
104+ fi
105+
106+ echo " ✅ All tests passed. Proceeding with commit."
26107fi
27108
28109exit 0
0 commit comments