4

我一直无法弄清楚如何在我的提交中仅对 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"
$

任何人都可以帮忙吗?我觉得我已经接近答案了。

4

1 回答 1

2

从原始海报

原来我使用了错误的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()
于 2014-07-08T21:54:00.533 回答