Fix "diff --check" whitespace detection
authorWincent Colaiuta <win@wincent.com>
Wed, 12 Dec 2007 16:22:59 +0000 (17:22 +0100)
committerJunio C Hamano <gitster@pobox.com>
Wed, 12 Dec 2007 19:24:33 +0000 (11:24 -0800)
"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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff.c
t/t4015-diff-whitespace.sh
diff --git a/diff.c b/diff.c
index d97ebc501af20929a07e754cd74bea06c7114811..9c79ee289151e9fc3eb1aaad91dd979bfee63102 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -1044,11 +1044,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')
index 79fdff3f3a87cfe45b7fefa96e350675bd4e048c..6adf9d11d03c3e9b1b7ee1ee46364f0e3a755016 100755 (executable)
@@ -117,4 +117,13 @@ EOF
 git diff -b > out
 test_expect_success 'another test, with -b' 'git diff expect out'
 
+
+test_expect_success 'check mixed spaces and tabs in indent' '
+
+       # This is indented with SP HT SP.
+       echo "   foo();" > x &&
+       git diff --check | grep "space before tab"
+
+'
+
 test_done