Skip to content

Commit 0336d00

Browse files
pks-tgitster
authored andcommitted
builtin/config: move legacy mode into its own function
In `cmd_config()` we first try to parse the provided arguments as subcommands and, if this is successful, call the respective functions of that subcommand. Otherwise we continue with the "legacy" mode that uses implicit actions and/or flags. Disentangle this by moving the legacy mode into its own function. This allows us to move the options into the respective functions and clearly separates concerns. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a577d2f commit 0336d00

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

builtin/config.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,31 +1069,13 @@ static struct option builtin_subcommand_options[] = {
10691069
OPT_END(),
10701070
};
10711071

1072-
int cmd_config(int argc, const char **argv, const char *prefix)
1072+
static int cmd_config_actions(int argc, const char **argv, const char *prefix)
10731073
{
10741074
char *value = NULL, *comment = NULL;
10751075
int flags = 0;
10761076
int ret = 0;
10771077
struct key_value_info default_kvi = KVI_INIT;
10781078

1079-
given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
1080-
1081-
/*
1082-
* This is somewhat hacky: we first parse the command line while
1083-
* keeping all args intact in order to determine whether a subcommand
1084-
* has been specified. If so, we re-parse it a second time, but this
1085-
* time we drop KEEP_ARGV0. This is so that we don't munge the command
1086-
* line in case no subcommand was given, which would otherwise confuse
1087-
* us when parsing the legacy-style modes that don't use subcommands.
1088-
*/
1089-
argc = parse_options(argc, argv, prefix, builtin_subcommand_options, builtin_config_usage,
1090-
PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_ARGV0|PARSE_OPT_KEEP_UNKNOWN_OPT);
1091-
if (subcommand) {
1092-
argc = parse_options(argc, argv, prefix, builtin_subcommand_options, builtin_config_usage,
1093-
PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_UNKNOWN_OPT);
1094-
return subcommand(argc, argv, prefix);
1095-
}
1096-
10971079
argc = parse_options(argc, argv, prefix, builtin_config_options,
10981080
builtin_config_usage,
10991081
PARSE_OPT_STOP_AT_NON_OPTION);
@@ -1306,3 +1288,26 @@ int cmd_config(int argc, const char **argv, const char *prefix)
13061288
free(value);
13071289
return ret;
13081290
}
1291+
1292+
int cmd_config(int argc, const char **argv, const char *prefix)
1293+
{
1294+
given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
1295+
1296+
/*
1297+
* This is somewhat hacky: we first parse the command line while
1298+
* keeping all args intact in order to determine whether a subcommand
1299+
* has been specified. If so, we re-parse it a second time, but this
1300+
* time we drop KEEP_ARGV0. This is so that we don't munge the command
1301+
* line in case no subcommand was given, which would otherwise confuse
1302+
* us when parsing the legacy-style modes that don't use subcommands.
1303+
*/
1304+
argc = parse_options(argc, argv, prefix, builtin_subcommand_options, builtin_config_usage,
1305+
PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_ARGV0|PARSE_OPT_KEEP_UNKNOWN_OPT);
1306+
if (subcommand) {
1307+
argc = parse_options(argc, argv, prefix, builtin_subcommand_options, builtin_config_usage,
1308+
PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_UNKNOWN_OPT);
1309+
return subcommand(argc, argv, prefix);
1310+
}
1311+
1312+
return cmd_config_actions(argc, argv, prefix);
1313+
}

0 commit comments

Comments
 (0)