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

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

From
GSGurjeet Singh <gurjeet@singh.im>
Date
Oct 13, 2022, 05:51 UTC
Message-ID
<CABwTF4U-KXHF7=8RWY7Ecbspz205Msa3syZFiWYDg3XmZsNGVw@mail.gmail.com>

Git diff seems to get confused about word boundaries, and includes the first word from the next line.

In the output below, you'd be forgiven to assume that the word 'ab' was removed from first line, and was replaced with the word 'opt1'. But as you can see in the contents of file '1.txt', that word was on the _second_ line.

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.

This confusion is exhibited by the --color-words option, too, where I discovered it first. But when trying to create a reproducible test case, I discovered that this may be a problem with the word-boundary identification, and the --word-diff=plain is a better way to demonstrate the bug.

I have also eliminated the possibility that this may be due to some misconfiguration in my GIt config files, by setting some environment variables, as seen in the last command.

$ 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

# Git installed on macOS, via Nixpkgs
$ git --version
git version 2.35.1

# Also tested on Git installed via Homebrew
$ /usr/local/bin/git --version
git version 2.38.0

# Try to run with a clean environment.
$ export GIT_CONFIG_GLOBAL=/dev/null
$ export GIT_CONFIG_SYSTEM=/dev/null
$ export GIT_CONFIG_NOSYSTEM=yes
$ git diff --word-diff=plain /tmp/1.txt /tmp/2.txt # Same buggy output

# **** Expected **** output
$ 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 {+opt1+}
    [-ab-]{+ac+} = [-cd ef-]{+pq opt2+}

Best regards,
Gurjeet
http://Gurje.et
Next: Johannes Sixt
Message 1 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.