Skip to content

Commit ebaf0a5

Browse files
committed
Merge branch 'nd/complete-config-vars'
Continuing with the idea to programatically enumerate various pieces of data required for command line completion, teach the codebase to report the list of configuration variables subcommands care about to help complete them. * nd/complete-config-vars: completion: complete general config vars in two steps log-tree: allow to customize 'grafted' color completion: support case-insensitive config vars completion: keep other config var completion in camelCase completion: drop the hard coded list of config vars am: move advice.amWorkDir parsing back to advice.c advice: keep config name in camelCase in advice_config[] fsck: produce camelCase config key names help: add --config to list all available config fsck: factor out msg_id_info[] lazy initialization code grep: keep all colors in an array Add and use generic name->id mapping code for color slot parsing
2 parents 1102405 + f22f682 commit ebaf0a5

File tree

21 files changed

+445
-553
lines changed

21 files changed

+445
-553
lines changed

Documentation/config.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,8 @@ color.diff.<slot>::
11621162
color.decorate.<slot>::
11631163
Use customized color for 'git log --decorate' output. `<slot>` is one
11641164
of `branch`, `remoteBranch`, `tag`, `stash` or `HEAD` for local
1165-
branches, remote-tracking branches, tags, stash and HEAD, respectively.
1165+
branches, remote-tracking branches, tags, stash and HEAD, respectively
1166+
and `grafted` for grafted commits.
11661167

11671168
color.grep::
11681169
When set to `always`, always highlight matches. When `false` (or

Documentation/git-help.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ OPTIONS
4545
When used with `--verbose` print description for all recognized
4646
commands.
4747

48+
-c::
49+
--config::
50+
List all available configuration variables. This is a short
51+
summary of the list in linkgit:git-config[1].
52+
4853
-g::
4954
--guides::
5055
Prints a list of useful guides on the standard output. This

advice.c

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cache.h"
22
#include "config.h"
33
#include "color.h"
4+
#include "help.h"
45

56
int advice_push_update_rejected = 1;
67
int advice_push_non_ff_current = 1;
@@ -16,6 +17,7 @@ int advice_implicit_identity = 1;
1617
int advice_detached_head = 1;
1718
int advice_set_upstream_failure = 1;
1819
int advice_object_name_warning = 1;
20+
int advice_amworkdir = 1;
1921
int advice_rm_hints = 1;
2022
int advice_add_embedded_repo = 1;
2123
int advice_ignored_hook = 1;
@@ -53,28 +55,29 @@ static struct {
5355
const char *name;
5456
int *preference;
5557
} advice_config[] = {
56-
{ "pushupdaterejected", &advice_push_update_rejected },
57-
{ "pushnonffcurrent", &advice_push_non_ff_current },
58-
{ "pushnonffmatching", &advice_push_non_ff_matching },
59-
{ "pushalreadyexists", &advice_push_already_exists },
60-
{ "pushfetchfirst", &advice_push_fetch_first },
61-
{ "pushneedsforce", &advice_push_needs_force },
62-
{ "statushints", &advice_status_hints },
63-
{ "statusuoption", &advice_status_u_option },
64-
{ "commitbeforemerge", &advice_commit_before_merge },
65-
{ "resolveconflict", &advice_resolve_conflict },
66-
{ "implicitidentity", &advice_implicit_identity },
67-
{ "detachedhead", &advice_detached_head },
68-
{ "setupstreamfailure", &advice_set_upstream_failure },
69-
{ "objectnamewarning", &advice_object_name_warning },
70-
{ "rmhints", &advice_rm_hints },
71-
{ "addembeddedrepo", &advice_add_embedded_repo },
72-
{ "ignoredhook", &advice_ignored_hook },
73-
{ "waitingforeditor", &advice_waiting_for_editor },
74-
{ "graftfiledeprecated", &advice_graft_file_deprecated },
58+
{ "pushUpdateRejected", &advice_push_update_rejected },
59+
{ "pushNonFFCurrent", &advice_push_non_ff_current },
60+
{ "pushNonFFMatching", &advice_push_non_ff_matching },
61+
{ "pushAlreadyExists", &advice_push_already_exists },
62+
{ "pushFetchFirst", &advice_push_fetch_first },
63+
{ "pushNeedsForce", &advice_push_needs_force },
64+
{ "statusHints", &advice_status_hints },
65+
{ "statusUoption", &advice_status_u_option },
66+
{ "commitBeforeMerge", &advice_commit_before_merge },
67+
{ "resolveConflict", &advice_resolve_conflict },
68+
{ "implicitIdentity", &advice_implicit_identity },
69+
{ "detachedHead", &advice_detached_head },
70+
{ "setupStreamFailure", &advice_set_upstream_failure },
71+
{ "objectNameWarning", &advice_object_name_warning },
72+
{ "amWorkDir", &advice_amworkdir },
73+
{ "rmHints", &advice_rm_hints },
74+
{ "addEmbeddedRepo", &advice_add_embedded_repo },
75+
{ "ignoredHook", &advice_ignored_hook },
76+
{ "waitingForEditor", &advice_waiting_for_editor },
77+
{ "graftFileDeprecated", &advice_graft_file_deprecated },
7578

7679
/* make this an alias for backward compatibility */
77-
{ "pushnonfastforward", &advice_push_update_rejected }
80+
{ "pushNonFastForward", &advice_push_update_rejected }
7881
};
7982

8083
void advise(const char *advice, ...)
@@ -122,7 +125,7 @@ int git_default_advice_config(const char *var, const char *value)
122125
return 0;
123126

124127
for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
125-
if (strcmp(k, advice_config[i].name))
128+
if (strcasecmp(k, advice_config[i].name))
126129
continue;
127130
*advice_config[i].preference = git_config_bool(var, value);
128131
return 0;
@@ -131,6 +134,14 @@ int git_default_advice_config(const char *var, const char *value)
131134
return 0;
132135
}
133136

