From: Lukas Tenbrink <lukastenbrink@gmail.com>
To: git@vger.kernel.org
Subject: git diff -U0 header off-by-one error when deleting no lines
Date: Wed, 3 May 2023 22:48:44 +0200 [thread overview]
Message-ID: <B097ECB7-1976-40FC-B91D-A60D648F973B@gmail.com> (raw)
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 reply other threads:[~2023-05-03 20:49 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-03 20:48 Lukas Tenbrink [this message]
2023-05-03 21:19 ` git diff -U0 header off-by-one error when deleting no lines Junio C Hamano
2023-05-03 21:26 ` Lukas Tenbrink
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=B097ECB7-1976-40FC-B91D-A60D648F973B@gmail.com \
--to=lukastenbrink@gmail.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).