diff mbox series

[01/14] config.c: introduce kvi_fn(), use it for configsets

Message ID 49bc2f6eedc923be631379c9b65956855ec25f35.1682104398.git.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series config: remove global state from config iteration | expand

Commit Message

Glen Choo April 21, 2023, 7:13 p.m. UTC
From: Glen Choo <chooglen@google.com>

In order to pass "struct key_value_info" to config callbacks in a future
commit, we want to make sure that a "struct key_value_info" is available
in all code paths that invoke a config callback.

Enforce this by introducing a helper function, "kvi_fn()", that sets
"the_reader.config_kvi" before invoking the config callback, and
trivially use it in "configset_iter()". This closely simulates passing
an extra "struct key_value_info" such that making the switch will be
trivial.

This breaks our rule that no underlying machinery uses "the_reader", but
"struct config_reader" will be gone by the end of the series so there's
no point plumbing it as an arg.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 config.c | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/config.c b/config.c
index 493f47df8ae..daad892e4fd 100644
--- a/config.c
+++ b/config.c
@@ -660,6 +660,17 @@  out_free_ret_1:
 	return -CONFIG_INVALID_KEY;
 }
 
+static int kvi_fn(config_fn_t fn, const char *key, const char *value,
+		  struct key_value_info *kvi,
+		  void *data)
+{
+	int ret;
+	config_reader_set_kvi(&the_reader, kvi);
+	ret = fn(key, value, data);
+	config_reader_set_kvi(&the_reader, NULL);
+	return ret;
+}
+
 static int config_parse_pair(const char *key, const char *value,
 			  config_fn_t fn, void *data)
 {
@@ -2288,27 +2299,24 @@  int config_with_options(config_fn_t fn, void *data,
 	return ret;
 }
 
-static void configset_iter(struct config_reader *reader, struct config_set *set,
-			   config_fn_t fn, void *data)
+static void configset_iter(struct config_set *set, config_fn_t fn, void *data)
 {
 	int i, value_index;
 	struct string_list *values;
 	struct config_set_element *entry;
 	struct configset_list *list = &set->list;
+	struct key_value_info *kvi;
 
 	for (i = 0; i < list->nr; i++) {
 		entry = list->items[i].e;
 		value_index = list->items[i].value_index;
 		values = &entry->value_list;
+		kvi = values->items[value_index].util;
 
-		config_reader_set_kvi(reader, values->items[value_index].util);
-
-		if (fn(entry->key, values->items[value_index].string, data) < 0)
-			git_die_config_linenr(entry->key,
-					      reader->config_kvi->filename,
-					      reader->config_kvi->linenr);
-
-		config_reader_set_kvi(reader, NULL);
+		if (kvi_fn(fn, entry->key, values->items[value_index].string,
+			   kvi, data) < 0)
+			git_die_config_linenr(entry->key, kvi->filename,
+					      kvi->linenr);
 	}
 }
 
@@ -2696,7 +2704,7 @@  static void repo_config_clear(struct repository *repo)
 void repo_config(struct repository *repo, config_fn_t fn, void *data)
 {
 	git_config_check_init(repo);
-	configset_iter(&the_reader, repo->config, fn, data);
+	configset_iter(repo->config, fn, data);
 }
 
 int repo_config_get(struct repository *repo, const char *key)
@@ -2815,7 +2823,7 @@  void git_protected_config(config_fn_t fn, void *data)
 {
 	if (!protected_config.hash_initialized)
 		read_protected_config();
-	configset_iter(&the_reader, &protected_config, fn, data);
+	configset_iter(&protected_config, fn, data);
 }
 
 /* Functions used historically to read configuration from 'the_repository' */