From: Glen Choo <chooglen@xxxxxxxxxx> Protected config is read using `read_very_early_config()`, which has several downsides: - Every call to `read_very_early_config()` parses global and system-level config files anew, but this can be optimized by just parsing them once [1]. - Protected variables should respect "-c" because we can reasonably assume that it comes from the user. But, `read_very_early_config()` can't use "-c" because it is called so early that it does not have access to command line arguments. Introduce `git_protected_config()`, which reads protected config and caches the values in `the_repository.protected_config`. Then, refactor `safe.directory` to use `git_protected_config()`. This implementation can still be improved, however: - `git_protected_config()` iterates through every variable in `the_repository.protected_config`, which may still be too expensive to be called in every "git" invocation. There exist constant time lookup functions for non-protected config (repo_config_get_*()), but for simplicity, this commit does not implement similar functions for protected config. - Protected config is stored in `the_repository` so that we don't need to statically allocate it. But this might be confusing since protected config ignores repository config by definition. [1] While `git_protected_config()` should save on file I/O, I wasn't able to measure a meaningful difference between that and `read_very_early_config()` on my machine (which has an SSD). Signed-off-by: Glen Choo <chooglen@xxxxxxxxxx> --- config.c | 35 +++++++++++++++++++++++++++++++++++ config.h | 8 ++++++++ repository.c | 5 +++++ repository.h | 8 ++++++++ setup.c | 2 +- 5 files changed, 57 insertions(+), 1 deletion(-) diff --git a/config.c b/config.c index fa471dbdb89..c30bb7c5d09 100644 --- a/config.c +++ b/config.c @@ -2614,6 +2614,41 @@ int repo_config_get_pathname(struct repository *repo, return ret; } +/* Read protected config into the_repository->protected_config. */ +static void read_protected_config(void) +{ + char *xdg_config = NULL, *user_config = NULL, *system_config = NULL; + + CALLOC_ARRAY(the_repository->protected_config, 1); + git_configset_init(the_repository->protected_config); + + system_config = git_system_config(); + git_global_config(&user_config, &xdg_config); + + git_configset_add_file(the_repository->protected_config, system_config); + git_configset_add_file(the_repository->protected_config, xdg_config); + git_configset_add_file(the_repository->protected_config, user_config); + + free(system_config); + free(xdg_config); + free(user_config); +} + +/* Ensure that the_repository->protected_config has been initialized. */ +static void git_protected_config_check_init(void) +{ + if (the_repository->protected_config && + the_repository->protected_config->hash_initialized) + return; + read_protected_config(); +} + +void git_protected_config(config_fn_t fn, void *data) +{ + git_protected_config_check_init(); + configset_iter(the_repository->protected_config, fn, data); +} + /* Functions used historically to read configuration from 'the_repository' */ void git_config(config_fn_t fn, void *data) { diff --git a/config.h b/config.h index 7654f61c634..411965f52b5 100644 --- a/config.h +++ b/config.h @@ -505,6 +505,14 @@ int repo_config_get_maybe_bool(struct repository *repo, int repo_config_get_pathname(struct repository *repo, const char *key, const char **dest); +/* + * Functions for reading protected config. By definition, protected + * config ignores repository config, so it is unnecessary to read + * protected config from any `struct repository` other than + * the_repository. + */ +void git_protected_config(config_fn_t fn, void *data); + /** * Querying For Specific Variables * ------------------------------- diff --git a/repository.c b/repository.c index 5d166b692c8..ec319a5e09a 100644 --- a/repository.c +++ b/repository.c @@ -295,6 +295,11 @@ void repo_clear(struct repository *repo) FREE_AND_NULL(repo->remote_state); } + if (repo->protected_config) { + git_configset_clear(repo->protected_config); + FREE_AND_NULL(repo->protected_config); + } + repo_clear_path_cache(&repo->cached_paths); } diff --git a/repository.h b/repository.h index 6cc661e5a43..24251aac553 100644 --- a/repository.h +++ b/repository.h @@ -126,6 +126,14 @@ struct repository { struct repo_settings settings; + /* + * Config that comes from trusted sources, namely + * - system config files (e.g. /etc/gitconfig) + * - global config files (e.g. $HOME/.gitconfig, + * $XDG_CONFIG_HOME/git) + */ + struct config_set *protected_config; + /* Subsystems */ /* * Repository's config which contains key-value pairs from the usual diff --git a/setup.c b/setup.c index f818dd858c6..847d47f9195 100644 --- a/setup.c +++ b/setup.c @@ -1128,7 +1128,7 @@ static int ensure_valid_ownership(const char *path) is_path_owned_by_current_user(path)) return 1; - read_very_early_config(safe_directory_cb, &data); + git_protected_config(safe_directory_cb, &data); return data.is_safe; } -- gitgitgadget