git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] git-apply: complain about >=8 consecutive spaces in initial indent

From
JFJ. Bruce Fields <bfields@citi.umich.edu>
Date
Sep 16, 2007, 22:49 UTC
Message-ID
<1189982942187-git-send-email-bfields@citi.umich.edu>
In-Reply-To
<11899829424173-git-send-email-bfields@citi.umich.edu>

Complain if we find 8 spaces or more in a row as part of the initial whitespace on a line, and (with --whitespace=stripspace) replace such by a tab.

Well, linux's checkpatch.pl complains about this sort of thing.
Signed-off-by: J. Bruce Fields <bfields@citi.umich.edu>
---
 builtin-apply.c |   34 +++++++++++++++++++++++++++-------
 1 files changed, 27 insertions(+), 7 deletions(-)
diff --git a/builtin-apply.c b/builtin-apply.c
index 70359c1..fb63089 100644
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -918,6 +918,7 @@ static void check_whitespace(const char *line, int len)
 {
 	const char *err = "Adds trailing whitespace";
 	int seen_space = 0;
+	int consecutive_spaces = 0;
 	int i;
 
 	/*
@@ -944,6 +945,18 @@ static void check_whitespace(const char *line, int len)
 		else
 			break;
 	}
+
+	err = "Initial indent contains eight or more spaces in a row";
+	for (i = 1; i < len; i++) {
+		if (line[i] == ' ')
+			consecutive_spaces++;
+		else if (line[i] == '\t')
+			consecutive_spaces = 0;
+		else
+			break;
+		if (consecutive_spaces == 8)
+			goto error;
+	}
 	return;
 
  error:
@@ -1607,9 +1620,10 @@ 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 after_indent = -1;
 	int last_space_in_indent = -1;
 	int need_fix_leading_space = 0;
+	int consecutive_spaces = 0;
 	char *buf;
 
 	if ((new_whitespace != strip_whitespace) || !whitespace_error ||
@@ -1630,23 +1644,27 @@ static int apply_line(char *output, const char *patch, int plen)
 	for (i = 1; i < plen; i++) {
 		char ch = patch[i];
 		if (ch == '\t') {
-			last_tab_in_indent = i;
+			consecutive_spaces = 0;
 			if (0 <= last_space_in_indent)
 				need_fix_leading_space = 1;
 		}
-		else if (ch == ' ')
+		else if (ch == ' ') {
+			consecutive_spaces++;
 			last_space_in_indent = i;
-		else
+		} else
 			break;
+		if (consecutive_spaces == 8)
+			need_fix_leading_space = 1;
 	}
+	after_indent=i;
 
 	buf = output;
 	if (need_fix_leading_space) {
-		int consecutive_spaces = 0;
+		consecutive_spaces = 0;
 		/* between patch[1..last_tab_in_indent] strip the
 		 * funny spaces, updating them to tab as needed.
 		 */
-		for (i = 1; i < last_tab_in_indent; i++, plen--) {
+		for (i = 1; i < after_indent; i++, plen--) {
 			char ch = patch[i];
 			if (ch != ' ') {
 				consecutive_spaces = 0;
@@ -1660,7 +1678,9 @@ static int apply_line(char *output, const char *patch, int plen)
 			}
 		}
 		fixed = 1;
-		i = last_tab_in_indent;
+		i = after_indent;
+		i -= consecutive_spaces;
+		plen += consecutive_spaces;
 	}
 	else
 		i = 1;
-- 
1.5.3.1.42.gfe5df
Previous: J. Bruce FieldsNext: J. Bruce Fields
Message 3 of 16 in “whitespace-stripping”
  1. J. Bruce FieldsSep 16, 2007
  2. 1/3 git-apply: fix whitespace strippingJ. Bruce Fields, Sep 16, 2007
  3. 2/3 git-apply: complain about >=8 consecutive spaces in initial indentJ. Bruce Fields, Sep 16, 2007
  4. 3/3 git-apply: add tests for stripping of leading and trailing whitespaceJ. Bruce Fields, Sep 16, 2007
  5. Krzysztof HalasaSep 17, 2007
  6. J. Bruce FieldsSep 17, 2007
  7. Krzysztof HalasaSep 17, 2007
  8. J. Bruce FieldsSep 18, 2007
  9. Martin LanghoffSep 16, 2007
  10. J. Bruce FieldsSep 17, 2007
  11. Junio C HamanoSep 17, 2007
  12. J. Bruce FieldsSep 17, 2007
  13. git-diff: complain about >=8 consecutive spaces in initial indentJunio C Hamano, Oct 3, 2007
  14. David KastrupSep 18, 2007
  15. J. Bruce FieldsSep 18, 2007
  16. David KastrupSep 18, 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.