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>
This commit is contained in:
Patrick Steinhardt 2024-11-20 14:39:43 +01:00 committed by Junio C Hamano
parent 58e7568c61
commit 94aa96cd59

47
help.c
View File

@ -546,8 +546,10 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
return 0; return 0;
} }
static int autocorrect; struct help_unknown_cmd_config {
static struct cmdnames aliases; int autocorrect;
struct cmdnames aliases;
};
#define AUTOCORRECT_PROMPT (-3) #define AUTOCORRECT_PROMPT (-3)
#define AUTOCORRECT_NEVER (-2) #define AUTOCORRECT_NEVER (-2)
@ -555,28 +557,29 @@ static struct cmdnames aliases;
static int git_unknown_cmd_config(const char *var, const char *value, static int git_unknown_cmd_config(const char *var, const char *value,
const struct config_context *ctx, const struct config_context *ctx,
void *cb UNUSED) void *cb)
{ {
struct help_unknown_cmd_config *cfg = cb;
const char *p; const char *p;
if (!strcmp(var, "help.autocorrect")) { if (!strcmp(var, "help.autocorrect")) {
if (!value) if (!value)
return config_error_nonbool(var); return config_error_nonbool(var);
if (!strcmp(value, "never")) { if (!strcmp(value, "never")) {
autocorrect = AUTOCORRECT_NEVER; cfg->autocorrect = AUTOCORRECT_NEVER;
} else if (!strcmp(value, "immediate")) { } else if (!strcmp(value, "immediate")) {
autocorrect = AUTOCORRECT_IMMEDIATELY; cfg->autocorrect = AUTOCORRECT_IMMEDIATELY;
} else if (!strcmp(value, "prompt")) { } else if (!strcmp(value, "prompt")) {
autocorrect = AUTOCORRECT_PROMPT; cfg->autocorrect = AUTOCORRECT_PROMPT;
} else { } else {
int v = git_config_int(var, value, ctx->kvi); int v = git_config_int(var, value, ctx->kvi);
autocorrect = (v < 0) cfg->autocorrect = (v < 0)
? AUTOCORRECT_IMMEDIATELY : v; ? AUTOCORRECT_IMMEDIATELY : v;
} }
} }
/* Also use aliases for command lookup */ /* Also use aliases for command lookup */
if (skip_prefix(var, "alias.", &p)) if (skip_prefix(var, "alias.", &p))
add_cmdname(&aliases, p, strlen(p)); add_cmdname(&cfg->aliases, p, strlen(p));
return 0; return 0;
} }
@ -611,30 +614,28 @@ static const char bad_interpreter_advice[] =
const char *help_unknown_cmd(const char *cmd) const char *help_unknown_cmd(const char *cmd)
{ {
struct help_unknown_cmd_config cfg = { 0 };
int i, n, best_similarity = 0; int i, n, best_similarity = 0;
struct cmdnames main_cmds, other_cmds; struct cmdnames main_cmds = { 0 };
struct cmdnames other_cmds = { 0 };
struct cmdname_help *common_cmds; struct cmdname_help *common_cmds;
memset(&main_cmds, 0, sizeof(main_cmds)); read_early_config(the_repository, git_unknown_cmd_config, &cfg);
memset(&other_cmds, 0, sizeof(other_cmds));
memset(&aliases, 0, sizeof(aliases));
read_early_config(the_repository, git_unknown_cmd_config, NULL);
/* /*
* Disable autocorrection prompt in a non-interactive session * Disable autocorrection prompt in a non-interactive session
*/ */
if ((autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2))) if ((cfg.autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))
autocorrect = AUTOCORRECT_NEVER; cfg.autocorrect = AUTOCORRECT_NEVER;
if (autocorrect == AUTOCORRECT_NEVER) { if (cfg.autocorrect == AUTOCORRECT_NEVER) {
fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd); fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
exit(1); exit(1);
} }
load_command_list("git-", &main_cmds, &other_cmds); load_command_list("git-", &main_cmds, &other_cmds);
add_cmd_list(&main_cmds, &aliases); add_cmd_list(&main_cmds, &cfg.aliases);
add_cmd_list(&main_cmds, &other_cmds); add_cmd_list(&main_cmds, &other_cmds);
QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare); QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
uniq(&main_cmds); uniq(&main_cmds);
@ -693,7 +694,7 @@ const char *help_unknown_cmd(const char *cmd)
n++) n++)
; /* still counting */ ; /* still counting */
} }
if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) { if (cfg.autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
const char *assumed = main_cmds.names[0]->name; const char *assumed = main_cmds.names[0]->name;
main_cmds.names[0] = NULL; main_cmds.names[0] = NULL;
cmdnames_release(&main_cmds); cmdnames_release(&main_cmds);
@ -701,12 +702,12 @@ const char *help_unknown_cmd(const char *cmd)
_("WARNING: You called a Git command named '%s', " _("WARNING: You called a Git command named '%s', "
"which does not exist."), "which does not exist."),
cmd); cmd);
if (autocorrect == AUTOCORRECT_IMMEDIATELY) if (cfg.autocorrect == AUTOCORRECT_IMMEDIATELY)
fprintf_ln(stderr, fprintf_ln(stderr,
_("Continuing under the assumption that " _("Continuing under the assumption that "
"you meant '%s'."), "you meant '%s'."),
assumed); assumed);
else if (autocorrect == AUTOCORRECT_PROMPT) { else if (cfg.autocorrect == AUTOCORRECT_PROMPT) {
char *answer; char *answer;
struct strbuf msg = STRBUF_INIT; struct strbuf msg = STRBUF_INIT;
strbuf_addf(&msg, _("Run '%s' instead [y/N]? "), assumed); strbuf_addf(&msg, _("Run '%s' instead [y/N]? "), assumed);
@ -719,8 +720,8 @@ const char *help_unknown_cmd(const char *cmd)
fprintf_ln(stderr, fprintf_ln(stderr,
_("Continuing in %0.1f seconds, " _("Continuing in %0.1f seconds, "
"assuming that you meant '%s'."), "assuming that you meant '%s'."),
(float)autocorrect/10.0, assumed); (float)cfg.autocorrect/10.0, assumed);
sleep_millisec(autocorrect * 100); sleep_millisec(cfg.autocorrect * 100);
} }
return assumed; return assumed;
} }