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

git diff -U0 header off-by-one error when deleting no lines

From
LTLukas Tenbrink <lukastenbrink@gmail.com>
Date
May 3, 2023, 20:48 UTC
Message-ID
<B097ECB7-1976-40FC-B91D-A60D648F973B@gmail.com>

Thank you for filling out a Git bug report! Please answer the following questions to help us understand your issue.

What did you do before the bug happened? (Steps to reproduce your issue)
Full reproduction:

echo "A\nB\nC" > test1 echo "A\nB\nC\nD\nE\nF" > test2 git diff -U0 test1 test2

What did you expect to happen? (Expected behavior)

I expected the header of the change to show the correct "from line index". For reference, a plain git diff:

> git diff test1 test2
diff --git a/test1 b/test2
index b1e6722..cead32e 100644
--- a/test1
+++ b/test2
@@ -1,3 +1,6 @@
 A
 B
 C
+D
+E
+F

When passing -U0, I would expect the from line index to be 3 larger, because the 3 lines of context are omitted:

diff --git a/test1 b/test2
index b1e6722..cead32e 100644
--- a/test1
+++ b/test2
@@ -4,0 +4,3 @@ C
+D
+E
+F


What happened instead? (Actual behavior)

The actual output places the new text at line 3:

> git diff -U0 test1 test2
diff --git a/test1 b/test2
index b1e6722..cead32e 100644
--- a/test1
+++ b/test2
@@ -3,0 +4,3 @@ C
+D
+E
+F


What's different between what you expected and what actually happened?

The header information "-3" is incorrect, because if a script wishes to insert the added code using the line indicator, it will place the newly added code at line 3 (before the C).
Instead, I would expect "-4", because we wish to insert the DEF starting at line 4.


Anything else you want to add:

Notably, the bug does not occur when also deleting lines.
Now, we are correctly placed at the beginning of line 3:

echo "A\nB\nD\nE\nF" > test3

> git diff -U0 test1 test3
diff --git a/test1 b/test3
index b1e6722..98422e9 100644
--- a/test1
+++ b/test3
@@ -3 +3,3 @@ B
-C
+D
+E
+F


[System Info]
git version:
git version 2.39.2 (Apple Git-143)
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
feature: fsmonitor--daemon
uname: Darwin 22.4.0 Darwin Kernel Version 22.4.0: Mon Mar  6 21:00:17 PST 2023; root:xnu-8796.101.5~3/RELEASE_X86_64 x86_64
compiler info: clang: 14.0.3 (clang-1403.0.22.14.1)
libc info: no libc information available
$SHELL (typically, interactive shell): /bin/zsh


[Enabled Hooks]
Next: Junio C Hamano
Message 1 of 3 in “git diff -U0 header off-by-one error when deleting no lines”
  1. Lukas TenbrinkMay 3, 2023
  2. Junio C HamanoMay 3, 2023
  3. Lukas TenbrinkMay 3, 2023

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.