Skip to content

Commit 2facab3

Browse files
ayu-chgitster
authored andcommitted
environment: move access to "core.sparsecheckout" into repo_settings
The setting "core.sparsecheckout" is stored in the global `core_apply_sparse_checkout` and is populated in config.c. Refactor the code to store it in the variable `sparse_checkout` in the struct `repo_settings`. Call `prepare_repo_settings()` where necessary to ensure the `struct repo_settings` is initialized before use: - In "builtin/backfill.c", "builtin/mv.c" and "builtin/clone.c" call `prepare_repo_settings()` since their respective `cmd_*()` functions did not call it earlier. - In "dir.c", the function using 'settings.sparse_checkout' is invoked in multiple files that do not call `prepare_repo_settings()`, hence add a call directly to that function. - In "sparse-index.c", remove a call to `prepare_repo_settings()` from the function `is_sparse_index_allowed()` as it is called everytime before the function is called, and add a call to `prepare_repo_settings()` inside `convert_to_sparse()`, as it is used widely without having a call to `prepare_repo_settings()` before and relies on the setting. - In "wt-status.c", call `prepare_repo_settings()` before accessing the setting because the function using it is commonly used. Avoid reduntant calls to `prepare_repo_settings()` where it is already present: - In "builtin/worktree.c", it is already invoked in `cmd_worktree()` before the setting is accessed. - In "unpack-tress.c", the function accessing the setting already calls it. This also allows us to remove the definition `#define USE_THE_REPOSITORY_VARIABLE` from the file 'builtin/backfill.c'. This change is part of an ongoing effort to eliminate global variables, improve modularity and help libify the codebase. Mentored-by: Christian Couder <[email protected]> Mentored-by: Ghanshyam Thakkar <[email protected]> Signed-off-by: Ayush Chandekar <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 16bd9f2 commit 2facab3

15 files changed

+28
-34
lines changed

builtin/backfill.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/* We need this macro to access core_apply_sparse_checkout */
2-
#define USE_THE_REPOSITORY_VARIABLE
3-
41
#include "builtin.h"
52
#include "git-compat-util.h"
63
#include "config.h"
@@ -137,9 +134,9 @@ int cmd_backfill(int argc, const char **argv, const char *prefix, struct reposit
137134
0);
138135

139136
repo_config(repo, git_default_config, NULL);
140-
137+
prepare_repo_settings(repo);
141138
if (ctx.sparse < 0)
142-
ctx.sparse = core_apply_sparse_checkout;
139+
ctx.sparse = repo->settings.sparse_checkout;
143140

144141
result = do_backfill(&ctx);
145142
backfill_context_clear(&ctx);

builtin/clone.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,11 +617,12 @@ static int git_sparse_checkout_init(const char *repo)
617617
int result = 0;
618618
strvec_pushl(&cmd.args, "-C", repo, "sparse-checkout", "set", NULL);
619619

620+
prepare_repo_settings(the_repository);
620621
/*
621622
* We must apply the setting in the current process
622623
* for the later checkout to use the sparse-checkout file.
623624
*/
624-
core_apply_sparse_checkout = 1;
625+
the_repository->settings.sparse_checkout = 1;
625626

