Skip to content

Commit 3ac68a9

Browse files
pcloudsgitster
authored andcommitted
help: add --config to list all available config
Sometimes it helps to list all available config vars so the user can search for something they want. The config man page can also be used but it's harder to search if you want to focus on the variable name, for example. This is not the best way to collect the available config since it's not precise. Ideally we should have a centralized list of config in C code (pretty much like 'struct option'), but that's a lot more work. This will do for now. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a46baac commit 3ac68a9

File tree

13 files changed

+172
-1
lines changed

13 files changed

+172
-1
lines changed

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: 9 additions & 0 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;
@@ -131,6 +132,14 @@ int git_default_advice_config(const char *var, const char *value)
131132
return 0;
132133
}
133134

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

builtin/branch.c

Lines changed: 3 additions & 0 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]"),
@@ -67,6 +68,8 @@ static const char *color_branch_slots[] = {
6768
static struct string_list output = STRING_LIST_INIT_DUP;
6869
static unsigned int colopts;
6970

71+
define_list_config_array(color_branch_slots);
72+
7073
static int git_branch_config(const char *var, const char *value, void *cb)
7174
{
7275
const char *slot_name;

builtin/clean.c

Lines changed: 3 additions & 0 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;
@@ -91,6 +92,8 @@ struct menu_stuff {
9192
void *stuff;
9293
};
9394

95+
define_list_config_array(color_interactive_slots);
96+
9497
static int git_clean_config(const char *var, const char *value, void *cb)
9598
{
9699
const char *slot_name;

builtin/commit.c

Lines changed: 3 additions & 0 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>..."),
@@ -1185,6 +1186,8 @@ static int dry_run_commit(int argc, const char **argv, const char *prefix,
11851186
return commitable ? 0 : 1;
11861187
}
11871188

1189+
define_list_config_array_extra(color_status_slots, {"added"});
1190+
11881191
static int parse_status_slot(const char *slot)
11891192
{
11901193
if (!strcasecmp(slot, "added"))

builtin/help.c

Lines changed: 9 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,7 @@ 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")),
4850
OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
4951
OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
5052
HELP_FORMAT_WEB),
@@ -444,6 +446,13 @@ int cmd_help(int argc, const char **argv, const char *prefix)
444446
list_commands(colopts, &main_cmds, &other_cmds);
445447
}
446448

449+
if (show_config) {
450+
setup_pager();
451+
list_config_help();
452+
printf("\n%s\n", _("'git help config' for more information"));
453+
return 0;
454+
}
455+
447456
if (show_guides)
448457
list_common_guides_help();
449458

diff.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "argv-array.h"
2323
#include "graph.h"
2424
#include "packfile.h"
25+
#include "help.h"
2526

2627
#ifdef NO_FAST_WORKING_DIRECTORY
2728
#define FAST_WORKING_DIRECTORY 0
@@ -93,6 +94,8 @@ static NORETURN void die_want_option(const char *option_name)
9394
die(_("option '%s' requires a value"), option_name);
9495
}
9596

97+
define_list_config_array_extra(color_diff_slots, {"plain"});
98+
9699
static int parse_diff_color_slot(const char *var)
97100
{
98101
if (!strcasecmp(var, "plain"))

fsck.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "utf8.h"
1111
#include "sha1-array.h"
1212
#include "decorate.h"
13+
#include "help.h"
1314

1415
#define FSCK_FATAL -1
1516
#define FSCK_INFO -2
@@ -120,6 +121,17 @@ static int parse_msg_id(const char *text)
120121
return -1;
121122
}
122123

124+
void list_config_fsck_msg_ids(struct string_list *list, const char *prefix)
125+
{
126+
int i;
127+
128+
prepare_msg_ids();
129+
130+
/* TODO: we can do better by producing camelCase names */
131+
for (i = 0; i < FSCK_MSG_MAX; i++)
132+
list_config_item(list, prefix, msg_id_info[i].downcased);
133+
}
134+
123135
static int fsck_msg_type(enum fsck_msg_id msg_id,
124136
struct fsck_options *options)
125137
{

generate-cmdlist.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ print_command_list () {
7676
echo "};"
7777
}
7878

79+
print_config_list () {
80+
cat <<EOF
81+
static const char *config_name_list[] = {
82+
EOF
83+
grep '^[a-zA-Z].*\..*::$' Documentation/config.txt |
84+
sed '/deprecated/d; s/::$//; s/, */\n/g' |
85+
sort |
86+
while read line
87+
do
88+
echo " \"$line\","
89+
done
90+
cat <<EOF
91+
NULL,
92+
};
93+
EOF
94+
}
95+
7996
echo "/* Automatically generated by generate-cmdlist.sh */
8097
struct cmdname_help {
8198
const char *name;
@@ -88,3 +105,5 @@ echo
88105
define_category_names "$1"
89106
echo
90107
print_command_list "$1"
108+
echo
109+
print_config_list

grep.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "diffcore.h"
88
#include "commit.h"
99
#include "quote.h"
10+
#include "help.h"
1011

1112
static int grep_source_load(struct grep_source *gs);
1213
static int grep_source_is_binary(struct grep_source *gs);
@@ -80,6 +81,8 @@ static int parse_pattern_type_arg(const char *opt, const char *arg)
8081
die("bad %s argument: %s", opt, arg);
8182
}
8283

84+
define_list_config_array_extra(color_grep_slots, {"match"});
85+
8386
/*
8487
* Read the configuration file once and store it in
8588
* the grep_defaults template.

help.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,62 @@ void list_common_guides_help(void)
409409
putchar('\n');
410410
}
411411

412+
struct slot_expansion {
413+
const char *prefix;
414+
const char *placeholder;
415+
void (*fn)(struct string_list *list, const char *prefix);
416+
int found;
417+
};
418+
419+
void list_config_help(void)
420+
{
421+
struct slot_expansion slot_expansions[] = {
422+
{ "advice", "*", list_config_advices },
423+
{ "color.branch", "<slot>", list_config_color_branch_slots },
424+
{ "color.decorate", "<slot>", list_config_color_decorate_slots },
425+
{ "color.diff", "<slot>", list_config_color_diff_slots },
426+
{ "color.grep", "<slot>", list_config_color_grep_slots },
427+
{ "color.interactive", "<slot>", list_config_color_interactive_slots },
428+
{ "color.status", "<slot>", list_config_color_status_slots },
429+
{ "fsck", "<msg-id>", list_config_fsck_msg_ids },
430+
{ "receive.fsck", "<msg-id>", list_config_fsck_msg_ids },
431+
{ NULL, NULL, NULL }
432+
};
433+
const char **p;
434+
struct slot_expansion *e;
435+
struct string_list keys = STRING_LIST_INIT_DUP;
436+
int i;
437+
438+
for (p = config_name_list; *p; p++) {
439+
const char *var = *p;
440+
struct strbuf sb = STRBUF_INIT;
441+
442+
for (e = slot_expansions; e->prefix; e++) {
443+
444+
strbuf_reset(&sb);
445+
strbuf_addf(&sb, "%s.%s", e->prefix, e->placeholder);
446+
if (!strcasecmp(var, sb.buf)) {
447+
e->fn(&keys, e->prefix);
448+
e->found++;
449+
break;
450+
}
451+
}
452+
strbuf_release(&sb);
453+
if (!e->prefix)
454+
string_list_append(&keys, var);
455+
}
456+
457+
for (e = slot_expansions; e->prefix; e++)
458+
if (!e->found)
459+
BUG("slot_expansion %s.%s is not used",
460+
e->prefix, e->placeholder);
461+
462+
string_list_sort(&keys);
463+
for (i = 0; i < keys.nr; i++)
464+
puts(keys.items[i].string);
465+
string_list_clear(&keys, 0);
466+
}
467+
412468
void list_all_cmds_help(void)
413469
{
414470
print_cmd_by_category(main_categories);

help.h

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#ifndef HELP_H
22
#define HELP_H
33

4-
struct string_list;
4+
#include "string-list.h"
5+
#include "strbuf.h"
56

67
struct cmdnames {
78
int alloc;
@@ -21,6 +22,7 @@ static inline void mput_char(char c, unsigned int num)
2122
extern void list_common_cmds_help(void);
2223
extern void list_all_cmds_help(void);
2324
extern void list_common_guides_help(void);
25+
extern void list_config_help(void);
2426

2527
extern void list_all_main_cmds(struct string_list *list);
2628
extern void list_all_other_cmds(struct string_list *list);
@@ -42,4 +44,45 @@ extern void list_commands(unsigned int colopts, struct cmdnames *main_cmds, stru
4244
* ref to the command, to give suggested "correct" refs.
4345
*/
4446
extern void help_unknown_ref(const char *ref, const char *cmd, const char *error);
47+
48+
static inline void list_config_item(struct string_list *list,
49+
const char *prefix,
50+
const char *str)
51+
{
52+
string_list_append_nodup(list, xstrfmt("%s.%s", prefix, str));
53+
}
54+
55+
#define define_list_config_array(array) \
56+
void list_config_##array(struct string_list *list, const char *prefix) \
57+
{ \
58+
int i; \
59+
for (i = 0; i < ARRAY_SIZE(array); i++) \
60+
if (array[i]) \
61+
list_config_item(list, prefix, array[i]); \
62+
} \
63+
struct string_list
64+
65+
#define define_list_config_array_extra(array, values) \
66+
void list_config_##array(struct string_list *list, const char *prefix) \
67+
{ \
68+
int i; \
69+
static const char *extra[] = values; \
70+
for (i = 0; i < ARRAY_SIZE(extra); i++) \
71+
list_config_item(list, prefix, extra[i]); \
72+
for (i = 0; i < ARRAY_SIZE(array); i++) \
73+
if (array[i]) \
74+
list_config_item(list, prefix, array[i]); \
75+
} \
76+
struct string_list
77+
78+
/* These are actually scattered over many C files */
79+
void list_config_advices(struct string_list *list, const char *prefix);
80+
void list_config_color_branch_slots(struct string_list *list, const char *prefix);
81+
void list_config_color_decorate_slots(struct string_list *list, const char *prefix);
82+
void list_config_color_diff_slots(struct string_list *list, const char *prefix);
83+
void list_config_color_grep_slots(struct string_list *list, const char *prefix);
84+
void list_config_color_interactive_slots(struct string_list *list, const char *prefix);
85+
void list_config_color_status_slots(struct string_list *list, const char *prefix);
86+
void list_config_fsck_msg_ids(struct string_list *list, const char *prefix);
87+
4588
#endif /* HELP_H */

log-tree.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "gpg-interface.h"
1313
#include "sequencer.h"
1414
#include "line-log.h"
15+
#include "help.h"
1516

1617
static struct decoration name_decoration = { "object names" };
1718
static int decoration_loaded;
@@ -42,6 +43,8 @@ static const char *decorate_get_color(int decorate_use_color, enum decoration_ty
4243
return "";
4344
}
4445

46+
define_list_config_array(color_decorate_slots);
47+
4548
int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
4649
{
4750
int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);

0 commit comments

Comments
 (0)