Skip to content

Commit 3956da4

Browse files
committed
environment: move "core_sparse_checkout_cone" into struct repo_config_values
The `core_sparse_checkout_cone` variable was previously a global integer, uninitialized by default. Storing repository-dependent configuration in globals can lead to cross-repository state leakage. Move it into `repo_config_values` and initialize it to 0 by default. This ensures predictable behavior for repositories that do not set this configuration while preserving existing semantics. 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>
1 parent 0781465 commit 3956da4

File tree

6 files changed

+29
-24
lines changed

6 files changed

+29
-24
lines changed

builtin/mv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ int cmd_mv(int argc,
574574

575575
if (ignore_sparse &&
576576
cfg->apply_sparse_checkout &&
577-
core_sparse_checkout_cone) {
577+
cfg->core_sparse_checkout_cone) {
578578
/*
579579
* NEEDSWORK: we are *not* paying attention to
580580
* "out-to-out" move (<source> is out-of-cone and

builtin/sparse-checkout.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static int sparse_checkout_list(int argc, const char **argv, const char *prefix,
7373

7474
memset(&pl, 0, sizeof(pl));
7575

76-
pl.use_cone_patterns = core_sparse_checkout_cone;
76+
pl.use_cone_patterns = cfg->core_sparse_checkout_cone;
7777

7878
sparse_filename = get_sparse_checkout_filename();
7979
res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
@@ -335,6 +335,7 @@ static int write_patterns_and_update(struct repository *repo,
335335
FILE *fp;
336336
struct lock_file lk = LOCK_INIT;
337337
int result;
338+
struct repo_config_values *cfg = repo_config_values(the_repository);
338339

339340
sparse_filename = get_sparse_checkout_filename();
340341

@@ -354,7 +355,7 @@ static int write_patterns_and_update(struct repository *repo,
354355
if (!fp)
355356
die_errno(_("unable to fdopen %s"), get_lock_file_path(&lk));
356357

357-
if (core_sparse_checkout_cone)
358+
if (cfg->core_sparse_checkout_cone)
358359
write_cone_to_file(fp, pl);
359360
else
360361
write_patterns_to_file(fp, pl);
@@ -403,15 +404,15 @@ static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
403404

404405
/* If not specified, use previous definition of cone mode */
405406
if (*cone_mode == -1 && cfg->apply_sparse_checkout)
406-
*cone_mode = core_sparse_checkout_cone;
407+
*cone_mode = cfg->core_sparse_checkout_cone;
407408

408409
/* Set cone/non-cone mode appropriately */
409410
cfg->apply_sparse_checkout = 1;
410411
if (*cone_mode == 1 || *cone_mode == -1) {
411-
core_sparse_checkout_cone = 1;
412+
cfg->core_sparse_checkout_cone = 1;
412413
return MODE_CONE_PATTERNS;
413414
}
414-
core_sparse_checkout_cone = 0;
415+
cfg->core_sparse_checkout_cone = 0;
415416
return MODE_ALL_PATTERNS;
416417
}
417418

@@ -578,7 +579,9 @@ static void add_patterns_from_input(struct pattern_list *pl,
578579
FILE *file)
579580
{
580581
int i;
581-
if (core_sparse_checkout_cone) {
582+
struct repo_config_values *cfg = repo_config_values(the_repository);
583+
584+
if (cfg->core_sparse_checkout_cone) {
582585
struct strbuf line = STRBUF_INIT;
583586

584587
hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
@@ -637,13 +640,14 @@ static void add_patterns_cone_mode(int argc, const char **argv,
637640
struct pattern_entry *pe;
638641
struct hashmap_iter iter;
639642
struct pattern_list existing;
643+
struct repo_config_values *cfg = repo_config_values(the_repository);
640644
char *sparse_filename = get_sparse_checkout_filename();
641645

642646
add_patterns_from_input(pl, argc, argv,
643647
use_stdin ? stdin : NULL);
644648

645649
memset(&existing, 0, sizeof(existing));
646-
existing.use_cone_patterns = core_sparse_checkout_cone;
650+
existing.use_cone_patterns = cfg->core_sparse_checkout_cone;
647651

648652
if (add_patterns_from_file_to_list(sparse_filename, "", 0,
649653
&existing, NULL, 0))
@@ -691,7 +695,7 @@ static int modify_pattern_list(struct repository *repo,
691695

692696
switch (m) {
693697
case ADD:
694-
if (core_sparse_checkout_cone)
698+
if (cfg->core_sparse_checkout_cone)
695699
add_patterns_cone_mode(args->nr, args->v, pl, use_stdin);
696700
else
697701
add_patterns_literal(args->nr, args->v, pl, use_stdin);
@@ -724,11 +728,12 @@ static void sanitize_paths(struct repository *repo,
724728
const char *prefix, int skip_checks)
725729
{
726730
int i;
731+
struct repo_config_values *cfg = repo_config_values(the_repository);
727732

728733
if (!args->nr)
729734
return;
730735

731-
if (prefix && *prefix && core_sparse_checkout_cone) {
736+
if (prefix && *prefix && cfg->core_sparse_checkout_cone) {
732737
/*
733738
* The args are not pathspecs, so unfortunately we
734739
* cannot imitate how cmd_add() uses parse_pathspec().
@@ -745,10 +750,10 @@ static void sanitize_paths(struct repository *repo,
745750
if (skip_checks)
746751
return;
747752

748-
if (prefix && *prefix && !core_sparse_checkout_cone)
753+
if (prefix && *prefix && !cfg->core_sparse_checkout_cone)
749754
die(_("please run from the toplevel directory in non-cone mode"));
750755

751-
if (core_sparse_checkout_cone) {
756+
if (cfg->core_sparse_checkout_cone) {
752757
for (i = 0; i < args->nr; i++) {
753758
if (args->v[i][0] == '/')
754759
die(_("specify directories rather than patterns (no leading slash)"));
@@ -770,7 +775,7 @@ static void sanitize_paths(struct repository *repo,
770775
if (S_ISSPARSEDIR(ce->ce_mode))
771776
continue;
772777

773-
if (core_sparse_checkout_cone)
778+
if (cfg->core_sparse_checkout_cone)
774779
die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), args->v[i]);
775780
else
776781
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]);
@@ -837,6 +842,7 @@ static struct sparse_checkout_set_opts {
837842
static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
838843
struct repository *repo)
839844
{
845+
struct repo_config_values *cfg = repo_config_values(the_repository);
840846
int default_patterns_nr = 2;
841847
const char *default_patterns[] = {"/*", "!/*/", NULL};
842848

@@ -874,7 +880,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
874880
* non-cone mode, if nothing is specified, manually select just the
875881
* top-level directory (much as 'init' would do).
876882
*/
877-
if (!core_sparse_checkout_cone && !set_opts.use_stdin && argc == 0) {
883+
if (!cfg->core_sparse_checkout_cone && !set_opts.use_stdin && argc == 0) {
878884
for (int i = 0; i < default_patterns_nr; i++)
879885
strvec_push(&patterns, default_patterns[i]);
880886
} else {
@@ -978,7 +984,7 @@ static int sparse_checkout_clean(int argc, const char **argv,
978984
setup_work_tree();
979985
if (!cfg->apply_sparse_checkout)
980986
die(_("must be in a sparse-checkout to clean directories"));
981-
if (!core_sparse_checkout_cone)
987+
if (!cfg->core_sparse_checkout_cone)
982988
die(_("must be in a cone-mode sparse-checkout to clean directories"));
983989

984990
argc = parse_options(argc, argv, prefix,
@@ -1142,6 +1148,7 @@ static int sparse_checkout_check_rules(int argc, const char **argv, const char *
11421148
FILE *fp;
11431149
int ret;
11441150
struct pattern_list pl = {0};
1151+
struct repo_config_values *cfg = repo_config_values(the_repository);
11451152
char *sparse_filename;
11461153
check_rules_opts.cone_mode = -1;
11471154

@@ -1153,7 +1160,7 @@ static int sparse_checkout_check_rules(int argc, const char **argv, const char *
11531160
check_rules_opts.cone_mode = 1;
11541161

11551162
update_cone_mode(&check_rules_opts.cone_mode);
1156-
pl.use_cone_patterns = core_sparse_checkout_cone;
1163+
pl.use_cone_patterns = cfg->core_sparse_checkout_cone;
11571164
if (check_rules_opts.rules_file) {
11581165
fp = xfopen(check_rules_opts.rules_file, "r");
11591166
add_patterns_from_input(&pl, argc, argv, fp);

dir.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3508,8 +3508,9 @@ int get_sparse_checkout_patterns(struct pattern_list *pl)
35083508
{
35093509
int res;
35103510
char *sparse_filename = get_sparse_checkout_filename();
3511+
struct repo_config_values *cfg = repo_config_values(the_repository);
35113512

3512-
pl->use_cone_patterns = core_sparse_checkout_cone;
3513+
pl->use_cone_patterns = cfg->core_sparse_checkout_cone;
35133514
res = add_patterns_from_file_to_list(sparse_filename, "", 0, pl, NULL, 0);
35143515

35153516
free(sparse_filename);

environment.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
7070
#endif
7171
enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
7272
int grafts_keep_true_parents;
73-
int core_sparse_checkout_cone;
7473
int sparse_expect_files_outside_of_patterns;
7574
unsigned long pack_size_limit_cfg;
7675

@@ -526,7 +525,7 @@ int git_default_core_config(const char *var, const char *value,
526525
}
527526

528527
if (!strcmp(var, "core.sparsecheckoutcone")) {
529-
core_sparse_checkout_cone = git_config_bool(var, value);
528+
cfg->core_sparse_checkout_cone = git_config_bool(var, value);
530529
return 0;
531530
}
532531

@@ -742,4 +741,5 @@ void repo_config_values_init(struct repo_config_values *cfg)
742741
cfg->zlib_compression_level = Z_BEST_SPEED;
743742
cfg->pack_compression_level = Z_DEFAULT_COMPRESSION;
744743
cfg->precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
744+
cfg->core_sparse_checkout_cone = 0;
745745
}

environment.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ struct repo_config_values {
9595
int zlib_compression_level;
9696
int pack_compression_level;
9797
int precomposed_unicode;
98+
int core_sparse_checkout_cone;
9899

99100
/* section "branch" config values */
100101
enum branch_track branch_track;
@@ -174,10 +175,6 @@ extern char *apply_default_whitespace;
174175
extern char *apply_default_ignorewhitespace;
175176
extern unsigned long pack_size_limit_cfg;
176177

177-
extern int protect_hfs;
178-
extern int protect_ntfs;
179-
180-
extern int core_sparse_checkout_cone;
181178
extern int sparse_expect_files_outside_of_patterns;
182179

183180
enum rebase_setup_type {

sparse-index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ int is_sparse_index_allowed(struct index_state *istate, int flags)
154154
{
155155
struct repo_config_values *cfg = repo_config_values(the_repository);
156156

157-
if (!cfg->apply_sparse_checkout || !core_sparse_checkout_cone)
157+
if (!cfg->apply_sparse_checkout || !cfg->core_sparse_checkout_cone)
158158
return 0;
159159

160160
if (!(flags & SPARSE_INDEX_MEMORY_ONLY)) {

0 commit comments

Comments
 (0)