add -p: fix checkout -p with pathological context

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>

Phillip Wood committed Jun 12, 2019 at 02:25 UTC 2bd69b9024c8c7c1b77060e3ed996c74b4775b01
2 files changed +13 -1
git-add--interactive.perl
+5 -1
@@ -957,7 +957,11 @@ sub coalesce_overlapping_hunks {
957 next;
958 }
959 if ($ofs_delta) {
960 - $n_ofs += $ofs_delta;
960 + if ($patch_mode_flavour{IS_REVERSE}) {
961 + $o_ofs -= $ofs_delta;
962 + } else {
963 + $n_ofs += $ofs_delta;
964 + }
965 $_->{TEXT}->[0] = format_hunk_header($o_ofs, $o_cnt,
966 $n_ofs, $n_cnt);
967 }
t/t3701-add-interactive.sh
+8
@@ -571,4 +571,12 @@ test_expect_success 'add -p patch editing works with pathological context lines'
571 test_cmp expected-2 actual
572 '
573
574 +test_expect_success 'checkout -p works with pathological context lines' '
575 + test_write_lines a a a a a a >a &&
576 + git add a &&
577 + test_write_lines a b a b a b a b a b a > a&&
578 + test_write_lines s n n y q | git checkout -p &&
579 + test_write_lines a b a b a a b a b a >expect &&
580 + test_cmp expect a
581 +'
582 test_done