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

Re: [PATCH] Add a test-case for git-apply trying to add an ending line

From
Junio C Hamano <junkio@cox.net>
Date
May 24, 2006, 00:31 UTC
Message-ID
<7vd5e4z2je.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<20060523214836.22628.2179.stgit@localhost.localdomain>
Hmmmmm.
To git-apply, this patch:
        diff --git a/file b/file
        --- a/file
        +++ b/file
        @@ -1,2 +1,3 @@
         a
         b
        +c

currently means "I want to append a line c immediately after the lines that have a and then b". Nothing else. And applying it to

	a
        b
        c
produces
	a
        b
        c
        c

The first c is what your patch added, and the second c is what was originally there.

I do not think this is necessarily a bug. You _could_ make the EOF a special case (i.e. you _could_ interpret the patch that it also says, with "@@ -1,2", that "the result of this patch _must_ end with this line I just added"), and if you are going to do that, you would also need a symmetric special case for the beginning of the file, but I do not think it is the right thing to do in general.

Realistically, you would have something like this:
        diff --git a/apply.c b/apply.c
        index 0ed9d13..f99c6fe 100644
        --- a/apply.c
        +++ b/apply.c
        @@ -2297,3 +2297,8 @@ int main(int argc, char **argv)
                return 0; /* end of main */
         }
        +
        +static void this_function_is_unused(void)
        +{
        +	printf("hello, world\n");
        +}
You added a useless function at the end of the file.

While you prepared the above patch, the upstream updated the same file to end like this:

                return 0; /* end of main */
        }
        static int some_new_function(void)
        {
                return 314;
        }

Now, git-apply would produce this file if you apply the above patch:

                return 0; /* end of main */
        }
        static void this_function_is_unused(void)
        {
                printf("hello, world\n");
        }
        static int some_new_function(void)
        {
                return 314;
        }

I think this current behaviour is more desirable than special casing the end of file and refusing to apply.

In this particular case, expecting failure like your new test does is somewhat understandable, but if you change the test case to start with this file, you would realize that your expectation is not the only valid understanding of what is really happening:

	echo 'a' >file
	echo 'b' >>file
	echo 'd' >>file
Applying test-patch to this would result in
	a
        b
        c
        d
which I think is more useful behaviour.
Previous: Catalin MarinasNext: Junio C Hamano
Message 2 of 9 in “Add a test-case for git-apply trying to add an ending line”
  1. Add a test-case for git-apply trying to add an ending lineCatalin Marinas, May 23, 2006
  2. Junio C HamanoMay 24, 2006
  3. Junio C HamanoMay 24, 2006
  4. Junio C HamanoMay 24, 2006
  5. Linus TorvaldsMay 24, 2006
  6. Linus TorvaldsMay 24, 2006
  7. Junio C HamanoMay 24, 2006
  8. Catalin MarinasMay 24, 2006
  9. Linus TorvaldsMay 24, 2006

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.