builtin/config: convert flags to a local variable

Both the `do_all` and `use_key_regexp` bits essentially act like flags to `get_value()`. Let's convert them to actual flags so that we can get rid of the last two remaining global variables that track options. 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:43 UTC 35a7cfda56f85c8fccfbf7e8b5bae9a356dd0d45
1 file changed +29 -19
builtin/config.c
+29 -19
@@ -123,9 +123,6 @@ struct config_display_options {
123 .key_delim = ' ', \
124 }
125
126 -static int use_key_regexp;
127 -static int do_all;
128 -
126 #define TYPE_BOOL 1
127 #define TYPE_INT 2
128 #define TYPE_BOOL_OR_INT 3
@@ -319,6 +316,9 @@ static int format_config(const struct config_display_options *opts,
316 return 0;
317 }
318
319 +#define GET_VALUE_ALL (1 << 0)
320 +#define GET_VALUE_KEY_REGEXP (1 << 1)
321 +
322 struct collect_config_data {
323 const struct config_display_options *display_opts;
324 struct strbuf_list *values;
@@ -327,6 +327,7 @@ struct collect_config_data {
327 regex_t *regexp;
328 regex_t *key_regexp;
329 int do_not_match;
330 + unsigned get_value_flags;
331 unsigned flags;
332 };
333
@@ -337,9 +338,11 @@ static int collect_config(const char *key_, const char *value_,
338 struct strbuf_list *values = data->values;
339 const struct key_value_info *kvi = ctx->kvi;
340
340 - if (!use_key_regexp && strcmp(key_, data->key))
341 + if (!(data->get_value_flags & GET_VALUE_KEY_REGEXP) &&
342 + strcmp(key_, data->key))
343 return 0;
342 - if (use_key_regexp && regexec(data->key_regexp, key_, 0, NULL, 0))
344 + if ((data->get_value_flags & GET_VALUE_KEY_REGEXP) &&
345 + regexec(data->key_regexp, key_, 0, NULL, 0))
346 return 0;
347 if ((data->flags & CONFIG_FLAGS_FIXED_VALUE) &&
348 strcmp(data->value_pattern, (value_?value_:"")))
@@ -357,19 +360,21 @@ static int collect_config(const char *key_, const char *value_,
360
361 static int get_value(const struct config_location_options *opts,
362 const struct config_display_options *display_opts,
360 - const char *key_, const char *regex_, unsigned flags)
363 + const char *key_, const char *regex_,
364 + unsigned get_value_flags, unsigned flags)
365 {
366 int ret = CONFIG_GENERIC_ERROR;
367 struct strbuf_list values = {NULL};
368 struct collect_config_data data = {
369 .display_opts = display_opts,
370 .values = &values,
371 + .get_value_flags = get_value_flags,
372 .flags = flags,
373 };
374 char *key = NULL;
375 int i;
376
372 - if (use_key_regexp) {
377 + if (get_value_flags & GET_VALUE_KEY_REGEXP) {
378 char *tl;
379
380 /*
@@ -441,7 +446,7 @@ static int get_value(const struct config_location_options *opts,
446
447 for (i = 0; i < values.nr; i++) {
448 struct strbuf *buf = values.items + i;
444 - if (do_all || i == values.nr - 1)
449 + if ((get_value_flags & GET_VALUE_ALL) || i == values.nr - 1)
450 fwrite(buf->buf, 1, buf->len, stdout);
451 strbuf_release(buf);
452 }
@@ -848,11 +853,12 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix)
853 struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
854 const char *value_pattern = NULL, *url = NULL;
855 int flags = 0;
856 + unsigned get_value_flags = 0;
857 struct option opts[] = {
858 CONFIG_LOCATION_OPTIONS(location_opts),
859 OPT_GROUP(N_("Filter options")),
854 - OPT_BOOL(0, "all", &do_all, N_("return all values for multi-valued config options")),
855 - OPT_BOOL(0, "regexp", &use_key_regexp, N_("interpret the name as a regular expression")),
860 + OPT_BIT(0, "all", &get_value_flags, N_("return all values for multi-valued config options"), GET_VALUE_ALL),
861 + OPT_BIT(0, "regexp", &get_value_flags, N_("interpret the name as a regular expression"), GET_VALUE_KEY_REGEXP),
862 OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")),
863 OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
864 OPT_STRING(0, "url", &url, N_("URL"), N_("show config matching the given URL")),
@@ -872,9 +878,12 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix)
878
879 if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern)
880 die(_("--fixed-value only applies with 'value-pattern'"));
875 - if (display_opts.default_value && (do_all || url))
881 + if (display_opts.default_value &&
882 + ((get_value_flags & GET_VALUE_ALL) || url))
883 die(_("--default= cannot be used with --all or --url="));
877 - if (url && (do_all || use_key_regexp || value_pattern))
884 + if (url && ((get_value_flags & GET_VALUE_ALL) ||
885 + (get_value_flags & GET_VALUE_KEY_REGEXP) ||
886 + value_pattern))
887 die(_("--url= cannot be used with --all, --regexp or --value"));
888
889 location_options_init(&location_opts, prefix);
@@ -885,7 +894,8 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix)
894 if (url)
895 ret = get_urlmatch(&location_opts, &display_opts, argv[0], url);
896 else
888 - ret = get_value(&location_opts, &display_opts, argv[0], value_pattern, flags);
897 + ret = get_value(&location_opts, &display_opts, argv[0], value_pattern,
898 + get_value_flags, flags);
899
900 location_options_release(&location_opts);
901 return ret;
@@ -1290,19 +1300,19 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1300 }
1301 else if (actions == ACTION_GET) {
1302 check_argc(argc, 1, 2);
1293 - ret = get_value(&location_opts, &display_opts, argv[0], argv[1], flags);
1303 + ret = get_value(&location_opts, &display_opts, argv[0], argv[1],
1304 + 0, flags);
1305 }
1306 else if (actions == ACTION_GET_ALL) {
1296 - do_all = 1;
1307 check_argc(argc, 1, 2);
1298 - ret = get_value(&location_opts, &display_opts, argv[0], argv[1], flags);
1308 + ret = get_value(&location_opts, &display_opts, argv[0], argv[1],
1309 + GET_VALUE_ALL, flags);
1310 }
1311 else if (actions == ACTION_GET_REGEXP) {
1312 display_opts.show_keys = 1;
1302 - use_key_regexp = 1;
1303 - do_all = 1;
1313 check_argc(argc, 1, 2);
1305 - ret = get_value(&location_opts, &display_opts, argv[0], argv[1], flags);
1314 + ret = get_value(&location_opts, &display_opts, argv[0], argv[1],
1315 + GET_VALUE_ALL|GET_VALUE_KEY_REGEXP, flags);
1316 }
1317 else if (actions == ACTION_GET_URLMATCH) {
1318 check_argc(argc, 2, 2);