Commit
508ec9837c846e8c1675fb4cd11113e2bc3f4466
repository: move fetch_if_missing into struct repository
The global variable 'fetch_if_missing' controls whether a missing object check should prompt a lazy fetch from a promisor remote. In order to continue the libification effort, move it into 'struct repository' and initialize it to 1 by default to keep the previous behavior. builtin/fetch-pack.c, builtin/fsck.c, and builtin/rev-list.c are entered via commands marked RUN_SETUP in git.c:commands[]. Their 'repo' parameter is only NULL when '-h' is given outside of a repository, in which case either show_usage_if_asked() or parse_options()'s own '-h' handling exits the process before returning. We can therefore drop their UNUSED markers and assign to 'repo' directly. builtin/index-pack.c is entered via RUN_SETUP_GENTLY, so its 'repo' pointer can be NULL any time it is run outside of a repository, not only with '-h'. We keep a NULL check there and fall back to 'the_repository'. builtin/pack-objects.c needs two adjustments to make 'repo' reach every 'fetch_if_missing' call site: 'read_stdin_packs()' now takes a 'struct repository *'; 'option_parse_missing_action()', which is registered as an OPT_CALLBACK, receives a 'repo' through the option's 'value' field now. Additionally, update the partial clone documentation to reflect that this is now a per-repository flag. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Ayush Chandekar <ayu.chandekar@gmail.com> Mentored-by: Olamide Caleb Bello <belkid98@gmail.com> Signed-off-by: Tian Yuchen <cat@malon.dev> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Tian Yuchen <cat@malon.dev> · 2026-08-15 06:47 UTC · committed by Junio C Hamano
15 files changed,
+51
−49
- Documentation/technical/partial-clone.adoc
- builtin/fetch-pack.c
- builtin/fsck.c
- builtin/index-pack.c
- builtin/pack-objects.c
- builtin/prune.c
- builtin/rev-list.c
- common-init.c
- git.c
- midx-write.c
- odb.c
- odb.h
- repository.c
- repository.h
- revision.c
modified Documentation/technical/partial-clone.adoc
| @@ -159,7 +159,7 @@ and prefetch those objects in bulk. | |||
| 159 | 159 | - `repack` in GC has been updated to not touch promisor packfiles at all, | |
| 160 | 160 | and to only repack other objects. | |
| 161 | 161 | ||
| 162 | - | - The global variable "fetch_if_missing" is used to control whether an | |
| 162 | + | - The per-repository flag "fetch_if_missing" is used to control whether an | |
| 163 | 163 | object lookup will attempt to dynamically fetch a missing object or | |
| 164 | 164 | report an error. | |
| 165 | 165 | + | |
modified builtin/fetch-pack.c
| @@ -49,7 +49,7 @@ static void add_sought_entry(struct ref ***sought, int *nr, int *alloc, | |||
| 49 | 49 | int cmd_fetch_pack(int argc, | |
| 50 | 50 | const char **argv, | |
| 51 | 51 | const char *prefix UNUSED, | |
| 52 | - | struct repository *repo UNUSED) | |
| 52 | + | struct repository *repo) | |
| 53 | 53 | { | |
| 54 | 54 | int i, ret; | |
| 55 | 55 | struct ref *fetched_refs = NULL, *remote_refs = NULL; | |
| @@ -67,8 +67,6 @@ int cmd_fetch_pack(int argc, | |||
| 67 | 67 | struct packet_reader reader; | |
| 68 | 68 | enum protocol_version version; | |
| 69 | 69 | ||
| 70 | - | fetch_if_missing = 0; | |
| 71 | - | ||
| 72 | 70 | packet_trace_identity("fetch-pack"); | |
| 73 | 71 | ||
| 74 | 72 | memset(&args, 0, sizeof(args)); | |
| @@ -77,6 +75,8 @@ int cmd_fetch_pack(int argc, | |||
| 77 | 75 | ||
| 78 | 76 | show_usage_if_asked(argc, argv, fetch_pack_usage); | |
| 79 | 77 | ||
| 78 | + | repo->fetch_if_missing = 0; | |
| 79 | + | ||
| 80 | 80 | for (i = 1; i < argc && *argv[i] == '-'; i++) { | |
| 81 | 81 | const char *arg = argv[i]; | |
| 82 | 82 | ||
modified builtin/fsck.c
| @@ -1017,15 +1017,15 @@ int cmd_fsck(int argc, | |||
| 1017 | 1017 | .ref = NULL | |
| 1018 | 1018 | }; | |
| 1019 | 1019 | ||
| 1020 | - | /* fsck knows how to handle missing promisor objects */ | |
| 1021 | - | fetch_if_missing = 0; | |
| 1022 | - | ||
| 1023 | 1020 | errors_found = 0; | |
| 1024 | 1021 | disable_replace_refs(); | |
| 1025 | 1022 | save_commit_buffer = 0; | |
| 1026 | 1023 | ||
| 1027 | 1024 | argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0); | |
| 1028 | 1025 | ||
| 1026 | + | /* fsck knows how to handle missing promisor objects */ | |
| 1027 | + | repo->fetch_if_missing = 0; | |
| 1028 | + | ||
| 1029 | 1029 | fsck_options_init(&fsck_walk_options, repo, FSCK_OPTIONS_DEFAULT); | |
| 1030 | 1030 | fsck_walk_options.walk = mark_object; | |
| 1031 | 1031 | ||
modified builtin/index-pack.c
| @@ -1886,7 +1886,7 @@ static void repack_local_links(void) | |||
| 1886 | 1886 | int cmd_index_pack(int argc, | |
| 1887 | 1887 | const char **argv, | |
| 1888 | 1888 | const char *prefix, | |
| 1889 | - | struct repository *repo UNUSED) | |
| 1889 | + | struct repository *repo) | |
| 1890 | 1890 | { | |
| 1891 | 1891 | int i, fix_thin_pack = 0, verify = 0, stat_only = 0, rev_index; | |
| 1892 | 1892 | const char *curr_index; | |
| @@ -1903,15 +1903,15 @@ int cmd_index_pack(int argc, | |||
| 1903 | 1903 | int report_end_of_input = 0; | |
| 1904 | 1904 | int hash_algo = 0; | |
| 1905 | 1905 | ||
| 1906 | + | show_usage_if_asked(argc, argv, index_pack_usage); | |
| 1907 | + | ||
| 1906 | 1908 | /* | |
| 1907 | 1909 | * index-pack never needs to fetch missing objects except when | |
| 1908 | 1910 | * REF_DELTA bases are missing (which are explicitly handled). It only | |
| 1909 | 1911 | * accesses the repo to do hash collision checks and to check which | |
| 1910 | 1912 | * REF_DELTA bases need to be fetched. | |
| 1911 | 1913 | */ | |
| 1912 | - | fetch_if_missing = 0; | |
| 1913 | - | ||
| 1914 | - | show_usage_if_asked(argc, argv, index_pack_usage); | |
| 1914 | + | (repo ? repo : the_repository)->fetch_if_missing = 0; | |
| 1915 | 1915 | ||
| 1916 | 1916 | disable_replace_refs(); | |
| 1917 | 1917 | ||
modified builtin/pack-objects.c
| @@ -4087,9 +4087,10 @@ static void stdin_packs_read_input(struct rev_info *revs, | |||
| 4087 | 4087 | ||
| 4088 | 4088 | static void add_unreachable_loose_objects(struct rev_info *revs); | |
| 4089 | 4089 | ||
| 4090 | - | static void read_stdin_packs(enum stdin_packs_mode mode, int rev_list_unpacked) | |
| 4090 | + | static void read_stdin_packs(struct repository *repo, | |
| 4091 | + | enum stdin_packs_mode mode, int rev_list_unpacked) | |
| 4091 | 4092 | { | |
| 4092 | - | int prev_fetch_if_missing = fetch_if_missing; | |
| 4093 | + | int prev_fetch_if_missing = repo->fetch_if_missing; | |
| 4093 | 4094 | struct rev_info revs; | |
| 4094 | 4095 | ||
| 4095 | 4096 | /* | |
| @@ -4097,9 +4098,9 @@ static void read_stdin_packs(enum stdin_packs_mode mode, int rev_list_unpacked) | |||
| 4097 | 4098 | * walk is best-effort though we don't want to perform backfill fetches | |
| 4098 | 4099 | * for them. | |
| 4099 | 4100 | */ | |
| 4100 | - | fetch_if_missing = 0; | |
| 4101 | + | repo->fetch_if_missing = 0; | |
| 4101 | 4102 | ||
| 4102 | - | repo_init_revisions(the_repository, &revs, NULL); | |
| 4103 | + | repo_init_revisions(repo, &revs, NULL); | |
| 4103 | 4104 | /* | |
| 4104 | 4105 | * Use a revision walk to fill in the namehash of objects in the include | |
| 4105 | 4106 | * packs. To save time, we'll avoid traversing through objects that are | |
| @@ -4145,7 +4146,7 @@ static void read_stdin_packs(enum stdin_packs_mode mode, int rev_list_unpacked) | |||
| 4145 | 4146 | trace2_data_intmax("pack-objects", the_repository, "stdin_packs_hints", | |
| 4146 | 4147 | stdin_packs_hints_nr); | |
| 4147 | 4148 | ||
| 4148 | - | fetch_if_missing = prev_fetch_if_missing; | |
| 4149 | + | repo->fetch_if_missing = prev_fetch_if_missing; | |
| 4149 | 4150 | } | |
| 4150 | 4151 | ||
| 4151 | 4152 | static void add_cruft_object_entry(const struct object_id *oid, enum object_type type, | |
| @@ -4455,9 +4456,11 @@ static void show_object__ma_allow_promisor(struct object *obj, const char *name, | |||
| 4455 | 4456 | show_object(obj, name, data); | |
| 4456 | 4457 | } | |
| 4457 | 4458 | ||
| 4458 | - | static int option_parse_missing_action(const struct option *opt UNUSED, | |
| 4459 | + | static int option_parse_missing_action(const struct option *opt, | |
| 4459 | 4460 | const char *arg, int unset) | |
| 4460 | 4461 | { | |
| 4462 | + | struct repository *repo = opt->value; | |
| 4463 | + | ||
| 4461 | 4464 | assert(arg); | |
| 4462 | 4465 | assert(!unset); | |
| 4463 | 4466 | ||
| @@ -4469,14 +4472,14 @@ static int option_parse_missing_action(const struct option *opt UNUSED, | |||
| 4469 | 4472 | ||
| 4470 | 4473 | if (!strcmp(arg, "allow-any")) { | |
| 4471 | 4474 | arg_missing_action = MA_ALLOW_ANY; | |
| 4472 | - | fetch_if_missing = 0; | |
| 4475 | + | repo->fetch_if_missing = 0; | |
| 4473 | 4476 | fn_show_object = show_object__ma_allow_any; | |
| 4474 | 4477 | return 0; | |
| 4475 | 4478 | } | |
| 4476 | 4479 | ||
| 4477 | 4480 | if (!strcmp(arg, "allow-promisor")) { | |
| 4478 | 4481 | arg_missing_action = MA_ALLOW_PROMISOR; | |
| 4479 | - | fetch_if_missing = 0; | |
| 4482 | + | repo->fetch_if_missing = 0; | |
| 4480 | 4483 | fn_show_object = show_object__ma_allow_promisor; | |
| 4481 | 4484 | return 0; | |
| 4482 | 4485 | } | |
| @@ -5118,7 +5121,7 @@ static int parse_stdin_packs_mode(const struct option *opt, const char *arg, | |||
| 5118 | 5121 | int cmd_pack_objects(int argc, | |
| 5119 | 5122 | const char **argv, | |
| 5120 | 5123 | const char *prefix, | |
| 5121 | - | struct repository *repo UNUSED) | |
| 5124 | + | struct repository *repo) | |
| 5122 | 5125 | { | |
| 5123 | 5126 | int use_internal_rev_list = 0; | |
| 5124 | 5127 | int all_progress_implied = 0; | |
| @@ -5225,7 +5228,7 @@ int cmd_pack_objects(int argc, | |||
| 5225 | 5228 | N_("write a bitmap index if possible"), | |
| 5226 | 5229 | WRITE_BITMAP_QUIET, PARSE_OPT_HIDDEN), | |
| 5227 | 5230 | OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options), | |
| 5228 | - | OPT_CALLBACK_F(0, "missing", NULL, N_("action"), | |
| 5231 | + | OPT_CALLBACK_F(0, "missing", repo, N_("action"), | |
| 5229 | 5232 | N_("handling for missing objects"), PARSE_OPT_NONEG, | |
| 5230 | 5233 | option_parse_missing_action), | |
| 5231 | 5234 | OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects, | |
| @@ -5345,7 +5348,7 @@ int cmd_pack_objects(int argc, | |||
| 5345 | 5348 | exclude_promisor_objects_best_effort, | |
| 5346 | 5349 | "--exclude-promisor-objects-best-effort"); | |
| 5347 | 5350 | if (exclude_promisor_objects) { | |
| 5348 | - | fetch_if_missing = 0; | |
| 5351 | + | repo->fetch_if_missing = 0; | |
| 5349 | 5352 | ||
| 5350 | 5353 | /* --stdin-packs handles promisor objects separately. */ | |
| 5351 | 5354 | if (!stdin_packs) { | |
| @@ -5354,8 +5357,9 @@ int cmd_pack_objects(int argc, | |||
| 5354 | 5357 | } | |
| 5355 | 5358 | } else if (exclude_promisor_objects_best_effort) { | |
| 5356 | 5359 | use_internal_rev_list = 1; | |
| 5357 | - | fetch_if_missing = 0; | |
| 5358 | - | option_parse_missing_action(NULL, "allow-any", 0); | |
| 5360 | + | arg_missing_action = MA_ALLOW_ANY; | |
| 5361 | + | repo->fetch_if_missing = 0; | |
| 5362 | + | fn_show_object = show_object__ma_allow_any; | |
| 5359 | 5363 | /* revs configured below */ | |
| 5360 | 5364 | } | |
| 5361 | 5365 | if (unpack_unreachable || keep_unreachable || pack_loose_unreachable) | |
| @@ -5471,7 +5475,7 @@ int cmd_pack_objects(int argc, | |||
| 5471 | 5475 | progress_state = start_progress(the_repository, | |
| 5472 | 5476 | _("Enumerating objects"), 0); | |
| 5473 | 5477 | if (stdin_packs) { | |
| 5474 | - | read_stdin_packs(stdin_packs, rev_list_unpacked); | |
| 5478 | + | read_stdin_packs(repo, stdin_packs, rev_list_unpacked); | |
| 5475 | 5479 | } else if (cruft) { | |
| 5476 | 5480 | read_cruft_objects(); | |
| 5477 | 5481 | } else if (!use_internal_rev_list) { | |
modified builtin/prune.c
| @@ -194,7 +194,7 @@ int cmd_prune(int argc, | |||
| 194 | 194 | if (show_progress == -1) | |
| 195 | 195 | show_progress = isatty(2); | |
| 196 | 196 | if (exclude_promisor_objects) { | |
| 197 | - | fetch_if_missing = 0; | |
| 197 | + | repo->fetch_if_missing = 0; | |
| 198 | 198 | revs.exclude_promisor_objects = 1; | |
| 199 | 199 | } | |
| 200 | 200 | ||
modified builtin/rev-list.c
| @@ -500,7 +500,8 @@ static void print_disk_usage(off_t size) | |||
| 500 | 500 | strbuf_release(&sb); | |
| 501 | 501 | } | |
| 502 | 502 | ||
| 503 | - | static inline int parse_missing_action_value(const char *value) | |
| 503 | + | static inline int parse_missing_action_value(struct repository *repo, | |
| 504 | + | const char *value) | |
| 504 | 505 | { | |
| 505 | 506 | if (!strcmp(value, "error")) { | |
| 506 | 507 | arg_missing_action = MA_ERROR; | |
| @@ -509,25 +510,25 @@ static inline int parse_missing_action_value(const char *value) | |||
| 509 | 510 | ||
| 510 | 511 | if (!strcmp(value, "allow-any")) { | |
| 511 | 512 | arg_missing_action = MA_ALLOW_ANY; | |
| 512 | - | fetch_if_missing = 0; | |
| 513 | + | repo->fetch_if_missing = 0; | |
| 513 | 514 | return 1; | |
| 514 | 515 | } | |
| 515 | 516 | ||
| 516 | 517 | if (!strcmp(value, "print")) { | |
| 517 | 518 | arg_missing_action = MA_PRINT; | |
| 518 | - | fetch_if_missing = 0; | |
| 519 | + | repo->fetch_if_missing = 0; | |
| 519 | 520 | return 1; | |
| 520 | 521 | } | |
| 521 | 522 | ||
| 522 | 523 | if (!strcmp(value, "print-info")) { | |
| 523 | 524 | arg_missing_action = MA_PRINT_INFO; | |
| 524 | - | fetch_if_missing = 0; | |
| 525 | + | repo->fetch_if_missing = 0; | |
| 525 | 526 | return 1; | |
| 526 | 527 | } | |
| 527 | 528 | ||
| 528 | 529 | if (!strcmp(value, "allow-promisor")) { | |
| 529 | 530 | arg_missing_action = MA_ALLOW_PROMISOR; | |
| 530 | - | fetch_if_missing = 0; | |
| 531 | + | repo->fetch_if_missing = 0; | |
| 531 | 532 | return 1; | |
| 532 | 533 | } | |
| 533 | 534 | ||
| @@ -692,7 +693,7 @@ static void prepare_maximal_independent(struct rev_info *revs) | |||
| 692 | 693 | int cmd_rev_list(int argc, | |
| 693 | 694 | const char **argv, | |
| 694 | 695 | const char *prefix, | |
| 695 | - | struct repository *repo UNUSED) | |
| 696 | + | struct repository *repo) | |
| 696 | 697 | { | |
| 697 | 698 | struct rev_info revs; | |
| 698 | 699 | struct rev_list_info info; | |
| @@ -745,10 +746,10 @@ int cmd_rev_list(int argc, | |||
| 745 | 746 | for (i = 1; i < argc; i++) { | |
| 746 | 747 | const char *arg = argv[i]; | |
| 747 | 748 | if (!strcmp(arg, "--exclude-promisor-objects")) { | |
| 748 | - | fetch_if_missing = 0; | |
| 749 | + | repo->fetch_if_missing = 0; | |
| 749 | 750 | revs.exclude_promisor_objects = 1; | |
| 750 | 751 | } else if (skip_prefix(arg, "--missing=", &arg)) { | |
| 751 | - | parse_missing_action_value(arg); | |
| 752 | + | parse_missing_action_value(repo, arg); | |
| 752 | 753 | } else if (!strcmp(arg, "-z")) { | |
| 753 | 754 | line_term = '\0'; | |
| 754 | 755 | info_term = '\0'; | |
modified common-init.c
| @@ -47,7 +47,7 @@ static void setup_environment(void) | |||
| 47 | 47 | update_ref_namespace(NAMESPACE_REPLACE, git_replace_ref_base); | |
| 48 | 48 | ||
| 49 | 49 | if (git_env_bool(NO_LAZY_FETCH_ENVIRONMENT, 0)) | |
| 50 | - | fetch_if_missing = 0; | |
| 50 | + | the_repository->fetch_if_missing = 0; | |
| 51 | 51 | } | |
| 52 | 52 | ||
| 53 | 53 | void init_git(const char **argv) | |
modified git.c
| @@ -202,7 +202,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged) | |||
| 202 | 202 | if (envchanged) | |
| 203 | 203 | *envchanged = 1; | |
| 204 | 204 | } else if (!strcmp(cmd, "--no-lazy-fetch")) { | |
| 205 | - | fetch_if_missing = 0; | |
| 205 | + | the_repository->fetch_if_missing = 0; | |
| 206 | 206 | setenv(NO_LAZY_FETCH_ENVIRONMENT, "1", 1); | |
| 207 | 207 | if (envchanged) | |
| 208 | 208 | *envchanged = 1; | |
modified midx-write.c
| @@ -865,7 +865,7 @@ static void find_commits_for_midx_bitmap(struct commit_stack *commits, | |||
| 865 | 865 | * complain later that we don't have reachability closure (and fail | |
| 866 | 866 | * appropriately). | |
| 867 | 867 | */ | |
| 868 | - | fetch_if_missing = 0; | |
| 868 | + | ctx->repo->fetch_if_missing = 0; | |
| 869 | 869 | revs.exclude_promisor_objects = 1; | |
| 870 | 870 | ||
| 871 | 871 | if (prepare_revision_walk(&revs)) | |
modified odb.c
| @@ -528,8 +528,6 @@ void disable_obj_read_lock(void) | |||
| 528 | 528 | pthread_mutex_destroy(&obj_read_mutex); | |
| 529 | 529 | } | |
| 530 | 530 | ||
| 531 | - | int fetch_if_missing = 1; | |
| 532 | - | ||
| 533 | 531 | static int register_all_submodule_sources(struct object_database *odb) | |
| 534 | 532 | { | |
| 535 | 533 | int ret = odb->submodule_source_paths.nr; | |
| @@ -595,7 +593,7 @@ static int do_oid_object_info_extended(struct object_database *odb, | |||
| 595 | 593 | continue; | |
| 596 | 594 | ||
| 597 | 595 | /* Check if it is a missing object */ | |
| 598 | - | if (fetch_if_missing && repo_has_promisor_remote(odb->repo) && | |
| 596 | + | if (odb->repo->fetch_if_missing && repo_has_promisor_remote(odb->repo) && | |
| 599 | 597 | !already_retried && | |
| 600 | 598 | !(flags & OBJECT_INFO_SKIP_FETCH_OBJECT)) { | |
| 601 | 599 | promisor_remote_get_direct(odb->repo, real, 1); | |
modified odb.h
| @@ -15,14 +15,6 @@ struct repository; | |||
| 15 | 15 | struct strbuf; | |
| 16 | 16 | struct strvec; | |
| 17 | 17 | ||
| 18 | - | /* | |
| 19 | - | * Set this to 0 to prevent odb_read_object_info_extended() from fetching missing | |
| 20 | - | * blobs. This has a difference only if extensions.partialClone is set. | |
| 21 | - | * | |
| 22 | - | * Its default value is 1. | |
| 23 | - | */ | |
| 24 | - | extern int fetch_if_missing; | |
| 25 | - | ||
| 26 | 18 | /* | |
| 27 | 19 | * Compute the exact path an alternate is at and returns it. In case of | |
| 28 | 20 | * error NULL is returned and the human readable error is added to `err` | |
modified repository.c
| @@ -74,6 +74,7 @@ void initialize_repository(struct repository *repo) | |||
| 74 | 74 | index_state_init(repo->index, repo); | |
| 75 | 75 | repo->check_deprecated_config = true; | |
| 76 | 76 | repo->bare_cfg = -1; | |
| 77 | + | repo->fetch_if_missing = 1; | |
| 77 | 78 | repo_config_values_init(&repo->config_values_private_); | |
| 78 | 79 | ||
| 79 | 80 | /* | |
modified repository.h
| @@ -184,6 +184,12 @@ struct repository { | |||
| 184 | 184 | /* True if commit-graph has been disabled within this process. */ | |
| 185 | 185 | int commit_graph_disabled; | |
| 186 | 186 | ||
| 187 | + | /* | |
| 188 | + | * Controls whether the repository should lazily fetch missing | |
| 189 | + | * objects from promisor remotes. Defaults to 1. | |
| 190 | + | */ | |
| 191 | + | int fetch_if_missing; | |
| 192 | + | ||
| 187 | 193 | /* | |
| 188 | 194 | * Lazily-populated cache mapping hook event names to configured hooks. | |
| 189 | 195 | * NULL until first hook use. | |
modified revision.c
| @@ -2732,7 +2732,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg | |||
| 2732 | 2732 | revs->ignore_missing = 1; | |
| 2733 | 2733 | } else if (opt && opt->allow_exclude_promisor_objects && | |
| 2734 | 2734 | !strcmp(arg, "--exclude-promisor-objects")) { | |
| 2735 | - | if (fetch_if_missing) | |
| 2735 | + | if (revs->repo->fetch_if_missing) | |
| 2736 | 2736 | BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0"); | |
| 2737 | 2737 | revs->exclude_promisor_objects = 1; | |
| 2738 | 2738 | } else { | |