Skip to content

Commit 933e44d

Browse files
committed
"add -p": work-around an old laziness that does not coalesce hunks
Since 0beee4c (git-add--interactive: remove hunk coalescing, 2008-07-02), "git add--interactive" behaves lazily and passes overlapping hunks to the underlying "git apply" without coalescing. This was partially corrected by 7a26e65 (its partial revert, 2009-05-16), but overlapping hunks are still passed when the patch is edited. Teach "git apply" the --allow-overlap option that disables a safety feature that avoids misapplication of patches by not applying patches to overlapping hunks, and pass this option form "add -p" codepath. Do not even advertise the option, as this is merely a workaround, and the correct fix is to make "add -p" correctly coalesce adjacent patch hunks. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9dce832 commit 933e44d

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

builtin/apply.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static int apply = 1;
4343
static int apply_in_reverse;
4444
static int apply_with_reject;
4545
static int apply_verbosely;
46+
static int allow_overlap;
4647
static int no_add;
4748
static const char *fake_ancestor;
4849
static int line_termination = '\n';
@@ -2430,9 +2431,9 @@ static void update_image(struct image *img,
24302431
memcpy(img->line + applied_pos,
24312432
postimage->line,
24322433
postimage->nr * sizeof(*img->line));
2433-
for (i = 0; i < postimage->nr; i++)
2434-
img->line[applied_pos + i].flag |= LINE_PATCHED;
2435-
2434+
if (!allow_overlap)
2435+
for (i = 0; i < postimage->nr; i++)
2436+
img->line[applied_pos + i].flag |= LINE_PATCHED;
24362437
img->nr = nr;
24372438
}
24382439

@@ -3877,6 +3878,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
38773878
"don't expect at least one line of context"),
38783879
OPT_BOOLEAN(0, "reject", &apply_with_reject,
38793880
"leave the rejected hunks in corresponding *.rej files"),
3881+
OPT_BOOLEAN(0, "allow-overlap", &allow_overlap,
3882+
"allow overlapping hunks"),
38803883
OPT__VERBOSE(&apply_verbosely, "be verbose"),
38813884
OPT_BIT(0, "inaccurate-eof", &options,
38823885
"tolerate incorrectly detected missing new-line at the end of file",

git-add--interactive.perl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ sub add_untracked_cmd {
705705
sub run_git_apply {
706706
my $cmd = shift;
707707
my $fh;
708-
open $fh, '| git ' . $cmd . " --recount";
708+
open $fh, '| git ' . $cmd . " --recount --allow-overlap";
709709
print $fh @_;
710710
return close $fh;
711711
}

0 commit comments

Comments
 (0)