提交 44e4155f 编写于 作者: J Junio C Hamano

Merge branch 'kb/maint-diff-ws-check'

* kb/maint-diff-ws-check:
  diff: handle lines containing only whitespace and tabs better
  test-lib: extend test_decode_color to handle more color codes
...@@ -491,4 +491,41 @@ test_expect_success 'combined diff with autocrlf conversion' ' ...@@ -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 test_done
...@@ -35,10 +35,10 @@ aeff = aeff * ( aaa ) ...@@ -35,10 +35,10 @@ aeff = aeff * ( aaa )
EOF EOF
cat > expect <<\EOF cat > expect <<\EOF
<WHITE>diff --git a/pre b/post<RESET> <BOLD>diff --git a/pre b/post<RESET>
<WHITE>index 330b04f..5ed8eff 100644<RESET> <BOLD>index 330b04f..5ed8eff 100644<RESET>
<WHITE>--- a/pre<RESET> <BOLD>--- a/pre<RESET>
<WHITE>+++ b/post<RESET> <BOLD>+++ b/post<RESET>
<CYAN>@@ -1,3 +1,7 @@<RESET> <CYAN>@@ -1,3 +1,7 @@<RESET>
<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET> <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
...@@ -122,10 +122,10 @@ test_expect_success '--word-diff=plain --no-color' ' ...@@ -122,10 +122,10 @@ test_expect_success '--word-diff=plain --no-color' '
' '
cat > expect <<EOF cat > expect <<EOF
<WHITE>diff --git a/pre b/post<RESET> <BOLD>diff --git a/pre b/post<RESET>
<WHITE>index 330b04f..5ed8eff 100644<RESET> <BOLD>index 330b04f..5ed8eff 100644<RESET>
<WHITE>--- a/pre<RESET> <BOLD>--- a/pre<RESET>
<WHITE>+++ b/post<RESET> <BOLD>+++ b/post<RESET>
<CYAN>@@ -1,3 +1,7 @@<RESET> <CYAN>@@ -1,3 +1,7 @@<RESET>
<RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET> <RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
...@@ -143,10 +143,10 @@ test_expect_success '--word-diff=plain --color' ' ...@@ -143,10 +143,10 @@ test_expect_success '--word-diff=plain --color' '
' '
cat > expect <<\EOF cat > expect <<\EOF
<WHITE>diff --git a/pre b/post<RESET> <BOLD>diff --git a/pre b/post<RESET>
<WHITE>index 330b04f..5ed8eff 100644<RESET> <BOLD>index 330b04f..5ed8eff 100644<RESET>
<WHITE>--- a/pre<RESET> <BOLD>--- a/pre<RESET>
<WHITE>+++ b/post<RESET> <BOLD>+++ b/post<RESET>
<CYAN>@@ -1 +1 @@<RESET> <CYAN>@@ -1 +1 @@<RESET>
<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET> <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
<CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET> <CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
...@@ -163,10 +163,10 @@ test_expect_success 'word diff without context' ' ...@@ -163,10 +163,10 @@ test_expect_success 'word diff without context' '
' '
cat > expect <<\EOF cat > expect <<\EOF
<WHITE>diff --git a/pre b/post<RESET> <BOLD>diff --git a/pre b/post<RESET>
<WHITE>index 330b04f..5ed8eff 100644<RESET> <BOLD>index 330b04f..5ed8eff 100644<RESET>
<WHITE>--- a/pre<RESET> <BOLD>--- a/pre<RESET>
<WHITE>+++ b/post<RESET> <BOLD>+++ b/post<RESET>
<CYAN>@@ -1,3 +1,7 @@<RESET> <CYAN>@@ -1,3 +1,7 @@<RESET>
h(4),<GREEN>hh<RESET>[44] h(4),<GREEN>hh<RESET>[44]
...@@ -199,10 +199,10 @@ test_expect_success 'option overrides .gitattributes' ' ...@@ -199,10 +199,10 @@ test_expect_success 'option overrides .gitattributes' '
' '
cat > expect <<\EOF cat > expect <<\EOF
<WHITE>diff --git a/pre b/post<RESET> <BOLD>diff --git a/pre b/post<RESET>
<WHITE>index 330b04f..5ed8eff 100644<RESET> <BOLD>index 330b04f..5ed8eff 100644<RESET>
<WHITE>--- a/pre<RESET> <BOLD>--- a/pre<RESET>
<WHITE>+++ b/post<RESET> <BOLD>+++ b/post<RESET>
<CYAN>@@ -1,3 +1,7 @@<RESET> <CYAN>@@ -1,3 +1,7 @@<RESET>
h(4)<GREEN>,hh[44]<RESET> h(4)<GREEN>,hh[44]<RESET>
...@@ -231,10 +231,10 @@ test_expect_success 'command-line overrides config' ' ...@@ -231,10 +231,10 @@ test_expect_success 'command-line overrides config' '
' '
cat > expect <<\EOF cat > expect <<\EOF
<WHITE>diff --git a/pre b/post<RESET> <BOLD>diff --git a/pre b/post<RESET>
<WHITE>index 330b04f..5ed8eff 100644<RESET> <BOLD>index 330b04f..5ed8eff 100644<RESET>
<WHITE>--- a/pre<RESET> <BOLD>--- a/pre<RESET>
<WHITE>+++ b/post<RESET> <BOLD>+++ b/post<RESET>
<CYAN>@@ -1,3 +1,7 @@<RESET> <CYAN>@@ -1,3 +1,7 @@<RESET>
h(4),<GREEN>{+hh+}<RESET>[44] h(4),<GREEN>{+hh+}<RESET>[44]
...@@ -260,10 +260,10 @@ test_expect_success 'remove diff driver regex' ' ...@@ -260,10 +260,10 @@ test_expect_success 'remove diff driver regex' '
' '
cat > expect <<\EOF cat > expect <<\EOF
<WHITE>diff --git a/pre b/post<RESET> <BOLD>diff --git a/pre b/post<RESET>
<WHITE>index 330b04f..5ed8eff 100644<RESET> <BOLD>index 330b04f..5ed8eff 100644<RESET>
<WHITE>--- a/pre<RESET> <BOLD>--- a/pre<RESET>
<WHITE>+++ b/post<RESET> <BOLD>+++ b/post<RESET>
<CYAN>@@ -1,3 +1,7 @@<RESET> <CYAN>@@ -1,3 +1,7 @@<RESET>
h(4),<GREEN>hh[44<RESET>] h(4),<GREEN>hh[44<RESET>]
...@@ -282,10 +282,10 @@ echo 'aaa (aaa)' > pre ...@@ -282,10 +282,10 @@ echo 'aaa (aaa)' > pre
echo 'aaa (aaa) aaa' > post echo 'aaa (aaa) aaa' > post
cat > expect <<\EOF cat > expect <<\EOF
<WHITE>diff --git a/pre b/post<RESET> <BOLD>diff --git a/pre b/post<RESET>
<WHITE>index c29453b..be22f37 100644<RESET> <BOLD>index c29453b..be22f37 100644<RESET>
<WHITE>--- a/pre<RESET> <BOLD>--- a/pre<RESET>
<WHITE>+++ b/post<RESET> <BOLD>+++ b/post<RESET>
<CYAN>@@ -1 +1 @@<RESET> <CYAN>@@ -1 +1 @@<RESET>
aaa (aaa) <GREEN>aaa<RESET> aaa (aaa) <GREEN>aaa<RESET>
EOF EOF
...@@ -301,10 +301,10 @@ echo '(:' > pre ...@@ -301,10 +301,10 @@ echo '(:' > pre
echo '(' > post echo '(' > post
cat > expect <<\EOF cat > expect <<\EOF
<WHITE>diff --git a/pre b/post<RESET> <BOLD>diff --git a/pre b/post<RESET>
<WHITE>index 289cb9d..2d06f37 100644<RESET> <BOLD>index 289cb9d..2d06f37 100644<RESET>
<WHITE>--- a/pre<RESET> <BOLD>--- a/pre<RESET>
<WHITE>+++ b/post<RESET> <BOLD>+++ b/post<RESET>
<CYAN>@@ -1 +1 @@<RESET> <CYAN>@@ -1 +1 @@<RESET>
(<RED>:<RESET> (<RED>:<RESET>
EOF EOF
......
...@@ -238,14 +238,47 @@ test_set_editor () { ...@@ -238,14 +238,47 @@ test_set_editor () {
} }
test_decode_color () { test_decode_color () {
sed -e 's/.\[1m/<WHITE>/g' \ awk '
-e 's/.\[31m/<RED>/g' \ function name(n) {
-e 's/.\[32m/<GREEN>/g' \ if (n == 0) return "RESET";
-e 's/.\[33m/<YELLOW>/g' \ if (n == 1) return "BOLD";
-e 's/.\[34m/<BLUE>/g' \ if (n == 30) return "BLACK";
-e 's/.\[35m/<MAGENTA>/g' \ if (n == 31) return "RED";
-e 's/.\[36m/<CYAN>/g' \ if (n == 32) return "GREEN";
-e 's/.\[m/<RESET>/g' 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 () { nul_to_q () {
......
...@@ -174,8 +174,11 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule, ...@@ -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 */ /* Check indentation */
for (i = 0; i < len; i++) { for (i = 0; i < trailing_whitespace; i++) {
if (line[i] == ' ') if (line[i] == ' ')
continue; continue;
if (line[i] != '\t') if (line[i] != '\t')
...@@ -218,8 +221,6 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule, ...@@ -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". * Now the rest of the line starts at "written".
* The non-highlighted part ends at "trailing_whitespace". * The non-highlighted part ends at "trailing_whitespace".
*/ */
if (trailing_whitespace == -1)
trailing_whitespace = len;
/* Emit non-highlighted (middle) segment. */ /* Emit non-highlighted (middle) segment. */
if (trailing_whitespace - written > 0) { if (trailing_whitespace - written > 0) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册