Skip to content

Commit 7cd07f1

Browse files
barroitgitster
authored andcommitted
autocorrect: provide config resolution API
Add autocorrect_resolve(). This resolves and populates the correct values for autocorrect config. Make autocorrect config callback internal. The API is meant to provide a high-level way to retrieve the config. Allowing access to the config callback from outside violates that intent. Additionally, in some cases, without access to the config callback, two config iterations cannot be merged into one, which can hurt performance. This is fine, as the code path that calls autocorrect_resolve() is cold. Signed-off-by: Jiamu Sun <39@barroit.sh> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent f06f1f0 commit 7cd07f1

3 files changed

Lines changed: 30 additions & 30 deletions

File tree

autocorrect.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#define USE_THE_REPOSITORY_VARIABLE
2+
13
#include "git-compat-util.h"
24
#include "autocorrect.h"
35
#include "config.h"
@@ -29,13 +31,13 @@ static enum autocorrect_mode parse_autocorrect(const char *value)
2931
return AUTOCORRECT_DELAY;
3032
}
3133

32-
void autocorrect_resolve_config(const char *var, const char *value,
33-
const struct config_context *ctx, void *data)
34+
static int resolve_autocorrect(const char *var, const char *value,
35+
const struct config_context *ctx, void *data)
3436
{
3537
struct autocorrect *conf = data;
3638

3739
if (strcmp(var, "help.autocorrect"))
38-
return;
40+
return 0;
3941

4042
conf->mode = parse_autocorrect(value);
4143

@@ -53,6 +55,13 @@ void autocorrect_resolve_config(const char *var, const char *value,
5355
else if (conf->delay < 0 || conf->delay == 1)
5456
conf->mode = AUTOCORRECT_IMMEDIATELY;
5557
}
58+
59+
return 0;
60+
}
61+
62+
void autocorrect_resolve(struct autocorrect *conf)
63+
{
64+
read_early_config(the_repository, resolve_autocorrect, conf);
5665
}
5766

5867
void autocorrect_confirm(struct autocorrect *conf, const char *assumed)

autocorrect.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef AUTOCORRECT_H
22
#define AUTOCORRECT_H
33

4-
struct config_context;
5-
64
enum autocorrect_mode {
75
AUTOCORRECT_HINT,
86
AUTOCORRECT_NEVER,
@@ -16,8 +14,7 @@ struct autocorrect {
1614
int delay;
1715
};
1816

19-
void autocorrect_resolve_config(const char *var, const char *value,
20-
const struct config_context *ctx, void *data);
17+
void autocorrect_resolve(struct autocorrect *conf);
2118

2219
void autocorrect_confirm(struct autocorrect *conf, const char *assumed);
2320

help.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -537,32 +537,23 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
537537
return 0;
538538
}
539539

540-
struct help_unknown_cmd_config {
541-
struct autocorrect autocorrect;
542-
struct cmdnames aliases;
543-
};
544-
545-
static int git_unknown_cmd_config(const char *var, const char *value,
546-
const struct config_context *ctx,
547-
void *cb)
540+
static int resolve_aliases(const char *var, const char *value UNUSED,
541+
const struct config_context *ctx UNUSED, void *data)
548542
{
549-
struct help_unknown_cmd_config *cfg = cb;
543+
struct cmdnames *aliases = data;
550544
const char *subsection, *key;
551545
size_t subsection_len;
552546

553-
autocorrect_resolve_config(var, value, ctx, &cfg->autocorrect);
554-
555-
/* Also use aliases for command lookup */
556547
if (!parse_config_key(var, "alias", &subsection, &subsection_len,
557548
&key)) {
558549
if (subsection) {
559550
/* [alias "name"] command = value */
560551
if (!strcmp(key, "command"))
561-
add_cmdname(&cfg->aliases, subsection,
552+
add_cmdname(aliases, subsection,
562553
subsection_len);
563554
} else {
564555
/* alias.name = value */
565-
add_cmdname(&cfg->aliases, key, strlen(key));
556+
add_cmdname(aliases, key, strlen(key));
566557
}
567558
}
568559

@@ -599,22 +590,26 @@ static const char bad_interpreter_advice[] =
599590

600591
char *help_unknown_cmd(const char *cmd)
601592
{
602-
struct help_unknown_cmd_config cfg = { 0 };
593+
struct cmdnames aliases = { 0 };
594+
struct autocorrect autocorrect = { 0 };
603595
int i, n, best_similarity = 0;
604596
struct cmdnames main_cmds = { 0 };
605597
struct cmdnames other_cmds = { 0 };
606598
struct cmdname_help *common_cmds;
607599

608-
read_early_config(the_repository, git_unknown_cmd_config, &cfg);
600+
autocorrect_resolve(&autocorrect);
609601

610-
if (cfg.autocorrect.mode == AUTOCORRECT_NEVER) {
602+
if (autocorrect.mode == AUTOCORRECT_NEVER) {
611603
fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
612604
exit(1);
613605
}
614606

615607
load_command_list("git-", &main_cmds, &other_cmds);
616608

617-
add_cmd_list(&main_cmds, &cfg.aliases);
609+
/* Also use aliases for command lookup */
610+
read_early_config(the_repository, resolve_aliases, &aliases);
611+
612+
add_cmd_list(&main_cmds, &aliases);
618613
add_cmd_list(&main_cmds, &other_cmds);
619614
QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
620615
uniq(&main_cmds);
@@ -674,18 +669,17 @@ char *help_unknown_cmd(const char *cmd)
674669
; /* still counting */
675670
}
676671

677-
if (cfg.autocorrect.mode != AUTOCORRECT_HINT && n == 1 &&
672+
if (autocorrect.mode != AUTOCORRECT_HINT && n == 1 &&
678673
SIMILAR_ENOUGH(best_similarity)) {
679674
char *assumed = xstrdup(main_cmds.names[0]->name);
680675

681676
fprintf_ln(stderr,
682-
_("WARNING: You called a Git command named '%s', "
683-
"which does not exist."),
677+
_("WARNING: You called a Git command named '%s', which does not exist."),
684678
cmd);
685679

686-
autocorrect_confirm(&cfg.autocorrect, assumed);
680+
autocorrect_confirm(&autocorrect, assumed);
687681

688-
cmdnames_release(&cfg.aliases);
682+
cmdnames_release(&aliases);
689683
cmdnames_release(&main_cmds);
690684
cmdnames_release(&other_cmds);
691685
return assumed;

0 commit comments

Comments
 (0)