Bump to git 2.30.1
[platform/upstream/git.git] / git-add--interactive.perl
index 964c3a7..bc3a1e8 100755 (executable)
@@ -30,9 +30,9 @@ my ($fraginfo_color) =
        $diff_use_color ? (
                $repo->get_color('color.diff.frag', 'cyan'),
        ) : ();
-my ($diff_plain_color) =
+my ($diff_context_color) =
        $diff_use_color ? (
-               $repo->get_color('color.diff.plain', ''),
+               $repo->get_color($repo->config('color.diff.context') ? 'color.diff.context' : 'color.diff.plain', ''),
        ) : ();
 my ($diff_old_color) =
        $diff_use_color ? (
@@ -149,6 +149,20 @@ my %patch_modes = (
                FILTER => undef,
                IS_REVERSE => 0,
        },
+       'worktree_head' => {
+               DIFF => 'diff-index -p',
+               APPLY => sub { apply_patch 'apply -R', @_ },
+               APPLY_CHECK => 'apply -R',
+               FILTER => undef,
+               IS_REVERSE => 1,
+       },
+       'worktree_nothead' => {
+               DIFF => 'diff-index -R -p',
+               APPLY => sub { apply_patch 'apply', @_ },
+               APPLY_CHECK => 'apply',
+               FILTER => undef,
+               IS_REVERSE => 0,
+       },
 );
 
 $patch_mode = 'stage';
@@ -163,7 +177,9 @@ sub run_cmd_pipe {
        } else {
                my $fh = undef;
                open($fh, '-|', @_) or die;
-               return <$fh>;
+               my @out = <$fh>;
+               close $fh || die "Cannot close @_ ($!)";
+               return @out;
        }
 }
 
@@ -205,8 +221,15 @@ my $status_head = sprintf($status_fmt, __('staged'), __('unstaged'), __('path'))
        }
 }
 
