Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering a diff with a regular expression

Tags:

git

regex

diff

It seems that it would be extremely handy to be able to filter a diff so that trivial changes are not displayed. I would like to write a regular expression which would be run on the line and then pass it another string that uses the captured arguments to generate a canonical form. If the lines before and after produce the same output, then they would be removed from the diff.

For example, I am working on a PHP code base where a significant number of array accesses are written as my_array[my_key] when they should be my_array["my_key"] to prevent issues if a my_key constant is defined. It would be useful to generate a diff where the only change on the line wasn't adding some quotes.

I can't change them all at once, as we don't have the resources to test the entire code base, so am fixing this whenever I make a change to a function. How can I achieve this? Is there anything else similar to this that I can use to achieve a similar result. For example, a simpler method might be to skip the canonical form and just see if the input is transformed into the output. BTW, I am using Git

like image 764
Casebash Avatar asked Nov 21 '11 23:11

Casebash


People also ask

What does '$' mean in RegEx?

$ means "Match the end of the string" (the position after the last character in the string).

Can I use regular expression in SQL?

You can use RegEx in many languages like PHP, Python, and also SQL. RegEx lets you match patterns by character class (like all letters, or just vowels, or all digits), between alternatives, and other really flexible options.

What does $1 do in RegEx?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.


1 Answers

grepdiff can be used to filter the hunks in the diff file.

$ git diff -U1 | grepdiff 'console' --output-matching=hunk

It shows only the hunks that match with the given string "console".

like image 165
Naga Kiran Avatar answered Sep 19 '22 22:09

Naga Kiran