The setting "core.sparsecheckoutcone" is stored in the global `core_sparse_checkout_cone` and is populated in config.c. Refactor the code to store it in the variable `sparse_checkout_cone` in the struct `repo_settings`. Call `prepare_repo_settings()` where necessary to ensure the `struct repo_settings` is initialized before use: - In "dir.c", the function accessing the setting is usually called after `prepare_repo_settings()`, except for one code path in "unpack-trees.c", so add a call there. Avoid redundant calls to `prepare_repo_settings()` where it is already present: - In "builtin/mv.c" and "builtin/sparse-checkout.c", it is already invoked in their respective `cmd_*()` functions. - In "sparse-index.c", `prepare_repo_settings` is already called before the setting is accessed. This change is part of an ongoing effort to eliminate global variables, improve modularity and help libify the codebase. Mentored-by: Christian Couder <christian.couder@xxxxxxxxx> Mentored-by: Ghanshyam Thakkar <shyamthakkar001@xxxxxxxxx> Signed-off-by: Ayush Chandekar <ayu.chandekar@xxxxxxxxx> --- builtin/grep.c | 2 +- builtin/mv.c | 2 +- builtin/sparse-checkout.c | 28 ++++++++++++++-------------- config.c | 5 ----- dir.c | 2 +- environment.c | 1 - environment.h | 1 - repo-settings.c | 1 + repo-settings.h | 2 ++ sparse-index.c | 2 +- unpack-trees.c | 1 + 11 files changed, 22 insertions(+), 25 deletions(-) diff --git a/builtin/grep.c b/builtin/grep.c index 6111fee60b..8971a15ef4 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -491,7 +491,7 @@ static int grep_submodule(struct grep_opt *opt, * dictate the behavior for the submodule, making it "forget" its * sparse-checkout state. * - * 3. "core_sparse_checkout_cone" + * 3. "settings.sparse_checkout_cone" * ditto. * * Note that this list is not exhaustive. diff --git a/builtin/mv.c b/builtin/mv.c index 43ed2e3d0a..2d1326a18f 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -573,7 +573,7 @@ int cmd_mv(int argc, prepare_repo_settings(the_repository); if (ignore_sparse && the_repository->settings.sparse_checkout && - core_sparse_checkout_cone) { + the_repository->settings.sparse_checkout_cone) { /* * NEEDSWORK: we are *not* paying attention to * "out-to-out" move (<source> is out-of-cone and diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c index 8329d29a27..8a0ffba9d4 100644 --- a/builtin/sparse-checkout.c +++ b/builtin/sparse-checkout.c @@ -71,7 +71,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 = the_repository->settings.sparse_checkout_cone; sparse_filename = get_sparse_checkout_filename(); res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0); @@ -352,7 +352,7 @@ static int write_patterns_and_update(struct pattern_list *pl) if (!fp) die_errno(_("unable to fdopen %s"), get_lock_file_path(&lk)); - if (core_sparse_checkout_cone) + if (the_repository->settings.sparse_checkout_cone) write_cone_to_file(fp, pl); else write_patterns_to_file(fp, pl); @@ -398,15 +398,15 @@ static int set_config(enum sparse_checkout_mode mode) static enum sparse_checkout_mode update_cone_mode(int *cone_mode) { /* If not specified, use previous definition of cone mode */ if (*cone_mode == -1 && the_repository->settings.sparse_checkout) - *cone_mode = core_sparse_checkout_cone; + *cone_mode = the_repository->settings.sparse_checkout_cone; /* Set cone/non-cone mode appropriately */ the_repository->settings.sparse_checkout = 1; if (*cone_mode == 1 || *cone_mode == -1) { - core_sparse_checkout_cone = 1; + the_repository->settings.sparse_checkout_cone = 1; return MODE_CONE_PATTERNS; } - core_sparse_checkout_cone = 0; + the_repository->settings.sparse_checkout_cone = 0; return MODE_ALL_PATTERNS; } @@ -572,7 +572,7 @@ static void add_patterns_from_input(struct pattern_list *pl, FILE *file) { int i; - if (core_sparse_checkout_cone) { + if (the_repository->settings.sparse_checkout_cone) { struct strbuf line = STRBUF_INIT; hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0); @@ -637,7 +637,7 @@ static void add_patterns_cone_mode(int argc, const char **argv, use_stdin ? stdin : NULL); memset(&existing, 0, sizeof(existing)); - existing.use_cone_patterns = core_sparse_checkout_cone; + existing.use_cone_patterns = the_repository->settings.sparse_checkout_cone; if (add_patterns_from_file_to_list(sparse_filename, "", 0, &existing, NULL, 0)) @@ -683,7 +683,7 @@ static int modify_pattern_list(struct strvec *args, int use_stdin, switch (m) { case ADD: - if (core_sparse_checkout_cone) + if (the_repository->settings.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); @@ -719,7 +719,7 @@ static void sanitize_paths(struct strvec *args, if (!args->nr) return; - if (prefix && *prefix && core_sparse_checkout_cone) { + if (prefix && *prefix && the_repository->settings.sparse_checkout_cone) { /* * The args are not pathspecs, so unfortunately we * cannot imitate how cmd_add() uses parse_pathspec(). @@ -736,10 +736,10 @@ static void sanitize_paths(struct strvec *args, if (skip_checks) return; - if (prefix && *prefix && !core_sparse_checkout_cone) + if (prefix && *prefix && !the_repository->settings.sparse_checkout_cone) die(_("please run from the toplevel directory in non-cone mode")); - if (core_sparse_checkout_cone) { + if (the_repository->settings.sparse_checkout_cone) { for (i = 0; i < args->nr; i++) { if (args->v[i][0] == '/') die(_("specify directories rather than patterns (no leading slash)")); @@ -761,7 +761,7 @@ static void sanitize_paths(struct strvec *args, if (S_ISSPARSEDIR(ce->ce_mode)) continue; - if (core_sparse_checkout_cone) + if (the_repository->settings.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]); @@ -864,7 +864,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 (!the_repository->settings.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 { @@ -1041,7 +1041,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 = the_repository->settings.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/config.c b/config.c index 8fd4dd8c81..707fe0707a 100644 --- a/config.c +++ b/config.c @@ -1612,11 +1612,6 @@ static int git_default_core_config(const char *var, const char *value, return 0; } - if (!strcmp(var, "core.sparsecheckoutcone")) { - core_sparse_checkout_cone = git_config_bool(var, value); - return 0; - } - if (!strcmp(var, "core.precomposeunicode")) { precomposed_unicode = git_config_bool(var, value); return 0; diff --git a/dir.c b/dir.c index 5d1cb7a067..d2b0a5aef6 100644 --- a/dir.c +++ b/dir.c @@ -3460,7 +3460,7 @@ int get_sparse_checkout_patterns(struct pattern_list *pl) int res; char *sparse_filename = get_sparse_checkout_filename(); - pl->use_cone_patterns = core_sparse_checkout_cone; + pl->use_cone_patterns = the_repository->settings.sparse_checkout_cone; res = add_patterns_from_file_to_list(sparse_filename, "", 0, pl, NULL, 0); free(sparse_filename); diff --git a/environment.c b/environment.c index a379a9149e..7d46b80711 100644 --- a/environment.c +++ b/environment.c @@ -64,7 +64,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; int merge_log_config = -1; int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */ diff --git a/environment.h b/environment.h index 6a30512f3c..00a5b332a0 100644 --- a/environment.h +++ b/environment.h @@ -160,7 +160,6 @@ extern int precomposed_unicode; 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 { diff --git a/repo-settings.c b/repo-settings.c index 9270cca561..eebc1f941d 100644 --- a/repo-settings.c +++ b/repo-settings.c @@ -82,6 +82,7 @@ void prepare_repo_settings(struct repository *r) r->settings.pack_use_bitmap_boundary_traversal); repo_cfg_bool(r, "core.usereplacerefs", &r->settings.read_replace_refs, 1); repo_cfg_bool(r, "core.sparsecheckout", &r->settings.sparse_checkout, 0); + repo_cfg_bool(r, "core.sparsecheckoutcone", &r->settings.sparse_checkout_cone, 0); /* * The GIT_TEST_MULTI_PACK_INDEX variable is special in that diff --git a/repo-settings.h b/repo-settings.h index 9caa7c57a3..443e1399da 100644 --- a/repo-settings.h +++ b/repo-settings.h @@ -67,7 +67,9 @@ struct repo_settings { unsigned long big_file_threshold; char *hooks_path; + int sparse_checkout; + int sparse_checkout_cone; }; #define REPO_SETTINGS_INIT { \ .shared_repository = -1, \ diff --git a/sparse-index.c b/sparse-index.c index 8132c0f2fb..6fd782a8fc 100644 --- a/sparse-index.c +++ b/sparse-index.c @@ -151,7 +151,7 @@ static int index_has_unmerged_entries(struct index_state *istate) int is_sparse_index_allowed(struct index_state *istate, int flags) { prepare_repo_settings(istate->repo); - if (!istate->repo->settings.sparse_checkout || !core_sparse_checkout_cone) + if (!istate->repo->settings.sparse_checkout || !istate->repo->settings.sparse_checkout_cone) return 0; if (!(flags & SPARSE_INDEX_MEMORY_ONLY)) { diff --git a/unpack-trees.c b/unpack-trees.c index 02e32c4ba1..0e9813bddf 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -2144,6 +2144,7 @@ enum update_sparsity_result update_sparsity(struct unpack_trees_options *o, BUG("update_sparsity() called wrong"); trace_performance_enter(); + prepare_repo_settings(the_repository); /* If we weren't given patterns, use the recorded ones */ if (!pl) { -- 2.49.0