]> git.ipfire.org Git - thirdparty/git.git/commitdiff
env: move "core_sparse_checkout_cone" into `struct repo_config_values`
authorOlamide Caleb Bello <belkid98@gmail.com>
Thu, 23 Apr 2026 16:54:30 +0000 (17:54 +0100)
committerJunio C Hamano <gitster@pobox.com>
Sat, 25 Apr 2026 10:35:41 +0000 (19:35 +0900)
The `core.sparseCheckoutCone` configuration was previously stored in an
uninitialized global `int` variable, risking cross‑repository state
leakage.

Move it into `repo_config_values`, where eagerly‑parsed repository
configuration lives. `core.sparseCheckoutCone` is parsed eagerly
because it determines the fundamental sparse‑checkout mode and is
consulted very early during repository setup; a lazy parse could
leave the sparse‑checkout state undefined and complicate
libification. This preserves the existing behavior while tying the
value to the repository from which it was read, avoiding cross‑
repository state leakage and continuing the effort to reduce reliance
on global configuration state.

Update all references to use `repo_config_values()`.

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>
builtin/mv.c
builtin/sparse-checkout.c
dir.c
environment.c
environment.h
sparse-index.c

index 2215d34e31f29af260b0d9743778604dda6e0620..ef3a326c906897c789980f166816b0eca04ad9f7 100644 (file)
@@ -574,7 +574,7 @@ remove_entry:
 
                if (ignore_sparse &&
                    cfg->apply_sparse_checkout &&
-                   core_sparse_checkout_cone) {
+                   cfg->core_sparse_checkout_cone) {
                        /*
                         * NEEDSWORK: we are *not* paying attention to
                         * "out-to-out" move (<source> is out-of-cone and
index f4aa405da9376098079715d55371ee2f70a3c50a..92d017b81f9a327b4ea77436c422b1609602b8c1 100644 (file)
@@ -73,7 +73,7 @@ static int sparse_checkout_list(int argc, const char **argv, const char *prefix,
 
        memset(&pl, 0, sizeof(pl));
 
-       pl.use_cone_patterns = core_sparse_checkout_cone;
+       pl.use_cone_patterns = cfg->core_sparse_checkout_cone;
 
        sparse_filename = get_sparse_checkout_filename();
        res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
@@ -334,6 +334,7 @@ static int write_patterns_and_update(struct repository *repo,
        FILE *fp;
        struct lock_file lk = LOCK_INIT;
        int result;
+       struct repo_config_values *cfg = repo_config_values(the_repository);
 
        sparse_filename = get_sparse_checkout_filename();
 
@@ -353,7 +354,7 @@ static int write_patterns_and_update(struct repository *repo,
        if (!fp)
                die_errno(_("unable to fdopen %s"), get_lock_file_path(&lk));
 
-       if (core_sparse_checkout_cone)
+       if (cfg->core_sparse_checkout_cone)
                write_cone_to_file(fp, pl);
        else
                write_patterns_to_file(fp, pl);
@@ -402,15 +403,15 @@ static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
 
        /* If not specified, use previous definition of cone mode */
        if (*cone_mode == -1 && cfg->apply_sparse_checkout)
-               *cone_mode = core_sparse_checkout_cone;
+               *cone_mode = cfg->core_sparse_checkout_cone;
 
        /* Set cone/non-cone mode appropriately */
        cfg->apply_sparse_checkout = 1;
        if (*cone_mode == 1 || *cone_mode == -1) {
-               core_sparse_checkout_cone = 1;
+               cfg->core_sparse_checkout_cone = 1;
                return MODE_CONE_PATTERNS;
        }
-       core_sparse_checkout_cone = 0;
+       cfg->core_sparse_checkout_cone = 0;
        return MODE_ALL_PATTERNS;
 }
 
