diff: define block by number of alphanumeric chars
The existing behavior of diff --color-moved=zebra does not define the
minimum size of a block at all, instead relying on a heuristic applied
later to filter out sets of adjacent moved lines that are shorter than 3
lines long. This can be confusing, because a block could thus be colored
as moved at the source but not at the destination (or vice versa),
depending on its neighbors.
Instead, teach diff that the minimum size of a block is 20 alphanumeric
characters, the same heuristic used by "git blame". This allows diff to
still exclude uninteresting lines appearing on their own (such as those
solely consisting of one or a few closing braces), as was the intention
of the adjacent-moved-line heuristic.
This requires a change in some tests in that some of their lines are no
longer considered to be part of a block, because they are too short.
Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git diff HEAD --no-renames --color-moved| test_decode_color >actual &&
git diff HEAD --no-renames --color-moved |
grep -v "index" |
test_decode_color >actual &&
cat <<-\EOF >expected &&
<BOLD>diff --git a/lines.txt b/lines.txt<RESET>
<BOLD>index 47ea9c3..ba96a38 100644<RESET>
<BOLD>--- a/lines.txt<RESET>
<BOLD>+++ b/lines.txt<RESET>
<CYAN>@@ -1,16 +1,16 @@<RESET>
<MAGENTA>-line 1<RESET>
<MAGENTA>-line 2<RESET>
<MAGENTA>-line 3<RESET>
<MAGENTA>-long line 1<RESET>
<MAGENTA>-long line 2<RESET>
<MAGENTA>-long line 3<RESET>
line 4<RESET>
line 5<RESET>
line 6<RESET>
line 7<RESET>
line 8<RESET>
line 9<RESET>
<CYAN>+<RESET><CYAN>line 1<RESET>
<CYAN>+<RESET><CYAN>line 2<RESET>
<CYAN>+<RESET><CYAN>line 3<RESET>
<YELLOW>+<RESET><YELLOW>line 14<RESET>
<YELLOW>+<RESET><YELLOW>line 15<RESET>
<YELLOW>+<RESET><YELLOW>line 16<RESET>
<CYAN>+<RESET><CYAN>long line 1<RESET>
<CYAN>+<RESET><CYAN>long line 2<RESET>
<CYAN>+<RESET><CYAN>long line 3<RESET>
<YELLOW>+<RESET><YELLOW>long line 14<RESET>
<YELLOW>+<RESET><YELLOW>long line 15<RESET>
<YELLOW>+<RESET><YELLOW>long line 16<RESET>
line 10<RESET>
line 11<RESET>
line 12<RESET>
line 13<RESET>
<MAGENTA>-line 14<RESET>
<MAGENTA>-line 15<RESET>
<MAGENTA>-line 16<RESET>
<MAGENTA>-long line 14<RESET>
<MAGENTA>-long line 15<RESET>
<MAGENTA>-long line 16<RESET>
EOF
test_cmp expected actual
'
@ -1324,16 +1326,16 @@ line 1
@@ -1324,16 +1326,16 @@ line 1
line 2
line 3
line 4
line 5
line 6
line 7
long line 5
long line 6
long line 7
EOF
git add lines.txt &&
git commit -m "add poetry" &&
cat <<\EOF >lines.txt &&
line 5
line 6
line 7
long line 5
long line 6
long line 7
line 1
line 2
line 3
@ -1341,48 +1343,50 @@ line 4
@@ -1341,48 +1343,50 @@ line 4
EOF
test_config color.diff.oldMoved "magenta" &&
test_config color.diff.newMoved "cyan" &&
git diff HEAD --no-renames --color-moved| test_decode_color >actual &&
git diff HEAD --no-renames --color-moved |
grep -v "index" |
test_decode_color >actual &&
cat <<-\EOF >expected &&
<BOLD>diff --git a/lines.txt b/lines.txt<RESET>
<BOLD>index 734156d..eb89ead 100644<RESET>
<BOLD>--- a/lines.txt<RESET>
<BOLD>+++ b/lines.txt<RESET>
<CYAN>@@ -1,7 +1,7 @@<RESET>
<GREEN>+<RESET> <GREEN>line 5<RESET>
<GREEN>+<RESET> <GREEN>line 6<RESET>
<GREEN>+<RESET> <GREEN>line 7<RESET>
<GREEN>+<RESET> <GREEN>long line 5<RESET>
<GREEN>+<RESET> <GREEN>long line 6<RESET>
<GREEN>+<RESET> <GREEN>long line 7<RESET>
line 1<RESET>
line 2<RESET>
line 3<RESET>
line 4<RESET>
<RED>-line 5<RESET>
<RED>-line 6<RESET>
<RED>-line 7<RESET>
<RED>-long line 5<RESET>
<RED>-long line 6<RESET>
<RED>-long line 7<RESET>
EOF
test_cmp expected actual &&
git diff HEAD --no-renames -w --color-moved| test_decode_color >actual &&
git diff HEAD --no-renames -w --color-moved |
grep -v "index" |
test_decode_color >actual &&
cat <<-\EOF >expected &&
<BOLD>diff --git a/lines.txt b/lines.txt<RESET>
<BOLD>index 734156d..eb89ead 100644<RESET>
<BOLD>--- a/lines.txt<RESET>
<BOLD>+++ b/lines.txt<RESET>
<CYAN>@@ -1,7 +1,7 @@<RESET>
<CYAN>+<RESET> <CYAN>line 5<RESET>
<CYAN>+<RESET> <CYAN>line 6<RESET>
<CYAN>+<RESET> <CYAN>line 7<RESET>
<CYAN>+<RESET> <CYAN>long line 5<RESET>
<CYAN>+<RESET> <CYAN>long line 6<RESET>
<CYAN>+<RESET> <CYAN>long line 7<RESET>
line 1<RESET>
line 2<RESET>
line 3<RESET>
line 4<RESET>
<MAGENTA>-line 5<RESET>
<MAGENTA>-line 6<RESET>
<MAGENTA>-line 7<RESET>
<MAGENTA>-long line 5<RESET>
<MAGENTA>-long line 6<RESET>
<MAGENTA>-long line 7<RESET>
EOF
test_cmp expected actual
'
test_expect_success '--color-moved block at end of diff output respects MIN_BLOCK_LENGTH' '
test_expect_success '--color-moved block at end of diff output respects MIN_ALNUM_COUNT' '
git reset --hard &&
>bar &&
cat <<-\EOF >foo &&
@ -1419,6 +1423,90 @@ test_expect_success '--color-moved block at end of diff output respects MIN_BLOC
@@ -1419,6 +1423,90 @@ test_expect_success '--color-moved block at end of diff output respects MIN_BLOC