help: move tty check for autocorrection to autocorrect.c

TTY checking is the autocorrect config parser's responsibility. It must
ensure the parsed value is correct and reliable. Thus, move the check to
autocorrect_resolve_config().

Signed-off-by: Jiamu Sun <39@barroit.sh>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jiamu Sun
2026-03-17 00:36:16 +09:00
committed by Junio C Hamano
parent e0245a1169
commit 916b96c0ec
2 changed files with 16 additions and 14 deletions

View File

@@ -33,18 +33,26 @@ void autocorrect_resolve_config(const char *var, const char *value,
const struct config_context *ctx, void *data)
{
int *out = data;
int parsed;
if (!strcmp(var, "help.autocorrect")) {
int v = parse_autocorrect(value);
if (strcmp(var, "help.autocorrect"))
return;
if (!v) {
v = git_config_int(var, value, ctx->kvi);
if (v < 0 || v == 1)
v = AUTOCORRECT_IMMEDIATELY;
}
parsed = parse_autocorrect(value);
*out = v;
/*
* Disable autocorrection prompt in a non-interactive session
*/
if (parsed == AUTOCORRECT_PROMPT && (!isatty(0) || !isatty(2)))
parsed = AUTOCORRECT_NEVER;
if (!parsed) {
parsed = git_config_int(var, value, ctx->kvi);
if (parsed < 0 || parsed == 1)
parsed = AUTOCORRECT_IMMEDIATELY;
}
*out = parsed;
}
void autocorrect_confirm(int autocorrect, const char *assumed)

6
help.c
View File

@@ -607,12 +607,6 @@ char *help_unknown_cmd(const char *cmd)
read_early_config(the_repository, git_unknown_cmd_config, &cfg);
/*
* Disable autocorrection prompt in a non-interactive session
*/
if ((cfg.autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))
cfg.autocorrect = AUTOCORRECT_NEVER;
if (cfg.autocorrect == AUTOCORRECT_NEVER) {
fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
exit(1);