whitespace: fix initial-indent checking

After this patch, "written" counts the number of bytes up to and
including the most recently seen tab.  This allows us to detect (and
count) spaces by comparing to "i".

This allows catching initial indents like '\t        ' (a tab followed
by 8 spaces), while previously indent-with-non-tab caught only indents
that consisted entirely of spaces.

This also allows fixing an indent-with-non-tab regression, so we can
again detect indents like '\t \t'.

Also update tests to catch these cases.

Signed-off-by: J. Bruce Fields <bfields@citi.umich.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
J. Bruce Fields 2007-12-16 11:31:40 -05:00 committed by Junio C Hamano
parent 954ecd4353
commit 9afa2d4aa9
2 changed files with 19 additions and 6 deletions

View File

@ -125,6 +125,14 @@ test_expect_success 'check mixed spaces and tabs in indent' '
'
test_expect_success 'check mixed tabs and spaces in indent' '
# This is indented with HT SP HT.
echo " foo();" > x &&
git diff --check | grep "space before tab in indent"
'
test_expect_success 'check with no whitespace errors' '
git commit -m "snapshot" &&
@ -311,4 +319,11 @@ test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' '
'
test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' '
git config core.whitespace "indent-with-non-tab" &&
echo " foo ();" > x &&
! git diff --check
'
test_done

10
ws.c
View File

@ -146,19 +146,17 @@ unsigned check_and_emit_line(const char *line, int len, unsigned ws_rule,
/* Check for space before tab in initial indent. */
for (i = 0; i < len; i++) {
if (line[i] == ' ') {
written = i + 1;
if (line[i] == ' ')
continue;
}
if (line[i] != '\t')
break;
if ((ws_rule & WS_SPACE_BEFORE_TAB) && (written != 0))
if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i)
result |= WS_SPACE_BEFORE_TAB;
break;
written = i + 1;
}
/* Check for indent using non-tab. */
if ((ws_rule & WS_INDENT_WITH_NON_TAB) && written >= 8)
if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= 8)
result |= WS_INDENT_WITH_NON_TAB;
if (stream) {