Skip to content

Commit

Permalink
pre-commit hook: Ensure autoformatted files are added to the current …
Browse files Browse the repository at this point in the history
…commit

The current pre-commit hook does not stage the formatted files for
committing. As a result, files like blueprint-schema.json flicker
back and forth between their formatted and unformatted state as
different contributors have different committing strategies.

This commit is an attempt to standardize that behavior. Every formatted
file that was already staged for a commit, will now be added to that
very commit. This, in theory, should prevent misformatted files from
ever being committed.
  • Loading branch information
adamziel committed Dec 4, 2023
1 parent 90af3cf commit da87bc9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .husky/post-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

# Without this, git is reporting that the files formatted in the
# pre-commit hook are still modified after committing
UPDATED_FILES=$(git diff-tree --no-commit-id --name-only -r HEAD)
git update-index $UPDATED_FILES
13 changes: 13 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,17 @@
# This, weirdly, breaks nx:
# . "$(dirname -- "$0")/_/husky.sh"

# Save the list of files that are staged for commit
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)

# Run the format command
npm run format:uncommitted

# Re-add the files that were both staged and modified
for file in $STAGED_FILES; do
if git diff --name-only --diff-filter=ACM | grep -q "^$file$"; then
git add "$file"
fi
done

exit 0

0 comments on commit da87bc9

Please sign in to comment.