add-patch: response to unknown command

When the user gives an unknown command to the "add -p" prompt, the list of accepted commands with their explanation is given. This is the same output they get when they say '?'. However, the unknown command may be due to a user input error rather than the user not knowing the valid command. To reduce the likelihood of user confusion and error repetition, instead of displaying the list of accepted commands, display a short error message with the unknown command received, as feedback to the user. Include a reminder about the current command '?' in the new message, to guide the user if they want help. Signed-off-by: Rubén Justo <rjusto@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rubén Justo committed Apr 29, 2024 at 20:37 UTC 26998ed2a2ed2e36103da7131e44cd6a0c8d4565
2 files changed +19 -2
add-patch.c
+4 -1
@@ -1666,7 +1666,7 @@ soft_increment:
1666 }
1667 } else if (s->answer.buf[0] == 'p') {
1668 rendered_hunk_index = -1;
1669 - } else {
1669 + } else if (s->answer.buf[0] == '?') {
1670 const char *p = _(help_patch_remainder), *eol = p;
1671
1672 color_fprintf(stdout, s->s.help_color, "%s",
@@ -1690,6 +1690,9 @@ soft_increment:
1690 color_fprintf_ln(stdout, s->s.help_color,
1691 "%.*s", (int)(eol - p), p);
1692 }
1693 + } else {
1694 + err(s, _("Unknown command '%s' (use '?' for help)"),
1695 + s->answer.buf);
1696 }
1697 }
1698
t/t3701-add-interactive.sh
+15 -1
@@ -7,6 +7,8 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 . ./test-lib.sh
8 . "$TEST_DIRECTORY"/lib-terminal.sh
9
10 +SP=" "
11 +
12 diff_cmp () {
13 for x
14 do
@@ -55,6 +57,19 @@ test_expect_success 'warn about add.interactive.useBuiltin' '
57 done
58 '
59
60 +test_expect_success 'unknown command' '
61 + test_when_finished "git reset --hard; rm -f command" &&
62 + echo W >command &&
63 + git add -N command &&
64 + git diff command >expect &&
65 + cat >>expect <<-EOF &&
66 + (1/1) Stage addition [y,n,q,a,d,e,p,?]? Unknown command ${SQ}W${SQ} (use ${SQ}?${SQ} for help)
67 + (1/1) Stage addition [y,n,q,a,d,e,p,?]?$SP
68 + EOF
69 + git add -p -- command <command >actual 2>&1 &&
70 + test_cmp expect actual
71 +'
72 +
73 test_expect_success 'setup (initial)' '
74 echo content >file &&
75 git add file &&
@@ -231,7 +246,6 @@ test_expect_success 'setup file' '
246 '
247
248 test_expect_success 'setup patch' '
234 - SP=" " &&
249 NULL="" &&
250 cat >patch <<-EOF
251 @@ -1,4 +1,4 @@