git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Teach 'git-apply --whitespace=strip' to remove empty lines at the end of file
@ 2007-05-20  9:51 Marco Costalba
  2007-05-20 10:03 ` Junio C Hamano
  0 siblings, 1 reply; 16+ messages in thread
From: Marco Costalba @ 2007-05-20  9:51 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Git Mailing List

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
---

This one seems to pass all the tests.

 builtin-apply.c |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/builtin-apply.c b/builtin-apply.c
index 0399743..a96f669 100644
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -1671,6 +1671,7 @@ static int apply_one_fragment(struct buffer_desc *desc,
 	char *new = xmalloc(size);
 	const char *oldlines, *newlines;
 	int oldsize = 0, newsize = 0;
+	int trailing_added_lines = 0;
 	unsigned long leading, trailing;
 	int pos, lines;

@@ -1699,6 +1700,15 @@ static int apply_one_fragment(struct buffer_desc *desc,
 			else if (first == '+')
 				first = '-';
 		}
+		/*
+		 * Only fragments that add lines at the bottom
+		 * of a file end with a list of '+' lines
+		*/
+		if (first == '+')
+			trailing_added_lines++;
+		else
+			trailing_added_lines = 0;
+
 		switch (first) {
 		case '\n':
 			/* Newer GNU diff, empty context line */
@@ -1738,6 +1748,18 @@ static int apply_one_fragment(struct buffer_desc *desc,
 		newsize--;
 	}

+	if (new_whitespace == strip_whitespace) {
+		/* Any added empty lines is already cleaned-up here
+		 * becuase of 'strip_whitespace' flag, so just count '\n'
+		*/
+		int empty = 0;
+		while (   empty < trailing_added_lines
+		       && newsize - empty - 2 > 0
+		       && new[newsize - empty - 2] == '\n')
+			empty++;
+
+		newsize -= empty;
+	}
 	oldlines = old;
 	newlines = new;
 	leading = frag->leading;
-- 
1.5.2.rc3.90.gf33e-dirty

^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2007-05-22 11:13 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-20  9:51 [PATCH] Teach 'git-apply --whitespace=strip' to remove empty lines at the end of file Marco Costalba
2007-05-20 10:03 ` Junio C Hamano
2007-05-20 10:34   ` Marco Costalba
2007-05-20 11:12     ` Junio C Hamano
2007-05-20 12:45       ` Marco Costalba
2007-05-20 18:36         ` Junio C Hamano
2007-05-20 18:56           ` Marco Costalba
2007-05-20 19:16             ` Junio C Hamano
2007-05-20 20:55               ` Marco Costalba
2007-05-20 19:17             ` Frank Lichtenheld
2007-05-20 20:44               ` Marco Costalba
2007-05-21  8:59                 ` Josef Weidendorfer
2007-05-21  6:57           ` Junio C Hamano
2007-05-21 11:23             ` Marco Costalba
2007-05-22  1:31               ` Junio C Hamano
2007-05-22 11:13                 ` Marco Costalba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).