From da87bc9adbe73dd78f065eb7c21c6aabcb0f0338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Mon, 4 Dec 2023 19:29:36 +0100 Subject: [PATCH] pre-commit hook: Ensure autoformatted files are added to the current 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. --- .husky/post-commit | 6 ++++++ .husky/pre-commit | 13 +++++++++++++ 2 files changed, 19 insertions(+) create mode 100755 .husky/post-commit diff --git a/.husky/post-commit b/.husky/post-commit new file mode 100755 index 0000000000..648aa67194 --- /dev/null +++ b/.husky/post-commit @@ -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 diff --git a/.husky/pre-commit b/.husky/pre-commit index 21837032f3..c768db0d40 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -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 \ No newline at end of file