Skip to content

Commit 040b141

Browse files
pks-tgitster
authored andcommitted
builtin/config: convert key to a local variable
The `key` variable is used by the `get_value()` function for two purposes: - It is used to store the result of `git_config_parse_key()`, which is then passed on to `collect_config()`. - It is used as a store to convert the provided key to an all-lowercase key when `use_key_regexp` is set. Neither of these cases warrant a global variable at all. In the former case we can pass the key via `struct collect_config_data`. And in the latter case we really only want to have it as a temporary local variable such that we can free associated memory. Refactor the code accordingly to reduce our reliance on global state. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fdfaaa1 commit 040b141

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

builtin/config.c

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

126-
static char *key;
127126
static int use_key_regexp;
128127
static int do_all;
129128
static int fixed_value;
@@ -325,6 +324,7 @@ struct collect_config_data {
325324
const struct config_display_options *display_opts;
326325
struct strbuf_list *values;
327326
const char *value_pattern;
327+
const char *key;
328328
regex_t *regexp;
329329
regex_t *key_regexp;
330330
int do_not_match;
@@ -337,7 +337,7 @@ static int collect_config(const char *key_, const char *value_,
337337
struct strbuf_list *values = data->values;
338338
const struct key_value_info *kvi = ctx->kvi;
339339

340-
if (!use_key_regexp && strcmp(key_, key))
340+
if (!use_key_regexp && strcmp(key_, data->key))
341341
return 0;
342342
if (use_key_regexp && regexec(data->key_regexp, key_, 0, NULL, 0))
343343
return 0;
@@ -364,6 +364,7 @@ static int get_value(const struct config_location_options *opts,
364364
.display_opts = display_opts,
365365
.values = &values,
366366
};
367+
char *key = NULL;
367368
int i;
368369

369370
if (use_key_regexp) {
@@ -395,6 +396,8 @@ static int get_value(const struct config_location_options *opts,
395396
ret = CONFIG_INVALID_KEY;
396397
goto free_strings;
397398
}
399+
400+
data.key = key;
398401
}
399402

400403
if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))

0 commit comments

Comments
 (0)