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

Re: Git diff misattributes the first word of a line to the previous line

From
Philip Oakley <philipoakley@iee.email>
Date
Oct 13, 2022, 11:30 UTC
Message-ID
<4cb7d3cb-36ff-0302-45c0-aa61644ef90b@iee.email>
In-Reply-To
<f2453084-9f9a-74a1-6a43-0c1b91c95e93@kdbg.org>
Hi Gurjeet,
On 13/10/2022 07:45, Johannes Sixt wrote:
Show 8 quoted lines
> Am 13.10.22 um 07:51 schrieb Gurjeet Singh:
>> Git diff seems to get confused about word boundaries, and includes the
>> first word from the next line.
> No, that would misattribute the perceived malfunction.
>
>> It seems that the first word of a line gets attributed to the previous
>> line, ignoring the fact that there's an intervening newline before the
>> word.

Given that this effect is a part of the design (LF => whitespace), are there any changes to the *documentation* that could be made to help clarify this? E.g. looking back (you did check the manual? ;-) did you miss some aspect in the man pages that could have been more prominent, placed earlier, or clarified?

Why is this way of reporting even expected (e.g. confusion between flowed text, and line oriented code, without a mode change), etc. ?

Any other `retrospective` thoughts that could help?

-- Philip

Show 33 quoted lines
>> [...]
>> $ git diff --word-diff=plain /tmp/1.txt /tmp/2.txt
>> diff --git a/tmp/1.txt b/tmp/2.txt
>> index 8239f93..099fb80 100644
>> --- a/tmp/1.txt
>> +++ b/tmp/2.txt
>> @@ -1,2 +1,2 @@
>>     x = yz [-ab-]{+opt1+}
>> {+    ac+} = [-cd ef-]{+pq opt2+}
>>
>> $ cat /tmp/1.txt
>>     x = yz
>>     ab = cd ef
>>
>> $ cat /tmp/2.txt
>>     x = yz opt1
>>     ac = pq opt2
> The reason for this is that the implementation of word-diff does not
> treat newline characters in any special way. They are treated as
> "whitespace" like any other character that is not captured by the
> word-diff patterns. Whitespace characters following each word are
> recorded, but are disregarded when the word-diff is computed. When the
> text is reconstructed in the output, these recorded space characters are
> printed only for unchanged and added words, but are not printed for
> removed words (IIRC). Combine this with the fact that when there is a
> change, i.e., a combination of removal and addition, then the removal is
> printed before the addition, and you get the observed output.
>
> I don't see an easy solution for this without completely rewriting the
> implementation.
>
> -- Hannes
>
Previous: Johannes SixtNext: Bagas Sanjaya
Message 3 of 4 in “Git diff misattributes the first word of a line to the previous line”
  1. Gurjeet SinghOct 13, 2022
  2. Johannes SixtOct 13, 2022
  3. Philip OakleyOct 13, 2022
  4. Bagas SanjayaOct 13, 2022

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.