Skip to content

Commit 35a7cfd

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ab8bac8 commit 35a7cfd

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

builtin/config.c

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ struct config_display_options {
123123
.key_delim = ' ', \
124124
}
125125

126-
static int use_key_regexp;
127-
static int do_all;
128-
129126
#define TYPE_BOOL 1
130127
#define TYPE_INT 2
131128
#define TYPE_BOOL_OR_INT 3
@@ -319,6 +316,9 @@ static int format_config(const struct config_display_options *opts,
319316
return 0;
320317
}
321318

319+
#define GET_VALUE_ALL (1 << 0)
320+
#define GET_VALUE_KEY_REGEXP (1 << 1)
321+
322322
struct collect_config_data {
323323
const struct config_display_options *display_opts;
324324
struct strbuf_list *values;
@@ -327,6 +327,7 @@ struct collect_config_data {
327327
regex_t *regexp;
328328
regex_t *key_regexp;
329329
int do_not_match;
330+
unsigned get_value_flags;
330331
unsigned flags;
331332
};
332333

@@ -337,9 +338,11 @@ static int collect_config(const char *key_, const char *value_,
337338
struct strbuf_list *values = data->values;
338339
const struct key_value_info *kvi = ctx->kvi;
339340

340-
if (!use_key_regexp && strcmp(key_, data->key))
341+
if (!(data->get_value_flags & GET_VALUE_KEY_REGEXP) &&
342+
strcmp(key_, data->key))
341343
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))
343346
return 0;
344347
if ((data->flags & CONFIG_FLAGS_FIXED_VALUE) &&
345348
strcmp(data->value_pattern, (value_?value_:"")))
@@ -357,19 +360,21 @@ static int collect_config(const char *key_, const char *value_,
357360

358361
static int get_value(const struct config_location_options *opts,
359362
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)
361365
{
362366
int ret = CONFIG_GENERIC_ERROR;
363367
struct strbuf_list values = {NULL};
364368
struct collect_config_data data = {
365369
.display_opts = display_opts,
366370
.values = &values,
371+
.get_value_flags = get_value_flags,
367372
.flags = flags,
368373
};
369374
char *key = NULL;
370375
int i;
371376

372-
if (use_key_regexp) {
377+
if (get_value_flags & GET_VALUE_KEY_REGEXP) {
373378
char *tl;
374379

375380
/*
@@ -441,7 +446,7 @@ static int get_value(const struct config_location_options *opts,
441446

442447
for (i = 0; i < values.nr; i++) {
443448
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)
445450
fwrite(buf->buf, 1, buf->len, stdout);
446451
strbuf_release(buf);
447452
}
@@ -848,11 +853,12 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix)
848853
struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
849854
const char *value_pattern = NULL, *url = NULL;
850855
int flags = 0;
856+
unsigned get_value_flags = 0;
851857
struct option opts[] = {
852858
CONFIG_LOCATION_OPTIONS(location_opts),
853859
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),
856862
OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")),
857863
OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
858864
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)
872878

873879
if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern)
874880
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))
876883
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))
878887
die(_("--url= cannot be used with --all, --regexp or --value"));
879888

880889
location_options_init(&location_opts, prefix);
@@ -885,7 +894,8 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix)
885894
if (url)
886895
ret = get_urlmatch(&location_opts, &display_opts, argv[0], url);
887896
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);
889899

890900
location_options_release(&location_opts);
891901
return ret;
@@ -1290,19 +1300,19 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
12901300
}
12911301
else if (actions == ACTION_GET) {
12921302
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);
12941305
}
12951306
else if (actions == ACTION_GET_ALL) {
1296-
do_all = 1;
12971307
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);
12991310
}
13001311
else if (actions == ACTION_GET_REGEXP) {
13011312
display_opts.show_keys = 1;
1302-
use_key_regexp = 1;
1303-
do_all = 1;
13041313
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);
13061316
}
13071317
else if (actions == ACTION_GET_URLMATCH) {
13081318
check_argc(argc, 2, 2);

0 commit comments

Comments
 (0)