exclude Jest snapshots from git whitespace check
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
Git does have a way of specifying paths to exclude, though it's poorly documented and apparently not very well known. It's known as pathspec, and in this case can be used as follows:
git diff-index --check --cached HEAD -- ':!*.js.snap' .
where : is the special character that specifies that the pathspec is not just a regular path, and '!' specifies that files matching the rest of the path should be excluded from the list of matches.
Not as straightforward as Mercurial's -X or --exclude , but it's there.
As mentioned here, the path argument of git diff-index is of glob-style pattern, interpreted by the shell.
So this is more a shell issue than a git command problem.
See for instance "How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?"
You can either try and activate shopt extglob , or (simpler), do a post-step to your script, looking (with git status ) for any modified files with **/__snapshots/* in their full pathname, and cancelling their modification (git checkout).
下一篇: 从git空白检查中排除Jest快照
