Exclude Jest snapshots from git space check

I trim the space from git using git diff-index --check --cached HEAD -- . I want to add Jest tests using snapshots, but the snapshot files include spaces, and my tests will always fail if I delete it. Therefore, I want to exclude *.js.snap files from the space check. How to tell git to exclude *.js.snap files (or, alternatively, **/__snapshots/* ) from git diff-index ? I am using bash on OSX.

At the same time, I am working on a problem, changing my commit hook to 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 
+7
git whitespace githooks jestjs
source share
2 answers

Git has a way of specifying paths for exclusion, although it is poorly documented and apparently not well known. It is known as pathspec and in this case can be used as follows:

 git diff-index --check --cached HEAD -- ':!*.js.snap' . 

where : is a special character indicating that pathspec is not just a regular path, but '!' indicates that files matching the rest of the path should be excluded from the match list.

Not as simple as Mercurial -X or --exclude , but it is there.

+3
source share

Like the one mentioned here , the git diff-index path argument has a glob-style pattern interpreted by the shell.

So this is more of a shell problem than a problem with the git command.

See for example, “ How do I use reverse or negative wildcards when matching patterns in a unix / linux shell?

You can either activate shopt extglob , or (easier) perform the next step in the script by looking (with git status ) at any changed files using **/__snapshots/* along their full path and undoing their modification (git checkout).

+1
source share

All Articles