]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/config: move subcommand options into `cmd_config()`
authorPatrick Steinhardt <ps@pks.im>
Wed, 15 May 2024 06:41:47 +0000 (08:41 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 May 2024 14:17:52 +0000 (07:17 -0700)
Move the subcommand options as well as the `subcommand` variable into
`cmd_config()`. This reduces our reliance on global state.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/config.c

index f6c7e7a082bc9b850ba6435a0acfbdd4ea3ef683..58c8b0abda683f55acb5c00211e7ea4d8dd02da2 100644 (file)
@@ -75,7 +75,6 @@ static char delim = '=';
 static char key_delim = ' ';
 static char term = '\n';
 
-static parse_opt_subcommand_fn *subcommand;
 static int use_global_config, use_system_config, use_local_config;
 static int use_worktree_config;
 static struct git_config_source given_config_source;
@@ -1058,17 +1057,6 @@ static int cmd_config_edit(int argc, const char **argv, const char *prefix)
        return show_editor();
 }
 
-static struct option builtin_subcommand_options[] = {
-       OPT_SUBCOMMAND("list", &subcommand, cmd_config_list),
-       OPT_SUBCOMMAND("get", &subcommand, cmd_config_get),
-       OPT_SUBCOMMAND("set", &subcommand, cmd_config_set),
-       OPT_SUBCOMMAND("unset", &subcommand, cmd_config_unset),
-       OPT_SUBCOMMAND("rename-section", &subcommand, cmd_config_rename_section),
-       OPT_SUBCOMMAND("remove-section", &subcommand, cmd_config_remove_section),
-       OPT_SUBCOMMAND("edit", &subcommand, cmd_config_edit),
-       OPT_END(),
-};
-
 static int cmd_config_actions(int argc, const char **argv, const char *prefix)
 {
        char *value = NULL, *comment = NULL;
@@ -1291,6 +1279,18 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
 
 int cmd_config(int argc, const char **argv, const char *prefix)
 {
+       parse_opt_subcommand_fn *subcommand = NULL;
+       struct option subcommand_opts[] = {
+               OPT_SUBCOMMAND("list", &subcommand, cmd_config_list),
+               OPT_SUBCOMMAND("get", &subcommand, cmd_config_get),
+               OPT_SUBCOMMAND("set", &subcommand, cmd_config_set),
+               OPT_SUBCOMMAND("unset", &subcommand, cmd_config_unset),
+               OPT_SUBCOMMAND("rename-section", &subcommand, cmd_config_rename_section),
+               OPT_SUBCOMMAND("remove-section", &subcommand, cmd_config_remove_section),
+               OPT_SUBCOMMAND("edit", &subcommand, cmd_config_edit),
+               OPT_END(),
+       };
+
        given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
 
        /*
@@ -1301,10 +1301,10 @@ int cmd_config(int argc, const char **argv, const char *prefix)
         * line in case no subcommand was given, which would otherwise confuse
         * us when parsing the legacy-style modes that don't use subcommands.
         */
-       argc = parse_options(argc, argv, prefix, builtin_subcommand_options, builtin_config_usage,
+       argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage,
                             PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_ARGV0|PARSE_OPT_KEEP_UNKNOWN_OPT);
        if (subcommand) {
-               argc = parse_options(argc, argv, prefix, builtin_subcommand_options, builtin_config_usage,
+               argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage,
                       PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_UNKNOWN_OPT);
                return subcommand(argc, argv, prefix);
        }