@matheusbsbs / matheus-bagliano / commits / cfd1a9849c
Fork 0

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 <kevin@sb.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kevin Ballard committed Oct 20, 2010 at 15:17 UTC cfd1a9849cdb4b9c7fcd0cb71a28cbdbc3494329
2 files changed +41 -3
t/t4015-diff-whitespace.sh
+37
@@ -491,4 +491,41 @@ test_expect_success 'combined diff with autocrlf conversion' '
491
492 '
493
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 +
531 test_done
ws.c
+4 -3
@@ -174,8 +174,11 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
174 }
175 }
176
177 + if (trailing_whitespace == -1)
178 + trailing_whitespace = len;
179 +
180 /* Check indentation */
178 - for (i = 0; i < len; i++) {
181 + for (i = 0; i < trailing_whitespace; i++) {
182 if (line[i] == ' ')
183 continue;
184 if (line[i] != '\t')
@@ -218,8 +221,6 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
221 * Now the rest of the line starts at "written".
222 * The non-highlighted part ends at "trailing_whitespace".
223 */
221 - if (trailing_whitespace == -1)
222 - trailing_whitespace = len;
224
225 /* Emit non-highlighted (middle) segment. */
226 if (trailing_whitespace - written > 0) {