builtin/config: convert `key_regexp` to a local variable

The `key_regexp` variable is used by the `format_config()` callback when `use_key_regexp` is set. It is only ever set up by its only caller, `collect_config()` and can thus easily be moved into the `collect_config_data` structure. Do so to remove 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 fdfaaa1b68f61eccd7423da558e9c69e3c7bb908
1 file changed +8 -8
builtin/config.c
+8 -8
@@ -124,7 +124,6 @@ struct config_display_options {
124 }
125
126 static char *key;
127 -static regex_t *key_regexp;
127 static int use_key_regexp;
128 static int do_all;
129 static int fixed_value;
@@ -327,6 +326,7 @@ struct collect_config_data {
326 struct strbuf_list *values;
327 const char *value_pattern;
328 regex_t *regexp;
329 + regex_t *key_regexp;
330 int do_not_match;
331 };
332
@@ -339,7 +339,7 @@ static int collect_config(const char *key_, const char *value_,
339
340 if (!use_key_regexp && strcmp(key_, key))
341 return 0;
342 - if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
342 + if (use_key_regexp && regexec(data->key_regexp, key_, 0, NULL, 0))
343 return 0;
344 if (fixed_value && strcmp(data->value_pattern, (value_?value_:"")))
345 return 0;
@@ -383,10 +383,10 @@ static int get_value(const struct config_location_options *opts,
383 for (tl = key; *tl && *tl != '.'; tl++)
384 *tl = tolower(*tl);
385
386 - key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
387 - if (regcomp(key_regexp, key, REG_EXTENDED)) {
386 + data.key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
387 + if (regcomp(data.key_regexp, key, REG_EXTENDED)) {
388 error(_("invalid key pattern: %s"), key_);
389 - FREE_AND_NULL(key_regexp);
389 + FREE_AND_NULL(data.key_regexp);
390 ret = CONFIG_INVALID_PATTERN;
391 goto free_strings;
392 }
@@ -444,9 +444,9 @@ static int get_value(const struct config_location_options *opts,
444
445 free_strings:
446 free(key);
447 - if (key_regexp) {
448 - regfree(key_regexp);
449 - free(key_regexp);
447 + if (data.key_regexp) {
448 + regfree(data.key_regexp);
449 + free(data.key_regexp);
450 }
451 if (data.regexp) {
452 regfree(data.regexp);