]> git.ipfire.org Git - thirdparty/git.git/commitdiff
add -p: fix checkout -p with pathological context
authorPhillip Wood <phillip.wood@dunelm.org.uk>
Wed, 12 Jun 2019 09:25:27 +0000 (02:25 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 13 Jun 2019 17:00:30 +0000 (10:00 -0700)
Commit fecc6f3a68 ("add -p: adjust offsets of subsequent hunks when one is
skipped", 2018-03-01) fixed adding hunks in the correct place when a
previous hunk has been skipped. However it did not address patches that
are applied in reverse. In that case we need to adjust the pre-image
offset so that when apply reverses the patch the post-image offset is
adjusted correctly. We subtract rather than add the delta as the patch
is reversed (the easiest way to think about it is to consider a hunk of
deletions that is skipped - in that case we want to reduce offset so we
need to subtract).

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-add--interactive.perl
t/t3701-add-interactive.sh

index 8361ef45e722f84140995e48f26ff2fa3c1b520b..85d0dc11960c08f3af30015d99228429d9dd7e64 100755 (executable)
@@ -957,7 +957,11 @@ sub coalesce_overlapping_hunks {
                        next;
                }
                if ($ofs_delta) {
-                       $n_ofs += $ofs_delta;
+                       if ($patch_mode_flavour{IS_REVERSE}) {
+                               $o_ofs -= $ofs_delta;
+                       } else {
+                               $n_ofs += $ofs_delta;
+                       }
                        $_->{TEXT}->[0] = format_hunk_header($o_ofs, $o_cnt,
                                                             $n_ofs, $n_cnt);
                }
index f1bb879ea43321a1ebc3b933a2a190a8e23eed86..2c54f84a8dda705aaa91f359734894117b893c03 100755 (executable)
@@ -571,4 +571,12 @@ test_expect_success 'add -p patch editing works with pathological context lines'
        test_cmp expected-2 actual
 '
 
+test_expect_success 'checkout -p works with pathological context lines' '
+       test_write_lines a a a a a a >a &&
+       git add a &&
+       test_write_lines a b a b a b a b a b a > a&&
+       test_write_lines s n n y q | git checkout -p &&
+       test_write_lines a b a b a a b a b a >expect &&
+       test_cmp expect a
+'
 test_done