Skip to content

Commit c69e8b6

Browse files
committed
Merge branch 'jc/maint-add-p-overlapping-hunks' into maint
* jc/maint-add-p-overlapping-hunks: t3701: add-p-fix makes the last test to pass "add -p": work-around an old laziness that does not coalesce hunks add--interactive.perl: factor out repeated --recount option t3701: Editing a split hunk in an "add -p" session add -p: 'q' should really quit
2 parents 43d532e + 0bf9fc0 commit c69e8b6

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
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

@@ -3889,6 +3890,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
38893890
"don't expect at least one line of context"),
38903891
OPT_BOOLEAN(0, "reject", &apply_with_reject,
38913892
"leave the rejected hunks in corresponding *.rej files"),
3893+
OPT_BOOLEAN(0, "allow-overlap", &allow_overlap,
3894+
"allow overlapping hunks"),
38923895
OPT__VERBOSE(&apply_verbosely, "be verbose"),
38933896
OPT_BIT(0, "inaccurate-eof", &options,
38943897
"tolerate incorrectly detected missing new-line at the end of file",

git-add--interactive.perl

Lines changed: 12 additions & 13 deletions
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;
708+
open $fh, '| git ' . $cmd . " --recount --allow-overlap";
709709
print $fh @_;
710710
return close $fh;
711711
}
@@ -1050,7 +1050,7 @@ sub edit_hunk_manually {
10501050

10511051
sub diff_applies {
10521052
my $fh;
1053-
return run_git_apply($patch_mode_flavour{APPLY_CHECK} . ' --recount --check',
1053+
return run_git_apply($patch_mode_flavour{APPLY_CHECK} . ' --check',
10541054
map { @{$_->{TEXT}} } @_);
10551055
}
10561056

@@ -1139,7 +1139,7 @@ sub help_patch_cmd {
11391139

11401140
sub apply_patch {
11411141
my $cmd = shift;
1142-
my $ret = run_git_apply $cmd . ' --recount', @_;
1142+
my $ret = run_git_apply $cmd, @_;
11431143
if (!$ret) {
11441144
print STDERR @_;
11451145
}
@@ -1148,17 +1148,17 @@ sub apply_patch {
11481148

11491149
sub apply_patch_for_checkout_commit {
11501150
my $reverse = shift;
1151-
my $applies_index = run_git_apply 'apply '.$reverse.' --cached --recount --check', @_;
1152-
my $applies_worktree = run_git_apply 'apply '.$reverse.' --recount --check', @_;
1151+
my $applies_index = run_git_apply 'apply '.$reverse.' --cached --check', @_;
1152+
my $applies_worktree = run_git_apply 'apply '.$reverse.' --check', @_;
11531153

11541154
if ($applies_worktree && $applies_index) {
1155-
run_git_apply 'apply '.$reverse.' --cached --recount', @_;
1156-
run_git_apply 'apply '.$reverse.' --recount', @_;
1155+
run_git_apply 'apply '.$reverse.' --cached', @_;
1156+
run_git_apply 'apply '.$reverse, @_;
11571157
return 1;
11581158
} elsif (!$applies_index) {
11591159
print colored $error_color, "The selected hunks do not apply to the index!\n";
11601160
if (prompt_yesno "Apply them to the worktree anyway? ") {
1161-
return run_git_apply 'apply '.$reverse.' --recount', @_;
1161+
return run_git_apply 'apply '.$reverse, @_;
11621162
} else {
11631163
print colored $error_color, "Nothing was applied.\n";
11641164
return 0;
@@ -1366,14 +1366,13 @@ sub patch_update_file {
13661366
next;
13671367
}
13681368
elsif ($line =~ /^q/i) {
1369-
while ($ix < $num) {
1370-
if (!defined $hunk[$ix]{USE}) {
1371-
$hunk[$ix]{USE} = 0;
1369+
for ($i = 0; $i < $num; $i++) {
1370+
if (!defined $hunk[$i]{USE}) {
1371+
$hunk[$i]{USE} = 0;
13721372
}
1373-
$ix++;
13741373
}
13751374
$quit = 1;
1376-
next;
1375+
last;
13771376
}
13781377
elsif ($line =~ m|^/(.*)|) {
13791378
my $regex = $1;

t/t3701-add-interactive.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,40 @@ test_expect_success PERL 'deleting an empty file' '
294294
test_cmp expected diff
295295
'
296296

297+
test_expect_success PERL 'split hunk setup' '
298+
git reset --hard &&
299+
for i in 10 20 30 40 50 60
300+
do
301+
echo $i
302+
done >test &&
303+
git add test &&
304+
test_tick &&
305+
git commit -m test &&
306+
307+
for i in 10 15 20 21 22 23 24 30 40 50 60
308+
do
309+
echo $i
310+
done >test
311+
'
312+
313+
test_expect_success PERL 'split hunk "add -p (edit)"' '
314+
# Split, say Edit and do nothing. Then:
315+
#
316+
# 1. Broken version results in a patch that does not apply and
317+
# only takes [y/n] (edit again) so the first q is discarded
318+
# and then n attempts to discard the edit. Repeat q enough
319+
# times to get out.
320+
#
321+
# 2. Correct version applies the (not)edited version, and asks
322+
# about the next hunk, against wich we say q and program
323+
# exits.
324+
for a in s e q n q q
325+
do
326+
echo $a
327+
done |
328+
EDITOR=: git add -p &&
329+
git diff >actual &&
330+
! grep "^+15" actual
331+
'
332+
297333
test_done

0 commit comments

Comments
 (0)