Skip to content

Commit 4c44db7

Browse files
pks-tgitster
authored andcommitted
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>
1 parent da3ead3 commit 4c44db7

1 file changed

Lines changed: 22 additions & 11 deletions

File tree

builtin/fsck.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -533,14 +533,20 @@ struct snapshot {
533533
/* TODO: Consider also snapshotting the index of each worktree. */
534534
};
535535

536+
struct snapshot_ref_data {
537+
struct repository *repo;
538+
struct snapshot *snap;
539+
};
540+
536541
static int snapshot_ref(const struct reference *ref, void *cb_data)
537542
{
538-
struct snapshot *snap = cb_data;
543+
struct snapshot_ref_data *data = cb_data;
544+
struct snapshot *snap = data->snap;
539545
struct object *obj;
540546

541-
obj = parse_object(the_repository, ref->oid);
547+
obj = parse_object(data->repo, ref->oid);
542548
if (!obj) {
543-
if (is_promisor_object(the_repository, ref->oid)) {
549+
if (is_promisor_object(data->repo, ref->oid)) {
544550
/*
545551
* Increment default_refs anyway, because this is a
546552
* valid ref.
@@ -581,25 +587,30 @@ static int fsck_handle_ref(const struct reference *ref, void *cb_data UNUSED)
581587
return 0;
582588
}
583589

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)
585592
{
586593
struct refs_for_each_ref_options opts = {
587594
.flags = REFS_FOR_EACH_INCLUDE_BROKEN,
588595
};
596+
struct snapshot_ref_data data = {
597+
.repo = repo,
598+
.snap = snap,
599+
};
589600
struct worktree **worktrees, **p;
590601
const char *head_points_at;
591602
struct object_id head_oid;
592603

593604
for (int i = 0; i < argc; i++) {
594605
const char *arg = argv[i];
595606
struct object_id oid;
596-
if (!repo_get_oid(the_repository, arg, &oid)) {
607+
if (!repo_get_oid(repo, arg, &oid)) {
597608
struct reference ref = {
598609
.name = arg,
599610
.oid = &oid,
600611
};
601612

602-
snapshot_ref(&ref, snap);
613+
snapshot_ref(&ref, &data);
603614
continue;
604615
}
605616
error(_("invalid parameter: expected sha1, got '%s'"), arg);
@@ -611,8 +622,8 @@ static void snapshot_refs(struct snapshot *snap, int argc, const char **argv)
611622
return;
612623
}
613624

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);
616627

617628
worktrees = get_worktrees();
618629
for (p = worktrees; *p; p++) {
@@ -621,7 +632,7 @@ static void snapshot_refs(struct snapshot *snap, int argc, const char **argv)
621632

622633
strbuf_worktree_ref(wt, &refname, "HEAD");
623634

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),
625636
refname.buf, 0, &head_oid, NULL);
626637

627638
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)
630641
.oid = &head_oid,
631642
};
632643

633-
snapshot_ref(&ref, snap);
644+
snapshot_ref(&ref, &data);
634645
}
635646
strbuf_release(&refname);
636647

@@ -1039,7 +1050,7 @@ int cmd_fsck(int argc,
10391050
* objects. We can still walk over new objects that are added during the
10401051
* execution of fsck but won't miss any objects that were reachable.
10411052
*/
1042-
snapshot_refs(&snap, argc, argv);
1053+
snapshot_refs(repo, &snap, argc, argv);
10431054

10441055
/* Ensure we get a "fresh" view of the odb */
10451056
odb_reprepare(repo->objects);

0 commit comments

Comments
 (0)