add -p: only bind search key if there's more than one hunk

If there is only a single hunk then disable searching as there is
nothing to search for. Also print a specific error message if the user
tries to search with '/' when there's only a single hunk rather than
just listing the key bindings.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Phillip Wood 2018-02-13 10:32:40 +00:00 committed by Junio C Hamano
parent 01a6966021
commit 88f6ffc1c2

View File

@ -1185,7 +1185,7 @@ d - do not apply this hunk or any of the later hunks in the file"),
sub help_patch_cmd { sub help_patch_cmd {
local $_; local $_;
my $other = $_[0] . ",/,?"; my $other = $_[0] . ",?";
print colored $help_color, __($help_patch_modes{$patch_mode}), "\n", print colored $help_color, __($help_patch_modes{$patch_mode}), "\n",
map { "$_\n" } grep { map { "$_\n" } grep {
my $c = quotemeta(substr($_, 0, 1)); my $c = quotemeta(substr($_, 0, 1));
@ -1308,39 +1308,39 @@ sub display_hunks {
my %patch_update_prompt_modes = ( my %patch_update_prompt_modes = (
stage => { stage => {
mode => N__("Stage mode change [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,?]? "), deletion => N__("Stage deletion [y,n,q,a,d%s,?]? "),
hunk => N__("Stage this hunk [y,n,q,a,d,/%s,?]? "), hunk => N__("Stage this hunk [y,n,q,a,d%s,?]? "),
}, },
stash => { stash => {
mode => N__("Stash mode change [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,?]? "), deletion => N__("Stash deletion [y,n,q,a,d%s,?]? "),
hunk => N__("Stash this hunk [y,n,q,a,d,/%s,?]? "), hunk => N__("Stash this hunk [y,n,q,a,d%s,?]? "),
}, },
reset_head => { reset_head => {
mode => N__("Unstage mode change [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,?]? "), deletion => N__("Unstage deletion [y,n,q,a,d%s,?]? "),
hunk => N__("Unstage this hunk [y,n,q,a,d,/%s,?]? "), hunk => N__("Unstage this hunk [y,n,q,a,d%s,?]? "),
}, },
reset_nothead => { reset_nothead => {
mode => N__("Apply mode change 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,?]? "), 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,?]? "), hunk => N__("Apply this hunk to index [y,n,q,a,d%s,?]? "),
}, },
checkout_index => { checkout_index => {
mode => N__("Discard mode change 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,?]? "), 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,?]? "), hunk => N__("Discard this hunk from worktree [y,n,q,a,d%s,?]? "),
}, },
checkout_head => { checkout_head => {
mode => N__("Discard mode change 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,?]? "), 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,?]? "), hunk => N__("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? "),
}, },
checkout_nothead => { checkout_nothead => {
mode => N__("Apply mode change 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,?]? "), 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,?]? "), hunk => N__("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "),
}, },
); );
@ -1396,7 +1396,7 @@ sub patch_update_file {
$other .= ',J'; $other .= ',J';
} }
if ($num > 1) { if ($num > 1) {
$other .= ',g'; $other .= ',g,/';
} }
for ($i = 0; $i < $num; $i++) { for ($i = 0; $i < $num; $i++) {
if (!defined $hunk[$i]{USE}) { if (!defined $hunk[$i]{USE}) {
@ -1484,6 +1484,10 @@ sub patch_update_file {
} }
elsif ($line =~ m|^/(.*)|) { elsif ($line =~ m|^/(.*)|) {
my $regex = $1; my $regex = $1;
unless ($other =~ m|/|) {
error_msg __("No other hunks to search\n");
next;
}
if ($1 eq "") { if ($1 eq "") {
print colored $prompt_color, __("search for regex? "); print colored $prompt_color, __("search for regex? ");
$regex = <STDIN>; $regex = <STDIN>;