Skip to content

Commit 94aa96c

Browse files
pks-tgitster
authored andcommitted
help: refactor to not use globals for reading config
We're reading the "help.autocorrect" and "alias.*" configuration into global variables, which makes it hard to manage their lifetime correctly. Refactor the code to use a struct instead. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 58e7568 commit 94aa96c

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

help.c

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -546,37 +546,40 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
546546
return 0;
547547
}
548548

549-
static int autocorrect;
550-
static struct cmdnames aliases;
549+
struct help_unknown_cmd_config {
550+
int autocorrect;
551+
struct cmdnames aliases;
552+
};
551553

552554
#define AUTOCORRECT_PROMPT (-3)
553555
#define AUTOCORRECT_NEVER (-2)
554556
#define AUTOCORRECT_IMMEDIATELY (-1)
555557

556558
static int git_unknown_cmd_config(const char *var, const char *value,
557559
const struct config_context *ctx,
558-
void *cb UNUSED)
560+
void *cb)
559561
{
562+
struct help_unknown_cmd_config *cfg = cb;
560563
const char *p;
561564

562565
if (!strcmp(var, "help.autocorrect")) {
563566
if (!value)
564567
return config_error_nonbool(var);
565568
if (!strcmp(value, "never")) {
566-
autocorrect = AUTOCORRECT_NEVER;
569+
cfg->autocorrect = AUTOCORRECT_NEVER;
567570
} else if (!strcmp(value, "immediate")) {
568-
autocorrect = AUTOCORRECT_IMMEDIATELY;
571+
cfg->autocorrect = AUTOCORRECT_IMMEDIATELY;
569572
} else if (!strcmp(value, "prompt")) {
570-
autocorrect = AUTOCORRECT_PROMPT;
573+
cfg->autocorrect = AUTOCORRECT_PROMPT;
571574
} else {
572575
int v = git_config_int(var, value, ctx->kvi);
573-
autocorrect = (v < 0)
576+
cfg->autocorrect = (v < 0)
574577
? AUTOCORRECT_IMMEDIATELY : v;
575578
}
576579
}
577580
/* Also use aliases for command lookup */
578581
if (skip_prefix(var, "alias.", &p))
579-
add_cmdname(&aliases, p, strlen(p));
582+
add_cmdname(&cfg->aliases, p, strlen(p));
580583

581584
return 0;
582585
}
@@ -611,30 +614,28 @@ static const char bad_interpreter_advice[] =
611614

612615
const char *help_unknown_cmd(const char *cmd)
613616
{
617+
struct help_unknown_cmd_config cfg = { 0 };
614618
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 };
616621
struct cmdname_help *common_cmds;
617622

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);
623624

624625
/*
625626
* Disable autocorrection prompt in a non-interactive session
626627
*/
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;
629630

630-
if (autocorrect == AUTOCORRECT_NEVER) {
631+
if (cfg.autocorrect == AUTOCORRECT_NEVER) {
631632
fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
632633
exit(1);
633634
}
634635

635636
load_command_list("git-", &main_cmds, &other_cmds);
636637

637-
add_cmd_list(&main_cmds, &aliases);
638+
add_cmd_list(&main_cmds, &cfg.aliases);
638639
add_cmd_list(&main_cmds, &other_cmds);
639640
QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
640641
uniq(&main_cmds);
@@ -693,20 +694,20 @@ const char *help_unknown_cmd(const char *cmd)
693694
n++)
694695
; /* still counting */
695696
}
696-
if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
697+
if (cfg.autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
697698
const char *assumed = main_cmds.names[0]->name;
698699
main_cmds.names[0] = NULL;
699700
cmdnames_release(&main_cmds);
700701
fprintf_ln(stderr,
701702
_("WARNING: You called a Git command named '%s', "
702703
"which does not exist."),
703704
cmd);
704-
if (autocorrect == AUTOCORRECT_IMMEDIATELY)
705+
if (cfg.autocorrect == AUTOCORRECT_IMMEDIATELY)
705706
fprintf_ln(stderr,
706707
_("Continuing under the assumption that "
707708
"you meant '%s'."),
708709
assumed);
709-
else if (autocorrect == AUTOCORRECT_PROMPT) {
710+
else if (cfg.autocorrect == AUTOCORRECT_PROMPT) {
710711
char *answer;
711712
struct strbuf msg = STRBUF_INIT;
712713
strbuf_addf(&msg, _("Run '%s' instead [y/N]? "), assumed);
@@ -719,8 +720,8 @@ const char *help_unknown_cmd(const char *cmd)
719720
fprintf_ln(stderr,
720721
_("Continuing in %0.1f seconds, "
721722
"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);
724725
}
725726
return assumed;
726727
}

0 commit comments

Comments
 (0)