config.c: plumb the_reader through callbacks

The remaining references to "cf_global" are in config callback functions. Remove them by plumbing "struct config_reader" via the "*data" arg. In both of the callbacks here, we are only reading from "reader->source". So in the long run, if we had a way to expose readonly information from "reader->source" (probably in the form of "struct key_value_info"), we could undo this patch (i.e. remove "struct config_reader" fom "*data"). Signed-off-by: Glen Choo <chooglen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Glen Choo committed Mar 28, 2023 at 17:51 UTC a798a56c8aea492a2078c40e16a4a46f3fea5133
1 file changed +43 -31
config.c
+43 -31
@@ -62,6 +62,9 @@ struct config_reader {
62 static struct config_reader the_reader;
63
64 /*
65 + * FIXME The comments are temporarily out of date since "cf_global" has been
66 + * moved to the_reader, but not current_*.
67 + *
68 * These variables record the "current" config source, which
69 * can be accessed by parsing callbacks.
70 *
@@ -76,11 +79,7 @@ static struct config_reader the_reader;
79 * at the variables, it's either a bug for it to be called in the first place,
80 * or it's a function which can be reused for non-config purposes, and should
81 * fall back to some sane behavior).
79 - *
80 - * FIXME "cf_global" has been replaced by "the_reader.source", remove
81 - * "cf_global" once we plumb "the_reader" through all of the callback functions.
82 */
83 -static struct config_source *cf_global;
83 static struct key_value_info *current_config_kvi;
84
85 /*
@@ -98,8 +97,6 @@ static inline void config_reader_push_source(struct config_reader *reader,
97 {
98 top->prev = reader->source;
99 reader->source = top;
101 - /* FIXME remove this when cf_global is removed. */
102 - cf_global = reader->source;
100 }
101
102 static inline struct config_source *config_reader_pop_source(struct config_reader *reader)
@@ -109,8 +106,6 @@ static inline struct config_source *config_reader_pop_source(struct config_reade
106 BUG("tried to pop config source, but we weren't reading config");
107 ret = reader->source;
108 reader->source = reader->source->prev;
112 - /* FIXME remove this when cf_global is removed. */
113 - cf_global = reader->source;
109 return ret;
110 }
111
@@ -175,6 +170,7 @@ struct config_include_data {
170 void *data;
171 const struct config_options *opts;
172 struct git_config_source *config_source;
173 + struct config_reader *config_reader;
174
175 /*
176 * All remote URLs discovered when reading all config files.
@@ -465,6 +461,7 @@ static int include_condition_is_true(struct config_source *cf,
461 static int git_config_include(const char *var, const char *value, void *data)
462 {
463 struct config_include_data *inc = data;
464 + struct config_source *cf = inc->config_reader->source;
465 const char *cond, *key;
466 size_t cond_len;
467 int ret;
@@ -478,16 +475,16 @@ static int git_config_include(const char *var, const char *value, void *data)
475 return ret;
476
477 if (!strcmp(var, "include.path"))
481 - ret = handle_path_include(cf_global, value, inc);
478 + ret = handle_path_include(cf, value, inc);
479
480 if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
484 - cond && include_condition_is_true(cf_global, inc, cond, cond_len) &&
481 + cond && include_condition_is_true(cf, inc, cond, cond_len) &&
482 !strcmp(key, "path")) {
483 config_fn_t old_fn = inc->fn;
484
485 if (inc->opts->unconditional_remote_url)
486 inc->fn = forbid_remote_url;
490 - ret = handle_path_include(cf_global, value, inc);
487 + ret = handle_path_include(cf, value, inc);
488 inc->fn = old_fn;
489 }
490
@@ -2228,6 +2225,7 @@ int config_with_options(config_fn_t fn, void *data,
2225 inc.data = data;
2226 inc.opts = opts;
2227 inc.config_source = config_source;
2228 + inc.config_reader = &the_reader;
2229 fn = git_config_include;
2230 data = &inc;
2231 }
@@ -2348,7 +2346,9 @@ static struct config_set_element *configset_find_element(struct config_set *cs,
2346 return found_entry;
2347 }
2348
2351 -static int configset_add_value(struct config_set *cs, const char *key, const char *value)
2349 +static int configset_add_value(struct config_reader *reader,
2350 + struct config_set *cs, const char *key,
2351 + const char *value)
2352 {
2353 struct config_set_element *e;
2354 struct string_list_item *si;
@@ -2374,12 +2374,12 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
2374 l_item->e = e;
2375 l_item->value_index = e->value_list.nr - 1;
2376
2377 - if (!cf_global)
2377 + if (!reader->source)
2378 BUG("configset_add_value has no source");
2379 - if (cf_global->name) {
2380 - kv_info->filename = strintern(cf_global->name);
2381 - kv_info->linenr = cf_global->linenr;
2382 - kv_info->origin_type = cf_global->origin_type;
2379 + if (reader->source->name) {
2380 + kv_info->filename = strintern(reader->source->name);
2381 + kv_info->linenr = reader->source->linenr;
2382 + kv_info->origin_type = reader->source->origin_type;
2383 } else {
2384 /* for values read from `git_config_from_parameters()` */
2385 kv_info->filename = NULL;
@@ -2434,16 +2434,25 @@ void git_configset_clear(struct config_set *cs)
2434 cs->list.items = NULL;
2435 }
2436
2437 +struct configset_add_data {
2438 + struct config_set *config_set;
2439 + struct config_reader *config_reader;
2440 +};
2441 +#define CONFIGSET_ADD_INIT { 0 }
2442 +
2443 static int config_set_callback(const char *key, const char *value, void *cb)
2444 {
2439 - struct config_set *cs = cb;
2440 - configset_add_value(cs, key, value);
2445 + struct configset_add_data *data = cb;
2446 + configset_add_value(data->config_reader, data->config_set, key, value);
2447 return 0;
2448 }
2449
2450 int git_configset_add_file(struct config_set *cs, const char *filename)
2451 {
2446 - return git_config_from_file(config_set_callback, filename, cs);
2452 + struct configset_add_data data = CONFIGSET_ADD_INIT;
2453 + data.config_reader = &the_reader;
2454 + data.config_set = cs;
2455 + return git_config_from_file(config_set_callback, filename, &data);
2456 }
2457
2458 int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
@@ -2558,6 +2567,7 @@ int git_configset_get_pathname(struct config_set *cs, const char *key, const cha
2567 static void repo_read_config(struct repository *repo)
2568 {
2569 struct config_options opts = { 0 };
2570 + struct configset_add_data data = CONFIGSET_ADD_INIT;
2571
2572 opts.respect_includes = 1;
2573 opts.commondir = repo->commondir;
@@ -2569,8 +2579,10 @@ static void repo_read_config(struct repository *repo)
2579 git_configset_clear(repo->config);
2580
2581 git_configset_init(repo->config);
2582 + data.config_set = repo->config;
2583 + data.config_reader = &the_reader;
2584
2573 - if (config_with_options(config_set_callback, repo->config, NULL, &opts) < 0)
2585 + if (config_with_options(config_set_callback, &data, NULL, &opts) < 0)
2586 /*
2587 * config_with_options() normally returns only
2588 * zero, as most errors are fatal, and
@@ -2696,9 +2708,12 @@ static void read_protected_config(void)
2708 .ignore_worktree = 1,
2709 .system_gently = 1,
2710 };
2711 + struct configset_add_data data = CONFIGSET_ADD_INIT;
2712 +
2713 git_configset_init(&protected_config);
2700 - config_with_options(config_set_callback, &protected_config,
2701 - NULL, &opts);
2714 + data.config_set = &protected_config;
2715 + data.config_reader = &the_reader;
2716 + config_with_options(config_set_callback, &data, NULL, &opts);
2717 }
2718
2719 void git_protected_config(config_fn_t fn, void *data)
@@ -2883,6 +2898,7 @@ void git_die_config(const char *key, const char *err, ...)
2898 */
2899
2900 struct config_store_data {
2901 + struct config_reader *config_reader;
2902 size_t baselen;
2903 char *key;
2904 int do_not_match;
@@ -2897,6 +2913,7 @@ struct config_store_data {
2913 unsigned int parsed_nr, parsed_alloc, *seen, seen_nr, seen_alloc;
2914 unsigned int key_seen:1, section_seen:1, is_keys_section:1;
2915 };
2916 +#define CONFIG_STORE_INIT { 0 }
2917
2918 static void config_store_data_clear(struct config_store_data *store)
2919 {
@@ -2931,12 +2948,7 @@ static int store_aux_event(enum config_event_t type,
2948 size_t begin, size_t end, void *data)
2949 {
2950 struct config_store_data *store = data;
2934 - /*
2935 - * FIXME Keep using "cf" so that we can avoid rewrapping a
2936 - * really long line below. Remove this when "cf" gets plumbed
2937 - * correctly.
2938 - */
2939 - struct config_source *cf = cf_global;
2951 + struct config_source *cf = store->config_reader->source;
2952
2953 ALLOC_GROW(store->parsed, store->parsed_nr + 1, store->parsed_alloc);
2954 store->parsed[store->parsed_nr].begin = begin;
@@ -3254,9 +3266,9 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
3266 char *filename_buf = NULL;
3267 char *contents = NULL;
3268 size_t contents_sz;
3257 - struct config_store_data store;
3269 + struct config_store_data store = CONFIG_STORE_INIT;
3270
3259 - memset(&store, 0, sizeof(store));
3271 + store.config_reader = &the_reader;
3272
3273 /* parse-key returns negative; flip the sign to feed exit(3) */
3274 ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);