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

[PATCH 1/4] Fix "diff --check" whitespace detection

From
Wincent Colaiuta <win@wincent.com>
Date
Dec 12, 2007, 16:22 UTC
Message-ID
<1197476582-18956-2-git-send-email-win@wincent.com>
In-Reply-To
<1197476582-18956-1-git-send-email-win@wincent.com>

"diff --check" would only detect spaces before tabs if a tab was the last character in the leading indent. Fix that and add a test case to make sure the bug doesn't regress in the future.

Signed-off-by: Wincent Colaiuta <win@wincent.com>
---

Although this patch is made to apply on top of the topic I posted earlier, it can be applied to master with a couple of tweaks. Let me know if you want me to send such a patch.

In any case, [2/4] and [3/4] factor away the code that had the bug in it anyway...

 diff.c                     |   13 ++++++++++---
 t/t4015-diff-whitespace.sh |    8 ++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/diff.c b/diff.c
index e89c7ce..c9b3884 100644
--- a/diff.c
+++ b/diff.c
@@ -1010,11 +1010,18 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
 		int i, spaces = 0, space_before_tab = 0, white_space_at_end = 0;
 
 		/* check space before tab */
-		for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
+		for (i = 1; i < len; i++) {
 			if (line[i] == ' ')
 				spaces++;
-		if (line[i - 1] == '\t' && spaces)
-			space_before_tab = 1;
+			else if (line[i] == '\t') {
+				if (spaces) {
+					space_before_tab = 1;
+					break;
+				}
+			}
+			else
+				break;
+		}
 
 		/* check whitespace at line end */
 		if (line[len - 1] == '\n')
diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh
index 5aaf2db..ff77a16 100755
--- a/t/t4015-diff-whitespace.sh
+++ b/t/t4015-diff-whitespace.sh
@@ -248,4 +248,12 @@ test_expect_failure 'check with space before tab in indent (diff-tree)' '
 
 '
 
+# was a bug: space before tab only caught if tab was last in the indent
+test_expect_failure 'check mixed spaces and tabs in indent' '
+
+	echo " 	 foo();" > x &&
+	git diff --check
+
+'
+
 test_done
-- 
1.5.3.7.1159.g2f071-dirty
Previous: Wincent ColaiutaNext: Wincent Colaiuta
Message 2 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.