gpt4 book ai didi

git - 如何仅对某些文件类型运行 'git diff --check'

转载 作者:太空狗 更新时间:2023-10-29 13:24:42 25 4
gpt4 key购买 nike

我一直无法弄清楚如何在我的提交中仅对 Python 文件运行“git diff --check”。我有一个包含 3 个文件的提交:

$ git show --name-only
...
tools/gitHooks/preReceive.py
tools/gitHooks/unittests/testPreReceive.py
Makefile

最后 2 个文件中有尾随空格。

$ git diff --check HEAD~1 HEAD
tools/gitHooks/unittests/testPreReceive.py:75: trailing whitespace.
+newmock = Mock()
Makefile:41: new blank line at EOF.

我想将空格检查限制为仅对 Python 文件进行检查。这样做会给我正确的文件:

$ git diff --name-only HEAD~1 HEAD | grep ".py"
tools/gitHooks/preReceive.py
tools/gitHooks/unittests/testPreReceive.py

但是当我将文件名通过管道传递给“git diff --check”(有或没有 xargs)时,我得到了错误的输出。

$ git diff --name-only HEAD~1 HEAD | grep ".py" | xargs git diff --check HEAD~1 HEAD --
$
$ git diff --name-only HEAD~1 HEAD | grep ".py" | git diff --check HEAD~1 HEAD --
tools/gitHooks/unittests/testPreReceive.py:75: trailing whitespace.
+newmock = Mock()
Makefile:41: new blank line at EOF.

我也试过这个以及它的一些变体,但没有成功:

$ git diff --check HEAD~1 HEAD -- "*.py"
$

有人可以帮忙吗?我觉得我离答案很近了。

最佳答案

From the original poster :

原来我用错了 grep。

$ git diff --name-only HEAD~1 HEAD | grep \.py$ | \
$ xargs git diff --check HEAD~1 HEAD --
tools/gitHooks/unittests/testPreReceive.py:75: trailing whitespace.
+newmock = Mock()

关于git - 如何仅对某些文件类型运行 'git diff --check',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12427583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com