Skip to content

Commit 916b96c

Browse files
barroitgitster
authored andcommitted
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>
1 parent e0245a1 commit 916b96c

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

autocorrect.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,26 @@ void autocorrect_resolve_config(const char *var, const char *value,
3333
const struct config_context *ctx, void *data)
3434
{
3535
int *out = data;
36+
int parsed;
3637

37-
if (!strcmp(var, "help.autocorrect")) {
38-
int v = parse_autocorrect(value);
38+
if (strcmp(var, "help.autocorrect"))
39+
return;
3940

40-
if (!v) {
41-
v = git_config_int(var, value, ctx->kvi);
42-
if (v < 0 || v == 1)
43-
v = AUTOCORRECT_IMMEDIATELY;
44-
}
41+
parsed = parse_autocorrect(value);
4542

46-
*out = v;
43+
/*
44+
* Disable autocorrection prompt in a non-interactive session
45+
*/
46+
if (parsed == AUTOCORRECT_PROMPT && (!isatty(0) || !isatty(2)))
47+
parsed = AUTOCORRECT_NEVER;
48+
49+
if (!parsed) {
50+
parsed = git_config_int(var, value, ctx->kvi);
51+
if (parsed < 0 || parsed == 1)
52+
parsed = AUTOCORRECT_IMMEDIATELY;
4753
}
54+
55+
*out = parsed;
4856
}
4957

5058
void autocorrect_confirm(int autocorrect, const char *assumed)

help.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -607,12 +607,6 @@ char *help_unknown_cmd(const char *cmd)
607607

608608
read_early_config(the_repository, git_unknown_cmd_config, &cfg);
609609

610-
/*
611-
* Disable autocorrection prompt in a non-interactive session
612-
*/
613-
if ((cfg.autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))
614-
cfg.autocorrect = AUTOCORRECT_NEVER;
615-
616610
if (cfg.autocorrect == AUTOCORRECT_NEVER) {
617611
fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
618612
exit(1);

0 commit comments

Comments
 (0)