Here's my (simplified, no error checking) git hook:
git diff --cached | clang-format-diff -p1 > $PATCHFILE
git patch-apply -p0 $PATCHFILE # modify the working tree
git patch-apply -p0 --cached $PATCHFILE # modify what is staged
This works fine, however, when a user is running git commit -a
, the documentation to git diff
informs me that "there are various ways to check your working tree":
$ git diff (1) $ git diff --cached (2) $ git diff HEAD (3)
Changes in the working tree not yet staged for the next commit.
Changes between the index and your last commit; what you would be committing if you run "git commit" without "-a" option.
Changes in the working tree since your last commit; what you would be committing if you run "git commit -a"
As a consequence, the script does not work when a using is git commit -a
. This is because it gets called at a stage where the index apparently has not had the changes added.
I would like to apply the clang-format-diff changes only on staged files.
Question:
- Why, apparently, does
git commit -a
call the pre-commit hook before staging files? Isn't that a bug? - What should I do? Can I somehow detect inside the pre-commit hook that I am inside a
git commit -a
situation? I would rather avoid formatting unstaged changes.
Notes
- I am using git 2.11.0.
- Related questions but not duplicate: