builtin/fsck: stop using `the_repository` when snapshotting refs

We depedn on `the_repository` when snapshotting refs. Refactor this to use a context-provided repository instead that is injected via the `struct snapshot_ref_data`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 23, 2026 at 16:02 UTC 4c44db7dc55c1aac0d0414ed22c27ea965cc2c77
1 file changed +22 -11
builtin/fsck.c
+22 -11
@@ -533,14 +533,20 @@ struct snapshot {
533 /* TODO: Consider also snapshotting the index of each worktree. */
534 };
535
536 +struct snapshot_ref_data {
537 + struct repository *repo;
538 + struct snapshot *snap;
539 +};
540 +
541 static int snapshot_ref(const struct reference *ref, void *cb_data)
542 {
538 - struct snapshot *snap = cb_data;
543 + struct snapshot_ref_data *data = cb_data;
544 + struct snapshot *snap = data->snap;
545 struct object *obj;
546
541 - obj = parse_object(the_repository, ref->oid);
547 + obj = parse_object(data->repo, ref->oid);
548 if (!obj) {
543 - if (is_promisor_object(the_repository, ref->oid)) {
549 + if (is_promisor_object(data->repo, ref->oid)) {
550 /*
551 * Increment default_refs anyway, because this is a
552 * valid ref.
@@ -581,11 +587,16 @@ static int fsck_handle_ref(const struct reference *ref, void *cb_data UNUSED)
587 return 0;
588 }
589
584 -static void snapshot_refs(struct snapshot *snap, int argc, const char **argv)
590 +static void snapshot_refs(struct repository *repo,
591 + struct snapshot *snap, int argc, const char **argv)
592 {
593 struct refs_for_each_ref_options opts = {
594 .flags = REFS_FOR_EACH_INCLUDE_BROKEN,
595 };
596 + struct snapshot_ref_data data = {
597 + .repo = repo,
598 + .snap = snap,
599 + };
600 struct worktree **worktrees, **p;
601 const char *head_points_at;
602 struct object_id head_oid;
@@ -593,13 +604,13 @@ static void snapshot_refs(struct snapshot *snap, int argc, const char **argv)
604 for (int i = 0; i < argc; i++) {
605 const char *arg = argv[i];
606 struct object_id oid;
596 - if (!repo_get_oid(the_repository, arg, &oid)) {
607 + if (!repo_get_oid(repo, arg, &oid)) {
608 struct reference ref = {
609 .name = arg,
610 .oid = &oid,
611 };
612
602 - snapshot_ref(&ref, snap);
613 + snapshot_ref(&ref, &data);
614 continue;
615 }
616 error(_("invalid parameter: expected sha1, got '%s'"), arg);
@@ -611,8 +622,8 @@ static void snapshot_refs(struct snapshot *snap, int argc, const char **argv)
622 return;
623 }
624
614 - refs_for_each_ref_ext(get_main_ref_store(the_repository),
615 - snapshot_ref, snap, &opts);
625 + refs_for_each_ref_ext(get_main_ref_store(repo),
626 + snapshot_ref, &data, &opts);
627
628 worktrees = get_worktrees();
629 for (p = worktrees; *p; p++) {
@@ -621,7 +632,7 @@ static void snapshot_refs(struct snapshot *snap, int argc, const char **argv)
632
633 strbuf_worktree_ref(wt, &refname, "HEAD");
634
624 - head_points_at = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
635 + head_points_at = refs_resolve_ref_unsafe(get_main_ref_store(repo),
636 refname.buf, 0, &head_oid, NULL);
637
638 if (head_points_at && !is_null_oid(&head_oid)) {
@@ -630,7 +641,7 @@ static void snapshot_refs(struct snapshot *snap, int argc, const char **argv)
641 .oid = &head_oid,
642 };
643
633 - snapshot_ref(&ref, snap);
644 + snapshot_ref(&ref, &data);
645 }
646 strbuf_release(&refname);
647
@@ -1039,7 +1050,7 @@ int cmd_fsck(int argc,
1050 * objects. We can still walk over new objects that are added during the
1051 * execution of fsck but won't miss any objects that were reachable.
1052 */
1042 - snapshot_refs(&snap, argc, argv);
1053 + snapshot_refs(repo, &snap, argc, argv);
1054
1055 /* Ensure we get a "fresh" view of the odb */
1056 odb_reprepare(repo->objects);