Skip to content

Commit fecc6f3

Browse files
phillipwoodgitster
authored andcommitted
add -p: adjust offsets of subsequent hunks when one is skipped
Since commit 8cbd431 ("git-add--interactive: replace hunk recounting with apply --recount", 2008-7-2) if a hunk is skipped then we rely on the context lines to apply subsequent hunks in the right place. While this works most of the time it is possible for hunks to end up being applied in the wrong place. To fix this adjust the offset of subsequent hunks to correct for any change in the number of insertions or deletions due to the skipped hunk. The change in offset due to edited hunks that have the number of insertions or deletions changed is ignored here, it will be fixed in the next commit. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 23fea4c commit fecc6f3

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

git-add--interactive.perl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,14 +926,25 @@ sub coalesce_overlapping_hunks {
926926
my @out = ();
927927

928928
my ($last_o_ctx, $last_was_dirty);
929+
my $ofs_delta = 0;
929930

930-
for (grep { $_->{USE} } @in) {
931+
for (@in) {
931932
if ($_->{TYPE} ne 'hunk') {
932933
push @out, $_;
933934
next;
934935
}
935936
my $text = $_->{TEXT};
936-
my ($o_ofs) = parse_hunk_header($text->[0]);
937+
my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
938+
parse_hunk_header($text->[0]);
939+
unless ($_->{USE}) {
940+
$ofs_delta += $o_cnt - $n_cnt;
941+
next;
942+
}
943+
if ($ofs_delta) {
944+
$n_ofs += $ofs_delta;
945+
$_->{TEXT}->[0] = format_hunk_header($o_ofs, $o_cnt,
946+
$n_ofs, $n_cnt);
947+
}
937948
if (defined $last_o_ctx &&
938949
$o_ofs <= $last_o_ctx &&
939950
!$_->{DIRTY} &&

t/t3701-add-interactive.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ test_expect_success 'set up pathological context' '
496496
test_write_lines +b " a" >patch
497497
'
498498

499-
test_expect_failure 'add -p works with pathological context lines' '
499+
test_expect_success 'add -p works with pathological context lines' '
500500
git reset &&
501501
printf "%s\n" n y |
502502
git add -p &&

0 commit comments

Comments
 (0)