这是一个对我来说效果很好的解决方案。我已经编写了解决方案以及有关该git (log|diff) -G<regex>
选项的一些其他缺失文档。
它基本上使用与先前答案相同的解决方案,但专门针对以 a*
或 a开头的注释,#
有时在*
... 之前有一个空格,但它仍然需要允许#ifdef
,#include
等更改。
-G
该选项似乎不支持向前看和向后看,通常也不支持?
,而且我在使用时也遇到了问题*
。 +
不过,似乎运作良好。
(注意,在 Git v2.7.0 上测试过)
多行注释版
git diff -w -G'(^[^\*# /])|(^#\w)|(^\s+[^\*#/])'
-w
忽略空格
-G
仅显示与以下正则表达式匹配的差异行
(^[^\*# /])
任何不以星号、哈希或空格开头的行
(^#\w)
任何#
以字母开头的行
(^\s+[^\*#/])
任何以空格开头后跟注释字符的行
基本上一个 SVN 钩子现在修改每个文件进出,并修改每个文件上的多行注释块。现在我可以将我的更改与 SVN 进行比较,而无需 SVN 在评论中删除的 FYI 信息。
从技术上讲,这将允许#TODO
在 diff 中显示 Python 和 Bash 注释,如果除法运算符在 C++ 中的新行上开始,则可以忽略它:
a = b
/ c;
Git中的文档-G
似乎也很缺乏,所以这里的信息应该会有所帮助:
git diff -G<regex>
-G<regex>
查找补丁文本包含匹配的添加/删除行的差异<regex>
。
为了说明 和 之间的区别-S<regex> --pickaxe-regex
,-G<regex>
请考虑在同一文件中具有以下差异的提交:
+ return !regexec(regexp, two->ptr, 1, ®match, 0);
...
- hit = !regexec(regexp, mf2.ptr, 1, ®match, 0);
虽然git log -G"regexec\(regexp"
将显示此提交,
git log -S"regexec\(regexp" --pickaxe-regex
但不会(因为该字符串的出现次数没有改变)。
有关更多信息,请参阅gitdiffcore (7)中的镐条目。
(注意,在 Git v2.7.0 上测试过)
-G
使用基本的正则表达式。
- 不支持
?
, *
, !
, {
,}
正则表达式语法。
- 分组
()
和 OR-ing 组适用于|
.
- 支持通配符,例如
\s
,\W
等。
- 不支持前瞻和后瞻。
- 开始和结束线锚
^$
工作。
- 自 Git 1.7.4 起该功能已可用。
排除的文件 v 排除的差异
请注意,该-G
选项过滤将被区分的文件。
但是如果一个文件被“差异化”,那些之前被“排除/包含”的行都将显示在差异中。
例子
仅显示至少有一行提及foo
.
git diff -G'foo'
显示除以 a 开头的行之外的所有内容的文件差异#
git diff -G'^[^#]'
显示有差异的文件提及FIXME
或TODO
git diff -G`(FIXME)|(TODO)`
另见git log -G
, git grep
, git log -S
, --pickaxe-regex
, 和--pickaxe-all
更新:-G 选项正在使用哪个正则表达式工具?
https://github.com/git/git/search?utf8=%E2%9C%93&q=regcomp&type=
https://github.com/git/git/blob/master/diffcore-pickaxe.c
if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
int cflags = REG_EXTENDED | REG_NEWLINE;
if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE))
cflags |= REG_ICASE;
regcomp_or_die(®ex, needle, cflags);
regexp = ®ex;
// and in the regcom_or_die function
regcomp(regex, needle, cflags);
http://man7.org/linux/man-pages/man3/regexec.3.html
REG_EXTENDED
Use POSIX Extended Regular Expression syntax when interpreting
regex. If not set, POSIX Basic Regular Expression syntax is
used.
// ...
REG_NEWLINE
Match-any-character operators don't match a newline.
A nonmatching list ([^...]) not containing a newline does not
match a newline.
Match-beginning-of-line operator (^) matches the empty string
immediately after a newline, regardless of whether eflags, the
execution flags of regexec(), contains REG_NOTBOL.
Match-end-of-line operator ($) matches the empty string
immediately before a newline, regardless of whether eflags
contains REG_NOTEOL.