Skip to content

Commit e0245a1

Browse files
barroitgitster
authored andcommitted
help: make autocorrect handling reusable
Move config parsing and prompt/delay handling into autocorrect.c and expose them in autocorrect.h. This makes autocorrect reusable regardless of which target links against it. Signed-off-by: Jiamu Sun <39@barroit.sh> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 386fe44 commit e0245a1

5 files changed

Lines changed: 94 additions & 60 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,7 @@ LIB_OBJS += archive-tar.o
10981098
LIB_OBJS += archive-zip.o
10991099
LIB_OBJS += archive.o
11001100
LIB_OBJS += attr.o
1101+
LIB_OBJS += autocorrect.o
11011102
LIB_OBJS += base85.o
11021103
LIB_OBJS += bisect.o
11031104
LIB_OBJS += blame.o

autocorrect.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "git-compat-util.h"
2+
#include "autocorrect.h"
3+
#include "config.h"
4+
#include "parse.h"
5+
#include "strbuf.h"
6+
#include "prompt.h"
7+
#include "gettext.h"
8+
9+
static int parse_autocorrect(const char *value)
10+
{
11+
switch (git_parse_maybe_bool_text(value)) {
12+
case 1:
13+
return AUTOCORRECT_IMMEDIATELY;
14+
case 0:
15+
return AUTOCORRECT_SHOW;
16+
default: /* other random text */
17+
break;
18+
}
19+
20+
if (!strcmp(value, "prompt"))
21+
return AUTOCORRECT_PROMPT;
22+
if (!strcmp(value, "never"))
23+
return AUTOCORRECT_NEVER;
24+
if (!strcmp(value, "immediate"))
25+
return AUTOCORRECT_IMMEDIATELY;
26+
if (!strcmp(value, "show"))
27+
return AUTOCORRECT_SHOW;
28+
29+
return 0;
30+
}
31+
32+
void autocorrect_resolve_config(const char *var, const char *value,
33+
const struct config_context *ctx, void *data)
34+
{
35+
int *out = data;
36+
37+
if (!strcmp(var, "help.autocorrect")) {
38+
int v = parse_autocorrect(value);
39+
40+
if (!v) {
41+
v = git_config_int(var, value, ctx->kvi);
42+
if (v < 0 || v == 1)
43+
v = AUTOCORRECT_IMMEDIATELY;
44+
}
45+
46+
*out = v;
47+
}
48+
}
49+
50+
void autocorrect_confirm(int autocorrect, const char *assumed)
51+
{
52+
if (autocorrect == AUTOCORRECT_IMMEDIATELY) {
53+
fprintf_ln(stderr,
54+
_("Continuing under the assumption that you meant '%s'."),
55+
assumed);
56+
} else if (autocorrect == AUTOCORRECT_PROMPT) {
57+
char *answer;
58+
struct strbuf msg = STRBUF_INIT;
59+
60+
strbuf_addf(&msg, _("Run '%s' instead [y/N]? "), assumed);
61+
answer = git_prompt(msg.buf, PROMPT_ECHO);
62+
strbuf_release(&msg);
63+
64+
if (!(starts_with(answer, "y") || starts_with(answer, "Y")))
65+
exit(1);
66+
} else {
67+
fprintf_ln(stderr,
68+
_("Continuing in %0.1f seconds, assuming that you meant '%s'."),
69+
(float)autocorrect / 10.0, assumed);
70+
sleep_millisec(autocorrect * 100);
71+
}
72+
}

autocorrect.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef AUTOCORRECT_H
2+
#define AUTOCORRECT_H
3+
4+
#define AUTOCORRECT_SHOW (-4)
5+
#define AUTOCORRECT_PROMPT (-3)
6+
#define AUTOCORRECT_NEVER (-2)
7+
#define AUTOCORRECT_IMMEDIATELY (-1)
8+
9+
struct config_context;
10+
11+
void autocorrect_resolve_config(const char *var, const char *value,
12+
const struct config_context *ctx, void *data);
13+
14+
void autocorrect_confirm(int autocorrect, const char *assumed);
15+
16+
#endif /* AUTOCORRECT_H */

