Skip to content

Commit 20d84b7

Browse files
committed
environment: consolidate config values
The config values parsed in `git_default_config()` 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. It is important to note that `git_default_config()` is a wrapper to other `git_default_*_config()` such as `git_default_core_config()`. Therefore to access and substitute some of these global variables, the change has to be made in the functions themselves which parse and store the values in the global variables. This commit consolidates the variables `git_attributes_file` and `core_apply_sparse_checkout` as an initial effort for subsequent relocations. Suggested-by: Phillip Wood <phillip.wood123@gmail.com> Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Usman Akinyemi <usmanakinyemi202@gmail.com> Signed-off-by: Olamide Caleb Bello <belkid98@gmail.com>
1 parent e0bfec3 commit 20d84b7

File tree

16 files changed

+55
-31
lines changed

16 files changed

+55
-31
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)

builtin/backfill.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* We need this macro to access core_apply_sparse_checkout */
21
#define USE_THE_REPOSITORY_VARIABLE
32

43
#include "builtin.h"
@@ -139,7 +138,7 @@ int cmd_backfill(int argc, const char **argv, const char *prefix, struct reposit
139138
repo_config(repo, git_default_config, NULL);
140139

141140
if (ctx.sparse < 0)
142-
ctx.sparse = core_apply_sparse_checkout;
141+
ctx.sparse = repo->cfg_values->sparse_checkout;
143142

144143
result = do_backfill(&ctx);
145144
backfill_context_clear(&ctx);

builtin/clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ static int git_sparse_checkout_init(const char *repo)
623623
* We must apply the setting in the current process
624624
* for the later checkout to use the sparse-checkout file.
625625
*/
626-
core_apply_sparse_checkout = 1;
626+
the_repository->cfg_values->sparse_checkout = 1;
627627

628628
cmd.git_cmd = 1;
629629
if (run_command(&cmd)) {

builtin/mv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ int cmd_mv(int argc,
572572
rename_index_entry_at(the_repository->index, pos, dst);
573573

574574
if (ignore_sparse &&
575-
core_apply_sparse_checkout &&
575+
the_repository->cfg_values->sparse_checkout &&
576576
core_sparse_checkout_cone) {
577577
/*
578578
* NEEDSWORK: we are *not* paying attention to

builtin/sparse-checkout.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static int sparse_checkout_list(int argc, const char **argv, const char *prefix,
6363
int res;
6464

6565
setup_work_tree();
66-
if (!core_apply_sparse_checkout)
66+
if (!the_repository->cfg_values->sparse_checkout)
6767
die(_("this worktree is not sparse"));
6868

6969
argc = parse_options(argc, argv, prefix,
@@ -400,11 +400,11 @@ static int set_config(struct repository *repo,
400400

401401
static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
402402
/* If not specified, use previous definition of cone mode */
403-
if (*cone_mode == -1 && core_apply_sparse_checkout)
403+
if (*cone_mode == -1 && the_repository->cfg_values->sparse_checkout)
404404
*cone_mode = core_sparse_checkout_cone;
405405

406406
/* Set cone/non-cone mode appropriately */
407-
core_apply_sparse_checkout = 1;
407+
the_repository->cfg_values->sparse_checkout = 1;
408408
if (*cone_mode == 1 || *cone_mode == -1) {
409409
core_sparse_checkout_cone = 1;
410410
return MODE_CONE_PATTERNS;
@@ -418,7 +418,7 @@ static int update_modes(struct repository *repo, int *cone_mode, int *sparse_ind
418418
int mode, record_mode;
419419

420420
/* Determine if we need to record the mode; ensure sparse checkout on */
421-
record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
421+
record_mode = (*cone_mode != -1) || !repo->cfg_values->sparse_checkout;
422422

423423
mode = update_cone_mode(cone_mode);
424424
if (record_mode && set_config(repo, mode))
@@ -699,9 +699,9 @@ static int modify_pattern_list(struct repository *repo,
699699
break;
700700
}
701701

702-
if (!core_apply_sparse_checkout) {
702+
if (!repo->cfg_values->sparse_checkout) {
703703
set_config(repo, MODE_ALL_PATTERNS);
704-
core_apply_sparse_checkout = 1;
704+
repo->cfg_values->sparse_checkout = 1;
705705
changed_config = 1;
706706
}
707707

@@ -798,7 +798,7 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix,
798798
int ret;
799799

800800
setup_work_tree();
801-
if (!core_apply_sparse_checkout)
801+
if (!repo->cfg_values->sparse_checkout)
802802
die(_("no sparse-checkout to add to"));
803803

804804
repo_read_index(repo);
@@ -907,7 +907,7 @@ static int sparse_checkout_reapply(int argc, const char **argv,
907907
};
908908

909909
setup_work_tree();
910-
if (!core_apply_sparse_checkout)
910+
if (!repo->cfg_values->sparse_checkout)
911911
die(_("must be in a sparse-checkout to reapply sparsity patterns"));
912912

913913
reapply_opts.cone_mode = -1;
@@ -969,7 +969,7 @@ static int sparse_checkout_clean(int argc, const char **argv,
969969
};
970970

971971
setup_work_tree();
972-
if (!core_apply_sparse_checkout)
972+
if (!repo->cfg_values->sparse_checkout)
973973
die(_("must be in a sparse-checkout to clean directories"));
974974
if (!core_sparse_checkout_cone)
975975
die(_("must be in a cone-mode sparse-checkout to clean directories"));
@@ -1035,7 +1035,7 @@ static int sparse_checkout_disable(int argc, const char **argv,
10351035
struct pattern_list pl;
10361036

10371037
/*
1038-
* We do not exit early if !core_apply_sparse_checkout; due to the
1038+
* We do not exit early if !repo->cfg_values->sparse_checkout; due to the
10391039
* ability for users to manually muck things up between
10401040
* direct editing of .git/info/sparse-checkout
10411041
* running read-tree -m u HEAD or update-index --skip-worktree
@@ -1061,7 +1061,7 @@ static int sparse_checkout_disable(int argc, const char **argv,
10611061
hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
10621062
hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
10631063
pl.use_cone_patterns = 0;
1064-
core_apply_sparse_checkout = 1;
1064+
repo->cfg_values->sparse_checkout = 1;
10651065

10661066
add_pattern("/*", empty_base, 0, &pl, 0);
10671067

builtin/worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ static int add_worktree(const char *path, const char *refname,
536536
* If the current worktree has sparse-checkout enabled, then copy
537537
* the sparse-checkout patterns from the current worktree.
538538
*/
539-
if (core_apply_sparse_checkout)
539+
if (the_repository->cfg_values->sparse_checkout)
540540
copy_sparse_checkout(sb_repo.buf);
541541

542542
/*

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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ 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+
int sparse_checkout;
144+
145+
};
138146
#define CONFIG_CONTEXT_INIT { 0 }
139147

140148
/**
@@ -187,6 +195,7 @@ int git_config_from_blob_oid(config_fn_t fn, const char *name,
187195
void git_config_push_parameter(const char *text);
188196
void git_config_push_env(const char *spec);
189197
int git_config_from_parameters(config_fn_t fn, void *data);
198+
void config_values_clear(struct config_values *cfg);
190199

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

dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ enum pattern_match_result path_matches_pattern_list(
15511551

15521552
int init_sparse_checkout_patterns(struct index_state *istate)
15531553
{
1554-
if (!core_apply_sparse_checkout)
1554+
if (!istate->repo->cfg_values->sparse_checkout)
15551555
return 1;
15561556
if (istate->sparse_checkout_patterns)
15571557
return 0;

environment.c

Lines changed: 5 additions & 5 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;
@@ -75,7 +74,6 @@ enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
7574
#endif
7675
enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
7776
int grafts_keep_true_parents;
78-
int core_apply_sparse_checkout;
7977
int core_sparse_checkout_cone;
8078
int sparse_expect_files_outside_of_patterns;
8179
int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
@@ -327,6 +325,8 @@ static enum fsync_component parse_fsync_components(const char *var, const char *
327325
static int git_default_core_config(const char *var, const char *value,
328326
const struct config_context *ctx, void *cb)
329327
{
328+
struct config_values *cfg = the_repository->cfg_values;
329+
330330
/* This needs a better name */
331331
if (!strcmp(var, "core.filemode")) {
332332
trust_executable_bit = git_config_bool(var, value);
@@ -364,8 +364,8 @@ static int git_default_core_config(const char *var, const char *value,
364364
}
365365

366366
if (!strcmp(var, "core.attributesfile")) {
367-
FREE_AND_NULL(git_attributes_file);
368-
return git_config_pathname(&git_attributes_file, var, value);
367+
FREE_AND_NULL(cfg->attributes_file_path);
368+
return git_config_pathname(&cfg->attributes_file_path, var, value);
369369
}
370370

371371
if (!strcmp(var, "core.bare")) {
@@ -545,7 +545,7 @@ static int git_default_core_config(const char *var, const char *value,
545545
}
546546

547547
if (!strcmp(var, "core.sparsecheckout")) {
548-
core_apply_sparse_checkout = git_config_bool(var, value);
548+
cfg->sparse_checkout = git_config_bool(var, value);
549549
return 0;
550550
}
551551

0 commit comments

Comments
 (0)