diff mbox series

[18/21] builtin/config: convert `key` to a local variable

Message ID 2aee7ec5d8dcd301decec75c6d0fd49da700ac3a.1715339393.git.ps@pks.im (mailing list archive)
State Superseded
Headers show
Series builtin/config: remove global state | expand

Commit Message

Patrick Steinhardt May 10, 2024, 11:25 a.m. UTC
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 si
    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.

Both of these cases don't 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>
---
 builtin/config.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/builtin/config.c b/builtin/config.c
index 1c6d9c734c..8cedf67a4f 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -122,7 +122,6 @@  struct config_display_options {
 	.key_delim = ' ', \
 }
 
-static char *key;
 static int use_key_regexp;
 static int do_all;
 static int fixed_value;
@@ -324,6 +323,7 @@  struct collect_config_data {
 	const struct config_display_options *display_opts;
 	struct strbuf_list *values;
 	const char *value_pattern;
+	const char *key;
 	regex_t *regexp;
 	regex_t *key_regexp;
 	int do_not_match;
@@ -336,7 +336,7 @@  static int collect_config(const char *key_, const char *value_,
 	struct strbuf_list *values = data->values;
 	const struct key_value_info *kvi = ctx->kvi;
 
-	if (!use_key_regexp && strcmp(key_, key))
+	if (!use_key_regexp && strcmp(key_, data->key))
 		return 0;
 	if (use_key_regexp && regexec(data->key_regexp, key_, 0, NULL, 0))
 		return 0;
@@ -363,6 +363,7 @@  static int get_value(const struct config_location_options *opts,
 		.display_opts = display_opts,
 		.values = &values,
 	};
+	char *key = NULL;
 	int i;
 
 	if (use_key_regexp) {
@@ -394,6 +395,8 @@  static int get_value(const struct config_location_options *opts,
 			ret = CONFIG_INVALID_KEY;
 			goto free_strings;
 		}
+
+		data.key = key;
 	}
 
 	if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))