626627
cmd.git_cmd = 1;
627628
if (run_command(&cmd)) {

builtin/grep.c

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

builtin/mv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,9 @@ int cmd_mv(int argc,
570570
&st,
571571
0);
572572
rename_index_entry_at(the_repository->index, pos, dst);
573-
573+
prepare_repo_settings(the_repository);
574574
if (ignore_sparse &&
575-
core_apply_sparse_checkout &&
575+
the_repository->settings.sparse_checkout &&
576576
core_sparse_checkout_cone) {
577577
/*
578578
* NEEDSWORK: we are *not* paying attention to

builtin/sparse-checkout.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static int sparse_checkout_list(int argc, const char **argv, const char *prefix,
6262
int res;
6363

6464
setup_work_tree();
65-
if (!core_apply_sparse_checkout)
65+
if (!the_repository->settings.sparse_checkout)
6666
die(_("this worktree is not sparse"));
6767

6868
argc = parse_options(argc, argv, prefix,
@@ -397,11 +397,11 @@ static int set_config(enum sparse_checkout_mode mode)
397397

398398
static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
399399
/* If not specified, use previous definition of cone mode */
400-
if (*cone_mode == -1 && core_apply_sparse_checkout)
400+
if (*cone_mode == -1 && the_repository->settings.sparse_checkout)
401401
*cone_mode = core_sparse_checkout_cone;
402402

403403
/* Set cone/non-cone mode appropriately */
404-
core_apply_sparse_checkout = 1;
404+
the_repository->settings.sparse_checkout = 1;
405405
if (*cone_mode == 1 || *cone_mode == -1) {
406406
core_sparse_checkout_cone = 1;
407407
return MODE_CONE_PATTERNS;
@@ -415,7 +415,7 @@ static int update_modes(int *cone_mode, int *sparse_index)
415415
int mode, record_mode;
416416

417417
/* Determine if we need to record the mode; ensure sparse checkout on */
418-
record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
418+
record_mode = (*cone_mode != -1) || !the_repository->settings.sparse_checkout;
419419

420420
mode = update_cone_mode(cone_mode);
421421
if (record_mode && set_config(mode))
@@ -695,9 +695,9 @@ static int modify_pattern_list(struct strvec *args, int use_stdin,
695695
break;
696696
}
697697

698-
if (!core_apply_sparse_checkout) {
698+
if (!the_repository->settings.sparse_checkout) {
699699
set_config(MODE_ALL_PATTERNS);
700-
core_apply_sparse_checkout = 1;
700+
the_repository->settings.sparse_checkout = 1;
701701
changed_config = 1;
702702
}
703703

@@ -793,7 +793,7 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix,
793793
int ret;
794794

795795
setup_work_tree();
796-
if (!core_apply_sparse_checkout)
796+
if (!the_repository->settings.sparse_checkout)
797797
die(_("no sparse-checkout to add to"));
798798

799799
repo_read_index(the_repository);
@@ -902,7 +902,7 @@ static int sparse_checkout_reapply(int argc, const char **argv,
902902
};
903903

904904
setup_work_tree();
905-
if (!core_apply_sparse_checkout)
905+
if (!the_repository->settings.sparse_checkout)
906906
die(_("must be in a sparse-checkout to reapply sparsity patterns"));
907907

908908
reapply_opts.cone_mode = -1;
@@ -935,7 +935,7 @@ static int sparse_checkout_disable(int argc, const char **argv,
935935
struct pattern_list pl;
936936

937937
/*
938-
* We do not exit early if !core_apply_sparse_checkout; due to the
938+
* We do not exit early if !sparse_checkout; due to the
939939
* ability for users to manually muck things up between
940940
* direct editing of .git/info/sparse-checkout
941941
* running read-tree -m u HEAD or update-index --skip-worktree
@@ -961,11 +961,10 @@ static int sparse_checkout_disable(int argc, const char **argv,
961961
hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
962962
hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
963963
pl.use_cone_patterns = 0;
964-
core_apply_sparse_checkout = 1;
964+
the_repository->settings.sparse_checkout = 1;
965965

966966
add_pattern("/*", empty_base, 0, &pl, 0);
967967

968-
prepare_repo_settings(the_repository);
969968
the_repository->settings.sparse_index = 0;
970969

971970
if (update_working_directory(&pl))

builtin/worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ static int add_worktree(const char *path, const char *refname,
536536
* If the current worktree has sparse-checkout enabled, then copy
537537
* the sparse-checkout patterns from the current worktree.
538538
*/
539-
if (core_apply_sparse_checkout)
539+
if (the_repository->settings.sparse_checkout)
540540
copy_sparse_checkout(sb_repo.buf);
541541

542542
/*

config.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,11 +1612,6 @@ static int git_default_core_config(const char *var, const char *value,
16121612
return 0;
16131613
}
16141614

1615-
if (!strcmp(var, "core.sparsecheckout")) {
1616-
core_apply_sparse_checkout = git_config_bool(var, value);
1617-
return 0;
1618-
}
1619-
16201615
if (!strcmp(var, "core.sparsecheckoutcone")) {
16211616
core_sparse_checkout_cone = git_config_bool(var, value);
16221617
return 0;

dir.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,7 @@ enum pattern_match_result path_matches_pattern_list(
15031503

15041504
int init_sparse_checkout_patterns(struct index_state *istate)
15051505
{
1506-
if (!core_apply_sparse_checkout)
1506+
if (!istate->repo->settings.sparse_checkout)
15071507
return 1;
15081508
if (istate->sparse_checkout_patterns)
15091509
return 0;
@@ -1526,6 +1526,7 @@ static int path_in_sparse_checkout_1(const char *path,
15261526
enum pattern_match_result match = UNDECIDED;
15271527
const char *end, *slash;
15281528

1529+
prepare_repo_settings(istate->repo);
15291530
/*
15301531
* We default to accepting a path if the path is empty, there are no
15311532
* patterns, or the patterns are of the wrong type.

environment.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
6464
#endif
6565
enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
6666
int grafts_keep_true_parents;
67-
int core_apply_sparse_checkout;
6867
int core_sparse_checkout_cone;
6968
int sparse_expect_files_outside_of_patterns;
7069
int merge_log_config = -1;

environment.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ extern int precomposed_unicode;
160160
extern int protect_hfs;
161161
extern int protect_ntfs;
162162

163-
extern int core_apply_sparse_checkout;
164163
extern int core_sparse_checkout_cone;
165164
extern int sparse_expect_files_outside_of_patterns;
166165

0 commit comments

Comments
 (0)