Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git smart line and word diff

I'd like to git diff and combine the regular line-by-line diff with git diff --word-diff. The problem with line-by-line diffs is that they're unnecessary if I change one or two words and leave the line mostly intact--the chunking is too coarse. On the other hand if I change entire lines and use --word-diff, sometimes the diff algorithm will get confused and spit out incredibly confusing diffs, with lots of words inserted and deleted to "morph" one line into another.

Is there a way to specify that git should be smart about this and only --word-diff if it actually makes sense to do so (on a line-by-line basis, of course)?

like image 239
PythonNut Avatar asked Aug 14 '14 01:08

PythonNut


1 Answers

The smartest thing I have found for git diff --word-diff or git diff --color-words are the predefined patterns that come with git (as used in --word-diff-regex or diff.wordregex). They might not be perfect, but give quite good results AFAICT.

A list of predefined diff drivers (they all have predefined word regexes too) is given in the docs for .gitattributes. It is further stated that

you still need to enable this with the attribute mechanism, via .gitattributes

So to activate the python pattern for all *.py files, you could issue the following command in your repo root:

echo "*.py diff=python" >> .gitattributes

If you are interested in what the different preset patterns actually look like, take a look at git's source code

like image 182
raphinesse Avatar answered Oct 09 '22 06:10

raphinesse