137+
void list_config_advices(struct string_list *list, const char *prefix)
138+
{
139+
int i;
140+
141+
for (i = 0; i < ARRAY_SIZE(advice_config); i++)
142+
list_config_item(list, prefix, advice_config[i].name);
143+
}
144+
134145
int error_resolve_conflict(const char *me)
135146
{
136147
if (!strcmp(me, "cherry-pick"))

advice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extern int advice_implicit_identity;
1717
extern int advice_detached_head;
1818
extern int advice_set_upstream_failure;
1919
extern int advice_object_name_warning;
20+
extern int advice_amworkdir;
2021
extern int advice_rm_hints;
2122
extern int advice_add_embedded_repo;
2223
extern int advice_ignored_hook;

builtin/am.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,15 +1827,11 @@ static void am_run(struct am_state *state, int resume)
18271827
}
18281828

18291829
if (apply_status) {
1830-
int advice_amworkdir = 1;
1831-
18321830
printf_ln(_("Patch failed at %s %.*s"), msgnum(state),
18331831
linelen(state->msg), state->msg);
18341832

1835-
git_config_get_bool("advice.amworkdir", &advice_amworkdir);
1836-
18371833
if (advice_amworkdir)
1838-
printf_ln(_("Use 'git am --show-current-patch' to see the failed patch"));
1834+
advise(_("Use 'git am --show-current-patch' to see the failed patch"));
18391835

18401836
die_user_resolve(state);
18411837
}

builtin/branch.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "wt-status.h"
2323
#include "ref-filter.h"
2424
#include "worktree.h"
25+
#include "help.h"
2526

