All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Lukas Tenbrink <lukastenbrink@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: git diff -U0 header off-by-one error when deleting no lines
Date: Wed, 03 May 2023 14:19:23 -0700	[thread overview]
Message-ID: <xmqqednxp0ys.fsf@gitster.g> (raw)
In-Reply-To: <B097ECB7-1976-40FC-B91D-A60D648F973B@gmail.com> (Lukas Tenbrink's message of "Wed, 3 May 2023 22:48:44 +0200")

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".

  reply	other threads:[~2023-05-03 21:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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=xmqqednxp0ys.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=lukastenbrink@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.