Skip to content

Commit 04b22e5

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 line, and is followed by a hunk whose context overlaps with the first one's and which appends a line at the end. Note that this affects only the beginning of the file: when a hunk is plit into two, the first can change the context so that the second hunk thinks it should match the beginning of the file, but it no longer does because the first hunk was already applied. The same is not true for the end of the file, as the hunks are applied in order (by line numbers): the hunk that changes the end of the file is by definition the last hunk to be applied. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 27c8cee commit 04b22e5

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
@@ -2661,6 +2661,16 @@ static int find_pos(struct apply_state *state,
26612661
unsigned long backwards, forwards, current;
26622662
int backwards_lno, forwards_lno, current_lno;
26632663

2664+
/*
2665+
* When running with --allow-overlap, it is possible that a hunk is
2666+
* seen that pretends to start at the beginning (but no longer does),
2667+
* and that *still* needs to match the end. So trust `match_end` more
2668+
* than `match_beginning`.
2669+
*/
2670+
if (state->allow_overlap && match_beginning && match_end &&
2671+
img->nr - preimage->nr != 0)
2672+
match_beginning = 0;
2673+
26642674
/*
26652675
* If match_beginning or match_end is specified, there is no
26662676
* point starting from a wrong line that will never match and

0 commit comments

Comments
 (0)