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>

Olamide Caleb Bello committed Feb 16, 2026 at 17:38 UTC 4021751558126d39b642503e7ef4768131df45e7
12 files changed +47 -24
builtin/backfill.c
+2 -1
@@ -129,6 +129,7 @@ int cmd_backfill(int argc, const char **argv, const char *prefix, struct reposit
129 N_("Restrict the missing objects to the current sparse-checkout")),
130 OPT_END(),
131 };
132 + struct repo_config_values *cfg = repo_config_values(the_repository);
133
134 show_usage_with_options_if_asked(argc, argv,
135 builtin_backfill_usage, options);
@@ -139,7 +140,7 @@ int cmd_backfill(int argc, const char **argv, const char *prefix, struct reposit
140 repo_config(repo, git_default_config, NULL);
141
142 if (ctx.sparse < 0)
142 - ctx.sparse = core_apply_sparse_checkout;
143 + ctx.sparse = cfg->apply_sparse_checkout;
144
145 result = do_backfill(&ctx);
146 backfill_context_clear(&ctx);
builtin/clone.c
+3 -1
@@ -617,13 +617,15 @@ static int git_sparse_checkout_init(const char *repo)
617 {
618 struct child_process cmd = CHILD_PROCESS_INIT;
619 int result = 0;
620 + struct repo_config_values *cfg = repo_config_values(the_repository);
621 +
622 strvec_pushl(&cmd.args, "-C", repo, "sparse-checkout", "set", NULL);
623
624 /*
625 * We must apply the setting in the current process
626 * for the later checkout to use the sparse-checkout file.
627 */
626 - core_apply_sparse_checkout = 1;
628 + cfg->apply_sparse_checkout = 1;
629
630 cmd.git_cmd = 1;
631 if (run_command(&cmd)) {
builtin/grep.c
+1 -1
@@ -482,7 +482,7 @@ static int grep_submodule(struct grep_opt *opt,
482 * "forget" the sparse-index feature switch. As a result, the index
483 * of these submodules are expanded unexpectedly.
484 *
485 - * 2. "core_apply_sparse_checkout"
485 + * 2. "config_values_private_.apply_sparse_checkout"
486 * When running `grep` in the superproject, this setting is
487 * populated using the superproject's configs. However, once
488 * initialized, this config is globally accessible and is read by
builtin/mv.c
+2 -1
@@ -238,6 +238,7 @@ int cmd_mv(int argc,
238 struct hashmap moved_dirs = HASHMAP_INIT(pathmap_cmp, NULL);
239 struct strbuf pathbuf = STRBUF_INIT;
240 int ret;
241 + struct repo_config_values *cfg = repo_config_values(the_repository);
242
243 repo_config(the_repository, git_default_config, NULL);
244
@@ -572,7 +573,7 @@ remove_entry:
573 rename_index_entry_at(the_repository->index, pos, dst);
574
575 if (ignore_sparse &&
575 - core_apply_sparse_checkout &&
576 + cfg->apply_sparse_checkout &&
577 core_sparse_checkout_cone) {
578 /*
579 * NEEDSWORK: we are *not* paying attention to
builtin/sparse-checkout.c
+20 -11
@@ -61,9 +61,10 @@ static int sparse_checkout_list(int argc, const char **argv, const char *prefix,
61 struct pattern_list pl;
62 char *sparse_filename;
63 int res;
64 + struct repo_config_values *cfg = repo_config_values(the_repository);
65
66 setup_work_tree();
66 - if (!core_apply_sparse_checkout)
67 + if (!cfg->apply_sparse_checkout)
68 die(_("this worktree is not sparse"));
69
70 argc = parse_options(argc, argv, prefix,
@@ -399,12 +400,14 @@ static int set_config(struct repository *repo,
400 }
401
402 static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
403 + struct repo_config_values *cfg = repo_config_values(the_repository);
404 +
405 /* 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)
407 *cone_mode = core_sparse_checkout_cone;
408
409 /* Set cone/non-cone mode appropriately */
407 - core_apply_sparse_checkout = 1;
410 + cfg->apply_sparse_checkout = 1;
411 if (*cone_mode == 1 || *cone_mode == -1) {
412 core_sparse_checkout_cone = 1;
413 return MODE_CONE_PATTERNS;
@@ -416,9 +419,10 @@ static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
419 static int update_modes(struct repository *repo, int *cone_mode, int *sparse_index)
420 {
421 int mode, record_mode;
422 + struct repo_config_values *cfg = repo_config_values(the_repository);
423
424 /* 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;
426
427 mode = update_cone_mode(cone_mode);
428 if (record_mode && set_config(repo, mode))
@@ -684,6 +688,7 @@ static int modify_pattern_list(struct repository *repo,
688 int result;
689 int changed_config = 0;
690 struct pattern_list *pl = xcalloc(1, sizeof(*pl));
691 + struct repo_config_values *cfg = repo_config_values(the_repository);
692
693 switch (m) {
694 case ADD:
@@ -699,9 +704,9 @@ static int modify_pattern_list(struct repository *repo,
704 break;
705 }
706
702 - if (!core_apply_sparse_checkout) {
707 + if (!cfg->apply_sparse_checkout) {
708 set_config(repo, MODE_ALL_PATTERNS);
704 - core_apply_sparse_checkout = 1;
709 + cfg->apply_sparse_checkout = 1;
710 changed_config = 1;
711 }
712
@@ -796,9 +801,10 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix,
801 };
802 struct strvec patterns = STRVEC_INIT;
803 int ret;
804 + struct repo_config_values *cfg = repo_config_values(the_repository);
805
806 setup_work_tree();
801 - if (!core_apply_sparse_checkout)
807 + if (!cfg->apply_sparse_checkout)
808 die(_("no sparse-checkout to add to"));
809
810 repo_read_index(repo);
@@ -905,9 +911,10 @@ static int sparse_checkout_reapply(int argc, const char **argv,
911 N_("toggle the use of a sparse index")),
912 OPT_END(),
913 };
914 + struct repo_config_values *cfg = repo_config_values(the_repository);
915
916 setup_work_tree();
910 - if (!core_apply_sparse_checkout)
917 + if (!cfg->apply_sparse_checkout)
918 die(_("must be in a sparse-checkout to reapply sparsity patterns"));
919
920 reapply_opts.cone_mode = -1;
@@ -960,6 +967,7 @@ static int sparse_checkout_clean(int argc, const char **argv,
967 size_t worktree_len;
968 int force = 0, dry_run = 0, verbose = 0;
969 int require_force = 1;
970 + struct repo_config_values *cfg = repo_config_values(the_repository);
971
972 struct option builtin_sparse_checkout_clean_options[] = {
973 OPT__DRY_RUN(&dry_run, N_("dry run")),
@@ -969,7 +977,7 @@ static int sparse_checkout_clean(int argc, const char **argv,
977 };
978
979 setup_work_tree();
972 - if (!core_apply_sparse_checkout)
980 + if (!cfg->apply_sparse_checkout)
981 die(_("must be in a sparse-checkout to clean directories"));
982 if (!core_sparse_checkout_cone)
983 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,
1041 OPT_END(),
1042 };
1043 struct pattern_list pl;
1044 + struct repo_config_values *cfg = repo_config_values(the_repository);
1045
1046 /*
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
1048 * ability for users to manually muck things up between
1049 * direct editing of .git/info/sparse-checkout
1050 * 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,
1070 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
1071 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
1072 pl.use_cone_patterns = 0;
1064 - core_apply_sparse_checkout = 1;
1073 + cfg->apply_sparse_checkout = 1;
1074
1075 add_pattern("/*", empty_base, 0, &pl, 0);
1076
builtin/worktree.c
+2 -1
@@ -440,6 +440,7 @@ static int add_worktree(const char *path, const char *refname,
440 struct strbuf sb_name = STRBUF_INIT;
441 struct worktree **worktrees, *wt = NULL;
442 struct ref_store *wt_refs;
443 + struct repo_config_values *cfg = repo_config_values(the_repository);
444
445 worktrees = get_worktrees();
446 check_candidate_path(path, opts->force, worktrees, "add");
@@ -536,7 +537,7 @@ static int add_worktree(const char *path, const char *refname,
537 * If the current worktree has sparse-checkout enabled, then copy
538 * the sparse-checkout patterns from the current worktree.
539 */
539 - if (core_apply_sparse_checkout)
540 + if (cfg->apply_sparse_checkout)
541 copy_sparse_checkout(sb_repo.buf);
542
543 /*
dir.c
+3 -1
@@ -1551,7 +1551,9 @@ done:
1551
1552 int init_sparse_checkout_patterns(struct index_state *istate)
1553 {
1554 - if (!core_apply_sparse_checkout)
1554 + struct repo_config_values *cfg = repo_config_values(the_repository);
1555 +
1556 + if (!cfg->apply_sparse_checkout)
1557 return 1;
1558 if (istate->sparse_checkout_patterns)
1559 return 0;
environment.c
+2 -2
@@ -74,7 +74,6 @@ enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
74 #endif
75 enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
76 int grafts_keep_true_parents;
77 -int core_apply_sparse_checkout;
77 int core_sparse_checkout_cone;
78 int sparse_expect_files_outside_of_patterns;
79 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,
545 }
546
547 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);
549 return 0;
550 }
551
@@ -761,4 +760,5 @@ int git_default_config(const char *var, const char *value,
760 void repo_config_values_init(struct repo_config_values *cfg)
761 {
762 cfg->attributes_file = NULL;
763 + cfg->apply_sparse_checkout = 0;
764 }
environment.h
+1 -1
@@ -88,6 +88,7 @@ struct repository;
88 struct repo_config_values {
89 /* section "core" config values */
90 char *attributes_file;
91 + int apply_sparse_checkout;
92 };
93
94 struct repo_config_values *repo_config_values(struct repository *repo);
@@ -171,7 +172,6 @@ extern int precomposed_unicode;
172 extern int protect_hfs;
173 extern int protect_ntfs;
174
174 -extern int core_apply_sparse_checkout;
175 extern int core_sparse_checkout_cone;
176 extern int sparse_expect_files_outside_of_patterns;
177
sparse-index.c
+6 -2
@@ -152,7 +152,9 @@ static int index_has_unmerged_entries(struct index_state *istate)
152
153 int is_sparse_index_allowed(struct index_state *istate, int flags)
154 {
155 - if (!core_apply_sparse_checkout || !core_sparse_checkout_cone)
155 + struct repo_config_values *cfg = repo_config_values(the_repository);
156 +
157 + if (!cfg->apply_sparse_checkout || !core_sparse_checkout_cone)
158 return 0;
159
160 if (!(flags & SPARSE_INDEX_MEMORY_ONLY)) {
@@ -670,7 +672,9 @@ static void clear_skip_worktree_from_present_files_full(struct index_state *ista
672
673 void clear_skip_worktree_from_present_files(struct index_state *istate)
674 {
673 - if (!core_apply_sparse_checkout ||
675 + struct repo_config_values *cfg = repo_config_values(the_repository);
676 +
677 + if (!cfg->apply_sparse_checkout ||
678 sparse_expect_files_outside_of_patterns)
679 return;
680
unpack-trees.c
+2 -1
@@ -1888,6 +1888,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
1888 struct pattern_list pl;
1889 int free_pattern_list = 0;
1890 struct dir_struct dir = DIR_INIT;
1891 + struct repo_config_values *cfg = repo_config_values(the_repository);
1892
1893 if (o->reset == UNPACK_RESET_INVALID)
1894 BUG("o->reset had a value of 1; should be UNPACK_TREES_*_UNTRACKED");
@@ -1924,7 +1925,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
1925 if (o->prefix)
1926 update_sparsity_for_prefix(o->prefix, o->src_index);
1927
1927 - if (!core_apply_sparse_checkout || !o->update)
1928 + if (!cfg->apply_sparse_checkout || !o->update)
1929 o->skip_sparse_checkout = 1;
1930 if (!o->skip_sparse_checkout) {
1931 memset(&pl, 0, sizeof(pl));
wt-status.c
+3 -1
@@ -1763,8 +1763,10 @@ static void wt_status_check_sparse_checkout(struct repository *r,
1763 {
1764 int skip_worktree = 0;
1765 int i;
1766 + struct repo_config_values *cfg = repo_config_values(the_repository);
1767
1767 - if (!core_apply_sparse_checkout || r->index->cache_nr == 0) {
1768 + if (!cfg->apply_sparse_checkout ||
1769 + r->index->cache_nr == 0) {
1770 /*
1771 * Don't compute percentage of checked out files if we
1772 * aren't in a sparse checkout or would get division by 0.