From: Jishnu C K Date: Fri, 19 Jun 2026 17:04:09 GMT Subject: [PATCH v2] help: include arguments in autocorrect=prompt message Message-ID: <6a357689.0f9b68c4.317a5d.1919@mx.google.com> In-Reply-To: v2: Reworked as an incremental improvement to the existing autocorrect=prompt code path rather than a parallel reimplementation, per feedback from Junio and Justin. --- From a4e8fb6fd6dd6a501e565c7500cbf927d7cb0b42 Mon Sep 17 00:00:00 2001 From: calicomills Date: Fri, 19 Jun 2026 13:01:40 +0530 Subject: [PATCH v2 v2] help: include arguments in autocorrect=prompt message When 'help.autocorrect=prompt' is configured and the user mistypes a git command, the prompt currently shows only the corrected command name: Run 'checkout' instead [y/N]? This leaves the user unsure whether their original arguments will be preserved. Update the prompt to include the full corrected invocation: Run 'git checkout neo' instead [y/N]? The help_unknown_cmd() signature is updated to accept the args vector so the prompt can show the original arguments alongside the corrected command name. Callers that do not have access to the args (e.g. builtin/help.c) pass NULL, which is handled gracefully. Signed-off-by: calicomills --- help.c | 49 +++++++++++++---------------------- t/t9003-help-autocorrect.sh | 51 +++++-------------------------------- 2 files changed, 23 insertions(+), 77 deletions(-) diff --git a/help.c b/help.c index 30f32a7206..9ea4c076e1 100644 --- a/help.c +++ b/help.c @@ -739,7 +739,16 @@ char *help_unknown_cmd(const char *cmd, const struct strvec *args) else if (cfg.autocorrect == AUTOCORRECT_PROMPT) { char *answer; struct strbuf msg = STRBUF_INIT; - strbuf_addf(&msg, _("Run '%s' instead [y/N]? "), assumed); + struct strbuf full_cmd = STRBUF_INIT; + strbuf_addstr(&full_cmd, assumed); + if (args) { + for (size_t j = 1; j < args->nr; j++) { + strbuf_addch(&full_cmd, ' '); + strbuf_addstr(&full_cmd, args->v[j]); + } + } + strbuf_addf(&msg, _("Run 'git %s' instead [y/N]? "), full_cmd.buf); + strbuf_release(&full_cmd); answer = git_prompt(msg.buf, PROMPT_ECHO); strbuf_release(&msg); if (!(starts_with(answer, "y") || @@ -762,37 +771,13 @@ char *help_unknown_cmd(const char *cmd, const struct strvec *args) fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd); if (SIMILAR_ENOUGH(best_similarity)) { - if (n == 1 && isatty(0) && isatty(2)) { - char *answer; - struct strbuf msg = STRBUF_INIT; - struct strbuf full_cmd = STRBUF_INIT; - strbuf_addstr(&full_cmd, main_cmds.names[0]->name); - if (args) { - for (size_t j = 1; j < args->nr; j++) { - strbuf_addch(&full_cmd, ' '); - strbuf_addstr(&full_cmd, args->v[j]); - } - } - strbuf_addf(&msg, _("\nDid you mean 'git %s'? [y/N] "), - full_cmd.buf); - strbuf_release(&full_cmd); - answer = git_prompt(msg.buf, PROMPT_ECHO); - strbuf_release(&msg); - if (starts_with(answer, "y") || starts_with(answer, "Y")) { - char *assumed = xstrdup(main_cmds.names[0]->name); - cmdnames_release(&cfg.aliases); - cmdnames_release(&main_cmds); - cmdnames_release(&other_cmds); - return assumed; - } - } else { - fprintf_ln(stderr, - Q_("\nThe most similar command is", - "\nThe most similar commands are", - n)); - for (i = 0; i < n; i++) - fprintf(stderr, "\t%s\n", main_cmds.names[i]->name); - } + fprintf_ln(stderr, + Q_("\nThe most similar command is", + "\nThe most similar commands are", + n)); + + for (i = 0; i < n; i++) + fprintf(stderr, "\t%s\n", main_cmds.names[i]->name); } exit(1); diff --git a/t/t9003-help-autocorrect.sh b/t/t9003-help-autocorrect.sh index 6fe2da1595..75821d63e1 100755 --- a/t/t9003-help-autocorrect.sh +++ b/t/t9003-help-autocorrect.sh @@ -70,57 +70,18 @@ test_expect_success 'autocorrect works in work tree created from bare repo' ' git -C worktree -c help.autocorrect=immediate status ' -# Default behaviour (no help.autocorrect set): when there is exactly one -# similar command but the session is non-interactive, fall back to printing -# the suggestion list and exiting rather than showing a prompt. -test_expect_success 'default: single match non-interactive shows suggestion and fails' ' - test_might_fail git config --unset help.autocorrect && - - test_must_fail git lfg 2>actual && - grep "most similar command" actual && - grep "lgf" actual -' - -test_expect_success 'default: multiple matches non-interactive shows list and fails' ' - test_might_fail git config --unset help.autocorrect && - - test_must_fail git com 2>actual && - grep "most similar commands" actual && - grep "commit" actual -' - -# Interactive prompt tests require a real TTY. On macOS the TTY prereq is -# skipped due to IO::Pty reliability issues; these tests run on Linux CI. -test_expect_success TTY 'default: single match interactive, answer y runs command' ' - git config --unset help.autocorrect && - - write_script git-typotest <<-\EOF && - echo typotest-ran - EOF - PATH="$PATH:." export PATH && - - # Feed "y" to /dev/tty via a wrapper that answers the prompt - write_script answer-prompt <<-\EOF && - # Write the answer to the controlling terminal - printf "y\n" >/dev/tty - exec "$@" - EOF - - test_terminal ./answer-prompt git typotest 2>err >out && - grep "typotest-ran" out && - grep "Did you mean" err -' - -test_expect_success TTY 'default: single match interactive, answer n exits cleanly' ' - git config --unset help.autocorrect && +# autocorrect=prompt should include the original arguments in the prompt. +# Requires a TTY; skipped on macOS due to IO::Pty reliability issues. +test_expect_success TTY 'autocorrect=prompt includes arguments in prompt' ' + git config help.autocorrect prompt && write_script answer-prompt-no <<-\EOF && printf "n\n" >/dev/tty exec "$@" EOF - test_must_fail test_terminal ./answer-prompt-no git typotest 2>err && - grep "Did you mean" err + test_must_fail test_terminal ./answer-prompt-no git lfg --oneline 2>actual && + grep "lgf --oneline" actual ' test_done -- 2.50.1