2

Is there any option for git diff that allows to just consider hunks that match some specific criteria (e.g. a regular expression)?

I want diff to just show me hunks that, for example, start with line matching the regex .*<<<special_diff.* and end with a line matching the regex .*\sspecial_diff>>>

I am looking for solutions that would allow to do that, keeping performance fast and staying simple.

Note: it should be able to match multiple lines, so that I could match (into a hunk) a multiline block of text between specific delimiters.

Matching just if a hunk contains a full block (open + close delimiters); for example matching a multiline block delimited by <body> and </body>.

Kamafeather
  • 8,663
  • 14
  • 69
  • 99
  • Anonymously voting for closing a question, without even explaining why, it's pretty unfair, unethical and definitely useless for understanding what might need to be corrected. – Kamafeather May 09 '19 at 15:37

1 Answers1

1

Use git diff -Gregexp. Example: the commit 5c6d3a9@mimedecode contains 3 hunks. If I run git diff 5c6d3a9~ 5c6d3a9 I see all 3.

I can filter them using git diff -GReplaced 5c6d3a9~ 5c6d3a9 and I see only 2 of these 3 hunks. With git diff -Greplaced 5c6d3a9~ 5c6d3a9 nothing is shown because by default regexp searching is case-sensitive. To make it case-insensitive add -i: git diff -Greplaced -i 5c6d3a9~ 5c6d3a9; this again produces 2 hunks.

phd
  • 82,685
  • 13
  • 120
  • 165
  • Mmh, interesting. Could that be used to match also multiple lines? That's actually my final purpose, sorry if I didn't mention it before. – Kamafeather May 09 '19 at 16:56
  • I'm not sure but I think no. I suspect `git` applies regexps on line basis, not for the entire text at once. So you can search at the beginning of a line but not at the beginning of the hunk. – phd May 09 '19 at 17:49
  • Mmmh, I supposed that too . Would be great to have the chance to run some *awk* script; that way I guess it would be possible to process multiline blocks – Kamafeather May 09 '19 at 17:53
  • 1
    Then you need a tool that can process hunks. Something like [grepdiff](https://stackoverflow.com/a/35434714/7976758) or [splitpatch](https://github.com/jaalto/splitpatch). – phd May 09 '19 at 19:07