diff --git a/git-add--interactive.perl b/git-add--interactive.perl index 8361ef45e7..85d0dc1196 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -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); } diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh index f1bb879ea4..2c54f84a8d 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -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