Skip to content

Commit cfd1a98

Browse files
lilyballgitster
authored andcommitted
diff: handle lines containing only whitespace and tabs better
When a line contains nothing but whitespace with at least one tab and the core.whitespace config option contains blank-at-eol, the whitespace on the line is being printed twice, once unhighlighted (unless otherwise matched by one of the other core.whitespace values), and a second time highlighted for blank-at-eol. Update the leading indentation check to stop checking when it reaches the trailing whitespace. Signed-off-by: Kevin Ballard <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a471833 commit cfd1a98

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

t/t4015-diff-whitespace.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,4 +491,41 @@ test_expect_success 'combined diff with autocrlf conversion' '
491491
492492
'
493493

494+
# Start testing the colored format for whitespace checks
495+
496+
test_expect_success 'setup diff colors' '
497+
git config color.diff always &&
498+
git config color.diff.plain normal &&
499+
git config color.diff.meta bold &&
500+
git config color.diff.frag cyan &&
501+
git config color.diff.func normal &&
502+
git config color.diff.old red &&
503+
git config color.diff.new green &&
504+
git config color.diff.commit yellow &&
505+
git config color.diff.whitespace "normal red" &&
506+
507+
git config core.autocrlf false
508+
'
509+
cat >expected <<\EOF
510+
<BOLD>diff --git a/x b/x<RESET>
511+
<BOLD>index 9daeafb..2874b91 100644<RESET>
512+
<BOLD>--- a/x<RESET>
513+
<BOLD>+++ b/x<RESET>
514+
<CYAN>@@ -1 +1,4 @@<RESET>
515+
test<RESET>
516+
<GREEN>+<RESET><GREEN>{<RESET>
517+
<GREEN>+<RESET><BRED> <RESET>
518+
<GREEN>+<RESET><GREEN>}<RESET>
519+
EOF
520+
521+
test_expect_success 'diff that introduces a line with only tabs' '
522+
git config core.whitespace blank-at-eol &&
523+
git reset --hard &&
524+
echo "test" > x &&
525+
git commit -m "initial" x &&
526+
echo "{NTN}" | tr "NT" "\n\t" >> x &&
527+
git -c color.diff=always diff | test_decode_color >current &&
528+
test_cmp expected current
529+
'
530+
494531
test_done

ws.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,11 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
174174
}
175175
}
176176

177+
if (trailing_whitespace == -1)
178+
trailing_whitespace = len;
179+
177180
/* Check indentation */
178-
for (i = 0; i < len; i++) {
181+
for (i = 0; i < trailing_whitespace; i++) {
179182
if (line[i] == ' ')
180183
continue;
181184
if (line[i] != '\t')
@@ -218,8 +221,6 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
218221
* Now the rest of the line starts at "written".
219222
* The non-highlighted part ends at "trailing_whitespace".
220223
*/
221-
if (trailing_whitespace == -1)
222-
trailing_whitespace = len;
223224

224225
/* Emit non-highlighted (middle) segment. */
225226
if (trailing_whitespace - written > 0) {

0 commit comments

Comments
 (0)