From: Marco Costalba Date: Tue, 22 May 2007 11:13:21 GMT Subject: Re: [PATCH] Teach 'git-apply --whitespace=strip' to remove empty lines at the end of file Message-ID: In-Reply-To: <7vbqgdbq5j.fsf@assigned-by-dhcp.cox.net> On 5/22/07, Junio C Hamano wrote: > "Marco Costalba" writes: > > > On 5/21/07, Junio C Hamano wrote: > >> Junio C Hamano 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 > >> > > The final, and correct version is: if (new_whitespace == strip_whitespace && trailing_added_lines) { int n = 0; for ( ; n <= trailing_added_lines; n++) { /* counting trailing '\n' */ if (newsize == n) { n++; break; } if (new[newsize - 1 - n] != '\n') break; } trailing_added_lines = (n>0) ? --n : 0; } else trailing_added_lines = 0; but I understand is ugly as hell. The fact is, it is far easier to count '\n' *while* they are created then after at the end. So no problem for me if you drop my patch. Marco