help: add --config to list all available config
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Sat, 26 May 2018 13:55:24 +0000 (15:55 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 29 May 2018 05:51:28 +0000 (14:51 +0900)
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>
13 files changed:
Documentation/git-help.txt
advice.c
builtin/branch.c
builtin/clean.c
builtin/commit.c
builtin/help.c
diff.c
fsck.c
generate-cmdlist.sh
grep.c
help.c
help.h
log-tree.c
index a40fc38d8b425fae0a1ca074ecc8d603efc80f14..83d25d825aa5a9b68f3cff5290d1ebacef35ebde 100644 (file)
@@ -45,6 +45,11 @@ OPTIONS
        When used with `--verbose` print description for all recognized
        commands.
 
+-c::
+--config::
+       List all available configuration variables. This is a short
+       summary of the list in linkgit:git-config[1].
+
 -g::
 --guides::
        Prints a list of useful guides on the standard output. This
index 370a56d0546bb3fc7aa4a62203c600e9a8af2d7a..cf6c673ed772ff840916ee53bf3d388567d01b6d 100644 (file)
--- a/advice.c
+++ b/advice.c
@@ -1,6 +1,7 @@
 #include "cache.h"
 #include "config.h"
 #include "color.h"
+#include "help.h"
 
 int advice_push_update_rejected = 1;
 int advice_push_non_ff_current = 1;
@@ -131,6 +132,14 @@ int git_default_advice_config(const char *var, const char *value)
        return 0;
 }
 
