Skip to content

Commit

Permalink
diff: handle lines containing only whitespace and tabs better
Browse files Browse the repository at this point in the history
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 <kevin@sb.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Kevin Ballard authored and Junio C Hamano committed Oct 20, 2010
1 parent a471833 commit cfd1a98
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
37 changes: 37 additions & 0 deletions t/t4015-diff-whitespace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,41 @@ test_expect_success 'combined diff with autocrlf conversion' '
'

# Start testing the colored format for whitespace checks

test_expect_success 'setup diff colors' '
git config color.diff always &&
git config color.diff.plain normal &&
git config color.diff.meta bold &&
git config color.diff.frag cyan &&
git config color.diff.func normal &&
git config color.diff.old red &&
git config color.diff.new green &&
git config color.diff.commit yellow &&
git config color.diff.whitespace "normal red" &&
git config core.autocrlf false
'
cat >expected <<\EOF
<BOLD>diff --git a/x b/x<RESET>
<BOLD>index 9daeafb..2874b91 100644<RESET>
<BOLD>--- a/x<RESET>
<BOLD>+++ b/x<RESET>
<CYAN>@@ -1 +1,4 @@<RESET>
test<RESET>
<GREEN>+<RESET><GREEN>{<RESET>
<GREEN>+<RESET><BRED> <RESET>
<GREEN>+<RESET><GREEN>}<RESET>
EOF

test_expect_success 'diff that introduces a line with only tabs' '
git config core.whitespace blank-at-eol &&
git reset --hard &&
echo "test" > x &&
git commit -m "initial" x &&
echo "{NTN}" | tr "NT" "\n\t" >> x &&
git -c color.diff=always diff | test_decode_color >current &&
test_cmp expected current
'

test_done
7 changes: 4 additions & 3 deletions ws.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,11 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
}
}

if (trailing_whitespace == -1)
trailing_whitespace = len;

/* Check indentation */
for (i = 0; i < len; i++) {
for (i = 0; i < trailing_whitespace; i++) {
if (line[i] == ' ')
continue;
if (line[i] != '\t')
Expand Down Expand Up @@ -218,8 +221,6 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
* Now the rest of the line starts at "written".
* The non-highlighted part ends at "trailing_whitespace".
*/
if (trailing_whitespace == -1)
trailing_whitespace = len;

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

0 comments on commit cfd1a98

Please sign in to comment.