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
This commit is contained in:
Junio C Hamano 2011-05-16 16:36:46 -07:00
commit c69e8b6935
3 changed files with 54 additions and 16 deletions

View File

@ -43,6 +43,7 @@ static int apply = 1;
static int apply_in_reverse; static int apply_in_reverse;
static int apply_with_reject; static int apply_with_reject;
static int apply_verbosely; static int apply_verbosely;
static int allow_overlap;
static int no_add; static int no_add;
static const char *fake_ancestor; static const char *fake_ancestor;
static int line_termination = '\n'; static int line_termination = '\n';
@ -2430,9 +2431,9 @@ static void update_image(struct image *img,
memcpy(img->line + applied_pos, memcpy(img->line + applied_pos,
postimage->line, postimage->line,
postimage->nr * sizeof(*img->line)); postimage->nr * sizeof(*img->line));
for (i = 0; i < postimage->nr; i++) if (!allow_overlap)
img->line[applied_pos + i].flag |= LINE_PATCHED; for (i = 0; i < postimage->nr; i++)
img->line[applied_pos + i].flag |= LINE_PATCHED;
img->nr = nr; img->nr = nr;
} }
@ -3889,6 +3890,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
"don't expect at least one line of context"), "don't expect at least one line of context"),
OPT_BOOLEAN(0, "reject", &apply_with_reject, OPT_BOOLEAN(0, "reject", &apply_with_reject,
"leave the rejected hunks in corresponding *.rej files"), "leave the rejected hunks in corresponding *.rej files"),
OPT_BOOLEAN(0, "allow-overlap", &allow_overlap,
"allow overlapping hunks"),
OPT__VERBOSE(&apply_verbosely, "be verbose"), OPT__VERBOSE(&apply_verbosely, "be verbose"),
OPT_BIT(0, "inaccurate-eof", &options, OPT_BIT(0, "inaccurate-eof", &options,
"tolerate incorrectly detected missing new-line at the end of file", "tolerate incorrectly detected missing new-line at the end of file",

View File

@ -705,7 +705,7 @@ sub add_untracked_cmd {
sub run_git_apply { sub run_git_apply {
my $cmd = shift; my $cmd = shift;
my $fh; my $fh;
open $fh, '| git ' . $cmd; open $fh, '| git ' . $cmd . " --recount --allow-overlap";
print $fh @_; print $fh @_;
return close $fh; return close $fh;
} }
@ -1050,7 +1050,7 @@ EOF
sub diff_applies { sub diff_applies {
my $fh; my $fh;
return run_git_apply($patch_mode_flavour{APPLY_CHECK} . ' --recount --check', return run_git_apply($patch_mode_flavour{APPLY_CHECK} . ' --check',
map { @{$_->{TEXT}} } @_); map { @{$_->{TEXT}} } @_);
} }
@ -1139,7 +1139,7 @@ EOF
sub apply_patch { sub apply_patch {
my $cmd = shift; my $cmd = shift;
my $ret = run_git_apply $cmd . ' --recount', @_; my $ret = run_git_apply $cmd, @_;
if (!$ret) { if (!$ret) {
print STDERR @_; print STDERR @_;
} }
@ -1148,17 +1148,17 @@ sub apply_patch {
sub apply_patch_for_checkout_commit { sub apply_patch_for_checkout_commit {
my $reverse = shift; my $reverse = shift;
my $applies_index = run_git_apply 'apply '.$reverse.' --cached --recount --check', @_; my $applies_index = run_git_apply 'apply '.$reverse.' --cached --check', @_;
my $applies_worktree = run_git_apply 'apply '.$reverse.' --recount --check', @_; my $applies_worktree = run_git_apply 'apply '.$reverse.' --check', @_;
if ($applies_worktree && $applies_index) { if ($applies_worktree && $applies_index) {
run_git_apply 'apply '.$reverse.' --cached --recount', @_; run_git_apply 'apply '.$reverse.' --cached', @_;
run_git_apply 'apply '.$reverse.' --recount', @_; run_git_apply 'apply '.$reverse, @_;
return 1; return 1;
} elsif (!$applies_index) { } elsif (!$applies_index) {
print colored $error_color, "The selected hunks do not apply to the index!\n"; print colored $error_color, "The selected hunks do not apply to the index!\n";
if (prompt_yesno "Apply them to the worktree anyway? ") { if (prompt_yesno "Apply them to the worktree anyway? ") {
return run_git_apply 'apply '.$reverse.' --recount', @_; return run_git_apply 'apply '.$reverse, @_;
} else { } else {
print colored $error_color, "Nothing was applied.\n"; print colored $error_color, "Nothing was applied.\n";
return 0; return 0;
@ -1366,14 +1366,13 @@ sub patch_update_file {
next; next;
} }
elsif ($line =~ /^q/i) { elsif ($line =~ /^q/i) {
while ($ix < $num) { for ($i = 0; $i < $num; $i++) {
if (!defined $hunk[$ix]{USE}) { if (!defined $hunk[$i]{USE}) {
$hunk[$ix]{USE} = 0; $hunk[$i]{USE} = 0;
} }
$ix++;
} }
$quit = 1; $quit = 1;
next; last;
} }
elsif ($line =~ m|^/(.*)|) { elsif ($line =~ m|^/(.*)|) {
my $regex = $1; my $regex = $1;

View File

@ -294,4 +294,40 @@ test_expect_success PERL 'deleting an empty file' '
test_cmp expected diff test_cmp expected diff
' '
test_expect_success PERL 'split hunk setup' '
git reset --hard &&
for i in 10 20 30 40 50 60
do
echo $i
done >test &&
git add test &&
test_tick &&
git commit -m test &&
for i in 10 15 20 21 22 23 24 30 40 50 60
do
echo $i
done >test
'
test_expect_success PERL 'split hunk "add -p (edit)"' '
# Split, say Edit and do nothing. Then:
#
# 1. Broken version results in a patch that does not apply and
# only takes [y/n] (edit again) so the first q is discarded
# and then n attempts to discard the edit. Repeat q enough
# times to get out.
#
# 2. Correct version applies the (not)edited version, and asks
# about the next hunk, against wich we say q and program
# exits.
for a in s e q n q q
do
echo $a
done |
EDITOR=: git add -p &&
git diff >actual &&
! grep "^+15" actual
'
test_done test_done