这是一个对我来说效果很好的解决方案。我已经写了解决方案和一些附加的缺失文档git (log|diff) -G<regex>
option.
它基本上使用与之前的答案相同的解决方案,但专门针对以 a 开头的评论*
or a #
,有时前面有一个空格*
...但仍然需要允许#ifdef
, #include
等变化。
向前看和向后看似乎都不受支持-G
选项,也不?
一般来说,我在使用时遇到了问题*
, too. +
不过,似乎运作良好。
(注,在Git v2.7.0上测试)
多行注释版本
git diff -w -G'(^[^\*# /])|(^#\w)|(^\s+[^\*#/])'
-
-w
忽略空格
-
-G
仅显示与以下正则表达式匹配的差异行
-
(^[^\*# /])
任何不以星号、散列或空格开头的行
-
(^#\w)
任何以以下开头的行#
后面跟着一封信
-
(^\s+[^\*#/])
任何以空格开头,后跟注释字符的行
基本上,SVN 挂钩会立即修改输入和输出的每个文件,并修改每个文件上的多行注释块。现在我可以将我的更改与 SVN 进行比较,而无需 SVN 在评论中添加的仅供参考的信息。
从技术上讲,这将允许 Python 和 Bash 注释,例如#TODO
显示在 diff 中,如果除法运算符在 C++ 中以新行开始,则可以忽略它:
a = b
/ c;
还有关于-G
Git 中的内容似乎相当缺乏,所以这里的信息应该有所帮助:
git diff -G<regex>
-G<regex>
查找补丁文本包含添加/删除的匹配行的差异<regex>
.
为了说明之间的区别-S<regex> --pickaxe-regex
and -G<regex>
,
考虑在同一文件中进行具有以下差异的提交:
+ return !regexec(regexp, two->ptr, 1, ®match, 0);
...
- hit = !regexec(regexp, mf2.ptr, 1, ®match, 0);
While git log -G"regexec\(regexp"
将显示此提交,git log -S"regexec\(regexp" --pickaxe-regex
将不会
(因为该字符串出现的次数没有改变)。
See the pickaxe进入gitdiff核心(7) https://git-scm.com/docs/gitdiffcore了解更多信息。
(注,在Git v2.7.0上测试)
-
-G
使用基本的正则表达式。
- 不支持
?
, *
, !
, {
, }
正则表达式语法。
- 分组为
()
和 OR-ing 组一起工作|
.
- 通配符,例如
\s
, \W
等都支持。
- 前瞻和后瞻是not支持的。
- 起点和终点线锚点
^$
work.
- 该功能自 Git 1.7.4 起可用。
排除的文件 v 排除的差异
请注意,-G
选项过滤将被比较的文件。
但是,如果文件被“差异化”,那么之前“排除/包含”的那些行将all显示在 diff 中。
Examples
仅显示至少有一行提及的文件差异foo
.
git diff -G'foo'
显示除以 a 开头的行之外的所有内容的文件差异#
git diff -G'^[^#]'
显示有差异的文件FIXME
or TODO
git diff -G`(FIXME)|(TODO)`
也可以看看git log -G
, git grep
, git log -S
, --pickaxe-regex
, and --pickaxe-all
更新: -G 选项使用哪个正则表达式工具?
https://github.com/git/git/blob/master/diffcore-pickaxe.c 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 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.