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>

Phillip Wood committed Feb 13, 2018 at 10:32 UTC 88f6ffc1c256ef241cbed33dabbdd23c1a7ac6a9
1 file changed +27 -23
git-add--interactive.perl
+27 -23
@@ -1185,7 +1185,7 @@ d - do not apply this hunk or any of the later hunks in the file"),
1185
1186 sub help_patch_cmd {
1187 local $_;
1188 - my $other = $_[0] . ",/,?";
1188 + my $other = $_[0] . ",?";
1189 print colored $help_color, __($help_patch_modes{$patch_mode}), "\n",
1190 map { "$_\n" } grep {
1191 my $c = quotemeta(substr($_, 0, 1));
@@ -1308,39 +1308,39 @@ sub display_hunks {
1308
1309 my %patch_update_prompt_modes = (
1310 stage => {
1311 - mode => N__("Stage mode change [y,n,q,a,d,/%s,?]? "),
1312 - deletion => N__("Stage deletion [y,n,q,a,d,/%s,?]? "),
1313 - hunk => N__("Stage this hunk [y,n,q,a,d,/%s,?]? "),
1311 + mode => N__("Stage mode change [y,n,q,a,d%s,?]? "),
1312 + deletion => N__("Stage deletion [y,n,q,a,d%s,?]? "),
1313 + hunk => N__("Stage this hunk [y,n,q,a,d%s,?]? "),
1314 },
1315 stash => {
1316 - mode => N__("Stash mode change [y,n,q,a,d,/%s,?]? "),
1317 - deletion => N__("Stash deletion [y,n,q,a,d,/%s,?]? "),
1318 - hunk => N__("Stash this hunk [y,n,q,a,d,/%s,?]? "),
1316 + mode => N__("Stash mode change [y,n,q,a,d%s,?]? "),
1317 + deletion => N__("Stash deletion [y,n,q,a,d%s,?]? "),
1318 + hunk => N__("Stash this hunk [y,n,q,a,d%s,?]? "),
1319 },
1320 reset_head => {
1321 - mode => N__("Unstage mode change [y,n,q,a,d,/%s,?]? "),
1322 - deletion => N__("Unstage deletion [y,n,q,a,d,/%s,?]? "),
1323 - hunk => N__("Unstage this hunk [y,n,q,a,d,/%s,?]? "),
1321 + mode => N__("Unstage mode change [y,n,q,a,d%s,?]? "),
1322 + deletion => N__("Unstage deletion [y,n,q,a,d%s,?]? "),
1323 + hunk => N__("Unstage this hunk [y,n,q,a,d%s,?]? "),
1324 },
1325 reset_nothead => {
1326 - mode => N__("Apply mode change to index [y,n,q,a,d,/%s,?]? "),
1327 - deletion => N__("Apply deletion to index [y,n,q,a,d,/%s,?]? "),
1328 - hunk => N__("Apply this hunk to index [y,n,q,a,d,/%s,?]? "),
1326 + mode => N__("Apply mode change to index [y,n,q,a,d%s,?]? "),
1327 + deletion => N__("Apply deletion to index [y,n,q,a,d%s,?]? "),
1328 + hunk => N__("Apply this hunk to index [y,n,q,a,d%s,?]? "),
1329 },
1330 checkout_index => {
1331 - mode => N__("Discard mode change from worktree [y,n,q,a,d,/%s,?]? "),
1332 - deletion => N__("Discard deletion from worktree [y,n,q,a,d,/%s,?]? "),
1333 - hunk => N__("Discard this hunk from worktree [y,n,q,a,d,/%s,?]? "),
1331 + mode => N__("Discard mode change from worktree [y,n,q,a,d%s,?]? "),
1332 + deletion => N__("Discard deletion from worktree [y,n,q,a,d%s,?]? "),
1333 + hunk => N__("Discard this hunk from worktree [y,n,q,a,d%s,?]? "),
1334 },
1335 checkout_head => {
1336 - mode => N__("Discard mode change from index and worktree [y,n,q,a,d,/%s,?]? "),
1337 - deletion => N__("Discard deletion from index and worktree [y,n,q,a,d,/%s,?]? "),
1338 - hunk => N__("Discard this hunk from index and worktree [y,n,q,a,d,/%s,?]? "),
1336 + mode => N__("Discard mode change from index and worktree [y,n,q,a,d%s,?]? "),
1337 + deletion => N__("Discard deletion from index and worktree [y,n,q,a,d%s,?]? "),
1338 + hunk => N__("Discard this hunk from index and worktree [y,n,q,a,d%s,?]? "),
1339 },
1340 checkout_nothead => {
1341 - mode => N__("Apply mode change to index and worktree [y,n,q,a,d,/%s,?]? "),
1342 - deletion => N__("Apply deletion to index and worktree [y,n,q,a,d,/%s,?]? "),
1343 - hunk => N__("Apply this hunk to index and worktree [y,n,q,a,d,/%s,?]? "),
1341 + mode => N__("Apply mode change to index and worktree [y,n,q,a,d%s,?]? "),
1342 + deletion => N__("Apply deletion to index and worktree [y,n,q,a,d%s,?]? "),
1343 + hunk => N__("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "),
1344 },
1345 );
1346
@@ -1396,7 +1396,7 @@ sub patch_update_file {
1396 $other .= ',J';
1397 }
1398 if ($num > 1) {
1399 - $other .= ',g';
1399 + $other .= ',g,/';
1400 }
1401 for ($i = 0; $i < $num; $i++) {
1402 if (!defined $hunk[$i]{USE}) {
@@ -1484,6 +1484,10 @@ sub patch_update_file {
1484 }
1485 elsif ($line =~ m|^/(.*)|) {
1486 my $regex = $1;
1487 + unless ($other =~ m|/|) {
1488 + error_msg __("No other hunks to search\n");
1489 + next;
1490 + }
1491 if ($1 eq "") {
1492 print colored $prompt_color, __("search for regex? ");
1493 $regex = <STDIN>;