Skip to content

Commit 0915327

Browse files
jonathantanmygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 23b65f9 commit 0915327

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

diff.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,26 @@ static int shrink_potential_moved_blocks(struct moved_entry **pmb,
861861
return rp + 1;
862862
}
863863

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+
864884
/* Find blocks of moved code, delegate actual coloring decision to helper */
865885
static void mark_color_as_moved(struct diff_options *o,
866886
struct hashmap *add_lines,
@@ -896,13 +916,7 @@ static void mark_color_as_moved(struct diff_options *o,
896916
}
897917

898918
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);
906920
pmb_nr = 0;
907921
block_length = 0;
908922
continue;
@@ -944,6 +958,7 @@ static void mark_color_as_moved(struct diff_options *o,
944958
if (flipped_block)
945959
l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
946960
}
961+
adjust_last_block(o, n, block_length);
947962

948963
free(pmb);
949964
}

t/t4015-diff-whitespace.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,43 @@ EOF
13821382
test_cmp expected actual
13831383
'
13841384

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+
13851422
test_expect_success 'move detection with submodules' '
13861423
test_create_repo bananas &&
13871424
echo ripe >bananas/recipe &&

0 commit comments

Comments
 (0)