I am trimming whitespace from git commits using git diff-index --check --cached HEAD --
. I want to add Jest tests using snapshots, but the snapshot files include whitespace and my tests will always fail if I remove it. So I want to exclude the *.js.snap
files from the whitespace check. How do I tell git to exclude *.js.snap
(or, alternatively, **/__snapshots/*
) files from git diff-index
? I'm using bash on OSX.
In the meantime, I'm working around the problem by changing my commit hook to be interactive:
# If there are whitespace errors, print the offending file names and fail.
git diff-index --check --cached HEAD --
if [ $? -ne 0 ]; then
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty
while true; do
echo "Your commit introduces trailing whitespace. Are you sure you want to commit? y/n"
read yn
case $yn in
y ) exit 0;;
n ) exit 1;;
esac
done
fi