Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 views
in Technique[技术] by (71.8m points)

regex - ignoring changes matching a string in git diff

I've made a single simple change to a large number of files that are version controlled in git and I'd like to be able to check that no other changes are slipping into this large commit.

The changes are all of the form

-                       "main()",
+                       OOMPH_CURRENT_FUNCTION,

where "main()" could be the name of any function. I want to generate a diff of all changes that are not of this form.

The -G and -S options to git diff are tantalisingly close--they find changes that DO match a string or regexp.

Is there a good way to do this?

Attempts so far

Another question describes how regexs can be negated, using this approach I think the command should be

git diff -G '^((?!OOMPH_CURRENT_FUNCTION).)*$'

but this just returns the error message

fatal: invalid log-grep regex: Invalid preceding regular expression

so I guess git doesn't support this regex feature.

I also noticed that the standard unix diff has the -I option to "ignore changes whose lines all match RE". But I can't find the correct way to replace git's own diff with the unix diff tool.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try the following:

$ git diff > full_diff.txt
$ git diff -G "your pattern" > matching_diff.txt

You can then compare the two like so:

$ diff matching_diff.txt full_diff.txt

If all changes match the pattern, full_diff.txt and matching_diff.txt will be identical, and the last diff command will not return anything.

If there are changes that do not match the pattern, the last diff will highlight those.


You can combine all of the above steps and avoid having to create two extra files like so:

diff <(git diff -G "your pattern") <(git diff)  # works with other diff tools too

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...