From: Junio C Hamano Date: Thu, 6 Apr 2023 20:38:29 +0000 (-0700) Subject: Merge branch 'gc/config-parsing-cleanup' X-Git-Tag: v2.41.0-rc0~98 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=06e9e726d463b413d45703b31881de4ed99b3417;p=thirdparty%2Fgit.git Merge branch 'gc/config-parsing-cleanup' Config API clean-up to reduce its dependence on static variables * gc/config-parsing-cleanup: config.c: rename "struct config_source cf" config: report cached filenames in die_bad_number() config.c: remove current_parsing_scope config.c: remove current_config_kvi config.c: plumb the_reader through callbacks config.c: create config_reader and the_reader config.c: don't assign to "cf_global" directly config.c: plumb config_source through static fns --- 06e9e726d463b413d45703b31881de4ed99b3417 diff --cc config.c index a30490fd58,cf571d4528..03f38b621f --- a/config.c +++ b/config.c @@@ -2292,9 -2351,7 +2354,9 @@@ void read_very_early_config(config_fn_ config_with_options(cb, data, NULL, &opts); } -static struct config_set_element *configset_find_element(struct config_set *set, const char *key) +RESULT_MUST_BE_USED - static int configset_find_element(struct config_set *cs, const char *key, ++static int configset_find_element(struct config_set *set, const char *key, + struct config_set_element **dest) { struct config_set_element k; struct config_set_element *found_entry; @@@ -2311,23 -2365,21 +2373,25 @@@ hashmap_entry_init(&k.ent, strhash(normalized_key)); k.key = normalized_key; - found_entry = hashmap_get_entry(&cs->config_hash, &k, ent, NULL); + found_entry = hashmap_get_entry(&set->config_hash, &k, ent, NULL); free(normalized_key); - return found_entry; + *dest = found_entry; + return 0; } - static int configset_add_value(struct config_set *cs, const char *key, const char *value) + static int configset_add_value(struct config_reader *reader, + struct config_set *set, const char *key, + const char *value) { struct config_set_element *e; struct string_list_item *si; struct configset_list_item *l_item; struct key_value_info *kv_info = xmalloc(sizeof(*kv_info)); + int ret; - ret = configset_find_element(cs, key, &e); - e = configset_find_element(set, key); ++ ret = configset_find_element(set, key, &e); + if (ret) + return ret; /* * Since the keys are being fed by git_config*() callback mechanism, they * are already normalized. So simply add them without any further munging. @@@ -2413,79 -2471,41 +2483,82 @@@ static int config_set_callback(const ch return 0; } - int git_configset_add_file(struct config_set *cs, const char *filename) + int git_configset_add_file(struct config_set *set, const char *filename) { - return git_config_from_file(config_set_callback, filename, cs); + struct configset_add_data data = CONFIGSET_ADD_INIT; + data.config_reader = &the_reader; + data.config_set = set; + return git_config_from_file(config_set_callback, filename, &data); } - int git_configset_get_value(struct config_set *cs, const char *key, const char **value) + int git_configset_get_value(struct config_set *set, const char *key, const char **value) { const struct string_list *values = NULL; + int ret; + /* * Follows "last one wins" semantic, i.e., if there are multiple matches for the * queried key in the files of the configset, the value returned will be the last * value in the value list for that key. */ - if ((ret = git_configset_get_value_multi(cs, key, &values))) - values = git_configset_get_value_multi(set, key); ++ if ((ret = git_configset_get_value_multi(set, key, &values))) + return ret; - if (!values) - return 1; assert(values->nr > 0); *value = values->items[values->nr - 1].string; return 0; } - int git_configset_get_value_multi(struct config_set *cs, const char *key, -const struct string_list *git_configset_get_value_multi(struct config_set *set, const char *key) ++int git_configset_get_value_multi(struct config_set *set, const char *key, + const struct string_list **dest) +{ + struct config_set_element *e; + int ret; + - if ((ret = configset_find_element(cs, key, &e))) ++ if ((ret = configset_find_element(set, key, &e))) + return ret; + else if (!e) + return 1; + *dest = &e->value_list; + + return 0; +} + +static int check_multi_string(struct string_list_item *item, void *util) +{ + return item->string ? 0 : config_error_nonbool(util); +} + +int git_configset_get_string_multi(struct config_set *cs, const char *key, + const struct string_list **dest) +{ + int ret; + + if ((ret = git_configset_get_value_multi(cs, key, dest))) + return ret; + if ((ret = for_each_string_list((struct string_list *)*dest, + check_multi_string, (void *)key))) + return ret; + + return 0; +} + - int git_configset_get(struct config_set *cs, const char *key) ++int git_configset_get(struct config_set *set, const char *key) { - struct config_set_element *e = configset_find_element(set, key); - return e ? &e->value_list : NULL; + struct config_set_element *e; + int ret; + - if ((ret = configset_find_element(cs, key, &e))) ++ if ((ret = configset_find_element(set, key, &e))) + return ret; + else if (!e) + return 1; + return 0; } - int git_configset_get_string(struct config_set *cs, const char *key, char **dest) + int git_configset_get_string(struct config_set *set, const char *key, char **dest) { const char *value; - if (!git_configset_get_value(cs, key, &value)) + if (!git_configset_get_value(set, key, &value)) return git_config_string((const char **)dest, key, value); else return 1; @@@ -2615,15 -2638,9 +2691,15 @@@ static void repo_config_clear(struct re void repo_config(struct repository *repo, config_fn_t fn, void *data) { git_config_check_init(repo); - configset_iter(repo->config, fn, data); + configset_iter(&the_reader, repo->config, fn, data); } +int repo_config_get(struct repository *repo, const char *key) +{ + git_config_check_init(repo); + return git_configset_get(repo->config, key); +} + int repo_config_get_value(struct repository *repo, const char *key, const char **value) {