add--interactive: fix missing file prompt for patch mode with "-i"

When invoked as "git add -i", each menu interactive menu option prompts the user to select a list of files. This includes the "patch" option, which gets the list before starting the hunk-selection loop. As "git add -p", it behaves differently, and jumps straight to the hunk selection loop. Since 0539d5e6d (i18n: add--interactive: mark patch prompt for translation, 2016-12-14), the "add -i" case mistakenly jumps to straight to the hunk-selection loop. Prior to that commit the distinction between the two cases was managed by the $patch_mode variable. That commit used $patch_mode for something else, and moved the old meaning to the "$cmd" variable. But it forgot to update the $patch_mode check inside patch_update_cmd() which controls the file-list behavior. The simplest fix would be to change that line to check $cmd. But while we're here, let's use a less obscure name for this flag: $patch_mode_only, a boolean which tells whether we are in full-interactive mode or only in patch-mode. Reported-by: Henrik Grubbström <grubba@grubba.org> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 2, 2017 at 04:48 UTC c852bd54bd87fdcdc825f5d45c26aa745be13ba6
2 files changed +22 -4
git-add--interactive.perl
+4 -4
@@ -92,7 +92,7 @@ sub colored {
92 }
93
94 # command line options
95 -my $cmd;
95 +my $patch_mode_only;
96 my $patch_mode;
97 my $patch_mode_revision;
98
@@ -1299,7 +1299,7 @@ sub patch_update_cmd {
1299 }
1300 return 0;
1301 }
1302 - if ($patch_mode) {
1302 + if ($patch_mode_only) {
1303 @them = @mods;
1304 }
1305 else {
@@ -1721,7 +1721,7 @@ sub process_args {
1721 die sprintf(__("invalid argument %s, expecting --"),
1722 $arg) unless $arg eq "--";
1723 %patch_mode_flavour = %{$patch_modes{$patch_mode}};
1724 - $cmd = 1;
1724 + $patch_mode_only = 1;
1725 }
1726 elsif ($arg ne "--") {
1727 die sprintf(__("invalid argument %s, expecting --"), $arg);
@@ -1758,7 +1758,7 @@ sub main_loop {
1758
1759 process_args();
1760 refresh();
1761 -if ($cmd) {
1761 +if ($patch_mode_only) {
1762 patch_update_cmd();
1763 }
1764 else {
t/t3701-add-interactive.sh
+18
@@ -394,4 +394,22 @@ test_expect_success 'diffs can be colorized' '
394 grep "$(printf "\\033")" output
395 '
396
397 +test_expect_success 'patch-mode via -i prompts for files' '
398 + git reset --hard &&
399 +
400 + echo one >file &&
401 + echo two >test &&
402 + git add -i <<-\EOF &&
403 + patch
404 + test
405 +
406 + y
407 + quit
408 + EOF
409 +
410 + echo test >expect &&
411 + git diff --cached --name-only >actual &&
412 + test_cmp expect actual
413 +'
414 +
415 test_done