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

Re: [PATCH] Teach 'git-apply --whitespace=strip' to remove empty lines at end of file

From
Junio C Hamano <junkio@cox.net>
Date
May 19, 2007, 23:18 UTC
Message-ID
<7vabw0tnbr.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<7vsl9stnzp.fsf@assigned-by-dhcp.cox.net>
Junio C Hamano <junkio@cox.net> writes:
Show 10 quoted lines
> ... these may or may not be at the end of the file, so
> inspecting what blank lines they have at the end is not
> sufficient.  If "new" does not introduce new blank lines at its
> end, then you can be sure that you are not adding trailing blank
> lines, but even if "new" does introduce a new blank line at the
> end, you do not know if that is adding it to the end of the
> file, or in the middle.
>
> You do not know where the hunk is applied until you do the loop
> that follows the part your patch we are discussing.
If I were doing this, I would probably do it this way:
 (1) Inside apply_one_fragment(), where "case '+':" appears, count
     the blank (not just '\n', but matches /^\s*$/) lines at the
     end of "new" side.  As soon as you fall into "case ' ':" or
     "case '-':" or non-blank line in "case '+':", you reset the
     counter to zero, so that what you are counting is the
     number of blank lines that would have get added, if the
     hunk were to be applied at the end of the file.  Keep that
     number ofter you separated the fragment into new and old.
 (2) In the same function, inside the big "for (;;)" loop that
     figures out where to apply that "old" => "new" change, use
     the number you gathered in the step (1) to trim what is
     applied, where the real application happens, which is the
     part that has memmove()/memcpy(), only when you know you
     are applying the hunk at the end of the file.  That is the
     only place in the function that knows where the hunk is
     being applied.
Previous: Junio C Hamano
Message 5 of 5 in “Teach 'git-apply --whitespace=strip' to remove empty lines at end of file”
  1. Teach 'git-apply --whitespace=strip' to remove empty lines at end of fileMarco Costalba, May 19, 2007
  2. Junio C HamanoMay 19, 2007
  3. Marco CostalbaMay 19, 2007
  4. Junio C HamanoMay 19, 2007
  5. Junio C HamanoMay 19, 2007

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.