On Tue, Jan 24, 2017 at 09:39:31AM -0800, Phil Hord wrote:
> I noticed some weird spacing when comparing files with git diff
> --color-words. The space before a colored word disappears sometimes.
I _think_ this is working as designed, though it is a bit tricky (and it
may be possible to make it better).
> echo "FOO foo; foo = bar" > a
> echo "FOO foo = baz" > b
> git diff --color-words --no-index a b
> FOOfoo; foo = barbaz
It might be easier to see with --word-diff, which uses the same code but
marks it with punctuation:
$ git diff --word-diff
FOO[-foo;-] foo = [-bar-]{+baz+}
The key thing is that what you are seeing is the post-image, plus
word-based annotations. And the post-image does not have that space in
its context, so we would not expect to see:
FOO [-foo;-] foo = [-bar-]{+baz+}
(you would if we showed the pre-image plus annotations; but then I think
you'd probably end up with the opposite problem when text was added).
However, we also don't see the space in the removed part. I.e., I think:
FOO[- foo;-] foo = [-bar-]{+baz+}
would be correct. But I think because of the way word-diff is
implemented, a non-word character will never be part of the annotation.
The way it works is basically to break the string down on word
boundaries, so we have:
pre: FOO, foo;, foo, =, bar
post: FOO, foo, =, baz
as a set of tokens. We stick each of those on their own line and feed
them back to xdiff, so we get a diff like:
@@ -1,5 +1,4 @@
FOO
-foo;
foo
=
-bar
+baz
and then walk through the post-image buffer, either inserting chunks of
context from the original buffer, or the changed lines. But the changed
lines themselves do not include the non-word characters, so we have no
idea that a space went away.
-Peff