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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

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