@@ -577,7 +578,9 @@ static void add_patterns_from_input(struct pattern_list *pl,
                                    FILE *file)
 {
        int i;
-       if (core_sparse_checkout_cone) {
+       struct repo_config_values *cfg = repo_config_values(the_repository);
+
+       if (cfg->core_sparse_checkout_cone) {
                struct strbuf line = STRBUF_INIT;
 
                hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
@@ -636,13 +639,14 @@ static void add_patterns_cone_mode(int argc, const char **argv,
        struct pattern_entry *pe;
        struct hashmap_iter iter;
        struct pattern_list existing;
+       struct repo_config_values *cfg = repo_config_values(the_repository);
        char *sparse_filename = get_sparse_checkout_filename();
 
        add_patterns_from_input(pl, argc, argv,
                                use_stdin ? stdin : NULL);
 
        memset(&existing, 0, sizeof(existing));
-       existing.use_cone_patterns = core_sparse_checkout_cone;
+       existing.use_cone_patterns = cfg->core_sparse_checkout_cone;
 
        if (add_patterns_from_file_to_list(sparse_filename, "", 0,
                                           &existing, NULL, 0))
@@ -690,7 +694,7 @@ static int modify_pattern_list(struct repository *repo,
 
        switch (m) {
        case ADD:
-               if (core_sparse_checkout_cone)
+               if (cfg->core_sparse_checkout_cone)
                        add_patterns_cone_mode(args->nr, args->v, pl, use_stdin);
                else
                        add_patterns_literal(args->nr, args->v, pl, use_stdin);
@@ -723,11 +727,12 @@ static void sanitize_paths(struct repository *repo,
                           const char *prefix, int skip_checks)
 {
        int i;
+       struct repo_config_values *cfg = repo_config_values(the_repository);
 
        if (!args->nr)
                return;
 
-       if (prefix && *prefix && core_sparse_checkout_cone) {
+       if (prefix && *prefix && cfg->core_sparse_checkout_cone) {
                /*
                 * The args are not pathspecs, so unfortunately we
                 * cannot imitate how cmd_add() uses parse_pathspec().
@@ -744,10 +749,10 @@ static void sanitize_paths(struct repository *repo,
        if (skip_checks)
                return;
 
-       if (prefix && *prefix && !core_sparse_checkout_cone)
+       if (prefix && *prefix && !cfg->core_sparse_checkout_cone)
                die(_("please run from the toplevel directory in non-cone mode"));
 
-       if (core_sparse_checkout_cone) {
+       if (cfg->core_sparse_checkout_cone) {
                for (i = 0; i < args->nr; i++) {
                        if (args->v[i][0] == '/')
                                die(_("specify directories rather than patterns (no leading slash)"));
@@ -769,7 +774,7 @@ static void sanitize_paths(struct repository *repo,
                if (S_ISSPARSEDIR(ce->ce_mode))
                        continue;
 
-               if (core_sparse_checkout_cone)
+               if (cfg->core_sparse_checkout_cone)
                        die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), args->v[i]);
                else
                        warning(_("pass a leading slash before paths such as '%s' if you want a single file (see NON-CONE PROBLEMS in the git-sparse-checkout manual)."), args->v[i]);
@@ -836,6 +841,7 @@ static struct sparse_checkout_set_opts {
 static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
                               struct repository *repo)
 {
+       struct repo_config_values *cfg = repo_config_values(the_repository);
        int default_patterns_nr = 2;
        const char *default_patterns[] = {"/*", "!/*/", NULL};
 
@@ -873,7 +879,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
         * non-cone mode, if nothing is specified, manually select just the
         * top-level directory (much as 'init' would do).
         */
-       if (!core_sparse_checkout_cone && !set_opts.use_stdin && argc == 0) {
+       if (!cfg->core_sparse_checkout_cone && !set_opts.use_stdin && argc == 0) {
                for (int i = 0; i < default_patterns_nr; i++)
                        strvec_push(&patterns, default_patterns[i]);
        } else {
@@ -977,7 +983,7 @@ static int sparse_checkout_clean(int argc, const char **argv,
        setup_work_tree();
        if (!cfg->apply_sparse_checkout)
                die(_("must be in a sparse-checkout to clean directories"));
-       if (!core_sparse_checkout_cone)
+       if (!cfg->core_sparse_checkout_cone)
                die(_("must be in a cone-mode sparse-checkout to clean directories"));
 
        argc = parse_options(argc, argv, prefix,
@@ -1141,6 +1147,7 @@ static int sparse_checkout_check_rules(int argc, const char **argv, const char *
        FILE *fp;
        int ret;
        struct pattern_list pl = {0};
+       struct repo_config_values *cfg = repo_config_values(the_repository);
        char *sparse_filename;
        check_rules_opts.cone_mode = -1;
 
@@ -1152,7 +1159,7 @@ static int sparse_checkout_check_rules(int argc, const char **argv, const char *
                check_rules_opts.cone_mode = 1;
 
        update_cone_mode(&check_rules_opts.cone_mode);
-       pl.use_cone_patterns = core_sparse_checkout_cone;
+       pl.use_cone_patterns = cfg->core_sparse_checkout_cone;
        if (check_rules_opts.rules_file) {
                fp = xfopen(check_rules_opts.rules_file, "r");
                add_patterns_from_input(&pl, argc, argv, fp);
diff --git a/dir.c b/dir.c
index fcb8f6dd2aa96938cb79af05603cd78d25ead58c..4f493b64c68dd8d8033ac24b98f31f32e9edfffe 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -3508,8 +3508,9 @@ int get_sparse_checkout_patterns(struct pattern_list *pl)
 {
        int res;
        char *sparse_filename = get_sparse_checkout_filename();
+       struct repo_config_values *cfg = repo_config_values(the_repository);
 
-       pl->use_cone_patterns = core_sparse_checkout_cone;
+       pl->use_cone_patterns = cfg->core_sparse_checkout_cone;
        res = add_patterns_from_file_to_list(sparse_filename, "", 0, pl, NULL, 0);
 
        free(sparse_filename);
index 739b647ebe0ed120e049bce97d3d502ae89ca75e..b0e873e9f5b9011627410df92847ade0271fde51 100644 (file)
@@ -70,7 +70,6 @@ enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
 #endif
 enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
 int grafts_keep_true_parents;
-int core_sparse_checkout_cone;
 int sparse_expect_files_outside_of_patterns;
 unsigned long pack_size_limit_cfg;
 
@@ -526,7 +525,7 @@ int git_default_core_config(const char *var, const char *value,
        }
 
        if (!strcmp(var, "core.sparsecheckoutcone")) {
-               core_sparse_checkout_cone = git_config_bool(var, value);
+               cfg->core_sparse_checkout_cone = git_config_bool(var, value);
                return 0;
        }
 
@@ -723,4 +722,5 @@ void repo_config_values_init(struct repo_config_values *cfg)
        cfg->zlib_compression_level = Z_BEST_SPEED;
        cfg->pack_compression_level = Z_DEFAULT_COMPRESSION;
        cfg->precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
+       cfg->core_sparse_checkout_cone = 0;
 }
index 508cb1afbc9fda96d149ea31b6c0012e8f9a466f..befad9a38876e9ce32c622eeabbfa48ddbadc1e1 100644 (file)
@@ -96,6 +96,7 @@ struct repo_config_values {
        int zlib_compression_level;
        int pack_compression_level;
        int precomposed_unicode;
+       int core_sparse_checkout_cone;
 
        /* section "branch" config values */
        enum branch_track branch_track;
@@ -178,7 +179,6 @@ extern unsigned long pack_size_limit_cfg;
 extern int protect_hfs;
 extern int protect_ntfs;
 
-extern int core_sparse_checkout_cone;
 extern int sparse_expect_files_outside_of_patterns;
 
 enum rebase_setup_type {
index 13629c075d06e04cdeea1bc36459f61810b80b16..53cb8d64fc9b2cf792a455d1d4c07cff3e2ae026 100644 (file)
@@ -154,7 +154,7 @@ int is_sparse_index_allowed(struct index_state *istate, int flags)
 {
        struct repo_config_values *cfg = repo_config_values(the_repository);
 
-       if (!cfg->apply_sparse_checkout || !core_sparse_checkout_cone)
+       if (!cfg->apply_sparse_checkout || !cfg->core_sparse_checkout_cone)
                return 0;
 
        if (!(flags & SPARSE_INDEX_MEMORY_ONLY)) {