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 <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Derrick Stolee committed
Sep 12, 2025 at 10:30 UTC
064468e89919e9cf16d2afa59de33a242a4d71ab
1 file changed
+62
-55
builtin/sparse-checkout.c
+62
-55
@@ -204,12 +204,12 @@ static void clean_tracked_sparse_directories(struct repository *r)
204
ensure_full_index(r->index);
205
}
206
207
-static int update_working_directory(struct pattern_list *pl)
207
+static int update_working_directory(struct repository *r,
208
+ struct pattern_list *pl)
209
{
210
enum update_sparsity_result result;
211
struct unpack_trees_options o;
212
struct lock_file lock_file = LOCK_INIT;
212
- struct repository *r = the_repository;
213
struct pattern_list *old_pl;
214
215
/* 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)
327
string_list_clear(&sl, 0);
328
}
329
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)
332
{
333
char *sparse_filename;
334
FILE *fp;
@@ -336,15 +337,15 @@ static int write_patterns_and_update(struct pattern_list *pl)
337
338
sparse_filename = get_sparse_checkout_filename();
339
339
- if (safe_create_leading_directories(the_repository, sparse_filename))
340
+ if (safe_create_leading_directories(repo, sparse_filename))
341
die(_("failed to create directory for sparse-checkout file"));
342
343
hold_lock_file_for_update(&lk, sparse_filename, LOCK_DIE_ON_ERROR);
344
344
- result = update_working_directory(pl);
345
+ result = update_working_directory(repo, pl);
346
if (result) {
347
rollback_lock_file(&lk);
347
- update_working_directory(NULL);
348
+ update_working_directory(repo, NULL);
349
goto out;
350
}
351
@@ -372,25 +373,26 @@ enum sparse_checkout_mode {
373
MODE_CONE_PATTERNS = 2,
374
};
375
375
-static int set_config(enum sparse_checkout_mode mode)
376
+static int set_config(struct repository *repo,
377
+ enum sparse_checkout_mode mode)
378
{
379
/* Update to use worktree config, if not already. */
378
- if (init_worktree_config(the_repository)) {
380
+ if (init_worktree_config(repo)) {
381
error(_("failed to initialize worktree config"));
382
return 1;
383
}
384
383
- if (repo_config_set_worktree_gently(the_repository,
385
+ if (repo_config_set_worktree_gently(repo,
386
"core.sparseCheckout",
387
mode ? "true" : "false") ||
386
- repo_config_set_worktree_gently(the_repository,
388
+ repo_config_set_worktree_gently(repo,
389
"core.sparseCheckoutCone",
390
mode == MODE_CONE_PATTERNS ?
391
"true" : "false"))
392
return 1;
393
394
if (mode == MODE_NO_PATTERNS)
393
- return set_sparse_index_config(the_repository, 0);
395
+ return set_sparse_index_config(repo, 0);
396
397
return 0;
398
}
@@ -410,7 +412,7 @@ static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
412
return MODE_ALL_PATTERNS;
413
}
414
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)
416
{
417
int mode, record_mode;
418
@@ -418,20 +420,20 @@ static int update_modes(int *cone_mode, int *sparse_index)
420
record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
421
422
mode = update_cone_mode(cone_mode);
421
- if (record_mode && set_config(mode))
423
+ if (record_mode && set_config(repo, mode))
424
return 1;
425
426
/* Set sparse-index/non-sparse-index mode if specified */
427
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)
429
die(_("failed to modify sparse-index config"));
430
431
/* 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;
434
435
if (!*sparse_index)
434
- ensure_full_index(the_repository->index);
436
+ ensure_full_index(repo->index);
437
}
438
439
return 0;
@@ -448,7 +450,7 @@ static struct sparse_checkout_init_opts {
450
} init_opts;
451
452
static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
451
- struct repository *repo UNUSED)
453
+ struct repository *repo)
454
{
455
struct pattern_list pl;
456
char *sparse_filename;
@@ -464,7 +466,7 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
466
};
467
468
setup_work_tree();
467
- repo_read_index(the_repository);
469
+ repo_read_index(repo);
470
471
init_opts.cone_mode = -1;
472
init_opts.sparse_index = -1;
@@ -473,7 +475,7 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
475
builtin_sparse_checkout_init_options,
476
builtin_sparse_checkout_init_usage, 0);
477
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))
479
return 1;
480
481
memset(&pl, 0, sizeof(pl));
@@ -485,14 +487,14 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
487
if (res >= 0) {
488
free(sparse_filename);
489
clear_pattern_list(&pl);
488
- return update_working_directory(NULL);
490
+ return update_working_directory(repo, NULL);
491
}
492
491
- if (repo_get_oid(the_repository, "HEAD", &oid)) {
493
+ if (repo_get_oid(repo, "HEAD", &oid)) {
494
FILE *fp;
495
496
/* 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))
498
die(_("unable to create leading directories of %s"),
499
sparse_filename);
500
fp = xfopen(sparse_filename, "w");
@@ -511,7 +513,7 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
513
add_pattern("!/*/", empty_base, 0, &pl, 0);
514
pl.use_cone_patterns = init_opts.cone_mode;
515
514
- return write_patterns_and_update(&pl);
516
+ return write_patterns_and_update(repo, &pl);
517
}
518
519
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,
676
add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL);
677
}
678
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,
681
enum modify_type m)
682
{
683
int result;
@@ -696,22 +699,23 @@ static int modify_pattern_list(struct strvec *args, int use_stdin,
699
}
700
701
if (!core_apply_sparse_checkout) {
699
- set_config(MODE_ALL_PATTERNS);
702
+ set_config(repo, MODE_ALL_PATTERNS);
703
core_apply_sparse_checkout = 1;
704
changed_config = 1;
705
}
706
704
- result = write_patterns_and_update(pl);
707
+ result = write_patterns_and_update(repo, pl);
708
709
if (result && changed_config)
707
- set_config(MODE_NO_PATTERNS);
710
+ set_config(repo, MODE_NO_PATTERNS);
711
712
clear_pattern_list(pl);
713
free(pl);
714
return result;
715
}
716
714
-static void sanitize_paths(struct strvec *args,
717
+static void sanitize_paths(struct repository *repo,
718
+ struct strvec *args,
719
const char *prefix, int skip_checks)
720
{
721
int i;
@@ -752,7 +756,7 @@ static void sanitize_paths(struct strvec *args,
756
757
for (i = 0; i < args->nr; i++) {
758
struct cache_entry *ce;
755
- struct index_state *index = the_repository->index;
759
+ struct index_state *index = repo->index;
760
int pos = index_name_pos(index, args->v[i], strlen(args->v[i]));
761
762
if (pos < 0)
@@ -779,7 +783,7 @@ static struct sparse_checkout_add_opts {
783
} add_opts;
784
785
static int sparse_checkout_add(int argc, const char **argv, const char *prefix,
782
- struct repository *repo UNUSED)
786
+ struct repository *repo)
787
{
788
static struct option builtin_sparse_checkout_add_options[] = {
789
OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks,
@@ -796,7 +800,7 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix,
800
if (!core_apply_sparse_checkout)
801
die(_("no sparse-checkout to add to"));
802
799
- repo_read_index(the_repository);
803
+ repo_read_index(repo);
804
805
argc = parse_options(argc, argv, prefix,
806
builtin_sparse_checkout_add_options,
@@ -804,9 +808,9 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix,
808
809
for (int i = 0; i < argc; i++)
810
strvec_push(&patterns, argv[i]);
807
- sanitize_paths(&patterns, prefix, add_opts.skip_checks);
811
+ sanitize_paths(repo, &patterns, prefix, add_opts.skip_checks);
812
809
- ret = modify_pattern_list(&patterns, add_opts.use_stdin, ADD);
813
+ ret = modify_pattern_list(repo, &patterns, add_opts.use_stdin, ADD);
814
815
strvec_clear(&patterns);
816
return ret;
@@ -825,7 +829,7 @@ static struct sparse_checkout_set_opts {
829
} set_opts;
830
831
static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
828
- struct repository *repo UNUSED)
832
+ struct repository *repo)
833
{
834
int default_patterns_nr = 2;
835
const char *default_patterns[] = {"/*", "!/*/", NULL};
@@ -847,7 +851,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
851
int ret;
852
853
setup_work_tree();
850
- repo_read_index(the_repository);
854
+ repo_read_index(repo);
855
856
set_opts.cone_mode = -1;
857
set_opts.sparse_index = -1;
@@ -856,7 +860,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
860
builtin_sparse_checkout_set_options,
861
builtin_sparse_checkout_set_usage, 0);
862
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))
864
return 1;
865
866
/*
@@ -870,10 +874,10 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
874
} else {
875
for (int i = 0; i < argc; i++)
876
strvec_push(&patterns, argv[i]);
873
- sanitize_paths(&patterns, prefix, set_opts.skip_checks);
877
+ sanitize_paths(repo, &patterns, prefix, set_opts.skip_checks);
878
}
879
876
- ret = modify_pattern_list(&patterns, set_opts.use_stdin, REPLACE);
880
+ ret = modify_pattern_list(repo, &patterns, set_opts.use_stdin, REPLACE);
881
882
strvec_clear(&patterns);
883
return ret;
@@ -891,7 +895,7 @@ static struct sparse_checkout_reapply_opts {
895
896
static int sparse_checkout_reapply(int argc, const char **argv,
897
const char *prefix,
894
- struct repository *repo UNUSED)
898
+ struct repository *repo)
899
{
900
static struct option builtin_sparse_checkout_reapply_options[] = {
901
OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
@@ -912,12 +916,12 @@ static int sparse_checkout_reapply(int argc, const char **argv,
916
builtin_sparse_checkout_reapply_options,
917
builtin_sparse_checkout_reapply_usage, 0);
918
915
- repo_read_index(the_repository);
919
+ repo_read_index(repo);
920
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))
922
return 1;
923
920
- return update_working_directory(NULL);
924
+ return update_working_directory(repo, NULL);
925
}
926
927
static char const * const builtin_sparse_checkout_disable_usage[] = {
@@ -927,7 +931,7 @@ static char const * const builtin_sparse_checkout_disable_usage[] = {
931
932
static int sparse_checkout_disable(int argc, const char **argv,
933
const char *prefix,
930
- struct repository *repo UNUSED)
934
+ struct repository *repo)
935
{
936
static struct option builtin_sparse_checkout_disable_options[] = {
937
OPT_END(),
@@ -955,7 +959,7 @@ static int sparse_checkout_disable(int argc, const char **argv,
959
* are expecting to do that when disabling sparse-checkout.
960
*/
961
give_advice_on_expansion = 0;
958
- repo_read_index(the_repository);
962
+ repo_read_index(repo);
963
964
memset(&pl, 0, sizeof(pl));
965
hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
@@ -966,13 +970,13 @@ static int sparse_checkout_disable(int argc, const char **argv,
970
add_pattern("/*", empty_base, 0, &pl, 0);
971
972
prepare_repo_settings(the_repository);
969
- the_repository->settings.sparse_index = 0;
973
+ repo->settings.sparse_index = 0;
974
971
- if (update_working_directory(&pl))
975
+ if (update_working_directory(repo, &pl))
976
die(_("error while refreshing working directory"));
977
978
clear_pattern_list(&pl);
975
- return set_config(MODE_NO_PATTERNS);
979
+ return set_config(repo, MODE_NO_PATTERNS);
980
}
981
982
static char const * const builtin_sparse_checkout_check_rules_usage[] = {
@@ -987,14 +991,17 @@ static struct sparse_checkout_check_rules_opts {
991
char *rules_file;
992
} check_rules_opts;
993
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
+{
998
struct strbuf line = STRBUF_INIT;
999
struct strbuf unquoted = STRBUF_INIT;
1000
char *path;
1001
int line_terminator = null_terminated ? 0 : '\n';
1002
strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul
1003
: strbuf_getline;
997
- the_repository->index->sparse_checkout_patterns = pl;
1004
+ repo->index->sparse_checkout_patterns = pl;
1005
while (!getline_fn(&line, stdin)) {
1006
path = line.buf;
1007
if (!null_terminated && line.buf[0] == '"') {
@@ -1006,7 +1013,7 @@ static int check_rules(struct pattern_list *pl, int null_terminated) {
1013
path = unquoted.buf;
1014
}
1015
1009
- if (path_in_sparse_checkout(path, the_repository->index))
1016
+ if (path_in_sparse_checkout(path, repo->index))
1017
write_name_quoted(path, stdout, line_terminator);
1018
}
1019
strbuf_release(&line);
@@ -1016,7 +1023,7 @@ static int check_rules(struct pattern_list *pl, int null_terminated) {
1023
}
1024
1025
static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix,
1019
- struct repository *repo UNUSED)
1026
+ struct repository *repo)
1027
{
1028
static struct option builtin_sparse_checkout_check_rules_options[] = {
1029
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 *
1062
free(sparse_filename);
1063
}
1064
1058
- ret = check_rules(&pl, check_rules_opts.null_termination);
1065
+ ret = check_rules(repo, &pl, check_rules_opts.null_termination);
1066
clear_pattern_list(&pl);
1067
free(check_rules_opts.rules_file);
1068
return ret;
@@ -1084,8 +1091,8 @@ int cmd_sparse_checkout(int argc,
1091
1092
repo_config(the_repository, git_default_config, NULL);
1093
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;
1096
1097
return fn(argc, argv, prefix, repo);
1098
}