Skip to content

Commit 1abbefa

Browse files
committed
environment: consolidate config values
The config values parsed in `git_default_config()` such as `core.attributesFile` are loaded eagerly and stored in global variables. Storing these values in global variables can lead to unexpected behaviours when more than one Git repository run in the same Git process. Move these values into a `struct config_values` which can be accessed per repository. This centralization will be important in moving other variables and this will prevent us from moving any code from `git_default_config()`, ensuring the current behaviour remains the same while also enabling the libification of Git. Suggested-by: Phillip Wood <phillip.wood123@gmail.com> Signed-off-by: Olamide Caleb Bello <belkid98@gmail.com>
1 parent e0bfec3 commit 1abbefa

File tree

7 files changed

+31
-7
lines changed

7 files changed

+31
-7
lines changed

attr.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -881,10 +881,12 @@ const char *git_attr_system_file(void)
881881

882882
const char *git_attr_global_file(void)
883883
{
884-
if (!git_attributes_file)
885-
git_attributes_file = xdg_config_home("attributes");
884+
struct config_values *cfg = the_repository->cfg_values;
886885

887-
return git_attributes_file;
886+
if (!cfg->attributes_file_path)
887+
cfg->attributes_file_path = xdg_config_home("attributes");
888+
889+
return cfg->attributes_file_path;
888890
}
889891

890892
int git_attr_system_is_enabled(void)

config.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,11 @@ static int config_set_element_cmp(const void *cmp_data UNUSED,
17611761
return strcmp(e1->key, e2->key);
17621762
}
17631763

1764+
void config_values_clear(struct config_values *cfg)
1765+
{
1766+
free(cfg->attributes_file_path);
1767+
}
1768+
17641769
void git_configset_init(struct config_set *set)
17651770
{
17661771
hashmap_init(&set->config_hash, config_set_element_cmp, NULL, 0);

config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ struct config_context {
135135
/* Config source metadata for key and value. */
136136
const struct key_value_info *kvi;
137137
};
138+
139+
/* Holds default config values */
140+
struct config_values {
141+
/* core config values */
142+
char *attributes_file_path;
143+
};
138144
#define CONFIG_CONTEXT_INIT { 0 }
139145

140146
/**
@@ -187,6 +193,7 @@ int git_config_from_blob_oid(config_fn_t fn, const char *name,
187193
void git_config_push_parameter(const char *text);
188194
void git_config_push_env(const char *spec);
189195
int git_config_from_parameters(config_fn_t fn, void *data);
196+
void config_values_clear(struct config_values *cfg);
190197

191198
/*
192199
* Read config when the Git directory has not yet been set up. In case

environment.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ char *git_commit_encoding;
5353
char *git_log_output_encoding;
5454
char *apply_default_whitespace;
5555
char *apply_default_ignorewhitespace;
56-
char *git_attributes_file;
5756
int zlib_compression_level = Z_BEST_SPEED;
5857
int pack_compression_level = Z_DEFAULT_COMPRESSION;
5958
int fsync_object_files = -1;
@@ -327,6 +326,8 @@ static enum fsync_component parse_fsync_components(const char *var, const char *
327326
static int git_default_core_config(const char *var, const char *value,
328327
const struct config_context *ctx, void *cb)
329328
{
329+
struct config_values *cfg = the_repository->cfg_values;
330+
330331
/* This needs a better name */
331332
if (!strcmp(var, "core.filemode")) {
332333
trust_executable_bit = git_config_bool(var, value);
@@ -364,8 +365,8 @@ static int git_default_core_config(const char *var, const char *value,
364365
}
365366

366367
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_path);
369+
return git_config_pathname(&cfg->attributes_file_path, var, value);
369370
}
370371

371372
if (!strcmp(var, "core.bare")) {

environment.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ extern int assume_unchanged;
152152
extern int warn_on_object_refname_ambiguity;
153153
extern char *apply_default_whitespace;
154154
extern char *apply_default_ignorewhitespace;
155-
extern char *git_attributes_file;
156155
extern int zlib_compression_level;
157156
extern int pack_compression_level;
158157
extern unsigned long pack_size_limit_cfg;

repository.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void initialize_repository(struct repository *repo)
5555
repo->remote_state = remote_state_new();
5656
repo->parsed_objects = parsed_object_pool_new(repo);
5757
ALLOC_ARRAY(repo->index, 1);
58+
CALLOC_ARRAY(repo->cfg_values, 1);
5859
index_state_init(repo->index, repo);
5960
repo->check_deprecated_config = true;
6061

@@ -403,6 +404,11 @@ void repo_clear(struct repository *repo)
403404
FREE_AND_NULL(repo->remote_state);
404405
}
405406

407+
if (repo->cfg_values) {
408+
config_values_clear(repo->cfg_values);
409+
FREE_AND_NULL(repo->cfg_values);
410+
}
411+
406412
strmap_for_each_entry(&repo->submodule_ref_stores, &iter, e)
407413
ref_store_release(e->value);
408414
strmap_clear(&repo->submodule_ref_stores, 1);

repository.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct object_database;
1313
struct submodule_cache;
1414
struct promisor_remote_config;
1515
struct remote_state;
16+
struct config_values;
1617

1718
enum ref_storage_format {
1819
REF_STORAGE_FORMAT_UNKNOWN,
@@ -171,6 +172,9 @@ struct repository {
171172

172173
/* Should repo_config() check for deprecated settings */
173174
bool check_deprecated_config;
175+
176+
/* Repository's default configuration values */
177+
struct config_values *cfg_values;
174178
};
175179

176180
#ifdef USE_THE_REPOSITORY_VARIABLE

0 commit comments

Comments
 (0)