2627
static const char * const builtin_branch_usage[] = {
2728
N_("git branch [<options>] [-r | -a] [--merged | --no-merged]"),
@@ -55,25 +56,19 @@ enum color_branch {
5556
BRANCH_COLOR_UPSTREAM = 5
5657
};
5758

59+
static const char *color_branch_slots[] = {
60+
[BRANCH_COLOR_RESET] = "reset",
61+
[BRANCH_COLOR_PLAIN] = "plain",
62+
[BRANCH_COLOR_REMOTE] = "remote",
63+
[BRANCH_COLOR_LOCAL] = "local",
64+
[BRANCH_COLOR_CURRENT] = "current",
65+
[BRANCH_COLOR_UPSTREAM] = "upstream",
66+
};
67+
5868
static struct string_list output = STRING_LIST_INIT_DUP;
5969
static unsigned int colopts;
6070

61-
static int parse_branch_color_slot(const char *slot)
62-
{
63-
if (!strcasecmp(slot, "plain"))
64-
return BRANCH_COLOR_PLAIN;
65-
if (!strcasecmp(slot, "reset"))
66-
return BRANCH_COLOR_RESET;
67-
if (!strcasecmp(slot, "remote"))
68-
return BRANCH_COLOR_REMOTE;
69-
if (!strcasecmp(slot, "local"))
70-
return BRANCH_COLOR_LOCAL;
71-
if (!strcasecmp(slot, "current"))
72-
return BRANCH_COLOR_CURRENT;
73-
if (!strcasecmp(slot, "upstream"))
74-
return BRANCH_COLOR_UPSTREAM;
75-
return -1;
76-
}
71+
define_list_config_array(color_branch_slots);
7772

7873
static int git_branch_config(const char *var, const char *value, void *cb)
7974
{
@@ -86,7 +81,7 @@ static int git_branch_config(const char *var, const char *value, void *cb)
8681
return 0;
8782
}
8883
if (skip_prefix(var, "color.branch.", &slot_name)) {
89-
int slot = parse_branch_color_slot(slot_name);
84+
int slot = LOOKUP_CONFIG(color_branch_slots, slot_name);
9085
if (slot < 0)
9186
return 0;
9287
if (!value)

builtin/clean.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "column.h"
1717
#include "color.h"
1818
#include "pathspec.h"
19+
#include "help.h"
1920

2021
static int force = -1; /* unset */
2122
static int interactive;
@@ -42,6 +43,15 @@ enum color_clean {
4243
CLEAN_COLOR_ERROR = 5
4344
};
4445

46+
static const char *color_interactive_slots[] = {
47+
[CLEAN_COLOR_ERROR] = "error",
48+
[CLEAN_COLOR_HEADER] = "header",
49+
[CLEAN_COLOR_HELP] = "help",
50+
[CLEAN_COLOR_PLAIN] = "plain",
51+
[CLEAN_COLOR_PROMPT] = "prompt",
52+
[CLEAN_COLOR_RESET] = "reset",
53+
};
54+
4555
static int clean_use_color = -1;
4656
static char clean_colors[][COLOR_MAXLEN] = {
4757
[CLEAN_COLOR_ERROR] = GIT_COLOR_BOLD_RED,
@@ -82,22 +92,7 @@ struct menu_stuff {
8292
void *stuff;
8393
};
8494

85-
static int parse_clean_color_slot(const char *var)
86-
{
87-
if (!strcasecmp(var, "reset"))
88-
return CLEAN_COLOR_RESET;
89-
if (!strcasecmp(var, "plain"))
90-
return CLEAN_COLOR_PLAIN;
91-
if (!strcasecmp(var, "prompt"))
92-
return CLEAN_COLOR_PROMPT;
93-
if (!strcasecmp(var, "header"))
94-
return CLEAN_COLOR_HEADER;
95-
if (!strcasecmp(var, "help"))
96-
return CLEAN_COLOR_HELP;
97-
if (!strcasecmp(var, "error"))
98-
return CLEAN_COLOR_ERROR;
99-
return -1;
100-
}
95+
define_list_config_array(color_interactive_slots);
10196

10297
static int git_clean_config(const char *var, const char *value, void *cb)
10398
{
@@ -113,7 +108,7 @@ static int git_clean_config(const char *var, const char *value, void *cb)
113108
return 0;
114109
}
115110
if (skip_prefix(var, "color.interactive.", &slot_name)) {
116-
int slot = parse_clean_color_slot(slot_name);
111+
int slot = LOOKUP_CONFIG(color_interactive_slots, slot_name);
117112
if (slot < 0)
118113
return 0;
119114
if (!value)

builtin/commit.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "column.h"
3333
#include "sequencer.h"
3434
#include "mailmap.h"
35+
#include "help.h"
3536

3637
static const char * const builtin_commit_usage[] = {
3738
N_("git commit [<options>] [--] <pathspec>..."),
@@ -66,6 +67,18 @@ N_("If you wish to skip this commit, use:\n"
6667
"Then \"git cherry-pick --continue\" will resume cherry-picking\n"
6768
"the remaining commits.\n");
6869

70+
static const char *color_status_slots[] = {
71+
[WT_STATUS_HEADER] = "header",
72+
[WT_STATUS_UPDATED] = "updated",
73+
[WT_STATUS_CHANGED] = "changed",
74+
[WT_STATUS_UNTRACKED] = "untracked",
75+
[WT_STATUS_NOBRANCH] = "noBranch",
76+
[WT_STATUS_UNMERGED] = "unmerged",
77+
[WT_STATUS_LOCAL_BRANCH] = "localBranch",
78+
[WT_STATUS_REMOTE_BRANCH] = "remoteBranch",
79+
[WT_STATUS_ONBRANCH] = "branch",
80+
};
81+
6982
static const char *use_message_buffer;
7083
static struct lock_file index_lock; /* real index */
7184
static struct lock_file false_lock; /* used only for partial commits */
@@ -1183,27 +1196,14 @@ static int dry_run_commit(int argc, const char **argv, const char *prefix,
11831196
return commitable ? 0 : 1;
11841197
}
11851198

1199+
define_list_config_array_extra(color_status_slots, {"added"});
1200+
11861201
static int parse_status_slot(const char *slot)
11871202
{
1188-
if (!strcasecmp(slot, "header"))
1189-
return WT_STATUS_HEADER;
1190-
if (!strcasecmp(slot, "branch"))
1191-
return WT_STATUS_ONBRANCH;
1192-
if (!strcasecmp(slot, "updated") || !strcasecmp(slot, "added"))
1203+
if (!strcasecmp(slot, "added"))
11931204
return WT_STATUS_UPDATED;
1194-
if (!strcasecmp(slot, "changed"))
1195-
return WT_STATUS_CHANGED;
1196-
if (!strcasecmp(slot, "untracked"))
1197-
return WT_STATUS_UNTRACKED;
1198-
if (!strcasecmp(slot, "nobranch"))
1199-
return WT_STATUS_NOBRANCH;
1200-
if (!strcasecmp(slot, "unmerged"))
1201-
return WT_STATUS_UNMERGED;
1202-
if (!strcasecmp(slot, "localBranch"))
1203-
return WT_STATUS_LOCAL_BRANCH;
1204-
if (!strcasecmp(slot, "remoteBranch"))
1205-
return WT_STATUS_REMOTE_BRANCH;
1206-
return -1;
1205+
1206+
return LOOKUP_CONFIG(color_status_slots, slot);
12071207
}
12081208

12091209
static int git_status_config(const char *k, const char *v, void *cb)

builtin/help.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static const char *html_path;
3737

3838
static int show_all = 0;
3939
static int show_guides = 0;
40+
static int show_config;
4041
static int verbose;
4142
static unsigned int colopts;
4243
static enum help_format help_format = HELP_FORMAT_NONE;
@@ -45,6 +46,8 @@ static struct option builtin_help_options[] = {
4546
OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
4647
OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
4748
OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")),
49+
OPT_BOOL('c', "config", &show_config, N_("print all configuration variable names")),
50+
OPT_SET_INT_F(0, "config-for-completion", &show_config, "", 2, PARSE_OPT_HIDDEN),
4851
OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
4952
OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
5053
HELP_FORMAT_WEB),
@@ -444,6 +447,19 @@ int cmd_help(int argc, const char **argv, const char *prefix)
444447
list_commands(colopts, &main_cmds, &other_cmds);
445448
}
446449

450+
if (show_config) {
451+
int for_human = show_config == 1;
452+
453+
if (!for_human) {
454+
list_config_help(for_human);
455+
return 0;
456+
}
457+
setup_pager();
458+
list_config_help(for_human);
459+
printf("\n%s\n", _("'git help config' for more information"));
460+
return 0;
461+
}
462+
447463
if (show_guides)
448464
list_common_guides_help();
449465

config.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,3 +3245,16 @@ enum config_scope current_config_scope(void)
32453245
else
32463246
return current_parsing_scope;
32473247
}
3248+
3249+
int lookup_config(const char **mapping, int nr_mapping, const char *var)
3250+
{
3251+
int i;
3252+
3253+
for (i = 0; i < nr_mapping; i++) {
3254+
const char *name = mapping[i];
3255+
3256+
if (name && !strcasecmp(var, name))
3257+
return i;
3258+
}
3259+
return -1;
3260+
}

0 commit comments

Comments
 (0)