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

时间:2012-09-14 15:27:54

标签: git grep whitespace file-extension

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

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

最后两个文件中有空格。

$ 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"
$

有人可以帮忙吗?我觉得我接近答案。

1 个答案:

答案 0 :(得分:3)

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()