Git development
 help / color / mirror / Atom feed
From: "Marco Costalba" <mcostalba@gmail.com>
To: "Junio C Hamano" <junkio@cox.net>
Cc: "Git Mailing List" <git@vger.kernel.org>
Subject: [PATCH] Tech 'git-apply' that spaces before a tab are candidate for stripping
Date: Sat, 19 May 2007 18:44:33 +0200	[thread overview]
Message-ID: <e5bfff550705190944y44cb57baiccb139b1d649face@mail.gmail.com> (raw)

Currently 'git-apply' identify as stealth space a line starting with
spaces followed by a tab.

This patch updates the algorithm to find spaces + tab also in the
middle of a line and not only at the beginning.

As example lines like

"#define MY_VALUE   \t 1"

are now identified and cleaned up if option '--whitespace=strip' is
used.

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

There are a bunch of this type in current git tree.

  builtin-apply.c |   72 ++++++++++++++++++++++++++++++------------------------
 1 files changed, 40 insertions(+), 32 deletions(-)

diff --git a/builtin-apply.c b/builtin-apply.c
index f17f838..9e82757 100644
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -1586,6 +1586,32 @@ static void remove_last_line(const char **rbuf,
int *rsize)
  	*rsize = offset + 1;
 }

+static int copy_line(char **output_ptr, const char *patch, int start, int len)
+{
+	char *output = *output_ptr;
+	int space_start = 0;
+	int i, end = start + len;
+
+	for (i = start; i < end; i++) {
+
+		char ch = patch[i];
+
+		if (ch == ' ' && !space_start)
+			space_start = i;
+
+		else if (space_start && ch != ' ') {
+			if (ch == '\t') {
+				*output_ptr = output - (i - space_start);
+				return space_start;
+			}
+			space_start = 0;
+		}
+		*output++ = ch;
+	}
+	*output_ptr = output;
+	return 0;
+}
+
 struct buffer_desc {
 	char *buffer;
 	unsigned long size;
@@ -1602,17 +1628,14 @@ static int apply_line(char *output, const char
*patch, int plen)
 	int i;
 	int add_nl_to_tail = 0;
 	int fixed = 0;
-	int last_tab_in_indent = -1;
-	int last_space_in_indent = -1;
-	int need_fix_leading_space = 0;
-	char *buf;
+	int space_start;
+	const char *old = output;

 	if ((new_whitespace != strip_whitespace) || !whitespace_error ||
 	    *patch != '+') {
 		memcpy(output, patch + 1, plen);
 		return plen;
 	}
-
 	if (1 < plen && isspace(patch[plen-1])) {
 		if (patch[plen] == '\n')
 			add_nl_to_tail = 1;
@@ -1621,44 +1644,29 @@ static int apply_line(char *output, const char
*patch, int plen)
 			plen--;
 		fixed = 1;
 	}
+	space_start = copy_line(&output, patch, 1, plen);
+	while (space_start) {

-	for (i = 1; i < plen; i++) {
-		char ch = patch[i];
-		if (ch == '\t') {
-			last_tab_in_indent = i;
-			if (0 <= last_space_in_indent)
-				need_fix_leading_space = 1;
-		}
-		else if (ch == ' ')
-			last_space_in_indent = i;
-		else
-			break;
-	}
-
-	buf = output;
-	if (need_fix_leading_space) {
-		/* between patch[1..last_tab_in_indent] strip the
-		 * funny spaces, updating them to tab as needed.
+		/* strip the funny spaces, updating them to tab as needed
+		 * strip is done one space sequence at time, until line end
 		 */
-		for (i = 1; i < last_tab_in_indent; i++, plen--) {
+		fixed = 1;
+		for (i = space_start; i < plen + 1; i++) {
+
 			char ch = patch[i];
 			if (ch != ' ')
-				*output++ = ch;
+				break;
 			else if ((i % 8) == 0)
 				*output++ = '\t';
 		}
-		fixed = 1;
-		i = last_tab_in_indent;
+		space_start = copy_line(&output, patch, i, plen + 1 - i);
 	}
-	else
-		i = 1;
-
-	memcpy(output, patch + i, plen);
 	if (add_nl_to_tail)
-		output[plen++] = '\n';
+		*output++ = '\n';
 	if (fixed)
 		applied_after_stripping++;
-	return output + plen - buf;
+
+	return output - old;
 }

 static int apply_one_fragment(struct buffer_desc *desc, struct
fragment *frag, int inaccurate_eof)
-- 
1.5.2.rc3.88.g4c3ba-dirty

             reply	other threads:[~2007-05-19 16:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-05-19 16:44 Marco Costalba [this message]
2007-05-20  7:53 ` [PATCH] Tech 'git-apply' that spaces before a tab are candidate for stripping Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e5bfff550705190944y44cb57baiccb139b1d649face@mail.gmail.com \
    --to=mcostalba@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=junkio@cox.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox