Skip to content

Add Husky pre-commit hook to lint staged files #1700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Store list of staged files before formatting
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR)

# Run lint-staged to format only staged files
echo "Running formatter on staged files..."
npx lint-staged

# Check if there are any changes in previously staged files
UNSTAGED_CHANGES=0
for FILE in $STAGED_FILES; do
if [ -f "$FILE" ] && git diff --quiet "$FILE"; then
# No changes in this file
continue
else
# Changes detected
UNSTAGED_CHANGES=1
echo "File modified after formatting: $FILE"
fi
done

if [ $UNSTAGED_CHANGES -eq 1 ]; then
echo "Error: There are unstaged changes after running the formatter."
echo "Please stage the modified files and try committing again."
exit 1
fi
Loading