作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我一直无法弄清楚如何在我的提交中仅对 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"
$
有人可以帮忙吗?我觉得我离答案很近了。
最佳答案
原来我用错了 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/
我是一名优秀的程序员,十分优秀!