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



我一直无法弄清楚如何在提交中仅对Python文件运行"git diff--check"。我有一个包含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"
$

有人能帮忙吗?我觉得我已经接近答案了。

来自原始海报:

原来我用错了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()

最新更新