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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed May 15, 2024 at 08:41 UTC 0336d0055c4a15916fe6fabbe8031efa042412c0
1 file changed +24 -19
builtin/config.c
+24 -19
@@ -1069,31 +1069,13 @@ static struct option builtin_subcommand_options[] = {
1069 OPT_END(),
1070 };
1071
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)
1073 {
1074 char *value = NULL, *comment = NULL;
1075 int flags = 0;
1076 int ret = 0;
1077 struct key_value_info default_kvi = KVI_INIT;
1078
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 -
1079 argc = parse_options(argc, argv, prefix, builtin_config_options,
1080 builtin_config_usage,
1081 PARSE_OPT_STOP_AT_NON_OPTION);
@@ -1306,3 +1288,26 @@ int cmd_config(int argc, const char **argv, const char *prefix)
1288 free(value);
1289 return ret;
1290 }
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 +}