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

Patrick Steinhardt committed May 15, 2024 at 08:42 UTC 040b141df39a952af936abf73f4e2fa5fc9954b5
1 file changed +5 -2
builtin/config.c
+5 -2
@@ -123,7 +123,6 @@ struct config_display_options {
123 .key_delim = ' ', \
124 }
125
126 -static char *key;
126 static int use_key_regexp;
127 static int do_all;
128 static int fixed_value;
@@ -325,6 +324,7 @@ struct collect_config_data {
324 const struct config_display_options *display_opts;
325 struct strbuf_list *values;
326 const char *value_pattern;
327 + const char *key;
328 regex_t *regexp;
329 regex_t *key_regexp;
330 int do_not_match;
@@ -337,7 +337,7 @@ static int collect_config(const char *key_, const char *value_,
337 struct strbuf_list *values = data->values;
338 const struct key_value_info *kvi = ctx->kvi;
339
340 - if (!use_key_regexp && strcmp(key_, key))
340 + if (!use_key_regexp && strcmp(key_, data->key))
341 return 0;
342 if (use_key_regexp && regexec(data->key_regexp, key_, 0, NULL, 0))
343 return 0;
@@ -364,6 +364,7 @@ static int get_value(const struct config_location_options *opts,
364 .display_opts = display_opts,
365 .values = &values,
366 };
367 + char *key = NULL;
368 int i;
369
370 if (use_key_regexp) {
@@ -395,6 +396,8 @@ static int get_value(const struct config_location_options *opts,
396 ret = CONFIG_INVALID_KEY;
397 goto free_strings;
398 }
399 +
400 + data.key = key;
401 }
402
403 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))