git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: diff --color-words breaks spacing

From
Jeff King <peff@peff.net>
Date
Jan 25, 2017, 23:20 UTC
Message-ID
<20170125232038.bedgunhyesdytys3@sigill.intra.peff.net>
In-Reply-To
<CABURp0ryBtLALEnzRdoeYeUUdrzBxiT3+yTvW6B=vpqMG3p1Rw@mail.gmail.com>
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
Previous: Phil Hord
Message 2 of 2 in “diff --color-words breaks spacing”
  1. Phil HordJan 24, 2017
  2. Jeff KingJan 25, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.