从git空白检查中排除Jest快照

问题描述:

我正在使用git diff-index --check --cached HEAD --修改git提交中的空白。我想使用快照添加Jest测试,但快照文件包含空格,如果我删除它,我的测试总是失败。所以我想从空白检查中排除*.js.snap文件。如何让git从git diff-index中排除*.js.snap(或**/__snapshots/*)文件?我在OSX上使用bash。从git空白检查中排除Jest快照

在此期间,我在解决该问题,通过改变我的commit钩子是互动:

# 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 

由于mentioned heregit diff-index路径参数是通配符式样的模式,通过shell解释。

所以这是一个比git命令问题更多的shell问题。

在其完整的任何修改过的文件例如,见“How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?

你可以试着和激活shopt extglob,或者(简单),做了后一步你的脚本,寻找(与git status)与**/__snapshots/*路径名,并取消它们的修改(git checkout)。

+0

感谢您花时间回答。我花了很多时间玩负面通配符和GLOBIGNORE,但都没有效果。两者似乎只适用于不包含多个通配符('**/*')的模式。 – undefined

+0

我还是会奖励你的赏金,因为我不能把它给自己,而不是你现在关心的声誉! – undefined

+0

@undefined感谢您的反馈,并为您的答案+1。在此之前我提到了pathspec:http://*.com/a/38931091/6309和http://*.com/a/35622183/6309,以及http://*.com/a/33462321/6309中的示例 – VonC

Git确实有指定要排除的路径的方法,虽然它记录不准确,显然不是很知名。它被称为pathspec,在这种情况下,可以使用如下:“!”

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

其中:是特殊字符,指定的pathspec不只是一个普通的路径,指定匹配路径其余部分的文件应该从匹配列表中排除。

不像Mercurial的-X--exclude那么简单,但它在那里。