environment: stop storing `core.attributesFile` globally

The `core.attributeFile` config value is parsed in git_default_core_config(), loaded eagerly and stored in the global variable `git_attributes_file`. Storing this value in a global variable can lead to it being overwritten by another repository when more than one Git repository run in the same Git process. Create a new struct `repo_config_values` to hold this value and other repository dependent values parsed by `git_default_config()`. This will ensure the current behaviour remains the same while also enabling the libification of Git. An accessor function 'repo_config_values()' s created to ensure that we do not access an uninitialized repository, or an instance of a different repository than the current one. Suggested-by: Phillip Wood <phillip.wood123@gmail.com> Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Usman Akinyemi <usmanakinyemi202@gmail.com> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Olamide Caleb Bello <belkid98@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Olamide Caleb Bello committed Feb 16, 2026 at 17:38 UTC f9b3c1f731dd12144cd6d1e27787e99beb3a631f
6 files changed +45 -7
attr.c
+4 -3
@@ -881,10 +881,11 @@ const char *git_attr_system_file(void)
881
882 const char *git_attr_global_file(void)
883 {
884 - if (!git_attributes_file)
885 - git_attributes_file = xdg_config_home("attributes");
884 + struct repo_config_values *cfg = repo_config_values(the_repository);
885 + if (!cfg->attributes_file)
886 + cfg->attributes_file = xdg_config_home("attributes");
887
887 - return git_attributes_file;
888 + return cfg->attributes_file;
889 }
890
891 int git_attr_system_is_enabled(void)
environment.c
+9 -3
@@ -53,7 +53,6 @@ char *git_commit_encoding;
53 char *git_log_output_encoding;
54 char *apply_default_whitespace;
55 char *apply_default_ignorewhitespace;
56 -char *git_attributes_file;
56 int zlib_compression_level = Z_BEST_SPEED;
57 int pack_compression_level = Z_DEFAULT_COMPRESSION;
58 int fsync_object_files = -1;
@@ -327,6 +326,8 @@ next_name:
326 static int git_default_core_config(const char *var, const char *value,
327 const struct config_context *ctx, void *cb)
328 {
329 + struct repo_config_values *cfg = repo_config_values(the_repository);
330 +
331 /* This needs a better name */
332 if (!strcmp(var, "core.filemode")) {
333 trust_executable_bit = git_config_bool(var, value);
@@ -364,8 +365,8 @@ static int git_default_core_config(const char *var, const char *value,
365 }
366
367 if (!strcmp(var, "core.attributesfile")) {
367 - FREE_AND_NULL(git_attributes_file);
368 - return git_config_pathname(&git_attributes_file, var, value);
368 + FREE_AND_NULL(cfg->attributes_file);
369 + return git_config_pathname(&cfg->attributes_file, var, value);
370 }
371
372 if (!strcmp(var, "core.bare")) {
@@ -756,3 +757,8 @@ int git_default_config(const char *var, const char *value,
757 /* Add other config variables here and to Documentation/config.adoc. */
758 return 0;
759 }
760 +
761 +void repo_config_values_init(struct repo_config_values *cfg)
762 +{
763 + cfg->attributes_file = NULL;
764 +}
environment.h
+10 -1
@@ -84,6 +84,14 @@ extern const char * const local_repo_env[];
84
85 struct strvec;
86
87 +struct repository;
88 +struct repo_config_values {
89 + /* section "core" config values */
90 + char *attributes_file;
91 +};
92 +
93 +struct repo_config_values *repo_config_values(struct repository *repo);
94 +
95 /*
96 * Wrapper of getenv() that returns a strdup value. This value is kept
97 * in argv to be freed later.
@@ -107,6 +115,8 @@ const char *strip_namespace(const char *namespaced_ref);
115 int git_default_config(const char *, const char *,
116 const struct config_context *, void *);
117
118 +void repo_config_values_init(struct repo_config_values *cfg);
119 +
120 /*
121 * TODO: All the below state either explicitly or implicitly relies on
122 * `the_repository`. We should eventually get rid of these and make the
@@ -152,7 +162,6 @@ extern int assume_unchanged;
162 extern int warn_on_object_refname_ambiguity;
163 extern char *apply_default_whitespace;
164 extern char *apply_default_ignorewhitespace;
155 -extern char *git_attributes_file;
165 extern int zlib_compression_level;
166 extern int pack_compression_level;
167 extern unsigned long pack_size_limit_cfg;
oss-fuzz/fuzz-commit-graph.c
+1
@@ -10,6 +10,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
10 {
11 struct commit_graph *g;
12
13 + memset(the_repository, 0, sizeof(*the_repository));
14 initialize_repository(the_repository);
15
16 /*
repository.c
+14
@@ -50,13 +50,27 @@ static void set_default_hash_algo(struct repository *repo)
50 repo_set_hash_algo(repo, algo);
51 }
52
53 +struct repo_config_values *repo_config_values(struct repository *repo)
54 +{
55 + if (repo != the_repository)
56 + BUG("trying to read config from wrong repository instance");
57 + if (!repo->initialized)
58 + BUG("config values from uninitialized repository");
59 + return &repo->config_values_private_;
60 +}
61 +
62 void initialize_repository(struct repository *repo)
63 {
64 + if (repo->initialized)
65 + BUG("repository initialized already");
66 + repo->initialized = true;
67 +
68 repo->remote_state = remote_state_new();
69 repo->parsed_objects = parsed_object_pool_new(repo);
70 ALLOC_ARRAY(repo->index, 1);
71 index_state_init(repo->index, repo);
72 repo->check_deprecated_config = true;
73 + repo_config_values_init(&repo->config_values_private_);
74
75 /*
76 * When a command runs inside a repository, it learns what
repository.h
+7
@@ -3,6 +3,7 @@
3
4 #include "strmap.h"
5 #include "repo-settings.h"
6 +#include "environment.h"
7
8 struct config_set;
9 struct git_hash_algo;
@@ -148,6 +149,9 @@ struct repository {
149 /* Repository's compatibility hash algorithm. */
150 const struct git_hash_algo *compat_hash_algo;
151
152 + /* Repository's config values parsed by git_default_config() */
153 + struct repo_config_values config_values_private_;
154 +
155 /* Repository's reference storage format, as serialized on disk. */
156 enum ref_storage_format ref_storage_format;
157
@@ -171,6 +175,9 @@ struct repository {
175
176 /* Should repo_config() check for deprecated settings */
177 bool check_deprecated_config;
178 +
179 + /* Has this repository instance been initialized? */
180 + bool initialized;
181 };
182
183 #ifdef USE_THE_REPOSITORY_VARIABLE