Skip to content

Commit 064468e

Browse files
derrickstoleegitster
authored andcommitted
sparse-checkout: remove use of the_repository
The logic for the 'git sparse-checkout' builtin uses the_repository all over the place, despite some use of a repository struct in different method parameters. Complete this removal of the_repository by using 'repo' when possible. In one place, there was already a local variable 'r' that was set to the_repository, so move that to a method parameter. We cannot remove the USE_THE_REPOSITORY_VARIABLE declaration as we are still using global constants for the state of the sparse-checkout. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c44beea commit 064468e

File tree

1 file changed

+62
-55
lines changed

1 file changed

+62
-55
lines changed

builtin/sparse-checkout.c

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,12 @@ static void clean_tracked_sparse_directories(struct repository *r)
204204
ensure_full_index(r->index);
205205
}
206206

207-
static int update_working_directory(struct pattern_list *pl)
207+
static int update_working_directory(struct repository *r,
208+
struct pattern_list *pl)
208209
{
209210
enum update_sparsity_result result;
210211
struct unpack_trees_options o;
211212
struct lock_file lock_file = LOCK_INIT;
212-
struct repository *r = the_repository;
213213
struct pattern_list *old_pl;
214214

215215
/* If no branch has been checked out, there are no updates to make. */
@@ -327,7 +327,8 @@ static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
327327
string_list_clear(&sl, 0);
328328
}
329329

330-
static int write_patterns_and_update(struct pattern_list *pl)
330+
static int write_patterns_and_update(struct repository *repo,
331+
struct pattern_list *pl)
331332
{
332333
char *sparse_filename;
333334
FILE *fp;
@@ -336,15 +337,15 @@ static int write_patterns_and_update(struct pattern_list *pl)
336337

337338
sparse_filename = get_sparse_checkout_filename();
338339

339-
if (safe_create_leading_directories(the_repository, sparse_filename))
340+
if (safe_create_leading_directories(repo, sparse_filename))
340341
die(_("failed to create directory for sparse-checkout file"));
341342

342343
hold_lock_file_for_update(&lk, sparse_filename, LOCK_DIE_ON_ERROR);
343344

344-
result = update_working_directory(pl);
345+
result = update_working_directory(repo, pl);
345346
if (result) {
346347
rollback_lock_file(&lk);
347-
update_working_directory(NULL);
348+
update_working_directory(repo, NULL);
348349
goto out;
349350
}
350351

@@ -372,25 +373,26 @@ enum sparse_checkout_mode {
372373
MODE_CONE_PATTERNS = 2,
373374
};
374375

375-
static int set_config(enum sparse_checkout_mode mode)
376+
static int set_config(struct repository *repo,
377+
enum sparse_checkout_mode mode)
376378
{
377379
/* Update to use worktree config, if not already. */
378-
if (init_worktree_config(the_repository)) {
380+
if (init_worktree_config(repo)) {
379381
error(_("failed to initialize worktree config"));
380382
return 1;
381383
}
382384

383-
if (repo_config_set_worktree_gently(the_repository,
385+
if (repo_config_set_worktree_gently(repo,
384386
"core.sparseCheckout",
385387
mode ? "true" : "false") ||
386-
repo_config_set_worktree_gently(the_repository,
388+
repo_config_set_worktree_gently(repo,
387389
"core.sparseCheckoutCone",
388390
mode == MODE_CONE_PATTERNS ?
389391
"true" : "false"))
390392
return 1;
391393

392394
if (mode == MODE_NO_PATTERNS)
393-
return set_sparse_index_config(the_repository, 0);
395+
return set_sparse_index_config(repo, 0);
394396

395397
return 0;
396398
}
@@ -410,28 +412,28 @@ static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
410412
return MODE_ALL_PATTERNS;
411413
}
412414

413-
static int update_modes(int *cone_mode, int *sparse_index)
415+
static int update_modes(struct repository *repo, int *cone_mode, int *sparse_index)
414416
{
415417
int mode, record_mode;
416418

417419
/* Determine if we need to record the mode; ensure sparse checkout on */
418420
record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
419421

420422
mode = update_cone_mode(cone_mode);
421-
if (record_mode && set_config(mode))
423+
if (record_mode && set_config(repo, mode))
422424
return 1;
423425

424426
/* Set sparse-index/non-sparse-index mode if specified */
425427
if (*sparse_index >= 0) {
426-
if (set_sparse_index_config(the_repository, *sparse_index) < 0)
428+
if (set_sparse_index_config(repo, *sparse_index) < 0)
427429
die(_("failed to modify sparse-index config"));
428430

429431
/* force an index rewrite */
430-
repo_read_index(the_repository);
431-
the_repository->index->updated_workdir = 1;
432+
repo_read_index(repo);
433+
repo->index->updated_workdir = 1;
432434

433435
if (!*sparse_index)
434-
ensure_full_index(the_repository->index);
436+
ensure_full_index(repo->index);
435437
}
436438

437439
return 0;
@@ -448,7 +450,7 @@ static struct sparse_checkout_init_opts {
448450
} init_opts;
449451

450452
static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
451-
struct repository *repo UNUSED)
453+
struct repository *repo)
452454
{
453455
struct pattern_list pl;
454456
char *sparse_filename;
@@ -464,7 +466,7 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
464466
};
465467

466468
setup_work_tree();
467-
repo_read_index(the_repository);
469+
repo_read_index(repo);
468470

469471
init_opts.cone_mode = -1;
470472
init_opts.sparse_index = -1;
@@ -473,7 +475,7 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
473475
builtin_sparse_checkout_init_options,
474476
builtin_sparse_checkout_init_usage, 0);
475477

476-
if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index))
478+
if (update_modes(repo, &init_opts.cone_mode, &init_opts.sparse_index))
477479
return 1;
478480

