Skip to content

Commit 2bd69b9

Browse files
phillipwoodgitster
authored andcommitted
add -p: fix checkout -p with pathological context
Commit fecc6f3 ("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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f4d35a6 commit 2bd69b9

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

git-add--interactive.perl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,11 @@ sub coalesce_overlapping_hunks {
957957
next;
958958
}
959959
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+
}
961965
$_->{TEXT}->[0] = format_hunk_header($o_ofs, $o_cnt,
962966
$n_ofs, $n_cnt);
963967
}

t/t3701-add-interactive.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,4 +571,12 @@ test_expect_success 'add -p patch editing works with pathological context lines'
571571
test_cmp expected-2 actual
572572
'
573573

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+
'
574582
test_done

0 commit comments

Comments
 (0)