is there a way to have a pre-commit hook which auto-formats the code (for
example with astyle
) but does not destroy a partial commit?
Workflow:
# edit a file.txt
git add -p file.txt
# add one chunk, but not another
git commit -m 'a message'
[PRE_COMMIT_HOOK] Formatting source code
git status
# the "another" chunk is still not added
My problem is, that if you do a git add
inside the pre-commit hook, which is
required after the script formatted the source code, adds the "another" chunk,
too. But I don't want that.
Is there a way to achieve this?
I'd do this by doing the work with the low-level "plumbing" commands, my first attempt would be something along the lines of
git ls-files --stage \*.c | while read mode object stage path; do
case $mode in
10*)
formatted=`git show $object | indent | git hash-object -w --stdin`
git update-index --cacheinfo $mode $formatted "$path"
;;
esac
done
To avoid redundant processing, start from git diff-index --name-only --diff-filter=AM
output as @torek suggests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With