11
11
#include "version.h"
12
12
#include "refs.h"
13
13
#include "parse-options.h"
14
+ #include "prompt.h"
14
15
15
16
struct category_description {
16
17
uint32_t category ;
@@ -472,6 +473,7 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
472
473
static int autocorrect ;
473
474
static struct cmdnames aliases ;
474
475
476
+ #define AUTOCORRECT_PROMPT (-3)
475
477
#define AUTOCORRECT_NEVER (-2)
476
478
#define AUTOCORRECT_IMMEDIATELY (-1)
477
479
@@ -486,6 +488,8 @@ static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
486
488
autocorrect = AUTOCORRECT_NEVER ;
487
489
} else if (!strcmp (value , "immediate" )) {
488
490
autocorrect = AUTOCORRECT_IMMEDIATELY ;
491
+ } else if (!strcmp (value , "prompt" )) {
492
+ autocorrect = AUTOCORRECT_PROMPT ;
489
493
} else {
490
494
int v = git_config_int (var , value );
491
495
autocorrect = (v < 0 )
@@ -539,6 +543,12 @@ const char *help_unknown_cmd(const char *cmd)
539
543
540
544
read_early_config (git_unknown_cmd_config , NULL );
541
545
546
+ /*
547
+ * Disable autocorrection prompt in a non-interactive session
548
+ */
549
+ if ((autocorrect == AUTOCORRECT_PROMPT ) && (!isatty (0 ) || !isatty (2 )))
550
+ autocorrect = AUTOCORRECT_NEVER ;
551
+
542
552
if (autocorrect == AUTOCORRECT_NEVER ) {
543
553
fprintf_ln (stderr , _ ("git: '%s' is not a git command. See 'git --help'." ), cmd );
544
554
exit (1 );
@@ -618,7 +628,16 @@ const char *help_unknown_cmd(const char *cmd)
618
628
_ ("Continuing under the assumption that "
619
629
"you meant '%s'." ),
620
630
assumed );
621
- else {
631
+ else if (autocorrect == AUTOCORRECT_PROMPT ) {
632
+ char * answer ;
633
+ struct strbuf msg = STRBUF_INIT ;
634
+ strbuf_addf (& msg , _ ("Run '%s' instead? (y/N)" ), assumed );
635
+ answer = git_prompt (msg .buf , PROMPT_ECHO );
636
+ strbuf_release (& msg );
637
+ if (!(starts_with (answer , "y" ) ||
638
+ starts_with (answer , "Y" )))
639
+ exit (1 );
640
+ } else {
622
641
fprintf_ln (stderr ,
623
642
_ ("Continuing in %0.1f seconds, "
624
643
"assuming that you meant '%s'." ),
0 commit comments