]> git.ipfire.org Git - thirdparty/git.git/commitdiff
diff --color-moved: rewind when discarding pmb
authorPhillip Wood <phillip.wood@dunelm.org.uk>
Thu, 9 Dec 2021 10:29:58 +0000 (10:29 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 9 Dec 2021 21:24:05 +0000 (13:24 -0800)
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff.c
t/t4015-diff-whitespace.sh

diff --git a/diff.c b/diff.c
index 09af94e018c8f2e7f7128639475fa373eea405d0..1e1b5127d15601913c9a62d2cf7267981affc368 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -1205,7 +1205,15 @@ static void mark_color_as_moved(struct diff_options *o,
                if (!match) {
                        int i;
 
-                       adjust_last_block(o, n, block_length);
+                       if (!adjust_last_block(o, n, block_length) &&
+                           block_length > 1) {
+                               /*
+                                * Rewind in case there is another match
+                                * starting at the second line of the block
+                                */
+                               match = NULL;
+                               n -= block_length;
+                       }
                        for(i = 0; i < pmb_nr; i++)
                                moved_block_clear(&pmb[i]);
                        pmb_nr = 0;
@@ -1230,10 +1238,20 @@ static void mark_color_as_moved(struct diff_options *o,
                pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
 
                if (pmb_nr == 0) {
-                       fill_potential_moved_blocks(
-                               o, hm, match, l, &pmb, &pmb_alloc, &pmb_nr);
-                       if (adjust_last_block(o, n, block_length) &&
-                           pmb_nr && last_symbol != l->s)
+                       int contiguous = adjust_last_block(o, n, block_length);
+
+                       if (!contiguous && block_length > 1)
+                               /*
+                                * Rewind in case there is another match
+                                * starting at the second line of the block
+                                */
+                               n -= block_length;
+                       else
+                               fill_potential_moved_blocks(o, hm, match, l,
+                                                           &pmb, &pmb_alloc,
+                                                           &pmb_nr);
+
+                       if (contiguous && pmb_nr && last_symbol != l->s)
                                flipped_block = (flipped_block + 1) % 2;
                        else
                                flipped_block = 0;
index 2c13b62d3c654807b85307748c1ec52b6f4155ff..308dc1365966af0514d598a9ff0a46a2f8609ee9 100755 (executable)
@@ -1833,6 +1833,52 @@ test_expect_success '--color-moved treats adjacent blocks as separate for MIN_AL
        test_cmp expected actual
 '
 
+test_expect_success '--color-moved rewinds for MIN_ALNUM_COUNT' '
+       git reset --hard &&
+       test_write_lines >file \
+               A B C one two three four five six seven D E F G H I J &&
+       git add file &&
+       test_write_lines >file \
+               one two A B C D E F G H I J two three four five six seven &&
+       git diff --color-moved=zebra -- file &&
+
+       git diff --color-moved=zebra --color -- file >actual.raw &&
+       grep -v "index" actual.raw | test_decode_color >actual &&
+       cat >expected <<-\EOF &&
+       <BOLD>diff --git a/file b/file<RESET>
+       <BOLD>--- a/file<RESET>
+       <BOLD>+++ b/file<RESET>
+       <CYAN>@@ -1,13 +1,8 @@<RESET>
+       <GREEN>+<RESET><GREEN>one<RESET>
+       <GREEN>+<RESET><GREEN>two<RESET>
+        A<RESET>
+        B<RESET>
+        C<RESET>
+       <RED>-one<RESET>
+       <BOLD;MAGENTA>-two<RESET>
+       <BOLD;MAGENTA>-three<RESET>
+       <BOLD;MAGENTA>-four<RESET>
+       <BOLD;MAGENTA>-five<RESET>
+       <BOLD;MAGENTA>-six<RESET>
+       <BOLD;MAGENTA>-seven<RESET>
+        D<RESET>
+        E<RESET>
+        F<RESET>
+       <CYAN>@@ -15,3 +10,9 @@<RESET> <RESET>G<RESET>
+        H<RESET>
+        I<RESET>
+        J<RESET>
+       <BOLD;CYAN>+<RESET><BOLD;CYAN>two<RESET>
+       <BOLD;CYAN>+<RESET><BOLD;CYAN>three<RESET>
+       <BOLD;CYAN>+<RESET><BOLD;CYAN>four<RESET>
+       <BOLD;CYAN>+<RESET><BOLD;CYAN>five<RESET>
+       <BOLD;CYAN>+<RESET><BOLD;CYAN>six<RESET>
+       <BOLD;CYAN>+<RESET><BOLD;CYAN>seven<RESET>
+       EOF
+
+       test_cmp expected actual
+'
+
 test_expect_success 'move detection with submodules' '
        test_create_repo bananas &&
        echo ripe >bananas/recipe &&