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.

0 commit comments

Comments
 (0)