@@ -546,37 +546,40 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
546
546
return 0 ;
547
547
}
548
548
549
- static int autocorrect ;
550
- static struct cmdnames aliases ;
549
+ struct help_unknown_cmd_config {
550
+ int autocorrect ;
551
+ struct cmdnames aliases ;
552
+ };
551
553
552
554
#define AUTOCORRECT_PROMPT (-3)
553
555
#define AUTOCORRECT_NEVER (-2)
554
556
#define AUTOCORRECT_IMMEDIATELY (-1)
555
557
556
558
static int git_unknown_cmd_config (const char * var , const char * value ,
557
559
const struct config_context * ctx ,
558
- void * cb UNUSED )
560
+ void * cb )
559
561
{
562
+ struct help_unknown_cmd_config * cfg = cb ;
560
563
const char * p ;
561
564
562
565
if (!strcmp (var , "help.autocorrect" )) {
563
566
if (!value )
564
567
return config_error_nonbool (var );
565
568
if (!strcmp (value , "never" )) {
566
- autocorrect = AUTOCORRECT_NEVER ;
569
+ cfg -> autocorrect = AUTOCORRECT_NEVER ;
567
570
} else if (!strcmp (value , "immediate" )) {
568
- autocorrect = AUTOCORRECT_IMMEDIATELY ;
571
+ cfg -> autocorrect = AUTOCORRECT_IMMEDIATELY ;
569
572
} else if (!strcmp (value , "prompt" )) {
570
- autocorrect = AUTOCORRECT_PROMPT ;
573
+ cfg -> autocorrect = AUTOCORRECT_PROMPT ;
571
574
} else {
572
575
int v = git_config_int (var , value , ctx -> kvi );
573
- autocorrect = (v < 0 )
576
+ cfg -> autocorrect = (v < 0 )
574
577
? AUTOCORRECT_IMMEDIATELY : v ;
575
578
}
576
579
}
577
580
/* Also use aliases for command lookup */
578
581
if (skip_prefix (var , "alias." , & p ))
579
- add_cmdname (& aliases , p , strlen (p ));
582
+ add_cmdname (& cfg -> aliases , p , strlen (p ));
580
583
581
584
return 0 ;
582
585
}
@@ -611,30 +614,28 @@ static const char bad_interpreter_advice[] =
611
614
612
615
const char * help_unknown_cmd (const char * cmd )
613
616
{
617
+ struct help_unknown_cmd_config cfg = { 0 };
614
618
int i , n , best_similarity = 0 ;
615
- struct cmdnames main_cmds , other_cmds ;
619
+ struct cmdnames main_cmds = { 0 };
620
+ struct cmdnames other_cmds = { 0 };
616
621
struct cmdname_help * common_cmds ;
617
622
618
- memset (& main_cmds , 0 , sizeof (main_cmds ));
619
- memset (& other_cmds , 0 , sizeof (other_cmds ));
620
- memset (& aliases , 0 , sizeof (aliases ));
621
-
622
- read_early_config (the_repository , git_unknown_cmd_config , NULL );
623
+ read_early_config (the_repository , git_unknown_cmd_config , & cfg );
623
624
624
625
/*
625
626
* Disable autocorrection prompt in a non-interactive session
626
627
*/
627
- if ((autocorrect == AUTOCORRECT_PROMPT ) && (!isatty (0 ) || !isatty (2 )))
628
- autocorrect = AUTOCORRECT_NEVER ;
628
+ if ((cfg . autocorrect == AUTOCORRECT_PROMPT ) && (!isatty (0 ) || !isatty (2 )))
629
+ cfg . autocorrect = AUTOCORRECT_NEVER ;
629
630
630
- if (autocorrect == AUTOCORRECT_NEVER ) {
631
+ if (cfg . autocorrect == AUTOCORRECT_NEVER ) {
631
632
fprintf_ln (stderr , _ ("git: '%s' is not a git command. See 'git --help'." ), cmd );
632
633
exit (1 );
633
634
}
634
635
635
636
load_command_list ("git-" , & main_cmds , & other_cmds );
636
637
637
- add_cmd_list (& main_cmds , & aliases );
638
+ add_cmd_list (& main_cmds , & cfg . aliases );
638
639
add_cmd_list (& main_cmds , & other_cmds );
639
640
QSORT (main_cmds .names , main_cmds .cnt , cmdname_compare );
640
641
uniq (& main_cmds );
@@ -693,20 +694,20 @@ const char *help_unknown_cmd(const char *cmd)
693
694
n ++ )
694
695
; /* still counting */
695
696
}
696
- if (autocorrect && n == 1 && SIMILAR_ENOUGH (best_similarity )) {
697
+ if (cfg . autocorrect && n == 1 && SIMILAR_ENOUGH (best_similarity )) {
697
698
const char * assumed = main_cmds .names [0 ]-> name ;
698
699
main_cmds .names [0 ] = NULL ;
699
700
cmdnames_release (& main_cmds );
700
701
fprintf_ln (stderr ,
701
702
_ ("WARNING: You called a Git command named '%s', "
702
703
"which does not exist." ),
703
704
cmd );
704
- if (autocorrect == AUTOCORRECT_IMMEDIATELY )
705
+ if (cfg . autocorrect == AUTOCORRECT_IMMEDIATELY )
705
706
fprintf_ln (stderr ,
706
707
_ ("Continuing under the assumption that "
707
708
"you meant '%s'." ),
708
709
assumed );
709
- else if (autocorrect == AUTOCORRECT_PROMPT ) {
710
+ else if (cfg . autocorrect == AUTOCORRECT_PROMPT ) {
710
711
char * answer ;
711
712
struct strbuf msg = STRBUF_INIT ;
712
713
strbuf_addf (& msg , _ ("Run '%s' instead [y/N]? " ), assumed );
@@ -719,8 +720,8 @@ const char *help_unknown_cmd(const char *cmd)
719
720
fprintf_ln (stderr ,
720
721
_ ("Continuing in %0.1f seconds, "
721
722
"assuming that you meant '%s'." ),
722
- (float )autocorrect /10.0 , assumed );
723
- sleep_millisec (autocorrect * 100 );
723
+ (float )cfg . autocorrect /10.0 , assumed );
724
+ sleep_millisec (cfg . autocorrect * 100 );
724
725
}
725
726
return assumed ;
726
727
}
0 commit comments