diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh index 935d101fe8..a8736f7cbe 100755 --- a/t/t4015-diff-whitespace.sh +++ b/t/t4015-diff-whitespace.sh @@ -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 +diff --git a/x b/x +index 9daeafb..2874b91 100644 +--- a/x ++++ b/x +@@ -1 +1,4 @@ + test ++{ ++ ++} +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 diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh index 6f7548c3a1..3f3c7577ca 100755 --- a/t/t4034-diff-words.sh +++ b/t/t4034-diff-words.sh @@ -35,10 +35,10 @@ aeff = aeff * ( aaa ) EOF cat > expect <<\EOF -diff --git a/pre b/post -index 330b04f..5ed8eff 100644 ---- a/pre -+++ b/post +diff --git a/pre b/post +index 330b04f..5ed8eff 100644 +--- a/pre ++++ b/post @@ -1,3 +1,7 @@ h(4)h(4),hh[44] @@ -122,10 +122,10 @@ test_expect_success '--word-diff=plain --no-color' ' ' cat > expect <diff --git a/pre b/post -index 330b04f..5ed8eff 100644 ---- a/pre -+++ b/post +diff --git a/pre b/post +index 330b04f..5ed8eff 100644 +--- a/pre ++++ b/post @@ -1,3 +1,7 @@ [-h(4)-]{+h(4),hh[44]+} @@ -143,10 +143,10 @@ test_expect_success '--word-diff=plain --color' ' ' cat > expect <<\EOF -diff --git a/pre b/post -index 330b04f..5ed8eff 100644 ---- a/pre -+++ b/post +diff --git a/pre b/post +index 330b04f..5ed8eff 100644 +--- a/pre ++++ b/post @@ -1 +1 @@ h(4)h(4),hh[44] @@ -3,0 +4,4 @@ a = b + c @@ -163,10 +163,10 @@ test_expect_success 'word diff without context' ' ' cat > expect <<\EOF -diff --git a/pre b/post -index 330b04f..5ed8eff 100644 ---- a/pre -+++ b/post +diff --git a/pre b/post +index 330b04f..5ed8eff 100644 +--- a/pre ++++ b/post @@ -1,3 +1,7 @@ h(4),hh[44] @@ -199,10 +199,10 @@ test_expect_success 'option overrides .gitattributes' ' ' cat > expect <<\EOF -diff --git a/pre b/post -index 330b04f..5ed8eff 100644 ---- a/pre -+++ b/post +diff --git a/pre b/post +index 330b04f..5ed8eff 100644 +--- a/pre ++++ b/post @@ -1,3 +1,7 @@ h(4),hh[44] @@ -231,10 +231,10 @@ test_expect_success 'command-line overrides config' ' ' cat > expect <<\EOF -diff --git a/pre b/post -index 330b04f..5ed8eff 100644 ---- a/pre -+++ b/post +diff --git a/pre b/post +index 330b04f..5ed8eff 100644 +--- a/pre ++++ b/post @@ -1,3 +1,7 @@ h(4),{+hh+}[44] @@ -260,10 +260,10 @@ test_expect_success 'remove diff driver regex' ' ' cat > expect <<\EOF -diff --git a/pre b/post -index 330b04f..5ed8eff 100644 ---- a/pre -+++ b/post +diff --git a/pre b/post +index 330b04f..5ed8eff 100644 +--- a/pre ++++ b/post @@ -1,3 +1,7 @@ h(4),hh[44] @@ -282,10 +282,10 @@ echo 'aaa (aaa)' > pre echo 'aaa (aaa) aaa' > post cat > expect <<\EOF -diff --git a/pre b/post -index c29453b..be22f37 100644 ---- a/pre -+++ b/post +diff --git a/pre b/post +index c29453b..be22f37 100644 +--- a/pre ++++ b/post @@ -1 +1 @@ aaa (aaa) aaa EOF @@ -301,10 +301,10 @@ echo '(:' > pre echo '(' > post cat > expect <<\EOF -diff --git a/pre b/post -index 289cb9d..2d06f37 100644 ---- a/pre -+++ b/post +diff --git a/pre b/post +index 289cb9d..2d06f37 100644 +--- a/pre ++++ b/post @@ -1 +1 @@ (: EOF diff --git a/t/test-lib.sh b/t/test-lib.sh index 25f8bf95cd..c6afebb00d 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -238,14 +238,47 @@ test_set_editor () { } test_decode_color () { - sed -e 's/.\[1m//g' \ - -e 's/.\[31m//g' \ - -e 's/.\[32m//g' \ - -e 's/.\[33m//g' \ - -e 's/.\[34m//g' \ - -e 's/.\[35m//g' \ - -e 's/.\[36m//g' \ - -e 's/.\[m//g' + awk ' + function name(n) { + if (n == 0) return "RESET"; + if (n == 1) return "BOLD"; + if (n == 30) return "BLACK"; + if (n == 31) return "RED"; + if (n == 32) return "GREEN"; + if (n == 33) return "YELLOW"; + if (n == 34) return "BLUE"; + if (n == 35) return "MAGENTA"; + if (n == 36) return "CYAN"; + if (n == 37) return "WHITE"; + if (n == 40) return "BLACK"; + if (n == 41) return "BRED"; + if (n == 42) return "BGREEN"; + if (n == 43) return "BYELLOW"; + if (n == 44) return "BBLUE"; + if (n == 45) return "BMAGENTA"; + if (n == 46) return "BCYAN"; + if (n == 47) return "BWHITE"; + } + { + while (match($0, /\x1b\[[0-9;]*m/) != 0) { + printf "%s<", substr($0, 1, RSTART-1); + codes = substr($0, RSTART+2, RLENGTH-3); + if (length(codes) == 0) + printf "%s", name(0) + else { + n = split(codes, ary, ";"); + sep = ""; + for (i = 1; i <= n; i++) { + printf "%s%s", sep, name(ary[i]); + sep = ";" + } + } + printf ">"; + $0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1); + } + print + } + ' } nul_to_q () { diff --git a/ws.c b/ws.c index d7b8c33f14..7302f8f5a2 100644 --- a/ws.c +++ b/ws.c @@ -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') @@ -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) {