{"thread":{"id":"44960","subject":"diff --color-words breaks spacing","startedAt":"2017-01-24T17:39:57Z","lastAt":"2017-01-25T23:20:45Z","messageCount":2,"participants":["Phil Hord","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"310098","messageId":"CABURp0ryBtLALEnzRdoeYeUUdrzBxiT3+yTvW6B=vpqMG3p1Rw@mail.gmail.com","threadId":"44960","inReplyTo":null,"subject":"diff --color-words breaks spacing","fromName":"Phil Hord","fromEmail":"phil.hord@gmail.com","sentAt":"2017-01-24T17:39:31Z","receivedAt":"2017-01-24T17:39:57Z","isPatch":false,"sender":{"key":"phil.hord@gmail.com","avatar":"https://avatars.githubusercontent.com/u/123908?v=4"},"body":"I noticed some weird spacing when comparing files with git diff\n--color-words.  The space before a colored word disappears sometimes.\n\n$ git --version\ngit version 2.11.0.485.g4e59582ff\n\necho \"FOO foo; foo = bar\" > a\necho \"FOO foo = baz\" > b\ngit diff --color-words --no-index a b\nFOOfoo; foo = barbaz\n\nThere should be a space after FOO in the diff, even if git doesn't\nthink \"foo\" and \"foo;\" are the same word.\n\nIf I remove the semicolon, it looks better, but in fact it only moves\nthe error later. The missing space is now between the two \"foo\" words.\n\necho \"FOO foo foo = bar\" > a\necho \"FOO foo = baz\" > b\ngit diff --color-words --no-index a b\nFOO foofoo = barbaz\n\nHere's the same with the color codes changed to text for purposes of this email:\n\necho \"FOO foo; foo = bar\" > a\necho \"FOO foo = baz\" > b\ngit diff --color-words --no-index a b | tr  \\\\033 E\nFOOE[31mfoo;E[m foo = E[31mbarE[mE[32mbazE[m\n\necho \"FOO foo foo = bar\" > a\necho \"FOO foo = baz\" > b\ngit diff --color-words --no-index a b | tr  \\\\033 E\nFOO fooE[31mfooE[m = E[31mbarE[mE[32mbazE[m\n"},{"id":"310258","messageId":"20170125232038.bedgunhyesdytys3@sigill.intra.peff.net","threadId":"44960","inReplyTo":"CABURp0ryBtLALEnzRdoeYeUUdrzBxiT3+yTvW6B=vpqMG3p1Rw@mail.gmail.com","subject":"Re: diff --color-words breaks spacing","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-01-25T23:20:38Z","receivedAt":"2017-01-25T23:20:45Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, Jan 24, 2017 at 09:39:31AM -0800, Phil Hord wrote:\n\n> I noticed some weird spacing when comparing files with git diff\n> --color-words.  The space before a colored word disappears sometimes.\n\nI _think_ this is working as designed, though it is a bit tricky (and it\nmay be possible to make it better).\n\n> echo \"FOO foo; foo = bar\" > a\n> echo \"FOO foo = baz\" > b\n> git diff --color-words --no-index a b\n> FOOfoo; foo = barbaz\n\nIt might be easier to see with --word-diff, which uses the same code but\nmarks it with punctuation:\n\n  $ git diff --word-diff\n  FOO[-foo;-] foo = [-bar-]{+baz+}\n\nThe key thing is that what you are seeing is the post-image, plus\nword-based annotations. And the post-image does not have that space in\nits context, so we would not expect to see:\n\n  FOO [-foo;-] foo = [-bar-]{+baz+}\n\n(you would if we showed the pre-image plus annotations; but then I think\nyou'd probably end up with the opposite problem when text was added).\n\nHowever, we also don't see the space in the removed part. I.e., I think:\n\n  FOO[- foo;-] foo = [-bar-]{+baz+}\n\nwould be correct. But I think because of the way word-diff is\nimplemented, a non-word character will never be part of the annotation.\n\nThe way it works is basically to break the string down on word\nboundaries, so we have:\n\n  pre: FOO, foo;, foo, =, bar\n  post: FOO, foo, =, baz\n\nas a set of tokens. We stick each of those on their own line and feed\nthem back to xdiff, so we get a diff like:\n\n  @@ -1,5 +1,4 @@\n   FOO\n  -foo;\n   foo\n   =\n  -bar\n  +baz\n\nand then walk through the post-image buffer, either inserting chunks of\ncontext from the original buffer, or the changed lines. But the changed\nlines themselves do not include the non-word characters, so we have no\nidea that a space went away.\n\n-Peff\n"}]}