config: pass 'repo' directly to 'config_with_options()'

Add a 'struct repository' argument to 'config_with_options()' and remove the 'repo' field from 'struct git_config_source'. A 'struct repository' instance was originally added to the config source in e3e8bf046e9 (submodule-config: pass repo upon blob config read, 2021-08-16) to improve how submodule blob config content was accessed. At the time, this was the only use for a 'repository' instance, so it was naturally added only where it was needed: to 'struct git_config_source'. However, in upcoming patches, 'config_with_options()' will need the repository instance to access extension information (regardless of whether a 'config_source' exists). To make the 'struct repository' instance more easily accessible, move it into the function's arguments. Update all callers of 'config_with_options()' to pass the appropriate 'repo' value: * in 'builtin/config.c', use 'the_repository' * in 'submodule--config.c', use the 'repo' arg in 'config_from_gitmodules()' * in 'read_[very_]early_config()' & 'read_protected_config()', set 'repo' to NULL (repository instances aren't available there) * in 'populate_remote_urls()', use the repo instance that has been added to the 'struct config_include_data' * in 'repo_read_config()', use the given 'repo' arg Finally, note that this patch eliminates the fallback to 'the_repository' that previously existed for the 'config_source' repo instance if it was NULL. The fallback is no longer necessary, as the 'repo' is set explicitly in all cases where it is needed. Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed May 26, 2023 at 01:32 UTC 9b6b06c159e0a389aaafbce91dd85bb5244ac5ad
4 files changed +21 -16
builtin/config.c
+9 -5
@@ -375,7 +375,8 @@ static int get_value(const char *key_, const char *regex_, unsigned flags)
375 }
376
377 config_with_options(collect_config, &values,
378 - &given_config_source, &config_options);
378 + &given_config_source, the_repository,
379 + &config_options);
380
381 if (!values.nr && default_value) {
382 struct strbuf *item;
@@ -486,7 +487,8 @@ static void get_color(const char *var, const char *def_color)
487 get_color_found = 0;
488 parsed_color[0] = '\0';
489 config_with_options(git_get_color_config, NULL,
489 - &given_config_source, &config_options);
490 + &given_config_source, the_repository,
491 + &config_options);
492
493 if (!get_color_found && def_color) {
494 if (color_parse(def_color, parsed_color) < 0)
@@ -518,7 +520,8 @@ static int get_colorbool(const char *var, int print)
520 get_diff_color_found = -1;
521 get_color_ui_found = -1;
522 config_with_options(git_get_colorbool_config, NULL,
521 - &given_config_source, &config_options);
523 + &given_config_source, the_repository,
524 + &config_options);
525
526 if (get_colorbool_found < 0) {
527 if (!strcmp(get_colorbool_slot, "color.diff"))
@@ -607,7 +610,8 @@ static int get_urlmatch(const char *var, const char *url)
610 }
611
612 config_with_options(urlmatch_config_entry, &config,
610 - &given_config_source, &config_options);
613 + &given_config_source, the_repository,
614 + &config_options);
615
616 ret = !values.nr;
617
@@ -827,7 +831,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
831 if (actions == ACTION_LIST) {
832 check_argc(argc, 0, 0);
833 if (config_with_options(show_all_config, NULL,
830 - &given_config_source,
834 + &given_config_source, the_repository,
835 &config_options) < 0) {
836 if (given_config_source.file)
837 die_errno(_("unable to read config file '%s'"),
config.c
+9 -7
@@ -199,6 +199,7 @@ struct config_include_data {
199 void *data;
200 const struct config_options *opts;
201 struct git_config_source *config_source;
202 + struct repository *repo;
203 struct config_reader *config_reader;
204
205 /*
@@ -415,7 +416,8 @@ static void populate_remote_urls(struct config_include_data *inc)
416
417 inc->remote_urls = xmalloc(sizeof(*inc->remote_urls));
418 string_list_init_dup(inc->remote_urls);
418 - config_with_options(add_remote_url, inc->remote_urls, inc->config_source, &opts);
419 + config_with_options(add_remote_url, inc->remote_urls,
420 + inc->config_source, inc->repo, &opts);
421
422 config_reader_set_scope(inc->config_reader, store_scope);
423 }
@@ -2261,6 +2263,7 @@ static int do_git_config_sequence(struct config_reader *reader,
2263
2264 int config_with_options(config_fn_t fn, void *data,
2265 struct git_config_source *config_source,
2266 + struct repository *repo,
2267 const struct config_options *opts)
2268 {
2269 struct config_include_data inc = CONFIG_INCLUDE_INIT;
@@ -2271,6 +2274,7 @@ int config_with_options(config_fn_t fn, void *data,
2274 inc.fn = fn;
2275 inc.data = data;
2276 inc.opts = opts;
2277 + inc.repo = repo;
2278 inc.config_source = config_source;
2279 inc.config_reader = &the_reader;
2280 fn = git_config_include;
@@ -2289,8 +2293,6 @@ int config_with_options(config_fn_t fn, void *data,
2293 } else if (config_source && config_source->file) {
2294 ret = git_config_from_file(fn, config_source->file, data);
2295 } else if (config_source && config_source->blob) {
2292 - struct repository *repo = config_source->repo ?
2293 - config_source->repo : the_repository;
2296 ret = git_config_from_blob_ref(fn, repo, config_source->blob,
2297 data);
2298 } else {
@@ -2353,7 +2355,7 @@ void read_early_config(config_fn_t cb, void *data)
2355 opts.git_dir = gitdir.buf;
2356 }
2357
2356 - config_with_options(cb, data, NULL, &opts);
2358 + config_with_options(cb, data, NULL, NULL, &opts);
2359
2360 strbuf_release(&commondir);
2361 strbuf_release(&gitdir);
@@ -2373,7 +2375,7 @@ void read_very_early_config(config_fn_t cb, void *data)
2375 opts.ignore_cmdline = 1;
2376 opts.system_gently = 1;
2377
2376 - config_with_options(cb, data, NULL, &opts);
2378 + config_with_options(cb, data, NULL, NULL, &opts);
2379 }
2380
2381 RESULT_MUST_BE_USED
@@ -2681,7 +2683,7 @@ static void repo_read_config(struct repository *repo)
2683 data.config_set = repo->config;
2684 data.config_reader = &the_reader;
2685
2684 - if (config_with_options(config_set_callback, &data, NULL, &opts) < 0)
2686 + if (config_with_options(config_set_callback, &data, NULL, repo, &opts) < 0)
2687 /*
2688 * config_with_options() normally returns only
2689 * zero, as most errors are fatal, and
@@ -2825,7 +2827,7 @@ static void read_protected_config(void)
2827 git_configset_init(&protected_config);
2828 data.config_set = &protected_config;
2829 data.config_reader = &the_reader;
2828 - config_with_options(config_set_callback, &data, NULL, &opts);
2830 + config_with_options(config_set_callback, &data, NULL, NULL, &opts);
2831 }
2832
2833 void git_protected_config(config_fn_t fn, void *data)
config.h
+2 -2
@@ -3,6 +3,7 @@
3
4 #include "hashmap.h"
5 #include "string-list.h"
6 +#include "repository.h"
7
8
9 /**
@@ -49,8 +50,6 @@ const char *config_scope_name(enum config_scope scope);
50 struct git_config_source {
51 unsigned int use_stdin:1;
52 const char *file;
52 - /* The repository if blob is not NULL; leave blank for the_repository */
53 - struct repository *repo;
53 const char *blob;
54 enum config_scope scope;
55 };
@@ -196,6 +195,7 @@ void git_config(config_fn_t fn, void *);
195 */
196 int config_with_options(config_fn_t fn, void *,
197 struct git_config_source *config_source,
198 + struct repository *repo,
199 const struct config_options *opts);
200
201 /**
submodule-config.c
+1 -2
@@ -659,7 +659,6 @@ static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void
659 config_source.file = file;
660 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
661 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
662 - config_source.repo = repo;
662 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
663 if (repo != the_repository)
664 add_submodule_odb_by_path(repo->objects->odb->path);
@@ -667,7 +666,7 @@ static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void
666 goto out;
667 }
668
670 - config_with_options(fn, data, &config_source, &opts);
669 + config_with_options(fn, data, &config_source, repo, &opts);
670
671 out:
672 free(oidstr);