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
There does not seem to be any options to Git’s
diffcommand to support what you want to do. However, you could use theGIT_EXTERNAL_DIFFenvironment variable and a custom script (or any executable created using your preferred scripting or programming language) to manipulate a patch.I’ll assume you are on Linux; if not, you could tweak this concept to suit your environment. Let’s say you have a Git repo where
HEADhas a filefile05that contains:And a file
file06that contains:You change
file05to:And you change
file06to:Using the following shell script, let’s call it
mydiff.shand place it somewhere that’s in ourPATH:Executing the command:
Will output:
This output does not show changes for the added quotes in
file05andfile06. The external diff script basically uses the Gitdiff-filescommand to create the patch and filters the output through a GNUawkscript to manipulate it. This sample script does not handle all the different combinations of old and new files mentioned forGIT_EXTERNAL_DIFFnor does it output a valid patch, but it should be enough to get you started.You could use Perl regular expressions, Python
difflibor whatever you’re comfortable with to implement an external diff tool that suits your needs.