Skip to content

Commit cb1a9e2

Browse files
committed
apply --allow-overlap: fix a corner case
Yes, yes, this is supposed to be only a band-aid option for `git add -p` not Doing The Right Thing. But as long as we carry the `--allow-overlap` option, we might just as well get it right. This fixes the case where one hunk inserts a line before the first one, and a hunk whose context overlaps with the first one's appends a line at the end. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 36aeb30 commit cb1a9e2

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

apply.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2676,6 +2676,16 @@ static int find_pos(struct apply_state *state,
26762676
unsigned long backwards, forwards, current;
26772677
int backwards_lno, forwards_lno, current_lno;
26782678

2679+
/*
2680+
* When running with --allow-overlap, it is possible that a hunk is
2681+
* seen that pretends to start at the beginning (but no longer does),
2682+
* and that *still* needs to match the end. So trust `match_end` more
2683+
* than `match_beginning`.
2684+
*/
2685+
if (state->allow_overlap && match_beginning && match_end &&
2686+
img->nr - preimage->nr != 0)
2687+
match_beginning = 0;
2688+
26792689
/*
26802690
* If match_beginning or match_end is specified, there is no
26812691
* point starting from a wrong line that will never match and

0 commit comments

Comments
 (0)