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 the end of file

From
Junio C Hamano <junkio@cox.net>
Date
May 22, 2007, 01:31 UTC
Message-ID
<7vbqgdbq5j.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<e5bfff550705210423i34dc481es61d3b886ae77c5f7@mail.gmail.com>
"Marco Costalba" <mcostalba@gmail.com> writes:
Show 27 quoted lines
> On 5/21/07, Junio C Hamano <junkio@cox.net> wrote:
>> Junio C Hamano <junkio@cox.net> writes:
>>
>>
>> We somehow end up removing one LF too many, like this:
>>
>>     diff --git a/contrib/emacs/.gitignore b/contrib/emacs/.gitignore
>>     index c531d98..016d3b1 100644
>>     --- a/contrib/emacs/.gitignore
>>     +++ b/contrib/emacs/.gitignore
>>     @@ -1 +1 @@
>>     -*.elc
>>     +*.elc
>>     \ No newline at end of file
>>
>
> I also had that, but after adding
>
> +
> +               if (empty < trailing_added_lines)
> +                       empty--;
> +
>
> everything worked correctly. I made again the same test myself
> without problems.
>
> I really don't understand how could be broken.
Hmmm.  Puzzled.

Let's say that the patch is to create a file that has a single line, like this:

diff --git a/contrib/emacs/.gitignore b/contrib/emacs/.gitignore
new file mode 100644
index 0000000..c531d98
--- /dev/null
+++ b/contrib/emacs/.gitignore
@@ -0,0 +1 @@
+*.elc

The function "apply_one_fragment" gets two lines ('@' and '+').

We come to "while (size > 0)" loop.  During the first round,
'first' is '@' and the line is ignored.  In the second round,
'first' is '+', so apply_line appends the contents to 'new'
buffer, while we count trailing_added_lines.

End result is

 - newsize = 6, new has "*.elc\n";
 - oldsize = 0, and old has "";
 - trailing_added_lines = 1;

when we get to the "empty" counting code.

Then you count empty up to trailing_added_lines.  When we get to
the "if (empty < trailing_added_lines)" code, empty is 1.  You
do not decrement this, and take that number in
trailing_added_lines, to be used to strip the trailing run of
newlines in the for (;;) loop later.  That's how you can lose
the last newline that is not on a blank line.
Previous: Marco CostalbaNext: Marco Costalba
Message 15 of 16 in “Teach 'git-apply --whitespace=strip' to remove empty lines at the end of file”
  1. Teach 'git-apply --whitespace=strip' to remove empty lines at the end of fileMarco Costalba, May 20, 2007
  2. Junio C HamanoMay 20, 2007
  3. Marco CostalbaMay 20, 2007
  4. Junio C HamanoMay 20, 2007
  5. Marco CostalbaMay 20, 2007
  6. Junio C HamanoMay 20, 2007
  7. Marco CostalbaMay 20, 2007
  8. Junio C HamanoMay 20, 2007
  9. Marco CostalbaMay 20, 2007
  10. Frank LichtenheldMay 20, 2007
  11. Marco CostalbaMay 20, 2007
  12. Josef WeidendorferMay 21, 2007
  13. Junio C HamanoMay 21, 2007
  14. Marco CostalbaMay 21, 2007
  15. Junio C HamanoMay 22, 2007
  16. Marco CostalbaMay 22, 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.