]> git.ipfire.org Git - thirdparty/git.git/blobdiff - config.c
Merge branch 'vd/fix-perf-tests'
[thirdparty/git.git] / config.c
index 9b0e9c93285fb33a69ab3ee0b20ae95ad2e202e1..e8ebef77d5c92423f7af85d9176059e2a772a320 100644 (file)
--- a/config.c
+++ b/config.c
@@ -81,6 +81,17 @@ static enum config_scope current_parsing_scope;
 static int pack_compression_seen;
 static int zlib_compression_seen;
 
+/*
+ * Config that comes from trusted scopes, namely:
+ * - CONFIG_SCOPE_SYSTEM (e.g. /etc/gitconfig)
+ * - CONFIG_SCOPE_GLOBAL (e.g. $HOME/.gitconfig, $XDG_CONFIG_HOME/git)
+ * - CONFIG_SCOPE_COMMAND (e.g. "-c" option, environment variables)
+ *
+ * This is declared here for code cleanliness, but unlike the other
+ * static variables, this does not hold config parser state.
+ */
+static struct config_set protected_config;
+
 static int config_file_fgetc(struct config_source *conf)
 {
        return getc_unlocked(conf->u.file);
@@ -1968,6 +1979,8 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename,
        int ret = -1;
        FILE *f;
 
+       if (!filename)
+               BUG("filename cannot be NULL");
        f = fopen_or_warn(filename, "r");
        if (f) {
                ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
@@ -2378,6 +2391,11 @@ int git_configset_add_file(struct config_set *cs, const char *filename)
        return git_config_from_file(config_set_callback, filename, cs);
 }
 
+int git_configset_add_parameters(struct config_set *cs)
+{
+       return git_config_from_parameters(config_set_callback, cs);
+}
+
 int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
 {
        const struct string_list *values = NULL;
@@ -2619,6 +2637,36 @@ int repo_config_get_pathname(struct repository *repo,
        return ret;
 }
 
+/* Read values into protected_config. */
+static void read_protected_config(void)
+{
+       char *xdg_config = NULL, *user_config = NULL, *system_config = NULL;
+
+       git_configset_init(&protected_config);
+
+       system_config = git_system_config();
+       git_global_config(&user_config, &xdg_config);
+
+       if (system_config)
+               git_configset_add_file(&protected_config, system_config);
+       if (xdg_config)
+               git_configset_add_file(&protected_config, xdg_config);
+       if (user_config)
+               git_configset_add_file(&protected_config, user_config);
+       git_configset_add_parameters(&protected_config);
+
+       free(system_config);
+       free(xdg_config);
+       free(user_config);
+}
+
+void git_protected_config(config_fn_t fn, void *data)
+{
+       if (!protected_config.hash_initialized)
+               read_protected_config();
+       configset_iter(&protected_config, fn, data);
+}
+
 /* Functions used historically to read configuration from 'the_repository' */
 void git_config(config_fn_t fn, void *data)
 {