2016-09-16 32 views
7

我正在使用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 

回答

1

由於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://stackoverflow.com/a/38931091/6309和http://stackoverflow.com/a/35622183/6309,以及http://stackoverflow.com/a/33462321/6309中的示例 – VonC

3

Git確實有指定要排除的路徑的方法,雖然它記錄不準確,顯然不是很知名。它被稱爲pathspec,在這種情況下,可以使用如下:「!」

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

其中:是特殊字符,指定的pathspec不只是一個普通的路徑,指定匹配路徑其餘部分的文件應該從匹配列表中排除。

不像Mercurial的-X--exclude那麼簡單,但它在那裏。