how to show whitespace differences with git --word-diff?
Asked Answered
U

2

9

To illustrate the problem: see diff

the only diff in this paragraph (starting with A macro that needs is whitespace differences (newlines inserted / removed in certain places);

  • when running git diff it shows the paragraph before in red and paragraph after in red, making it hard to spot the difference
  • when running git diff --word-diff, it shows the paragraph after in gray and doesn't show the whitespace changes
  • when running git diff --word-diff-regex=. it shows the whitespace changes (great!) but [EDIT] it does character by character diff which is often unreadable as it mixes letters from different words to minimize the diff, eg: git show --word-diff-regex=. 4a720394bba39ce1e67d518b909cbb1c25f63d09 [- * patch compile-]r [-so `isM-]a[-inModule`-] [-is true when -d:isMainModuleIsAlwaysTr-]{+m+}u[-e-] [- T-]{+c+}h[-at'll give speedup-] be[-nefi-]t[-, and we don'-]t[- hav-]e{+r+} [-to p-]{+w+}a[-tch stdlib files-]{+y+}. ]#

What I want is an option to show whitespace differences while running --word-diff (or --word-diff-regex), eg via {+ +} and [- -]; Note: for --word-diff=color would be nice to show these to, eg also via {+ +} and [- -] since otherwise these would disappear.

Note: I'm using colors in my gitconfig.

Note: this doesn't help since whitespace differences are not shown in output of git diff --word-diff=porcelain

Unidirectional answered 31/8, 2018 at 23:30 Comment(2)
You say a character diff "is most often not what we want". Can you be more specific or give an example of what you don't want? Do you mean that you do not want non-whitespace changes to be shown at all? I.e. you only want to see whitespace changes while hiding any other changes?Tletski
see the EDIT that explains this. The character diff ignores word boundaries so will grab letters from different words to minimize the diff; if some piece of text is completely re-written, the result will be completely un-readable.Unidirectional
T
8

The --word-diff-regex command allows you to specify a regex to customise the --word-diff behaviour. The common example of using a full stop (.) will give a character-by-character match since the full stop regex matches any character. When the regex is not specified the default is different depending on the file type, but usually ignores whitespace changes while also using them as word boundaries.

You could use a regex that divides lines into words as well as white space areas by using something like:

git diff --word-diff-regex="[ ]+|[^ ]+"

With a slightly tweaked version of your linked example, the problem with git diff --word-diff-regex=. can be seen:

enter image description here

While git diff --word-diff-regex="[ ]+|[^ ]+" would give you:

enter image description here

Tletski answered 7/1, 2019 at 10:5 Comment(0)
U
2

https://github.com/so-fancy/diff-so-fancy or https://coderwall.com/p/nl-bdg/more-readable-git-word-diff-on-osx may actually be a better approach, showing a github-like diff where more context is shown, but also highlighting the part that actually changed.

Unidirectional answered 21/1, 2019 at 6:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.