我正在使用 this git hook 用于防止包含非 ascii 字符的提交。
当我找到一个非 ascii 字符时,我只想打印包含该字符的行(如果可能,还有文件名和行号)
git --no-pager diff HEAD -S$char -U0
--unified 选项将显示文件中的所有更改,但我只想显示找到的 -S 行。
最佳答案
I want to print only the line with the character (and if possible the filename and line number too)
这不完全是一种diff
格式,但您可以通过添加xargs
和grep
轻松获得您想要的内容:
%git diff --name-only -S"$char"|xargs grep -n "$char" /dev/null
module/main/src/main/java/Scratchpad.java:33: public void Reßet() {
使用 GNU grep,您可以将 grep -n "$char"/dev/null
替换为 grep -Hn "$char"
关于Git diff -S 只显示匹配的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46111711/