diff --color-moved: rewind when discarding pmb

Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Dec 9, 2021 at 10:29 UTC 0990658bf85a0763ffd628b1dd57a33c27b25450
2 files changed +69 -5
diff.c
+23 -5
@@ -1205,7 +1205,15 @@ static void mark_color_as_moved(struct diff_options *o,
1205 if (!match) {
1206 int i;
1207
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 + }
1217 for(i = 0; i < pmb_nr; i++)
1218 moved_block_clear(&pmb[i]);
1219 pmb_nr = 0;
@@ -1230,10 +1238,20 @@ static void mark_color_as_moved(struct diff_options *o,
1238 pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
1239
1240 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)
1255 flipped_block = (flipped_block + 1) % 2;
1256 else
1257 flipped_block = 0;
t/t4015-diff-whitespace.sh
+46
@@ -1833,6 +1833,52 @@ test_expect_success '--color-moved treats adjacent blocks as separate for MIN_AL
1833 test_cmp expected actual
1834 '
1835
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 +
1882 test_expect_success 'move detection with submodules' '
1883 test_create_repo bananas &&
1884 echo ripe >bananas/recipe &&