+void list_config_advices(struct string_list *list, const char *prefix)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(advice_config); i++)
+               list_config_item(list, prefix, advice_config[i].name);
+}
+
 int error_resolve_conflict(const char *me)
 {
        if (!strcmp(me, "cherry-pick"))
index 136d57caa4c91b30bca1ceb362a5faaf74cd25af..426c20b61d50a31306d37d9b2d80364fe957f9a9 100644 (file)
@@ -22,6 +22,7 @@
 #include "wt-status.h"
 #include "ref-filter.h"
 #include "worktree.h"
+#include "help.h"
 
 static const char * const builtin_branch_usage[] = {
        N_("git branch [<options>] [-r | -a] [--merged | --no-merged]"),
@@ -67,6 +68,8 @@ static const char *color_branch_slots[] = {
 static struct string_list output = STRING_LIST_INIT_DUP;
 static unsigned int colopts;
 
+define_list_config_array(color_branch_slots);
+
 static int git_branch_config(const char *var, const char *value, void *cb)
 {
        const char *slot_name;
index 0ccd95e693ed4f8453c7db4031bbd2d3a1a4dfae..ab402c204cbcaea6190608753311605d37ca83d2 100644 (file)
@@ -16,6 +16,7 @@
 #include "column.h"
 #include "color.h"
 #include "pathspec.h"
+#include "help.h"
 
 static int force = -1; /* unset */
 static int interactive;
@@ -91,6 +92,8 @@ struct menu_stuff {
        void *stuff;
 };
 
+define_list_config_array(color_interactive_slots);
+
 static int git_clean_config(const char *var, const char *value, void *cb)
 {
        const char *slot_name;
index f96755aa4b6a8f30cb886cb151c560481896d6e5..8bb39116145b575eeb74e18d85e79b7c0ab35f85 100644 (file)
@@ -32,6 +32,7 @@
 #include "column.h"
 #include "sequencer.h"
 #include "mailmap.h"
+#include "help.h"
 
 static const char * const builtin_commit_usage[] = {
        N_("git commit [<options>] [--] <pathspec>..."),
@@ -1185,6 +1186,8 @@ static int dry_run_commit(int argc, const char **argv, const char *prefix,
        return commitable ? 0 : 1;
 }
 
+define_list_config_array_extra(color_status_slots, {"added"});
+
 static int parse_status_slot(const char *slot)
 {
        if (!strcasecmp(slot, "added"))
index 58e0a5507f10365b43eaa2698da2c8bba18b12ed..ccb206e1d4d8f12c32864bccc5971af41d63d051 100644 (file)
@@ -37,6 +37,7 @@ static const char *html_path;
 
 static int show_all = 0;
 static int show_guides = 0;
+static int show_config;
 static int verbose;
 static unsigned int colopts;
 static enum help_format help_format = HELP_FORMAT_NONE;
@@ -45,6 +46,7 @@ static struct option builtin_help_options[] = {
        OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
        OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
        OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")),
+       OPT_BOOL('c', "config", &show_config, N_("print all configuration variable names")),
        OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
        OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
                        HELP_FORMAT_WEB),
@@ -444,6 +446,13 @@ int cmd_help(int argc, const char **argv, const char *prefix)
                list_commands(colopts, &main_cmds, &other_cmds);
        }
 
+       if (show_config) {
+               setup_pager();
+               list_config_help();
+               printf("\n%s\n", _("'git help config' for more information"));
+               return 0;
+       }
+
        if (show_guides)
                list_common_guides_help();
 
diff --git a/diff.c b/diff.c
index 56d7bfded6a8cc06f635a50898f2747fe1eac9b1..513410d46bfeff9c6341619133f873b898e42198 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -22,6 +22,7 @@
 #include "argv-array.h"
 #include "graph.h"
 #include "packfile.h"
+#include "help.h"
 
 #ifdef NO_FAST_WORKING_DIRECTORY
 #define FAST_WORKING_DIRECTORY 0
@@ -93,6 +94,8 @@ static NORETURN void die_want_option(const char *option_name)
        die(_("option '%s' requires a value"), option_name);
 }
 
+define_list_config_array_extra(color_diff_slots, {"plain"});
+
 static int parse_diff_color_slot(const char *var)
 {
        if (!strcasecmp(var, "plain"))
diff --git a/fsck.c b/fsck.c
index c004a37372a438bef18692ef2f7a022d282805d0..878a3c617c5019038c04919c697484ad9e291d9a 100644 (file)
--- a/fsck.c
+++ b/fsck.c
@@ -10,6 +10,7 @@
 #include "utf8.h"
 #include "sha1-array.h"
 #include "decorate.h"
+#include "help.h"
 
 #define FSCK_FATAL -1
 #define FSCK_INFO -2
@@ -120,6 +121,17 @@ static int parse_msg_id(const char *text)
        return -1;
 }
 
+void list_config_fsck_msg_ids(struct string_list *list, const char *prefix)
+{
+       int i;
+
+       prepare_msg_ids();
+
+       /* TODO: we can do better by producing camelCase names */
+       for (i = 0; i < FSCK_MSG_MAX; i++)
+               list_config_item(list, prefix, msg_id_info[i].downcased);
+}
+
 static int fsck_msg_type(enum fsck_msg_id msg_id,
        struct fsck_options *options)
 {
index 8d6d8b45ceb0138797eb7e54b15f7546925598ed..c4124acbe7802369ff64c12b48a513437c929a20 100755 (executable)
@@ -76,6 +76,23 @@ print_command_list () {
        echo "};"
 }
 
+print_config_list () {
+       cat <<EOF
+static const char *config_name_list[] = {
+EOF
+       grep '^[a-zA-Z].*\..*::$' Documentation/config.txt |
+       sed '/deprecated/d; s/::$//; s/,  */\n/g' |
+       sort |
+       while read line
+       do
+               echo "  \"$line\","
+       done
+       cat <<EOF
+       NULL,
+};
+EOF
+}
+
 echo "/* Automatically generated by generate-cmdlist.sh */
 struct cmdname_help {
        const char *name;
@@ -88,3 +105,5 @@ echo
 define_category_names "$1"
 echo
 print_command_list "$1"
+echo
+print_config_list
diff --git a/grep.c b/grep.c
index b7012f40f556f47c0a5fa26df88bbafdcf04fe1c..bf71b130c20391beeef7923ed2db71869ddf8c56 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -7,6 +7,7 @@
 #include "diffcore.h"
 #include "commit.h"
 #include "quote.h"
+#include "help.h"
 
 static int grep_source_load(struct grep_source *gs);
 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)
        die("bad %s argument: %s", opt, arg);
 }
 
+define_list_config_array_extra(color_grep_slots, {"match"});
+
 /*
  * Read the configuration file once and store it in
  * the grep_defaults template.
diff --git a/help.c b/help.c
index dd35fcc133094e4dc89898463868e89162637076..f078dfebad6e2da7c217c5b66eec898765274f44 100644 (file)
--- a/help.c
+++ b/help.c
@@ -409,6 +409,62 @@ void list_common_guides_help(void)
        putchar('\n');
 }
 
+struct slot_expansion {
+       const char *prefix;
+       const char *placeholder;
+       void (*fn)(struct string_list *list, const char *prefix);
+       int found;
+};
+
+void list_config_help(void)
+{
+       struct slot_expansion slot_expansions[] = {
+               { "advice", "*", list_config_advices },
+               { "color.branch", "<slot>", list_config_color_branch_slots },
+               { "color.decorate", "<slot>", list_config_color_decorate_slots },
+               { "color.diff", "<slot>", list_config_color_diff_slots },
+               { "color.grep", "<slot>", list_config_color_grep_slots },
+               { "color.interactive", "<slot>", list_config_color_interactive_slots },
+               { "color.status", "<slot>", list_config_color_status_slots },
+               { "fsck", "<msg-id>", list_config_fsck_msg_ids },
+               { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids },
+               { NULL, NULL, NULL }
+       };
+       const char **p;
+       struct slot_expansion *e;
+       struct string_list keys = STRING_LIST_INIT_DUP;
+       int i;
+
+       for (p = config_name_list; *p; p++) {
+               const char *var = *p;
+               struct strbuf sb = STRBUF_INIT;
+
+               for (e = slot_expansions; e->prefix; e++) {
+
+                       strbuf_reset(&sb);
+                       strbuf_addf(&sb, "%s.%s", e->prefix, e->placeholder);
+                       if (!strcasecmp(var, sb.buf)) {
+                               e->fn(&keys, e->prefix);
+                               e->found++;
+                               break;
+                       }
+               }
+               strbuf_release(&sb);
+               if (!e->prefix)
+                       string_list_append(&keys, var);
+       }
+
+       for (e = slot_expansions; e->prefix; e++)
+               if (!e->found)
+                       BUG("slot_expansion %s.%s is not used",
+                           e->prefix, e->placeholder);
+
+       string_list_sort(&keys);
+       for (i = 0; i < keys.nr; i++)
+               puts(keys.items[i].string);
+       string_list_clear(&keys, 0);
+}
+
 void list_all_cmds_help(void)
 {
        print_cmd_by_category(main_categories);
diff --git a/help.h b/help.h
index 3b38292a1b317ba32d934be6006cb86e0e7d0d94..b46fe6ee7303ff7d044df4727a47bc5e81bd9616 100644 (file)
--- a/help.h
+++ b/help.h
@@ -1,7 +1,8 @@
 #ifndef HELP_H
 #define HELP_H
 
-struct string_list;
+#include "string-list.h"
+#include "strbuf.h"
 
 struct cmdnames {
        int alloc;
@@ -21,6 +22,7 @@ static inline void mput_char(char c, unsigned int num)
 extern void list_common_cmds_help(void);
 extern void list_all_cmds_help(void);
 extern void list_common_guides_help(void);
+extern void list_config_help(void);
 
 extern void list_all_main_cmds(struct string_list *list);
 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
  * ref to the command, to give suggested "correct" refs.
  */
 extern void help_unknown_ref(const char *ref, const char *cmd, const char *error);
+
+static inline void list_config_item(struct string_list *list,
+                                   const char *prefix,
+                                   const char *str)
+{
+       string_list_append_nodup(list, xstrfmt("%s.%s", prefix, str));
+}
+
+#define define_list_config_array(array)                                        \
+void list_config_##array(struct string_list *list, const char *prefix) \
+{                                                                      \
+       int i;                                                          \
+       for (i = 0; i < ARRAY_SIZE(array); i++)                         \
+               if (array[i])                                           \
+                       list_config_item(list, prefix, array[i]);       \
+}                                                                      \
+struct string_list
+
+#define define_list_config_array_extra(array, values)                  \
+void list_config_##array(struct string_list *list, const char *prefix) \
+{                                                                      \
+       int i;                                                          \
+       static const char *extra[] = values;                            \
+       for (i = 0; i < ARRAY_SIZE(extra); i++)                         \
+               list_config_item(list, prefix, extra[i]);               \
+       for (i = 0; i < ARRAY_SIZE(array); i++)                         \
+               if (array[i])                                           \
+                       list_config_item(list, prefix, array[i]);       \
+}                                                                      \
+struct string_list
+
+/* These are actually scattered over many C files */
+void list_config_advices(struct string_list *list, const char *prefix);
+void list_config_color_branch_slots(struct string_list *list, const char *prefix);
+void list_config_color_decorate_slots(struct string_list *list, const char *prefix);
+void list_config_color_diff_slots(struct string_list *list, const char *prefix);
+void list_config_color_grep_slots(struct string_list *list, const char *prefix);
+void list_config_color_interactive_slots(struct string_list *list, const char *prefix);
+void list_config_color_status_slots(struct string_list *list, const char *prefix);
+void list_config_fsck_msg_ids(struct string_list *list, const char *prefix);
+
 #endif /* HELP_H */
index d85e179077a451aa8c1879b7e7f8fa75d7814e42..30706fe9266b2082494170e4362b03c820a0bee5 100644 (file)
@@ -12,6 +12,7 @@
 #include "gpg-interface.h"
 #include "sequencer.h"
 #include "line-log.h"
+#include "help.h"
 
 static struct decoration name_decoration = { "object names" };
 static int decoration_loaded;
@@ -42,6 +43,8 @@ static const char *decorate_get_color(int decorate_use_color, enum decoration_ty
        return "";
 }
 
+define_list_config_array(color_decorate_slots);
+
 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
 {
        int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);