Skip to content

Commit c89821a

Browse files
authored
Add Husky pre-commit hook to lint staged files (#1700)
Adds a pre-commit hook that automatically lints staged files before commiting them. This should help avoid wasting time accidentally pushing up commits that fail the formatter in CI.
1 parent 6fb0818 commit c89821a

File tree

3 files changed

+892
-10
lines changed

3 files changed

+892
-10
lines changed

.husky/pre-commit

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Store list of staged files before formatting
2+
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR)
3+
4+
# Run lint-staged to format only staged files
5+
echo "Running formatter on staged files..."
6+
npx lint-staged
7+
8+
# Check if there are any changes in previously staged files
9+
UNSTAGED_CHANGES=0
10+
for FILE in $STAGED_FILES; do
11+
if [ -f "$FILE" ] && git diff --quiet "$FILE"; then
12+
# No changes in this file
13+
continue
14+
else
15+
# Changes detected
16+
UNSTAGED_CHANGES=1
17+
echo "File modified after formatting: $FILE"
18+
fi
19+
done
20+
21+
if [ $UNSTAGED_CHANGES -eq 1 ]; then
22+
echo "Error: There are unstaged changes after running the formatter."
23+
echo "Please stage the modified files and try committing again."
24+
exit 1
25+
fi

0 commit comments

Comments
 (0)