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
MCMarco Costalba <mcostalba@gmail.com>
Date
May 19, 2007, 21:58 UTC
Message-ID
<e5bfff550705191458o3fa1d1a0ndd916d1efafdd2a7@mail.gmail.com>
In-Reply-To
<7vhcq8v97k.fsf@assigned-by-dhcp.cox.net>
On 5/19/07, Junio C Hamano <junkio@cox.net> wrote:
Show 78 quoted lines
> Marco Costalba <mcostalba@gmail.com> writes:
>
> > Signed-off-by: Marco Costalba <mcostalba@gmail.com>
> > ---
> >   builtin-apply.c |    4 ++++
> >   1 files changed, 4 insertions(+), 0 deletions(-)
> >
> > diff --git a/builtin-apply.c b/builtin-apply.c
> > index 0399743..f17f838 100644
> > --- a/builtin-apply.c
> > +++ b/builtin-apply.c
> > @@ -1738,6 +1738,10 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
> >               newsize--;
> >       }
> >
> > +     if (new_whitespace == strip_whitespace)
> > +             while (newsize > 1 && !strncmp(new + newsize - 2, "\n\n", 2))
> > +                     newsize--;
> > +
> >       oldlines = old;
> >       newlines = new;
> >       leading = frag->leading;
>
> I agree to what you are trying to do, but this patch is wrong.
> You are stripping trailing newlines that were NOT introduced by
> the patch, but happened to be present in the preimage (and in
> the context).
>
> Try it on this test vector:
>
> cat >AAA <<\EOF
> a
> b
> c
> d
>
>
> e
> f
> g
> h
> i
>
>
> j
> k
>
>
> EOF
> cat >P.diff <<\EOF
> diff --git a/AAA b/AAA
> index 59f6a9c..ffb28f5 100644
> --- a/AAA
> +++ b/AAA
> @@ -1,4 +1,4 @@
> -a
> +A
>  b
>  c
>  d
> @@ -6,12 +6,11 @@ d
>
>  e
>  f
> +
> +
>  g
>  h
> -i
> -
>
> -j
>  k
>
>
> EOF
>
>
What about this?
 builtin-apply.c |   13 +++++++++----
 1 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/builtin-apply.c b/builtin-apply.c
index 9e82757..113c71f 100644
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -1746,10 +1746,15 @@ static int apply_one_fragment(struct
buffer_desc *desc, struct fragment *frag, i
 		newsize--;
 	}

-	if (new_whitespace == strip_whitespace)
-		while (newsize > 1 && !strncmp(new + newsize - 2, "\n\n", 2))
-			newsize--;
-
+	if (new_whitespace == strip_whitespace) {
+		int cnt1 =  1, cnt2 = 1;
+		while (newsize -  cnt1 > 1 && new[newsize - cnt1] == '\n')
+			cnt1++;
+		while (oldsize -  cnt2 > 1 && new[newsize - cnt2] == '\n')
+			cnt2++;
+		if (cnt1 > cnt2 && cnt1 > 2)
+			newsize -= cnt1 - cnt2;
+	}
 	oldlines = old;
 	newlines = new;
 	leading = frag->leading;
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 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.