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

[PATCH 3/3] Make "diff --check" use shared whitespace functions

From
Wincent Colaiuta <win@wincent.com>
Date
Dec 12, 2007, 16:23 UTC
Message-ID
<1197476582-18956-4-git-send-email-win@wincent.com>
In-Reply-To
<1197476582-18956-3-git-send-email-win@wincent.com>
Use the logic refactored into ws.c rather than duplicating it.
Signed-off-by: Wincent Colaiuta <win@wincent.com>
---
 diff.c |   49 +++++++++++--------------------------------------
 1 files changed, 11 insertions(+), 38 deletions(-)
diff --git a/diff.c b/diff.c
index c9b3884..7ad3c63 100644
--- a/diff.c
+++ b/diff.c
@@ -996,7 +996,7 @@ struct checkdiff_t {
 	const char *filename;
 	int lineno, color_diff;
 	unsigned ws_rule;
-	int status;
+	unsigned status;
 };
 
 static void checkdiff_consume(void *priv, char *line, unsigned long len)
@@ -1005,45 +1005,18 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
 	const char *ws = diff_get_color(data->color_diff, DIFF_WHITESPACE);
 	const char *reset = diff_get_color(data->color_diff, DIFF_RESET);
 	const char *set = diff_get_color(data->color_diff, DIFF_FILE_NEW);
+	char *err;
 
 	if (line[0] == '+') {
-		int i, spaces = 0, space_before_tab = 0, white_space_at_end = 0;
-
-		/* check space before tab */
-		for (i = 1; i < len; i++) {
-			if (line[i] == ' ')
-				spaces++;
-			else if (line[i] == '\t') {
-				if (spaces) {
-					space_before_tab = 1;
-					break;
-				}
-			}
-			else
-				break;
-		}
-
-		/* check whitespace at line end */
-		if (line[len - 1] == '\n')
-			len--;
-		if (isspace(line[len - 1]))
-			white_space_at_end = 1;
-
-		if (space_before_tab || white_space_at_end) {
-			data->status = 1;
-			printf("%s:%d: %s", data->filename, data->lineno, ws);
-			if (space_before_tab) {
-				printf("space before tab");
-				if (white_space_at_end)
-					putchar(',');
-			}
-			if (white_space_at_end)
-				printf("whitespace at end");
-			printf(":%s ", reset);
-			emit_line_with_ws(1, set, reset, ws, line, len,
-					  data->ws_rule);
-		}
-
+		data->status = check_whitespace(line, len, data->ws_rule);
+		if (!data->status)
+			return;
+		err = whitespace_error_string(data->status);
+		printf("%s:%d: %s%s:%s ", data->filename, data->lineno,
+		    ws, err, reset);
+		free(err);
+		emit_line_with_ws(1, set, reset, ws, line, len,
+		    data->ws_rule);
 		data->lineno++;
 	} else if (line[0] == ' ')
 		data->lineno++;
-- 
1.5.3.7.1159.g2f071-dirty
Previous: Wincent ColaiutaNext: Wincent Colaiuta
Message 4 of 7 in “Refactoring for apply and diff”
  1. 0/4 Refactoring for apply and diffWincent Colaiuta, Dec 12, 2007
  2. 1/4 Fix "diff --check" whitespace detectionWincent Colaiuta, Dec 12, 2007
  3. 2/4 Extract and improve whitespace check from "git apply"Wincent Colaiuta, Dec 12, 2007
  4. 3/3 Make "diff --check" use shared whitespace functionsWincent Colaiuta, Dec 12, 2007
  5. 4/4 Add tests for "git diff --check" with core.whitespace optionsWincent Colaiuta, Dec 12, 2007
  6. Junio C HamanoDec 12, 2007
  7. Wincent ColaiutaDec 12, 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.