* git diff -U0 header off-by-one error when deleting no lines
@ 2023-05-03 20:48 Lukas Tenbrink
2023-05-03 21:19 ` Junio C Hamano
0 siblings, 1 reply; 3+ messages in thread
From: Lukas Tenbrink @ 2023-05-03 20:48 UTC (permalink / raw)
To: git
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]
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: git diff -U0 header off-by-one error when deleting no lines
2023-05-03 20:48 git diff -U0 header off-by-one error when deleting no lines Lukas Tenbrink
@ 2023-05-03 21:19 ` Junio C Hamano
2023-05-03 21:26 ` Lukas Tenbrink
0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2023-05-03 21:19 UTC (permalink / raw)
To: Lukas Tenbrink; +Cc: git
Lukas Tenbrink <lukastenbrink@gmail.com> writes:
> 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
For many folks, the above will place one line each in these files.
For portability, you'd need to do something like
printf "%s\n" A B C >test1
printf "%s\n" A B C D E F >test2
> git diff -U0 test1 test2
> 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
I think this is very much in line with how "diff -U0" by other
people do it, and any tool like "git am" that need to read "diff"
output need to understand how these output work correctly; "-U0"
output and ",1" that is omitted are things that have confused us
tool writers forever ;-).
With -U0, you'd get
$ diff -U0 test1 test2
--- test1 2023-05-03 14:02:27.718960038 -0700
+++ test2 2023-05-03 14:02:20.094156573 -0700
@@ -3,0 +4,3 @@
+D
+E
+F
which has "-3,0" that says "there were 3 lines before this hunk that
did not have any line before the change".
This might be unintuitive, but it is specified by POSIX.
Open
https://pubs.opengroup.org/onlinepubs/9699919799/
and look for "@@".
"@@-%s+%s@@", <file1 range>, <file2 range>
Each <range> field shall be of the form:
"%1d", <beginning line number>
or:
"%1d,1", <beginning line number>
if the range contains exactly one line, and:
"%1d,%1d", <beginning line number>, <number of lines>
otherwise.
If a range is empty, its beginning line number shall be the
number of the line just before the range, or 0 if the empty
range starts the file.
So a patch that adds lines to an empty file would look like
$ diff -U0 /dev/null test1
--- /dev/null 2023-04-29 22:24:54.395999895 -0700
+++ test1 2023-05-03 14:02:27.718960038 -0700
@@ -0,0 +1,3 @@
+A
+B
+C
just as specified. Note "-0,0" that says "there was no line before
this hunk before this change".
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: git diff -U0 header off-by-one error when deleting no lines
2023-05-03 21:19 ` Junio C Hamano
@ 2023-05-03 21:26 ` Lukas Tenbrink
0 siblings, 0 replies; 3+ messages in thread
From: Lukas Tenbrink @ 2023-05-03 21:26 UTC (permalink / raw)
To: git
> This might be unintuitive, but it is specified by POSIX.
>
> Open
>
> https://pubs.opengroup.org/onlinepubs/9699919799/
>
> and look for "@@".
>
> "@@-%s+%s@@", <file1 range>, <file2 range>
>
> Each <range> field shall be of the form:
>
> "%1d", <beginning line number>
>
> or:
>
> "%1d,1", <beginning line number>
>
> if the range contains exactly one line, and:
>
> "%1d,%1d", <beginning line number>, <number of lines>
>
> otherwise.
>
> If a range is empty, its beginning line number shall be the
> number of the line just before the range, or 0 if the empty
> range starts the file.
>
> So a patch that adds lines to an empty file would look like
>
> $ diff -U0 /dev/null test1
> --- /dev/null 2023-04-29 22:24:54.395999895 -0700
> +++ test1 2023-05-03 14:02:27.718960038 -0700
> @@ -0,0 +1,3 @@
> +A
> +B
> +C
>
> just as specified. Note "-0,0" that says "there was no line before
> this hunk before this change".
That is indeed fairly unintuitive, but I appreciate the detailed response. I shall adapt my scripts.
Thanks and best wishes,
Lukas
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-05-03 21:26 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-03 20:48 git diff -U0 header off-by-one error when deleting no lines Lukas Tenbrink
2023-05-03 21:19 ` Junio C Hamano
2023-05-03 21:26 ` Lukas Tenbrink
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).