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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
May 26, 2018 at 15:55 UTC
3ac68a93fd2b984e2a7e570217d2646a208ffcc3
13 files changed
+172
-1
Documentation/git-help.txt
+5
@@ -45,6 +45,11 @@ OPTIONS
45
When used with `--verbose` print description for all recognized
46
commands.
47
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
+
53
-g::
54
--guides::
55
Prints a list of useful guides on the standard output. This
advice.c
+9
@@ -1,6 +1,7 @@
1
#include "cache.h"
2
#include "config.h"
3
#include "color.h"
4
+#include "help.h"
5
6
int advice_push_update_rejected = 1;
7
int advice_push_non_ff_current = 1;
@@ -131,6 +132,14 @@ int git_default_advice_config(const char *var, const char *value)
132
return 0;
133
}
134
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
+
143
int error_resolve_conflict(const char *me)
144
{
145
if (!strcmp(me, "cherry-pick"))
builtin/branch.c
+3
@@ -22,6 +22,7 @@
22
#include "wt-status.h"
23
#include "ref-filter.h"
24
#include "worktree.h"
25
+#include "help.h"
26
27
static const char * const builtin_branch_usage[] = {
28
N_("git branch [<options>] [-r | -a] [--merged | --no-merged]"),
@@ -67,6 +68,8 @@ static const char *color_branch_slots[] = {
68
static struct string_list output = STRING_LIST_INIT_DUP;
69
static unsigned int colopts;
70
71
+define_list_config_array(color_branch_slots);
72
+
73
static int git_branch_config(const char *var, const char *value, void *cb)
74
{
75
const char *slot_name;
builtin/clean.c
+3
@@ -16,6 +16,7 @@
16
#include "column.h"
17
#include "color.h"
18
#include "pathspec.h"
19
+#include "help.h"
20
21
static int force = -1; /* unset */
22
static int interactive;
@@ -91,6 +92,8 @@ struct menu_stuff {
92
void *stuff;
93
};
94
95
+define_list_config_array(color_interactive_slots);
96
+
97
static int git_clean_config(const char *var, const char *value, void *cb)
98
{
99
const char *slot_name;
builtin/commit.c
+3
@@ -32,6 +32,7 @@
32
#include "column.h"
33
#include "sequencer.h"
34
#include "mailmap.h"
35
+#include "help.h"
36
37
static const char * const builtin_commit_usage[] = {
38
N_("git commit [<options>] [--] <pathspec>..."),
@@ -1185,6 +1186,8 @@ static int dry_run_commit(int argc, const char **argv, const char *prefix,
1186
return commitable ? 0 : 1;
1187
}
1188
1189
+define_list_config_array_extra(color_status_slots, {"added"});
1190
+
1191
static int parse_status_slot(const char *slot)
1192
{
1193
if (!strcasecmp(slot, "added"))
builtin/help.c
+9
@@ -37,6 +37,7 @@ static const char *html_path;
37
38
static int show_all = 0;
39
static int show_guides = 0;
40
+static int show_config;
41
static int verbose;
42
static unsigned int colopts;
43
static enum help_format help_format = HELP_FORMAT_NONE;
@@ -45,6 +46,7 @@ static struct option builtin_help_options[] = {
46
OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
47
OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
48
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('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
51
OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
52
HELP_FORMAT_WEB),
@@ -444,6 +446,13 @@ int cmd_help(int argc, const char **argv, const char *prefix)
446
list_commands(colopts, &main_cmds, &other_cmds);
447
}
448
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
+
456
if (show_guides)
457
list_common_guides_help();
458
diff.c
+3
@@ -22,6 +22,7 @@
22
#include "argv-array.h"
23
#include "graph.h"
24
#include "packfile.h"
25
+#include "help.h"
26
27
#ifdef NO_FAST_WORKING_DIRECTORY
28
#define FAST_WORKING_DIRECTORY 0
@@ -93,6 +94,8 @@ static NORETURN void die_want_option(const char *option_name)
94
die(_("option '%s' requires a value"), option_name);
95
}
96
97
+define_list_config_array_extra(color_diff_slots, {"plain"});
98
+
99
static int parse_diff_color_slot(const char *var)
100
{
101
if (!strcasecmp(var, "plain"))
fsck.c
+12
@@ -10,6 +10,7 @@
10
#include "utf8.h"
11
#include "sha1-array.h"
12
#include "decorate.h"
13
+#include "help.h"
14
15
#define FSCK_FATAL -1
16
#define FSCK_INFO -2
@@ -120,6 +121,17 @@ static int parse_msg_id(const char *text)
121
return -1;
122
}
123
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
+
135
static int fsck_msg_type(enum fsck_msg_id msg_id,
136
struct fsck_options *options)
137
{
generate-cmdlist.sh
+19
@@ -76,6 +76,23 @@ print_command_list () {
76
echo "};"
77
}
78
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
+
96
echo "/* Automatically generated by generate-cmdlist.sh */
97
struct cmdname_help {
98
const char *name;
@@ -88,3 +105,5 @@ echo
105
define_category_names "$1"
106
echo
107
print_command_list "$1"
108
+echo
109
+print_config_list
grep.c
+3
@@ -7,6 +7,7 @@
7
#include "diffcore.h"
8
#include "commit.h"
9
#include "quote.h"
10
+#include "help.h"
11
12
static int grep_source_load(struct grep_source *gs);
13
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)
81
die("bad %s argument: %s", opt, arg);
82
}
83
84
+define_list_config_array_extra(color_grep_slots, {"match"});
85
+
86
/*
87
* Read the configuration file once and store it in
88
* the grep_defaults template.
help.c
+56
@@ -409,6 +409,62 @@ void list_common_guides_help(void)
409
putchar('\n');
410
}
411
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
+
468
void list_all_cmds_help(void)
469
{
470
print_cmd_by_category(main_categories);
help.h
+44
-1
@@ -1,7 +1,8 @@
1
#ifndef HELP_H
2
#define HELP_H
3
4
-struct string_list;
4
+#include "string-list.h"
5
+#include "strbuf.h"
6
7
struct cmdnames {
8
int alloc;
@@ -21,6 +22,7 @@ static inline void mput_char(char c, unsigned int num)
22
extern void list_common_cmds_help(void);
23
extern void list_all_cmds_help(void);
24
extern void list_common_guides_help(void);
25
+extern void list_config_help(void);
26
27
extern void list_all_main_cmds(struct string_list *list);
28
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
44
* ref to the command, to give suggested "correct" refs.
45
*/
46
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
+
88
#endif /* HELP_H */
log-tree.c
+3
@@ -12,6 +12,7 @@
12
#include "gpg-interface.h"
13
#include "sequencer.h"
14
#include "line-log.h"
15
+#include "help.h"
16
17
static struct decoration name_decoration = { "object names" };
18
static int decoration_loaded;
@@ -42,6 +43,8 @@ static const char *decorate_get_color(int decorate_use_color, enum decoration_ty
43
return "";
44
}
45
46
+define_list_config_array(color_decorate_slots);
47
+
48
int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
49
{
50
int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);