Skip to content

Commit d12d861

Browse files
cloobTechgitster
authored andcommitted
environment: stop using core.sparseCheckout globally
The config value `core.sparseCheckout` is parsed in `git_default_core_config()` and stored globally in `core_apply_sparse_checkout`. This could cause it to be overwritten by another repository when different Git repositories run in the same process. Move the parsed value into `struct repo_config_values` in the_repository to retain current behaviours and move towards libifying Git. 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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 0c3f9f4 commit d12d861

File tree

12 files changed

+43
-24
lines changed

12 files changed

+43
-24
lines changed

builtin/backfill.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ int cmd_backfill(int argc, const char **argv, const char *prefix, struct reposit
129129
N_("Restrict the missing objects to the current sparse-checkout")),
130130
OPT_END(),
131131
};
132+
struct repo_config_values *cfg = repo_config_values(the_repository);
132133

133134
show_usage_with_options_if_asked(argc, argv,
134135
builtin_backfill_usage, options);
@@ -139,7 +140,7 @@ int cmd_backfill(int argc, const char **argv, const char *prefix, struct reposit
139140
repo_config(repo, git_default_config, NULL);
140141

141142
if (ctx.sparse < 0)
142-
ctx.sparse = core_apply_sparse_checkout;
143+
ctx.sparse = cfg->apply_sparse_checkout;
143144

144145
result = do_backfill(&ctx);
145146
backfill_context_clear(&ctx);

builtin/clone.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,13 +617,14 @@ static int git_sparse_checkout_init(const char *repo)
617617
{
618618
struct child_process cmd = CHILD_PROCESS_INIT;
619619
int result = 0;
620+
struct repo_config_values *cfg = repo_config_values(the_repository);
620621
strvec_pushl(&cmd.args, "-C", repo, "sparse-checkout", "set", NULL);
621622

622623
/*
623624
* We must apply the setting in the current process
624625
* for the later checkout to use the sparse-checkout file.
625626
*/
626-
core_apply_sparse_checkout = 1;
627+
cfg->apply_sparse_checkout = 1;
627628

628629
cmd.git_cmd = 1;
629630
if (run_command(&cmd)) {

builtin/grep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ static int grep_submodule(struct grep_opt *opt,
482482
* "forget" the sparse-index feature switch. As a result, the index
483483
* of these submodules are expanded unexpectedly.
484484
*
485-
* 2. "core_apply_sparse_checkout"
485+
* 2. "config_values_private_.apply_sparse_checkout"
486486
* When running `grep` in the superproject, this setting is
487487
* populated using the superproject's configs. However, once
488488
* initialized, this config is globally accessible and is read by

builtin/mv.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ int cmd_mv(int argc,
238238
struct hashmap moved_dirs = HASHMAP_INIT(pathmap_cmp, NULL);
239239
struct strbuf pathbuf = STRBUF_INIT;
240240
int ret;
241+
struct repo_config_values *cfg = repo_config_values(the_repository);
241242

242243
repo_config(the_repository, git_default_config, NULL);
243244

@@ -572,7 +573,7 @@ int cmd_mv(int argc,
572573
rename_index_entry_at(the_repository->index, pos, dst);
573574

574575
if (ignore_sparse &&
575-
core_apply_sparse_checkout &&
576+
cfg->apply_sparse_checkout &&
576577
core_sparse_checkout_cone) {
577578
/*
578579
* NEEDSWORK: we are *not* paying attention to

builtin/sparse-checkout.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ static int sparse_checkout_list(int argc, const char **argv, const char *prefix,
6161
struct pattern_list pl;
6262
char *sparse_filename;
6363
int res;
64+
struct repo_config_values *cfg = repo_config_values(the_repository);
6465

6566
setup_work_tree();
66-
if (!core_apply_sparse_checkout)
67+
if (!cfg->apply_sparse_checkout)
6768
die(_("this worktree is not sparse"));
6869

6970
argc = parse_options(argc, argv, prefix,
@@ -399,12 +400,14 @@ static int set_config(struct repository *repo,
399400
}
400401

401402
static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
403+
struct repo_config_values *cfg = repo_config_values(the_repository);
404+
402405
/* If not specified, use previous definition of cone mode */
403-
if (*cone_mode == -1 && core_apply_sparse_checkout)
406+
if (*cone_mode == -1 && cfg->apply_sparse_checkout)
404407
*cone_mode = core_sparse_checkout_cone;
405408

406409
/* Set cone/non-cone mode appropriately */
407-
core_apply_sparse_checkout = 1;
410+
cfg->apply_sparse_checkout = 1;
408411
if (*cone_mode == 1 || *cone_mode == -1) {
409412
core_sparse_checkout_cone = 1;
410413
return MODE_CONE_PATTERNS;
@@ -416,9 +419,10 @@ static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
416419
static int update_modes(struct repository *repo, int *cone_mode, int *sparse_index)
417420
{
418421
int mode, record_mode;
422+
struct repo_config_values *cfg = repo_config_values(the_repository);
419423

420424
/* Determine if we need to record the mode; ensure sparse checkout on */
421-
record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
425+
record_mode = (*cone_mode != -1) || !cfg->apply_sparse_checkout;
422426

423427
mode = update_cone_mode(cone_mode);
424428
if (record_mode && set_config(repo, mode))
@@ -684,6 +688,7 @@ static int modify_pattern_list(struct repository *repo,
684688
int result;
685689
int changed_config = 0;
686690
struct pattern_list *pl = xcalloc(1, sizeof(*pl));
691+
struct repo_config_values *cfg = repo_config_values(the_repository);
687692

688693
switch (m) {
689694
case ADD:
@@ -699,9 +704,9 @@ static int modify_pattern_list(struct repository *repo,
699704
break;
700705
}
701706

702-
if (!core_apply_sparse_checkout) {
707+
if (!cfg->apply_sparse_checkout) {
703708
set_config(repo, MODE_ALL_PATTERNS);
704-
core_apply_sparse_checkout = 1;
709+
cfg->apply_sparse_checkout = 1;
705710
changed_config = 1;
706711
}
707712

@@ -796,9 +801,10 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix,
796801
};
797802
struct strvec patterns = STRVEC_INIT;
798803
int ret;
804+
struct repo_config_values *cfg = repo_config_values(the_repository);
799805

800806
setup_work_tree();
801-
if (!core_apply_sparse_checkout)
807+
if (!cfg->apply_sparse_checkout)
802808
die(_("no sparse-checkout to add to"));
803809

804810
repo_read_index(repo);
@@ -905,9 +911,10 @@ static int sparse_checkout_reapply(int argc, const char **argv,
905911
N_("toggle the use of a sparse index")),
906912
OPT_END(),
907913
};
914+
struct repo_config_values *cfg = repo_config_values(the_repository);
908915

909916
setup_work_tree();
910-
if (!core_apply_sparse_checkout)
917+
if (!cfg->apply_sparse_checkout)
911918
die(_("must be in a sparse-checkout to reapply sparsity patterns"));
912919

913920
reapply_opts.cone_mode = -1;
@@ -960,6 +967,7 @@ static int sparse_checkout_clean(int argc, const char **argv,
960967
size_t worktree_len;
961968
int force = 0, dry_run = 0, verbose = 0;
962969
int require_force = 1;
970+
struct repo_config_values *cfg = repo_config_values(the_repository);
963971

964972
struct option builtin_sparse_checkout_clean_options[] = {
965973
OPT__DRY_RUN(&dry_run, N_("dry run")),
@@ -969,7 +977,7 @@ static int sparse_checkout_clean(int argc, const char **argv,
969977
};
970978

971979
setup_work_tree();
972-
if (!core_apply_sparse_checkout)
980+
if (!cfg->apply_sparse_checkout)
973981
die(_("must be in a sparse-checkout to clean directories"));
974982
if (!core_sparse_checkout_cone)
975983
die(_("must be in a cone-mode sparse-checkout to clean directories"));
@@ -1033,9 +1041,10 @@ static int sparse_checkout_disable(int argc, const char **argv,
10331041
OPT_END(),
10341042
};
10351043
struct pattern_list pl;
1044+
struct repo_config_values *cfg = repo_config_values(the_repository);
10361045

10371046
/*
1038-
* We do not exit early if !core_apply_sparse_checkout; due to the
1047+
* We do not exit early if !repo->config_values.apply_sparse_checkout; due to the
10391048
* ability for users to manually muck things up between
10401049
* direct editing of .git/info/sparse-checkout
10411050
* running read-tree -m u HEAD or update-index --skip-worktree
@@ -1061,7 +1070,7 @@ static int sparse_checkout_disable(int argc, const char **argv,
10611070
hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
10621071
hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
10631072
pl.use_cone_patterns = 0;
1064-
core_apply_sparse_checkout = 1;
1073+
cfg->apply_sparse_checkout = 1;
10651074

10661075
add_pattern("/*", empty_base, 0, &pl, 0);
10671076

builtin/worktree.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ static int add_worktree(const char *path, const char *refname,
440440
struct strbuf sb_name = STRBUF_INIT;
441441
struct worktree **worktrees, *wt = NULL;
442442
struct ref_store *wt_refs;
443+
struct repo_config_values *cfg = repo_config_values(the_repository);
443444

444445
worktrees = get_worktrees();
445446
check_candidate_path(path, opts->force, worktrees, "add");
@@ -536,7 +537,7 @@ static int add_worktree(const char *path, const char *refname,
536537
* If the current worktree has sparse-checkout enabled, then copy
537538
* the sparse-checkout patterns from the current worktree.
538539
*/
539-
if (core_apply_sparse_checkout)
540+
if (cfg->apply_sparse_checkout)
540541
copy_sparse_checkout(sb_repo.buf);
541542

542543
/*

dir.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,8 @@ 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+
struct repo_config_values *cfg = repo_config_values(the_repository);
1555+
if (!cfg->apply_sparse_checkout)
15551556
return 1;
15561557
if (istate->sparse_checkout_patterns)
15571558
return 0;

environment.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
7474
#endif
7575
enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
7676
int grafts_keep_true_parents;
77-
int core_apply_sparse_checkout;
7877
int core_sparse_checkout_cone;
7978
int sparse_expect_files_outside_of_patterns;
8079
int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
@@ -546,7 +545,7 @@ static int git_default_core_config(const char *var, const char *value,
546545
}
547546

548547
if (!strcmp(var, "core.sparsecheckout")) {
549-
core_apply_sparse_checkout = git_config_bool(var, value);
548+
cfg->apply_sparse_checkout = git_config_bool(var, value);
550549
return 0;
551550
}
552551

@@ -761,4 +760,5 @@ int git_default_config(const char *var, const char *value,
761760
void repo_config_values_init(struct repo_config_values *cfg)
762761
{
763762
cfg->attributes_file = NULL;
763+
cfg->apply_sparse_checkout = 0;
764764
}

environment.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ struct repository;
8888
struct repo_config_values {
8989
/* section "core" config values */
9090
char *attributes_file;
91+
int apply_sparse_checkout;
9192
};
9293

9394
struct repo_config_values *repo_config_values(struct repository *repo);
@@ -171,7 +172,6 @@ extern int precomposed_unicode;
171172
extern int protect_hfs;
172173
extern int protect_ntfs;
173174

174-
extern int core_apply_sparse_checkout;
175175
extern int core_sparse_checkout_cone;
176176
extern int sparse_expect_files_outside_of_patterns;
177177

sparse-index.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ static int index_has_unmerged_entries(struct index_state *istate)
152152

153153
int is_sparse_index_allowed(struct index_state *istate, int flags)
154154
{
155-
if (!core_apply_sparse_checkout || !core_sparse_checkout_cone)
155+
struct repo_config_values *cfg = repo_config_values(the_repository);
156+
if (!cfg->apply_sparse_checkout || !core_sparse_checkout_cone)
156157
return 0;
157158

158159
if (!(flags & SPARSE_INDEX_MEMORY_ONLY)) {
@@ -670,7 +671,8 @@ static void clear_skip_worktree_from_present_files_full(struct index_state *ista
670671

671672
void clear_skip_worktree_from_present_files(struct index_state *istate)
672673
{
673-
if (!core_apply_sparse_checkout ||
674+
struct repo_config_values *cfg = repo_config_values(the_repository);
675+
if (!cfg->apply_sparse_checkout ||
674676
sparse_expect_files_outside_of_patterns)
675677
return;
676678

0 commit comments

Comments
 (0)