我一直不知道如何在我提交的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"
$有人能帮上忙吗?我觉得我已经接近答案了。
发布于 2014-07-09 05:54:01
结果发现我用错了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()https://stackoverflow.com/questions/12427583
复制相似问题