-sub get_empty_tree {
-       return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
+{
+       my $empty_tree;
+       sub get_empty_tree {
+               return $empty_tree if defined $empty_tree;
+
+               ($empty_tree) = run_cmd_pipe(qw(git hash-object -t tree /dev/null));
+               chomp $empty_tree;
+               return $empty_tree;
+       }
 }
 
 sub get_diff_reference {
@@ -460,10 +483,8 @@ sub list_and_choose {
                my $last_lf = 0;
 
                if ($opts->{HEADER}) {
-                       if (!$opts->{LIST_FLAT}) {
-                               print "     ";
-                       }
-                       print colored $header_color, "$opts->{HEADER}\n";
+                       my $indent = $opts->{LIST_FLAT} ? "" : "     ";
+                       print colored $header_color, "$indent$opts->{HEADER}\n";
                }
                for ($i = 0; $i < @stuff; $i++) {
                        my $chosen = $chosen[$i] ? '*' : ' ';
@@ -677,7 +698,7 @@ sub add_untracked_cmd {
 sub run_git_apply {
        my $cmd = shift;
        my $fh;
-       open $fh, '| git ' . $cmd . " --recount --allow-overlap";
+       open $fh, '| git ' . $cmd . " --allow-overlap";
        print $fh @_;
        return close $fh;
 }
@@ -691,7 +712,7 @@ sub parse_diff {
        if (defined $patch_mode_revision) {
                push @diff_cmd, get_diff_reference($patch_mode_revision);
        }
-       my @diff = run_cmd_pipe("git", @diff_cmd, "--", $path);
+       my @diff = run_cmd_pipe("git", @diff_cmd, qw(--no-color --), $path);
        my @colored = ();
        if ($diff_use_color) {
                my @display_cmd = ("git", @diff_cmd, qw(--color --), $path);
@@ -705,6 +726,14 @@ sub parse_diff {
        }
        my (@hunk) = { TEXT => [], DISPLAY => [], TYPE => 'header' };
 
+       if (@colored && @colored != @diff) {
+               print STDERR
+                 "fatal: mismatched output from interactive.diffFilter\n",
+                 "hint: Your filter must maintain a one-to-one correspondence\n",
+                 "hint: between its input and output lines.\n";
+               exit 1;
+       }
+
        for (my $i = 0; $i < @diff; $i++) {
                if ($diff[$i] =~ /^@@ /) {
                        push @hunk, { TEXT => [], DISPLAY => [],
@@ -723,8 +752,13 @@ sub parse_diff_header {
        my $head = { TEXT => [], DISPLAY => [], TYPE => 'header' };
        my $mode = { TEXT => [], DISPLAY => [], TYPE => 'mode' };
        my $deletion = { TEXT => [], DISPLAY => [], TYPE => 'deletion' };
+       my $addition;
 
        for (my $i = 0; $i < @{$src->{TEXT}}; $i++) {
+               if ($src->{TEXT}->[$i] =~ /^new file/) {
+                       $addition = 1;
+                       $head->{TYPE} = 'addition';
+               }
                my $dest =
                   $src->{TEXT}->[$i] =~ /^(old|new) mode (\d+)$/ ? $mode :
                   $src->{TEXT}->[$i] =~ /^deleted file/ ? $deletion :
@@ -732,7 +766,7 @@ sub parse_diff_header {
                push @{$dest->{TEXT}}, $src->{TEXT}->[$i];
                push @{$dest->{DISPLAY}}, $src->{DISPLAY}->[$i];
        }
-       return ($head, $mode, $deletion);
+       return ($head, $mode, $deletion, $addition);
 }
 
 sub hunk_splittable {
@@ -751,6 +785,15 @@ sub parse_hunk_header {
        return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
 }
 
+sub format_hunk_header {
+       my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = @_;
+       return ("@@ -$o_ofs" .
+               (($o_cnt != 1) ? ",$o_cnt" : '') .
+               " +$n_ofs" .
+               (($n_cnt != 1) ? ",$n_cnt" : '') .
+               " @@\n");
+}
+
 sub split_hunk {
        my ($text, $display) = @_;
        my @split = ();
@@ -784,6 +827,11 @@ sub split_hunk {
                while (++$i < @$text) {
                        my $line = $text->[$i];
                        my $display = $display->[$i];
+                       if ($line =~ /^\\/) {
+                               push @{$this->{TEXT}}, $line;
+                               push @{$this->{DISPLAY}}, $display;
+                               next;
+                       }
                        if ($line =~ /^ /) {
                                if ($this->{ADDDEL} &&
                                    !defined $next_hunk_start) {
@@ -838,11 +886,7 @@ sub split_hunk {
                my $o_cnt = $hunk->{OCNT};
                my $n_cnt = $hunk->{NCNT};
 
-               my $head = ("@@ -$o_ofs" .
-                           (($o_cnt != 1) ? ",$o_cnt" : '') .
-                           " +$n_ofs" .
-                           (($n_cnt != 1) ? ",$n_cnt" : '') .
-                           " @@\n");
+               my $head = format_hunk_header($o_ofs, $o_cnt, $n_ofs, $n_cnt);
                my $display_head = $head;
                unshift @{$hunk->{TEXT}}, $head;
                if ($diff_use_color) {
@@ -886,6 +930,9 @@ sub merge_hunk {
                        $n_cnt++;
                        push @line, $line;
                        next;
+               } elsif ($line =~ /^\\/) {
+                       push @line, $line;
+                       next;
                }
 
                last if ($o1_ofs <= $ofs);
@@ -904,6 +951,9 @@ sub merge_hunk {
                        $n_cnt++;
                        push @line, $line;
                        next;
+               } elsif ($line =~ /^\\/) {
+                       push @line, $line;
+                       next;
                }
                $ofs++;
                $o_cnt++;
@@ -912,11 +962,7 @@ sub merge_hunk {
                }
                push @line, $line;
        }
-       my $head = ("@@ -$o0_ofs" .
-                   (($o_cnt != 1) ? ",$o_cnt" : '') .
-                   " +$n0_ofs" .
-                   (($n_cnt != 1) ? ",$n_cnt" : '') .
-                   " @@\n");
+       my $head = format_hunk_header($o0_ofs, $o_cnt, $n0_ofs, $n_cnt);
        @{$prev->{TEXT}} = ($head, @line);
 }
 
@@ -925,14 +971,39 @@ sub coalesce_overlapping_hunks {
        my @out = ();
 
        my ($last_o_ctx, $last_was_dirty);
+       my $ofs_delta = 0;
 
-       for (grep { $_->{USE} } @in) {
+       for (@in) {
                if ($_->{TYPE} ne 'hunk') {
                        push @out, $_;
                        next;
                }
                my $text = $_->{TEXT};
-               my ($o_ofs) = parse_hunk_header($text->[0]);
+               my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
+                                               parse_hunk_header($text->[0]);
+               unless ($_->{USE}) {
+                       $ofs_delta += $o_cnt - $n_cnt;
+                       # If this hunk has been edited then subtract
+                       # the delta that is due to the edit.
+                       if ($_->{OFS_DELTA}) {
+                               $ofs_delta -= $_->{OFS_DELTA};
+                       }
+                       next;
+               }
+               if ($ofs_delta) {
+                       if ($patch_mode_flavour{IS_REVERSE}) {
+                               $o_ofs -= $ofs_delta;
+                       } else {
+                               $n_ofs += $ofs_delta;
+                       }
+                       $_->{TEXT}->[0] = format_hunk_header($o_ofs, $o_cnt,
+                                                            $n_ofs, $n_cnt);
+               }
+               # If this hunk was edited then adjust the offset delta
+               # to reflect the edit.
+               if ($_->{OFS_DELTA}) {
+                       $ofs_delta += $_->{OFS_DELTA};
+               }
                if (defined $last_o_ctx &&
                    $o_ofs <= $last_o_ctx &&
                    !$_->{DIRTY} &&
@@ -975,7 +1046,7 @@ sub color_diff {
                colored((/^@/  ? $fraginfo_color :
                         /^\+/ ? $diff_new_color :
                         /^-/  ? $diff_old_color :
-                        $diff_plain_color),
+                        $diff_context_color),
                        $_);
        } @_;
 }
@@ -1002,8 +1073,38 @@ marked for discarding."),
        checkout_nothead => N__(
 "If the patch applies cleanly, the edited hunk will immediately be
 marked for applying."),
+       worktree_head => N__(
+"If the patch applies cleanly, the edited hunk will immediately be
+marked for discarding."),
+       worktree_nothead => N__(
+"If the patch applies cleanly, the edited hunk will immediately be
+marked for applying."),
 );
 
+sub recount_edited_hunk {
+       local $_;
+       my ($oldtext, $newtext) = @_;
+       my ($o_cnt, $n_cnt) = (0, 0);
+       for (@{$newtext}[1..$#{$newtext}]) {
+               my $mode = substr($_, 0, 1);
+               if ($mode eq '-') {
+                       $o_cnt++;
+               } elsif ($mode eq '+') {
+                       $n_cnt++;
+               } elsif ($mode eq ' ' or $mode eq "\n") {
+                       $o_cnt++;
+                       $n_cnt++;
+               }
+       }
+       my ($o_ofs, undef, $n_ofs, undef) =
+                                       parse_hunk_header($newtext->[0]);
+       $newtext->[0] = format_hunk_header($o_ofs, $o_cnt, $n_ofs, $n_cnt);
+       my (undef, $orig_o_cnt, undef, $orig_n_cnt) =
+                                       parse_hunk_header($oldtext->[0]);
+       # Return the change in the number of lines inserted by this hunk
+       return $orig_o_cnt - $orig_n_cnt - $o_cnt + $n_cnt;
+}
+
 sub edit_hunk_manually {
        my ($oldtext) = @_;
 
@@ -1031,7 +1132,7 @@ aborted and the hunk is left unchanged.
 EOF2
        close $fh;
 
-       chomp(my $editor = run_cmd_pipe(qw(git var GIT_EDITOR)));
+       chomp(my ($editor) = run_cmd_pipe(qw(git var GIT_EDITOR)));
        system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
 
        if ($? != 0) {
@@ -1102,25 +1203,32 @@ sub prompt_yesno {
 }
 
 sub edit_hunk_loop {
-       my ($head, $hunk, $ix) = @_;
-       my $text = $hunk->[$ix]->{TEXT};
+       my ($head, $hunks, $ix) = @_;
+       my $hunk = $hunks->[$ix];
+       my $text = $hunk->{TEXT};
 
        while (1) {
-               $text = edit_hunk_manually($text);
-               if (!defined $text) {
+               my $newtext = edit_hunk_manually($text);
+               if (!defined $newtext) {
                        return undef;
                }
                my $newhunk = {
-                       TEXT => $text,
-                       TYPE => $hunk->[$ix]->{TYPE},
+                       TEXT => $newtext,
+                       TYPE => $hunk->{TYPE},
                        USE => 1,
                        DIRTY => 1,
                };
+               $newhunk->{OFS_DELTA} = recount_edited_hunk($text, $newtext);
+               # If this hunk has already been edited then add the
+               # offset delta of the previous edit to get the real
+               # delta from the original unedited hunk.
+               $hunk->{OFS_DELTA} and
+                               $newhunk->{OFS_DELTA} += $hunk->{OFS_DELTA};
                if (diff_applies($head,
-                                @{$hunk}[0..$ix-1],
+                                @{$hunks}[0..$ix-1],
                                 $newhunk,
-                                @{$hunk}[$ix+1..$#{$hunk}])) {
-                       $newhunk->{DISPLAY} = [color_diff(@{$text})];
+                                @{$hunks}[$ix+1..$#{$hunks}])) {
+                       $newhunk->{DISPLAY} = [color_diff(@{$newtext})];
                        return $newhunk;
                }
                else {
@@ -1181,10 +1289,28 @@ n - do not apply this hunk to index and worktree
 q - quit; do not apply this hunk or any of the remaining ones
 a - apply this hunk and all later hunks in the file
 d - do not apply this hunk or any of the later hunks in the file"),
+       worktree_head => N__(
+"y - discard this hunk from worktree
+n - do not discard this hunk from worktree
+q - quit; do not discard this hunk or any of the remaining ones
+a - discard this hunk and all later hunks in the file
+d - do not discard this hunk or any of the later hunks in the file"),
+       worktree_nothead => N__(
+"y - apply this hunk to worktree
+n - do not apply this hunk to worktree
+q - quit; do not apply this hunk or any of the remaining ones
+a - apply this hunk and all later hunks in the file
+d - do not apply this hunk or any of the later hunks in the file"),
 );
 
 sub help_patch_cmd {
-       print colored $help_color, __($help_patch_modes{$patch_mode}), "\n", __ <<EOF ;
+       local $_;
+       my $other = $_[0] . ",?";
+       print colored $help_color, __($help_patch_modes{$patch_mode}), "\n",
+               map { "$_\n" } grep {
+                       my $c = quotemeta(substr($_, 0, 1));
+                       $other =~ /,$c/
+               } split "\n", __ <<EOF ;
 g - select a hunk to go to
 / - search for a hunk matching the given regex
 j - leave this hunk undecided, see next undecided hunk
@@ -1302,39 +1428,58 @@ sub display_hunks {
 
 my %patch_update_prompt_modes = (
        stage => {
-               mode => N__("Stage mode change [y,n,q,a,d,/%s,?]? "),
-               deletion => N__("Stage deletion [y,n,q,a,d,/%s,?]? "),
-               hunk => N__("Stage this hunk [y,n,q,a,d,/%s,?]? "),
+               mode => N__("Stage mode change [y,n,q,a,d%s,?]? "),
+               deletion => N__("Stage deletion [y,n,q,a,d%s,?]? "),
+               addition => N__("Stage addition [y,n,q,a,d%s,?]? "),
+               hunk => N__("Stage this hunk [y,n,q,a,d%s,?]? "),
        },
        stash => {
-               mode => N__("Stash mode change [y,n,q,a,d,/%s,?]? "),
-               deletion => N__("Stash deletion [y,n,q,a,d,/%s,?]? "),
-               hunk => N__("Stash this hunk [y,n,q,a,d,/%s,?]? "),
+               mode => N__("Stash mode change [y,n,q,a,d%s,?]? "),
+               deletion => N__("Stash deletion [y,n,q,a,d%s,?]? "),
+               addition => N__("Stash addition [y,n,q,a,d%s,?]? "),
+               hunk => N__("Stash this hunk [y,n,q,a,d%s,?]? "),
        },
        reset_head => {
-               mode => N__("Unstage mode change [y,n,q,a,d,/%s,?]? "),
-               deletion => N__("Unstage deletion [y,n,q,a,d,/%s,?]? "),
-               hunk => N__("Unstage this hunk [y,n,q,a,d,/%s,?]? "),
+               mode => N__("Unstage mode change [y,n,q,a,d%s,?]? "),
+               deletion => N__("Unstage deletion [y,n,q,a,d%s,?]? "),
+               addition => N__("Unstage addition [y,n,q,a,d%s,?]? "),
+               hunk => N__("Unstage this hunk [y,n,q,a,d%s,?]? "),
        },
        reset_nothead => {
-               mode => N__("Apply mode change to index [y,n,q,a,d,/%s,?]? "),
-               deletion => N__("Apply deletion to index [y,n,q,a,d,/%s,?]? "),
-               hunk => N__("Apply this hunk to index [y,n,q,a,d,/%s,?]? "),
+               mode => N__("Apply mode change to index [y,n,q,a,d%s,?]? "),
+               deletion => N__("Apply deletion to index [y,n,q,a,d%s,?]? "),
+               addition => N__("Apply addition to index [y,n,q,a,d%s,?]? "),
+               hunk => N__("Apply this hunk to index [y,n,q,a,d%s,?]? "),
        },
        checkout_index => {
-               mode => N__("Discard mode change from worktree [y,n,q,a,d,/%s,?]? "),
-               deletion => N__("Discard deletion from worktree [y,n,q,a,d,/%s,?]? "),
-               hunk => N__("Discard this hunk from worktree [y,n,q,a,d,/%s,?]? "),
+               mode => N__("Discard mode change from worktree [y,n,q,a,d%s,?]? "),
+               deletion => N__("Discard deletion from worktree [y,n,q,a,d%s,?]? "),
+               addition => N__("Discard addition from worktree [y,n,q,a,d%s,?]? "),
+               hunk => N__("Discard this hunk from worktree [y,n,q,a,d%s,?]? "),
        },
        checkout_head => {
-               mode => N__("Discard mode change from index and worktree [y,n,q,a,d,/%s,?]? "),
-               deletion => N__("Discard deletion from index and worktree [y,n,q,a,d,/%s,?]? "),
-               hunk => N__("Discard this hunk from index and worktree [y,n,q,a,d,/%s,?]? "),
+               mode => N__("Discard mode change from index and worktree [y,n,q,a,d%s,?]? "),
+               deletion => N__("Discard deletion from index and worktree [y,n,q,a,d%s,?]? "),
+               addition => N__("Discard addition from index and worktree [y,n,q,a,d%s,?]? "),
+               hunk => N__("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? "),
        },
        checkout_nothead => {
-               mode => N__("Apply mode change to index and worktree [y,n,q,a,d,/%s,?]? "),
-               deletion => N__("Apply deletion to index and worktree [y,n,q,a,d,/%s,?]? "),
-               hunk => N__("Apply this hunk to index and worktree [y,n,q,a,d,/%s,?]? "),
+               mode => N__("Apply mode change to index and worktree [y,n,q,a,d%s,?]? "),
+               deletion => N__("Apply deletion to index and worktree [y,n,q,a,d%s,?]? "),
+               addition => N__("Apply addition to index and worktree [y,n,q,a,d%s,?]? "),
+               hunk => N__("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "),
+       },
+       worktree_head => {
+               mode => N__("Discard mode change from worktree [y,n,q,a,d%s,?]? "),
+               deletion => N__("Discard deletion from worktree [y,n,q,a,d%s,?]? "),
+               addition => N__("Discard addition from worktree [y,n,q,a,d%s,?]? "),
+               hunk => N__("Discard this hunk from worktree [y,n,q,a,d%s,?]? "),
+       },
+       worktree_nothead => {
+               mode => N__("Apply mode change to worktree [y,n,q,a,d%s,?]? "),
+               deletion => N__("Apply deletion to worktree [y,n,q,a,d%s,?]? "),
+               addition => N__("Apply addition to worktree [y,n,q,a,d%s,?]? "),
+               hunk => N__("Apply this hunk to worktree [y,n,q,a,d%s,?]? "),
        },
 );
 
@@ -1343,7 +1488,7 @@ sub patch_update_file {
        my ($ix, $num);
        my $path = shift;
        my ($head, @hunk) = parse_diff($path);
-       ($head, my $mode, my $deletion) = parse_diff_header($head);
+       ($head, my $mode, my $deletion, my $addition) = parse_diff_header($head);
        for (@{$head->{DISPLAY}}) {
                print;
        }
@@ -1366,6 +1511,7 @@ sub patch_update_file {
                my ($prev, $next, $other, $undecided, $i);
                $other = '';
 
+               last if ($ix and !$num);
                if ($num <= $ix) {
                        $ix = 0;
                }
@@ -1390,7 +1536,7 @@ sub patch_update_file {
                        $other .= ',J';
                }
                if ($num > 1) {
-                       $other .= ',g';
+                       $other .= ',g,/';
                }
                for ($i = 0; $i < $num; $i++) {
                        if (!defined $hunk[$i]{USE}) {
@@ -1398,41 +1544,61 @@ sub patch_update_file {
                                last;
                        }
                }
-               last if (!$undecided);
+               last if (!$undecided && ($num || !$addition));
 
-               if ($hunk[$ix]{TYPE} eq 'hunk' &&
-                   hunk_splittable($hunk[$ix]{TEXT})) {
-                       $other .= ',s';
-               }
-               if ($hunk[$ix]{TYPE} eq 'hunk') {
-                       $other .= ',e';
-               }
-               for (@{$hunk[$ix]{DISPLAY}}) {
-                       print;
+               if ($num) {
+                       if ($hunk[$ix]{TYPE} eq 'hunk' &&
+                           hunk_splittable($hunk[$ix]{TEXT})) {
+                               $other .= ',s';
+                       }
+                       if ($hunk[$ix]{TYPE} eq 'hunk') {
+                               $other .= ',e';
+                       }
+                       for (@{$hunk[$ix]{DISPLAY}}) {
+                               print;
+                       }
                }
-               print colored $prompt_color,
-                       sprintf(__($patch_update_prompt_modes{$patch_mode}{$hunk[$ix]{TYPE}}), $other);
+               my $type = $num ? $hunk[$ix]{TYPE} : $head->{TYPE};
+               print colored $prompt_color, "(", ($ix+1), "/", ($num ? $num : 1), ") ",
+                       sprintf(__($patch_update_prompt_modes{$patch_mode}{$type}), $other);
 
                my $line = prompt_single_character;
                last unless defined $line;
                if ($line) {
                        if ($line =~ /^y/i) {
-                               $hunk[$ix]{USE} = 1;
+                               if ($num) {
+                                       $hunk[$ix]{USE} = 1;
+                               } else {
+                                       $head->{USE} = 1;
+                               }
                        }
                        elsif ($line =~ /^n/i) {
-                               $hunk[$ix]{USE} = 0;
+                               if ($num) {
+                                       $hunk[$ix]{USE} = 0;
+                               } else {
+                                       $head->{USE} = 0;
+                               }
                        }
                        elsif ($line =~ /^a/i) {
-                               while ($ix < $num) {
-                                       if (!defined $hunk[$ix]{USE}) {
-                                               $hunk[$ix]{USE} = 1;
+                               if ($num) {
+                                       while ($ix < $num) {
+                                               if (!defined $hunk[$ix]{USE}) {
+                                                       $hunk[$ix]{USE} = 1;
+                                               }
+                                               $ix++;
                                        }
+                               } else {
+                                       $head->{USE} = 1;
                                        $ix++;
                                }
                                next;
                        }
-                       elsif ($other =~ /g/ && $line =~ /^g(.*)/) {
+                       elsif ($line =~ /^g(.*)/) {
                                my $response = $1;
+                               unless ($other =~ /g/) {
+                                       error_msg __("No other hunks to goto\n");
+                                       next;
+                               }
                                my $no = $ix > 10 ? $ix - 10 : 0;
                                while ($response eq '') {
                                        $no = display_hunks(\@hunk, $no);
@@ -1459,26 +1625,39 @@ sub patch_update_file {
                                next;
                        }
                        elsif ($line =~ /^d/i) {
-                               while ($ix < $num) {
-                                       if (!defined $hunk[$ix]{USE}) {
-                                               $hunk[$ix]{USE} = 0;
+                               if ($num) {
+                                       while ($ix < $num) {
+                                               if (!defined $hunk[$ix]{USE}) {
+                                                       $hunk[$ix]{USE} = 0;
+                                               }
+                                               $ix++;
                                        }
+                               } else {
+                                       $head->{USE} = 0;
                                        $ix++;
                                }
                                next;
                        }
                        elsif ($line =~ /^q/i) {
-                               for ($i = 0; $i < $num; $i++) {
-                                       if (!defined $hunk[$i]{USE}) {
-                                               $hunk[$i]{USE} = 0;
+                               if ($num) {
+                                       for ($i = 0; $i < $num; $i++) {
+                                               if (!defined $hunk[$i]{USE}) {
+                                                       $hunk[$i]{USE} = 0;
+                                               }
                                        }
+                               } elsif (!defined $head->{USE}) {
+                                       $head->{USE} = 0;
                                }
                                $quit = 1;
                                last;
                        }
                        elsif ($line =~ m|^/(.*)|) {
                                my $regex = $1;
-                               if ($1 eq "") {
+                               unless ($other =~ m|/|) {
+                                       error_msg __("No other hunks to search\n");
+                                       next;
+                               }
+                               if ($regex eq "") {
                                        print colored $prompt_color, __("search for regex? ");
                                        $regex = <STDIN>;
                                        if (defined $regex) {
@@ -1546,7 +1725,11 @@ sub patch_update_file {
                                        next;
                                }
                        }
-                       elsif ($other =~ /s/ && $line =~ /^s/) {
+                       elsif ($line =~ /^s/) {
+                               unless ($other =~ /s/) {
+                                       error_msg __("Sorry, cannot split this hunk\n");
+                                       next;
+                               }
                                my @split = split_hunk($hunk[$ix]{TEXT}, $hunk[$ix]{DISPLAY});
                                if (1 < @split) {
                                        print colored $header_color, sprintf(
@@ -1558,7 +1741,11 @@ sub patch_update_file {
                                $num = scalar @hunk;
                                next;
                        }
-                       elsif ($other =~ /e/ && $line =~ /^e/) {
+                       elsif ($line =~ /^e/) {
+                               unless ($other =~ /e/) {
+                                       error_msg __("Sorry, cannot edit this hunk\n");
+                                       next;
+                               }
                                my $newhunk = edit_hunk_loop($head, \@hunk, $ix);
                                if (defined $newhunk) {
                                        splice @hunk, $ix, 1, $newhunk;
@@ -1577,7 +1764,7 @@ sub patch_update_file {
                }
        }
 
-       @hunk = coalesce_overlapping_hunks(@hunk);
+       @hunk = coalesce_overlapping_hunks(@hunk) if ($num);
 
        my $n_lofs = 0;
        my @result = ();
@@ -1587,7 +1774,7 @@ sub patch_update_file {
                }
        }
 
-       if (@result) {
+       if (@result or $head->{USE}) {
                my @patch = reassemble_patch($head->{TEXT}, @result);
                my $apply_routine = $patch_mode_flavour{APPLY};
                &$apply_routine(@patch);
@@ -1641,6 +1828,13 @@ sub process_args {
                                $arg = shift @ARGV or die __("missing --");
                                if ($arg ne '--') {
                                        $patch_mode_revision = $arg;
+
+                                       # NEEDSWORK: Instead of comparing to the literal "HEAD",
+                                       # compare the commit objects instead so that other ways of
+                                       # saying the same thing (such as "@") are also handled
+                                       # appropriately.
+                                       #
+                                       # This applies to the cases below too.
                                        $patch_mode = ($arg eq 'HEAD' ?
                                                       'reset_head' : 'reset_nothead');
                                        $arg = shift @ARGV or die __("missing --");
@@ -1655,6 +1849,16 @@ sub process_args {
                                                       'checkout_head' : 'checkout_nothead');
                                        $arg = shift @ARGV or die __("missing --");
                                }
+                       } elsif ($1 eq 'worktree') {
+                               $arg = shift @ARGV or die __("missing --");
+                               if ($arg eq '--') {
+                                       $patch_mode = 'checkout_index';
+                               } else {
+                                       $patch_mode_revision = $arg;
+                                       $patch_mode = ($arg eq 'HEAD' ?
+                                                      'worktree_head' : 'worktree_nothead');
+                                       $arg = shift @ARGV or die __("missing --");
+                               }
                        } elsif ($1 eq 'stage' or $1 eq 'stash') {
                                $patch_mode = $1;
                                $arg = shift @ARGV or die __("missing --");