479481
memset(&pl, 0, sizeof(pl));
@@ -485,14 +487,14 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
485487
if (res >= 0) {
486488
free(sparse_filename);
487489
clear_pattern_list(&pl);
488-
return update_working_directory(NULL);
490+
return update_working_directory(repo, NULL);
489491
}
490492

491-
if (repo_get_oid(the_repository, "HEAD", &oid)) {
493+
if (repo_get_oid(repo, "HEAD", &oid)) {
492494
FILE *fp;
493495

494496
/* assume we are in a fresh repo, but update the sparse-checkout file */
495-
if (safe_create_leading_directories(the_repository, sparse_filename))
497+
if (safe_create_leading_directories(repo, sparse_filename))
496498
die(_("unable to create leading directories of %s"),
497499
sparse_filename);
498500
fp = xfopen(sparse_filename, "w");
@@ -511,7 +513,7 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
511513
add_pattern("!/*/", empty_base, 0, &pl, 0);
512514
pl.use_cone_patterns = init_opts.cone_mode;
513515

514-
return write_patterns_and_update(&pl);
516+
return write_patterns_and_update(repo, &pl);
515517
}
516518

517519
static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
@@ -674,7 +676,8 @@ static void add_patterns_literal(int argc, const char **argv,
674676
add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL);
675677
}
676678

