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

标签 git whitespace githooks jestjs

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

与此同时,我通过将我的提交 Hook 更改为交互式来解决这个问题:

# 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 确实有一种指定要排除的路径的方法,尽管它的文档很少而且显然不是很为人所知。它被称为 pathspec , 在这种情况下可以按如下方式使用:

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

其中 : 是指定 pathspec 不只是常规路径的特殊字符,'!'指定匹配路径其余部分的文件应从匹配列表中排除。

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

关于git - 从 git 空白检查中排除 Jest 快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39537830/

相关文章:

git - 如何在 Visual Studio 2017 中使用多个 Git 存储库

git - 开源项目时处理以前的提交

Java:匹配新行开头的重复字符并替换为相同数量的替代字符

regex - 带有捕获组的 RegEx 中的空格问题

Git 钩子(Hook) : "fatal: not a git repository:"

git - 您通常在 Git 预提交 Hook 中包含哪些脚本/调用?

Git 创建完整的文件补丁以下载/分发?

git - 防止在主分支上 merge 某种类型的文件 - Git

php - 为什么空格会在 PHP 中导致 fatal error ?

git - 如何在推送时自动 checkout ?