【问题标题】:exclude Jest snapshots from git whitespace check从 git 空白检查中排除 Jest 快照
【发布时间】:2016-09-16 18:20:41
【问题描述】:

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

与此同时,我正在通过将提交挂钩更改为交互式来解决该问题:

# 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 whitespace githooks jestjs


    【解决方案1】:

    Git 确实有一种方法可以指定要排除的路径,尽管它的文档记录很差,而且显然不是很为人所知。它被称为pathspec,在这种情况下可以如下使用:

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

    其中: 是指定路径规范不仅仅是常规路径的特殊字符,而“!”指定应从匹配列表中排除与路径其余部分匹配的文件。

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

    【讨论】:

      【解决方案2】:

      作为mentioned heregit diff-index 的路径参数是 glob 样式的,由 shell 解释。

      所以这更像是一个 shell 问题而不是 git 命令问题。

      参见例如“How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?

      您可以尝试激活shopt extglob,或者(更简单),对您的脚本执行一个后续步骤,查找(使用git status)在其完整路径名中带有**/__snapshots/* 的任何修改过的文件,并取消它们修改(git checkout)。

      【讨论】:

      猜你喜欢
      • 2020-03-25
      • 2020-06-17
      • 2019-06-22
      • 2017-10-22
      • 2012-08-16
      • 2020-03-01
      • 2021-05-29
      • 1970-01-01
      • 2011-07-30
      相关资源
      最近更新 更多