677-
static int modify_pattern_list(struct strvec *args, int use_stdin,
679+
static int modify_pattern_list(struct repository *repo,
680+
struct strvec *args, int use_stdin,
678681
enum modify_type m)
679682
{
680683
int result;
@@ -696,22 +699,23 @@ static int modify_pattern_list(struct strvec *args, int use_stdin,
696699
}
697700

698701
if (!core_apply_sparse_checkout) {
699-
set_config(MODE_ALL_PATTERNS);
702+
set_config(repo, MODE_ALL_PATTERNS);
700703
core_apply_sparse_checkout = 1;
701704
changed_config = 1;
702705
}
703706

704-
result = write_patterns_and_update(pl);
707+
result = write_patterns_and_update(repo, pl);
705708

706709
if (result && changed_config)
707-
set_config(MODE_NO_PATTERNS);
710+
set_config(repo, MODE_NO_PATTERNS);
708711

709712
clear_pattern_list(pl);
710713
free(pl);
711714
return result;
712715
}
713716

714-
static void sanitize_paths(struct strvec *args,
717+
static void sanitize_paths(struct repository *repo,
718+
struct strvec *args,
715719
const char *prefix, int skip_checks)
716720
{
717721
int i;
@@ -752,7 +756,7 @@ static void sanitize_paths(struct strvec *args,
752756

753757
for (i = 0; i < args->nr; i++) {
754758
struct cache_entry *ce;
755-
struct index_state *index = the_repository->index;
759+
struct index_state *index = repo->index;
756760
int pos = index_name_pos(index, args->v[i], strlen(args->v[i]));
757761

758762
if (pos < 0)
@@ -779,7 +783,7 @@ static struct sparse_checkout_add_opts {
779783
} add_opts;
780784

781785
static int sparse_checkout_add(int argc, const char **argv, const char *prefix,
782-
struct repository *repo UNUSED)
786+
struct repository *repo)
783787
{
784788
static struct option builtin_sparse_checkout_add_options[] = {
785789
OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks,
@@ -796,17 +800,17 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix,
796800
if (!core_apply_sparse_checkout)
797801
die(_("no sparse-checkout to add to"));
798802

799-
repo_read_index(the_repository);
803+
repo_read_index(repo);
800804

801805
argc = parse_options(argc, argv, prefix,
802806
builtin_sparse_checkout_add_options,
803807
builtin_sparse_checkout_add_usage, 0);
804808

805809
for (int i = 0; i < argc; i++)
806810
strvec_push(&patterns, argv[i]);
807-
sanitize_paths(&patterns, prefix, add_opts.skip_checks);
811+
sanitize_paths(repo, &patterns, prefix, add_opts.skip_checks);
808812

809-
ret = modify_pattern_list(&patterns, add_opts.use_stdin, ADD);
813+
ret = modify_pattern_list(repo, &patterns, add_opts.use_stdin, ADD);
810814

811815
strvec_clear(&patterns);
812816
return ret;
@@ -825,7 +829,7 @@ static struct sparse_checkout_set_opts {
825829
} set_opts;
826830

827831
static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
828-
struct repository *repo UNUSED)
832+
struct repository *repo)
829833
{
830834
int default_patterns_nr = 2;
831835
const char *default_patterns[] = {"/*", "!/*/", NULL};
@@ -847,7 +851,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
847851
int ret;
848852

849853
setup_work_tree();
850-
repo_read_index(the_repository);
854+
repo_read_index(repo);
851855

852856
set_opts.cone_mode = -1;
853857
set_opts.sparse_index = -1;
@@ -856,7 +860,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
856860
builtin_sparse_checkout_set_options,
857861
builtin_sparse_checkout_set_usage, 0);
858862

859-
if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
863+
if (update_modes(repo, &set_opts.cone_mode, &set_opts.sparse_index))
860864
return 1;
861865

862866
/*
@@ -870,10 +874,10 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
870874
} else {
871875
for (int i = 0; i < argc; i++)
872876
strvec_push(&patterns, argv[i]);
873-
sanitize_paths(&patterns, prefix, set_opts.skip_checks);
877+
sanitize_paths(repo, &patterns, prefix, set_opts.skip_checks);
874878
}
875879

876-
ret = modify_pattern_list(&patterns, set_opts.use_stdin, REPLACE);
880+
ret = modify_pattern_list(repo, &patterns, set_opts.use_stdin, REPLACE);
877881

878882
strvec_clear(&patterns);
879883
return ret;
@@ -891,7 +895,7 @@ static struct sparse_checkout_reapply_opts {
891895

892896
static int sparse_checkout_reapply(int argc, const char **argv,
893897
const char *prefix,
894-
struct repository *repo UNUSED)
898+
struct repository *repo)
895899
{
896900
static struct option builtin_sparse_checkout_reapply_options[] = {
897901
OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
@@ -912,12 +916,12 @@ static int sparse_checkout_reapply(int argc, const char **argv,
912916
builtin_sparse_checkout_reapply_options,
913917
builtin_sparse_checkout_reapply_usage, 0);
914918

915-
repo_read_index(the_repository);
919+
repo_read_index(repo);
916920

917-
if (update_modes(&reapply_opts.cone_mode, &reapply_opts.sparse_index))
921+
if (update_modes(repo, &reapply_opts.cone_mode, &reapply_opts.sparse_index))
918922
return 1;
919923

920-
return update_working_directory(NULL);
924+
return update_working_directory(repo, NULL);
921925
}
922926

923927
static char const * const builtin_sparse_checkout_disable_usage[] = {
@@ -927,7 +931,7 @@ static char const * const builtin_sparse_checkout_disable_usage[] = {
927931

928932
static int sparse_checkout_disable(int argc, const char **argv,
929933
const char *prefix,
930-
struct repository *repo UNUSED)
934+
struct repository *repo)
931935
{
932936
static struct option builtin_sparse_checkout_disable_options[] = {
933937
OPT_END(),
@@ -955,7 +959,7 @@ static int sparse_checkout_disable(int argc, const char **argv,
955959
* are expecting to do that when disabling sparse-checkout.
956960
*/
957961
give_advice_on_expansion = 0;
958-
repo_read_index(the_repository);
962+
repo_read_index(repo);
959963

960964
memset(&pl, 0, sizeof(pl));
961965
hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
@@ -966,13 +970,13 @@ static int sparse_checkout_disable(int argc, const char **argv,
966970
add_pattern("/*", empty_base, 0, &pl, 0);
967971

968972
prepare_repo_settings(the_repository);
969-
the_repository->settings.sparse_index = 0;
973+
repo->settings.sparse_index = 0;
970974

971-
if (update_working_directory(&pl))
975+
if (update_working_directory(repo, &pl))
972976
die(_("error while refreshing working directory"));
973977

974978
clear_pattern_list(&pl);
975-
return set_config(MODE_NO_PATTERNS);
979+
return set_config(repo, MODE_NO_PATTERNS);
976980
}
977981

978982
static char const * const builtin_sparse_checkout_check_rules_usage[] = {
@@ -987,14 +991,17 @@ static struct sparse_checkout_check_rules_opts {
987991
char *rules_file;
988992
} check_rules_opts;
989993

990-
static int check_rules(struct pattern_list *pl, int null_terminated) {
994+
static int check_rules(struct repository *repo,
995+
struct pattern_list *pl,
996+
int null_terminated)
997+
{
991998
struct strbuf line = STRBUF_INIT;
992999
struct strbuf unquoted = STRBUF_INIT;
9931000
char *path;
9941001
int line_terminator = null_terminated ? 0 : '\n';
9951002
strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul
9961003
: strbuf_getline;
997-
the_repository->index->sparse_checkout_patterns = pl;
1004+
repo->index->sparse_checkout_patterns = pl;
9981005
while (!getline_fn(&line, stdin)) {
9991006
path = line.buf;
10001007
if (!null_terminated && line.buf[0] == '"') {
@@ -1006,7 +1013,7 @@ static int check_rules(struct pattern_list *pl, int null_terminated) {
10061013
path = unquoted.buf;
10071014
}
10081015

1009-
if (path_in_sparse_checkout(path, the_repository->index))
1016+
if (path_in_sparse_checkout(path, repo->index))
10101017
write_name_quoted(path, stdout, line_terminator);
10111018
}
10121019
strbuf_release(&line);
@@ -1016,7 +1023,7 @@ static int check_rules(struct pattern_list *pl, int null_terminated) {
10161023
}
10171024

10181025
static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix,
1019-
struct repository *repo UNUSED)
1026+
struct repository *repo)
10201027
{
10211028
static struct option builtin_sparse_checkout_check_rules_options[] = {
10221029
OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
@@ -1055,7 +1062,7 @@ static int sparse_checkout_check_rules(int argc, const char **argv, const char *
10551062
free(sparse_filename);
10561063
}
10571064

1058-
ret = check_rules(&pl, check_rules_opts.null_termination);
1065+
ret = check_rules(repo, &pl, check_rules_opts.null_termination);
10591066
clear_pattern_list(&pl);
10601067
free(check_rules_opts.rules_file);
10611068
return ret;
@@ -1084,8 +1091,8 @@ int cmd_sparse_checkout(int argc,
10841091

10851092
repo_config(the_repository, git_default_config, NULL);
10861093

1087-
prepare_repo_settings(the_repository);
1088-
the_repository->settings.command_requires_full_index = 0;
1094+
prepare_repo_settings(repo);
1095+
repo->settings.command_requires_full_index = 0;
10891096

10901097
return fn(argc, argv, prefix, repo);
10911098
}

0 commit comments

Comments
 (0)