help.c

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "repository.h"
2323
#include "alias.h"
2424
#include "utf8.h"
25+
#include "autocorrect.h"
2526

2627
#ifndef NO_CURL
2728
#include "git-curl-compat.h" /* For LIBCURL_VERSION only */
@@ -541,34 +542,6 @@ struct help_unknown_cmd_config {
541542
struct cmdnames aliases;
542543
};
543544

544-
#define AUTOCORRECT_SHOW (-4)
545-
#define AUTOCORRECT_PROMPT (-3)
546-
#define AUTOCORRECT_NEVER (-2)
547-
#define AUTOCORRECT_IMMEDIATELY (-1)
548-
549-
static int parse_autocorrect(const char *value)
550-
{
551-
switch (git_parse_maybe_bool_text(value)) {
552-
case 1:
553-
return AUTOCORRECT_IMMEDIATELY;
554-
case 0:
555-
return AUTOCORRECT_SHOW;
556-
default: /* other random text */
557-
break;
558-
}
559-
560-
if (!strcmp(value, "prompt"))
561-
return AUTOCORRECT_PROMPT;
562-
if (!strcmp(value, "never"))
563-
return AUTOCORRECT_NEVER;
564-
if (!strcmp(value, "immediate"))
565-
return AUTOCORRECT_IMMEDIATELY;
566-
if (!strcmp(value, "show"))
567-
return AUTOCORRECT_SHOW;
568-
569-
return 0;
570-
}
571-
572545
static int git_unknown_cmd_config(const char *var, const char *value,
573546
const struct config_context *ctx,
574547
void *cb)
@@ -577,17 +550,7 @@ static int git_unknown_cmd_config(const char *var, const char *value,
577550
const char *subsection, *key;
578551
size_t subsection_len;
579552

580-
if (!strcmp(var, "help.autocorrect")) {
581-
int v = parse_autocorrect(value);
582-
583-
if (!v) {
584-
v = git_config_int(var, value, ctx->kvi);
585-
if (v < 0 || v == 1)
586-
v = AUTOCORRECT_IMMEDIATELY;
587-
}
588-
589-
cfg->autocorrect = v;
590-
}
553+
autocorrect_resolve_config(var, value, ctx, &cfg->autocorrect);
591554

592555
/* Also use aliases for command lookup */
593556
if (!parse_config_key(var, "alias", &subsection, &subsection_len,
@@ -724,27 +687,8 @@ char *help_unknown_cmd(const char *cmd)
724687
_("WARNING: You called a Git command named '%s', "
725688
"which does not exist."),
726689
cmd);
727-
if (cfg.autocorrect == AUTOCORRECT_IMMEDIATELY)
728-
fprintf_ln(stderr,
729-
_("Continuing under the assumption that "
730-
"you meant '%s'."),
731-
assumed);
732-
else if (cfg.autocorrect == AUTOCORRECT_PROMPT) {
733-
char *answer;
734-
struct strbuf msg = STRBUF_INIT;
735-
strbuf_addf(&msg, _("Run '%s' instead [y/N]? "), assumed);
736-
answer = git_prompt(msg.buf, PROMPT_ECHO);
737-
strbuf_release(&msg);
738-
if (!(starts_with(answer, "y") ||
739-
starts_with(answer, "Y")))
740-
exit(1);
741-
} else {
742-
fprintf_ln(stderr,
743-
_("Continuing in %0.1f seconds, "
744-
"assuming that you meant '%s'."),
745-
(float)cfg.autocorrect/10.0, assumed);
746-
sleep_millisec(cfg.autocorrect * 100);
747-
}
690+
691+
autocorrect_confirm(cfg.autocorrect, assumed);
748692

749693
cmdnames_release(&cfg.aliases);
750694
cmdnames_release(&main_cmds);

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ libgit_sources = [
283283
'archive-zip.c',
284284
'archive.c',
285285
'attr.c',
286+
'autocorrect.c',
286287
'base85.c',
287288
'bisect.c',
288289
'blame.c',

0 commit comments

Comments
 (0)