diff: respect MIN_BLOCK_LENGTH for last block

Currently, MIN_BLOCK_LENGTH is only checked when diff encounters a line that does not belong to the current block. In particular, this means that MIN_BLOCK_LENGTH is not checked after all lines are encountered. Perform that check. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Aug 15, 2017 at 18:27 UTC 09153277f8948d3ed8cb6712f135a9caeaa006f6
2 files changed +59 -7
diff.c
+22 -7
@@ -861,6 +861,26 @@ static int shrink_potential_moved_blocks(struct moved_entry **pmb,
861 return rp + 1;
862 }
863
864 +/*
865 + * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
866 + *
867 + * Otherwise, if the last block has fewer lines than
868 + * COLOR_MOVED_MIN_BLOCK_LENGTH, unset DIFF_SYMBOL_MOVED_LINE on all lines in
869 + * that block.
870 + *
871 + * The last block consists of the (n - block_length)'th line up to but not
872 + * including the nth line.
873 + */
874 +static void adjust_last_block(struct diff_options *o, int n, int block_length)
875 +{
876 + int i;
877 + if (block_length >= COLOR_MOVED_MIN_BLOCK_LENGTH ||
878 + o->color_moved == COLOR_MOVED_PLAIN)
879 + return;
880 + for (i = 1; i < block_length + 1; i++)
881 + o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE;
882 +}
883 +
884 /* Find blocks of moved code, delegate actual coloring decision to helper */
885 static void mark_color_as_moved(struct diff_options *o,
886 struct hashmap *add_lines,
@@ -896,13 +916,7 @@ static void mark_color_as_moved(struct diff_options *o,
916 }
917
918 if (!match) {
899 - if (block_length < COLOR_MOVED_MIN_BLOCK_LENGTH &&
900 - o->color_moved != COLOR_MOVED_PLAIN) {
901 - for (i = 1; i < block_length + 1; i++) {
902 - l = &o->emitted_symbols->buf[n - i];
903 - l->flags &= ~DIFF_SYMBOL_MOVED_LINE;
904 - }
905 - }
919 + adjust_last_block(o, n, block_length);
920 pmb_nr = 0;
921 block_length = 0;
922 continue;
@@ -944,6 +958,7 @@ static void mark_color_as_moved(struct diff_options *o,
958 if (flipped_block)
959 l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
960 }
961 + adjust_last_block(o, n, block_length);
962
963 free(pmb);
964 }
t/t4015-diff-whitespace.sh
+37
@@ -1382,6 +1382,43 @@ EOF
1382 test_cmp expected actual
1383 '
1384
1385 +test_expect_success '--color-moved block at end of diff output respects MIN_BLOCK_LENGTH' '
1386 + git reset --hard &&
1387 + >bar &&
1388 + cat <<-\EOF >foo &&
1389 + irrelevant_line
1390 + line1
1391 + EOF
1392 + git add foo bar &&
1393 + git commit -m x &&
1394 +
1395 + cat <<-\EOF >bar &&
1396 + line1
1397 + EOF
1398 + cat <<-\EOF >foo &&
1399 + irrelevant_line
1400 + EOF
1401 +
1402 + git diff HEAD --color-moved=zebra --no-renames |
1403 + grep -v "index" |
1404 + test_decode_color >actual &&
1405 + cat >expected <<-\EOF &&
1406 + <BOLD>diff --git a/bar b/bar<RESET>
1407 + <BOLD>--- a/bar<RESET>
1408 + <BOLD>+++ b/bar<RESET>
1409 + <CYAN>@@ -0,0 +1 @@<RESET>
1410 + <GREEN>+<RESET><GREEN>line1<RESET>
1411 + <BOLD>diff --git a/foo b/foo<RESET>
1412 + <BOLD>--- a/foo<RESET>
1413 + <BOLD>+++ b/foo<RESET>
1414 + <CYAN>@@ -1,2 +1 @@<RESET>
1415 + irrelevant_line<RESET>
1416 + <RED>-line1<RESET>
1417 + EOF
1418 +
1419 + test_cmp expected actual
1420 +'
1421 +
1422 test_expect_success 'move detection with submodules' '
1423 test_create_repo bananas &&
1424 echo ripe >bananas/recipe &&