Skip to content

Commit f2111ce

Browse files
derrickstoleedscho
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]>
1 parent a9718b4 commit f2111ce

File tree

1 file changed

+61
-54
lines changed

1 file changed

+61
-54
lines changed

builtin/sparse-checkout.c

Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,12 @@ static void clean_tracked_sparse_directories(struct repository *r)
212212
"sparse-checkout:was full");
213213
}
214214

215-
static int update_working_directory(struct pattern_list *pl)
215+
static int update_working_directory(struct repository *r,
216+
struct pattern_list *pl)
216217
{
217218
enum update_sparsity_result result;
218219
struct unpack_trees_options o;
219220
struct lock_file lock_file = LOCK_INIT;
220-
struct repository *r = the_repository;
221221
struct pattern_list *old_pl;
222222

223223
/* If no branch has been checked out, there are no updates to make. */
@@ -335,7 +335,8 @@ static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
335335
string_list_clear(&sl, 0);
336336
}
337337

338-
static int write_patterns_and_update(struct pattern_list *pl)
338+
static int write_patterns_and_update(struct repository *repo,
339+
struct pattern_list *pl)
339340
{
340341
char *sparse_filename;
341342
FILE *fp;
@@ -344,15 +345,15 @@ static int write_patterns_and_update(struct pattern_list *pl)
344345

345346
sparse_filename = get_sparse_checkout_filename();
346347

347-
if (safe_create_leading_directories(the_repository, sparse_filename))
348+
if (safe_create_leading_directories(repo, sparse_filename))
348349
die(_("failed to create directory for sparse-checkout file"));
349350

350351
hold_lock_file_for_update(&lk, sparse_filename, LOCK_DIE_ON_ERROR);
351352

352-
result = update_working_directory(pl);
353+
result = update_working_directory(repo, pl);
353354
if (result) {
354355
rollback_lock_file(&lk);
355-
update_working_directory(NULL);
356+
update_working_directory(repo, NULL);
356357
goto out;
357358
}
358359

@@ -380,25 +381,26 @@ enum sparse_checkout_mode {
380381
MODE_CONE_PATTERNS = 2,
381382
};
382383

383-
static int set_config(enum sparse_checkout_mode mode)
384+
static int set_config(struct repository *repo,
385+
enum sparse_checkout_mode mode)
384386
{
385387
/* Update to use worktree config, if not already. */
386-
if (init_worktree_config(the_repository)) {
388+
if (init_worktree_config(repo)) {
387389
error(_("failed to initialize worktree config"));
388390
return 1;
389391
}
390392

391-
if (repo_config_set_worktree_gently(the_repository,
393+
if (repo_config_set_worktree_gently(repo,
392394
"core.sparseCheckout",
393395
mode ? "true" : "false") ||
394-
repo_config_set_worktree_gently(the_repository,
396+
repo_config_set_worktree_gently(repo,
395397
"core.sparseCheckoutCone",
396398
mode == MODE_CONE_PATTERNS ?
397399
"true" : "false"))
398400
return 1;
399401

400402
if (mode == MODE_NO_PATTERNS)
401-
return set_sparse_index_config(the_repository, 0);
403+
return set_sparse_index_config(repo, 0);
402404

403405
return 0;
404406
}
@@ -418,28 +420,28 @@ static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
418420
return MODE_ALL_PATTERNS;
419421
}
420422

421-
static int update_modes(int *cone_mode, int *sparse_index)
423+
static int update_modes(struct repository *repo, int *cone_mode, int *sparse_index)
422424
{
423425
int mode, record_mode;
424426

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

428430
mode = update_cone_mode(cone_mode);
429-
if (record_mode && set_config(mode))
431+
if (record_mode && set_config(repo, mode))
430432
return 1;
431433

432434
/* Set sparse-index/non-sparse-index mode if specified */
433435
if (*sparse_index >= 0) {
434-
if (set_sparse_index_config(the_repository, *sparse_index) < 0)
436+
if (set_sparse_index_config(repo, *sparse_index) < 0)
435437
die(_("failed to modify sparse-index config"));
436438

437439
/* force an index rewrite */
438-
repo_read_index(the_repository);
439-
the_repository->index->updated_workdir = 1;
440+
repo_read_index(repo);
441+
repo->index->updated_workdir = 1;
440442

441443
if (!*sparse_index)
442-
ensure_full_index_with_reason(the_repository->index,
444+
ensure_full_index_with_reason(repo->index,
443445
"sparse-checkout:disabling sparse index");
444446
}
445447

@@ -457,7 +459,7 @@ static struct sparse_checkout_init_opts {
457459
} init_opts;
458460

459461
static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
460-
struct repository *repo UNUSED)
462+
struct repository *repo)
461463
{
462464
struct pattern_list pl;
463465
char *sparse_filename;
@@ -473,7 +475,7 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
473475
};
474476

475477
setup_work_tree();
476-
repo_read_index(the_repository);
478+
repo_read_index(repo);
477479

478480
init_opts.cone_mode = -1;
479481
init_opts.sparse_index = -1;
@@ -482,7 +484,7 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
482484
builtin_sparse_checkout_init_options,
483485
builtin_sparse_checkout_init_usage, 0);
484486

485-
if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index))
487+
if (update_modes(repo, &init_opts.cone_mode, &init_opts.sparse_index))
486488
return 1;
487489

488490
memset(&pl, 0, sizeof(pl));
@@ -494,14 +496,14 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
494496
if (res >= 0) {
495497
free(sparse_filename);
496498
clear_pattern_list(&pl);
497-
return update_working_directory(NULL);
499+
return update_working_directory(repo, NULL);
498500
}
499501

500-
if (repo_get_oid(the_repository, "HEAD", &oid)) {
502+
if (repo_get_oid(repo, "HEAD", &oid)) {
501503
FILE *fp;
502504

503505
/* assume we are in a fresh repo, but update the sparse-checkout file */
504-
if (safe_create_leading_directories(the_repository, sparse_filename))
506+
if (safe_create_leading_directories(repo, sparse_filename))
505507
die(_("unable to create leading directories of %s"),
506508
sparse_filename);
507509
fp = xfopen(sparse_filename, "w");
@@ -520,7 +522,7 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
520522
add_pattern("!/*/", empty_base, 0, &pl, 0);
521523
pl.use_cone_patterns = init_opts.cone_mode;
522524

523-
return write_patterns_and_update(&pl);
525+
return write_patterns_and_update(repo, &pl);
524526
}
525527

526528
static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
@@ -683,7 +685,8 @@ static void add_patterns_literal(int argc, const char **argv,
683685
add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL);
684686
}
685687

686-
static int modify_pattern_list(struct strvec *args, int use_stdin,
688+
static int modify_pattern_list(struct repository *repo,
689+
struct strvec *args, int use_stdin,
687690
enum modify_type m)
688691
{
689692
int result;
@@ -705,22 +708,23 @@ static int modify_pattern_list(struct strvec *args, int use_stdin,
705708
}
706709

707710
if (!core_apply_sparse_checkout) {
708-
set_config(MODE_ALL_PATTERNS);
711+
set_config(repo, MODE_ALL_PATTERNS);
709712
core_apply_sparse_checkout = 1;
710713
changed_config = 1;
711714
}
712715

713-
result = write_patterns_and_update(pl);
716+
result = write_patterns_and_update(repo, pl);
714717

715718
if (result && changed_config)
716-
set_config(MODE_NO_PATTERNS);
719+
set_config(repo, MODE_NO_PATTERNS);
717720

718721
clear_pattern_list(pl);
719722
free(pl);
720723
return result;
721724
}
722725

723-
static void sanitize_paths(struct strvec *args,
726+
static void sanitize_paths(struct repository *repo,
727+
struct strvec *args,
724728
const char *prefix, int skip_checks)
725729
{
726730
int i;
@@ -761,7 +765,7 @@ static void sanitize_paths(struct strvec *args,
761765

762766
for (i = 0; i < args->nr; i++) {
763767
struct cache_entry *ce;
764-
struct index_state *index = the_repository->index;
768+
struct index_state *index = repo->index;
765769
int pos = index_name_pos(index, args->v[i], strlen(args->v[i]));
766770

767771
if (pos < 0)
@@ -788,7 +792,7 @@ static struct sparse_checkout_add_opts {
788792
} add_opts;
789793

790794
static int sparse_checkout_add(int argc, const char **argv, const char *prefix,
791-
struct repository *repo UNUSED)
795+
struct repository *repo)
792796
{
793797
static struct option builtin_sparse_checkout_add_options[] = {
794798
OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks,
@@ -805,17 +809,17 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix,
805809
if (!core_apply_sparse_checkout)
806810
die(_("no sparse-checkout to add to"));
807811

808-
repo_read_index(the_repository);
812+
repo_read_index(repo);
809813

810814
argc = parse_options(argc, argv, prefix,
811815
builtin_sparse_checkout_add_options,
812816
builtin_sparse_checkout_add_usage, 0);
813817

814818
for (int i = 0; i < argc; i++)
815819
strvec_push(&patterns, argv[i]);
816-
sanitize_paths(&patterns, prefix, add_opts.skip_checks);
820+
sanitize_paths(repo, &patterns, prefix, add_opts.skip_checks);
817821

818-
ret = modify_pattern_list(&patterns, add_opts.use_stdin, ADD);
822+
ret = modify_pattern_list(repo, &patterns, add_opts.use_stdin, ADD);
819823

820824
strvec_clear(&patterns);
821825
return ret;
@@ -834,7 +838,7 @@ static struct sparse_checkout_set_opts {
834838
} set_opts;
835839

836840
static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
837-
struct repository *repo UNUSED)
841+
struct repository *repo)
838842
{
839843
int default_patterns_nr = 2;
840844
const char *default_patterns[] = {"/*", "!/*/", NULL};
@@ -856,7 +860,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
856860
int ret;
857861

858862
setup_work_tree();
859-
repo_read_index(the_repository);
863+
repo_read_index(repo);
860864

861865
set_opts.cone_mode = -1;
862866
set_opts.sparse_index = -1;
@@ -865,7 +869,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
865869
builtin_sparse_checkout_set_options,
866870
builtin_sparse_checkout_set_usage, 0);
867871

868-
if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
872+
if (update_modes(repo, &set_opts.cone_mode, &set_opts.sparse_index))
869873
return 1;
870874

871875
/*
@@ -879,10 +883,10 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
879883
} else {
880884
for (int i = 0; i < argc; i++)
881885
strvec_push(&patterns, argv[i]);
882-
sanitize_paths(&patterns, prefix, set_opts.skip_checks);
886+
sanitize_paths(repo, &patterns, prefix, set_opts.skip_checks);
883887
}
884888

885-
ret = modify_pattern_list(&patterns, set_opts.use_stdin, REPLACE);
889+
ret = modify_pattern_list(repo, &patterns, set_opts.use_stdin, REPLACE);
886890

887891
strvec_clear(&patterns);
888892
return ret;
@@ -900,7 +904,7 @@ static struct sparse_checkout_reapply_opts {
900904

901905
static int sparse_checkout_reapply(int argc, const char **argv,
902906
const char *prefix,
903-
struct repository *repo UNUSED)
907+
struct repository *repo)
904908
{
905909
static struct option builtin_sparse_checkout_reapply_options[] = {
906910
OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
@@ -921,12 +925,12 @@ static int sparse_checkout_reapply(int argc, const char **argv,
921925
builtin_sparse_checkout_reapply_options,
922926
builtin_sparse_checkout_reapply_usage, 0);
923927

924-
repo_read_index(the_repository);
928+
repo_read_index(repo);
925929

926-
if (update_modes(&reapply_opts.cone_mode, &reapply_opts.sparse_index))
930+
if (update_modes(repo, &reapply_opts.cone_mode, &reapply_opts.sparse_index))
927931
return 1;
928932

929-
return update_working_directory(NULL);
933+
return update_working_directory(repo, NULL);
930934
}
931935

932936
static char const * const builtin_sparse_checkout_disable_usage[] = {
@@ -936,7 +940,7 @@ static char const * const builtin_sparse_checkout_disable_usage[] = {
936940

937941
static int sparse_checkout_disable(int argc, const char **argv,
938942
const char *prefix,
939-
struct repository *repo UNUSED)
943+
struct repository *repo)
940944
{
941945
static struct option builtin_sparse_checkout_disable_options[] = {
942946
OPT_END(),
@@ -964,7 +968,7 @@ static int sparse_checkout_disable(int argc, const char **argv,
964968
* are expecting to do that when disabling sparse-checkout.
965969
*/
966970
give_advice_on_expansion = 0;
967-
repo_read_index(the_repository);
971+
repo_read_index(repo);
968972

969973
memset(&pl, 0, sizeof(pl));
970974
hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
@@ -977,11 +981,11 @@ static int sparse_checkout_disable(int argc, const char **argv,
977981
prepare_repo_settings(the_repository);
978982
the_repository->settings.sparse_index = 0;
979983

980-
if (update_working_directory(&pl))
984+
if (update_working_directory(repo, &pl))
981985
die(_("error while refreshing working directory"));
982986

983987
clear_pattern_list(&pl);
984-
return set_config(MODE_NO_PATTERNS);
988+
return set_config(repo, MODE_NO_PATTERNS);
985989
}
986990

987991
static char const * const builtin_sparse_checkout_check_rules_usage[] = {
@@ -996,14 +1000,17 @@ static struct sparse_checkout_check_rules_opts {
9961000
char *rules_file;
9971001
} check_rules_opts;
9981002

999-
static int check_rules(struct pattern_list *pl, int null_terminated) {
1003+
static int check_rules(struct repository *repo,
1004+
struct pattern_list *pl,
1005+
int null_terminated)
1006+
{
10001007
struct strbuf line = STRBUF_INIT;
10011008
struct strbuf unquoted = STRBUF_INIT;
10021009
char *path;
10031010
int line_terminator = null_terminated ? 0 : '\n';
10041011
strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul
10051012
: strbuf_getline;
1006-
the_repository->index->sparse_checkout_patterns = pl;
1013+
repo->index->sparse_checkout_patterns = pl;
10071014
while (!getline_fn(&line, stdin)) {
10081015
path = line.buf;
10091016
if (!null_terminated && line.buf[0] == '"') {
@@ -1015,7 +1022,7 @@ static int check_rules(struct pattern_list *pl, int null_terminated) {
10151022
path = unquoted.buf;
10161023
}
10171024

1018-
if (path_in_sparse_checkout(path, the_repository->index))
1025+
if (path_in_sparse_checkout(path, repo->index))
10191026
write_name_quoted(path, stdout, line_terminator);
10201027
}
10211028
strbuf_release(&line);
@@ -1025,7 +1032,7 @@ static int check_rules(struct pattern_list *pl, int null_terminated) {
10251032
}
10261033

10271034
static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix,
1028-
struct repository *repo UNUSED)
1035+
struct repository *repo)
10291036
{
10301037
static struct option builtin_sparse_checkout_check_rules_options[] = {
10311038
OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
@@ -1064,7 +1071,7 @@ static int sparse_checkout_check_rules(int argc, const char **argv, const char *
10641071
free(sparse_filename);
10651072
}
10661073

1067-
ret = check_rules(&pl, check_rules_opts.null_termination);
1074+
ret = check_rules(repo, &pl, check_rules_opts.null_termination);
10681075
clear_pattern_list(&pl);
10691076
free(check_rules_opts.rules_file);
10701077
return ret;
@@ -1093,8 +1100,8 @@ int cmd_sparse_checkout(int argc,
10931100

10941101
repo_config(the_repository, git_default_config, NULL);
10951102

1096-
prepare_repo_settings(the_repository);
1097-
the_repository->settings.command_requires_full_index = 0;
1103+
prepare_repo_settings(repo);
1104+
repo->settings.command_requires_full_index = 0;
10981105

10991106
return fn(argc, argv, prefix, repo);
11001107
}

0 commit comments

Comments
 (0)