Skip to content

Commit 0990658

Browse files
phillipwoodgitster
authored andcommitted
diff --color-moved: rewind when discarding pmb
Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7dfe427 commit 0990658

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

diff.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,15 @@ static void mark_color_as_moved(struct diff_options *o,
12051205
if (!match) {
12061206
int i;
12071207

1208-
adjust_last_block(o, n, block_length);
1208+
if (!adjust_last_block(o, n, block_length) &&
1209+
block_length > 1) {
1210+
/*
1211+
* Rewind in case there is another match
1212+
* starting at the second line of the block
1213+
*/
1214+
match = NULL;
1215+
n -= block_length;
1216+
}
12091217
for(i = 0; i < pmb_nr; i++)
12101218
moved_block_clear(&pmb[i]);
12111219
pmb_nr = 0;
@@ -1230,10 +1238,20 @@ static void mark_color_as_moved(struct diff_options *o,
12301238
pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
12311239

12321240
if (pmb_nr == 0) {
1233-
fill_potential_moved_blocks(
1234-
o, hm, match, l, &pmb, &pmb_alloc, &pmb_nr);
1235-
if (adjust_last_block(o, n, block_length) &&
1236-
pmb_nr && last_symbol != l->s)
1241+
int contiguous = adjust_last_block(o, n, block_length);
1242+
1243+
if (!contiguous && block_length > 1)
1244+
/*
1245+
* Rewind in case there is another match
1246+
* starting at the second line of the block
1247+
*/
1248+
n -= block_length;
1249+
else
1250+
fill_potential_moved_blocks(o, hm, match, l,
1251+
&pmb, &pmb_alloc,
1252+
&pmb_nr);
1253+
1254+
if (contiguous && pmb_nr && last_symbol != l->s)
12371255
flipped_block = (flipped_block + 1) % 2;
12381256
else
12391257
flipped_block = 0;

t/t4015-diff-whitespace.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,52 @@ test_expect_success '--color-moved treats adjacent blocks as separate for MIN_AL
18331833
test_cmp expected actual
18341834
'
18351835

1836+
test_expect_success '--color-moved rewinds for MIN_ALNUM_COUNT' '
1837+
git reset --hard &&
1838+
test_write_lines >file \
1839+
A B C one two three four five six seven D E F G H I J &&
1840+
git add file &&
1841+
test_write_lines >file \
1842+
one two A B C D E F G H I J two three four five six seven &&
1843+
git diff --color-moved=zebra -- file &&
1844+
1845+
git diff --color-moved=zebra --color -- file >actual.raw &&
1846+
grep -v "index" actual.raw | test_decode_color >actual &&
1847+
cat >expected <<-\EOF &&
1848+
<BOLD>diff --git a/file b/file<RESET>
1849+
<BOLD>--- a/file<RESET>
1850+
<BOLD>+++ b/file<RESET>
1851+
<CYAN>@@ -1,13 +1,8 @@<RESET>
1852+
<GREEN>+<RESET><GREEN>one<RESET>
1853+
<GREEN>+<RESET><GREEN>two<RESET>
1854+
A<RESET>
1855+
B<RESET>
1856+
C<RESET>
1857+
<RED>-one<RESET>
1858+
<BOLD;MAGENTA>-two<RESET>
1859+
<BOLD;MAGENTA>-three<RESET>
1860+
<BOLD;MAGENTA>-four<RESET>
1861+
<BOLD;MAGENTA>-five<RESET>
1862+
<BOLD;MAGENTA>-six<RESET>
1863+
<BOLD;MAGENTA>-seven<RESET>
1864+
D<RESET>
1865+
E<RESET>
1866+
F<RESET>
1867+
<CYAN>@@ -15,3 +10,9 @@<RESET> <RESET>G<RESET>
1868+
H<RESET>
1869+
I<RESET>
1870+
J<RESET>
1871+
<BOLD;CYAN>+<RESET><BOLD;CYAN>two<RESET>
1872+
<BOLD;CYAN>+<RESET><BOLD;CYAN>three<RESET>
1873+
<BOLD;CYAN>+<RESET><BOLD;CYAN>four<RESET>
1874+
<BOLD;CYAN>+<RESET><BOLD;CYAN>five<RESET>
1875+
<BOLD;CYAN>+<RESET><BOLD;CYAN>six<RESET>
1876+
<BOLD;CYAN>+<RESET><BOLD;CYAN>seven<RESET>
1877+
EOF
1878+
1879+
test_cmp expected actual
1880+
'
1881+
18361882
test_expect_success 'move detection with submodules' '
18371883
test_create_repo bananas &&
18381884
echo ripe >bananas/recipe &&

0 commit comments

Comments
 (0)