git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 06/12] builtin/fsck: stop using `the_repository` when snapshotting refs

From
Patrick Steinhardt <ps@pks.im>
Date
Mar 23, 2026, 15:02 UTC
Message-ID
<20260323-b4-pks-fsck-without-the-repository-v2-6-e8dc79bca651@pks.im>
In-Reply-To
<20260323-b4-pks-fsck-without-the-repository-v2-0-e8dc79bca651@pks.im>

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>
---
 builtin/fsck.c | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)
diff --git a/builtin/fsck.c b/builtin/fsck.c
index 59680e6daf..edbff16add 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -533,14 +533,20 @@ struct snapshot {
 	/* TODO: Consider also snapshotting the index of each worktree. */
 };
 
+struct snapshot_ref_data {
+	struct repository *repo;
+	struct snapshot *snap;
+};
+
 static int snapshot_ref(const struct reference *ref, void *cb_data)
 {
-	struct snapshot *snap = cb_data;
+	struct snapshot_ref_data *data = cb_data;
+	struct snapshot *snap = data->snap;
 	struct object *obj;
 
-	obj = parse_object(the_repository, ref->oid);
+	obj = parse_object(data->repo, ref->oid);
 	if (!obj) {
-		if (is_promisor_object(the_repository, ref->oid)) {
+		if (is_promisor_object(data->repo, ref->oid)) {
 			/*
 			 * Increment default_refs anyway, because this is a
 			 * valid ref.
@@ -581,11 +587,16 @@ static int fsck_handle_ref(const struct reference *ref, void *cb_data UNUSED)
 	return 0;
 }
 
-static void snapshot_refs(struct snapshot *snap, int argc, const char **argv)
+static void snapshot_refs(struct repository *repo,
+			  struct snapshot *snap, int argc, const char **argv)
 {
 	struct refs_for_each_ref_options opts = {
 		.flags = REFS_FOR_EACH_INCLUDE_BROKEN,
 	};
+	struct snapshot_ref_data data = {
+		.repo = repo,
+		.snap = snap,
+	};
 	struct worktree **worktrees, **p;
 	const char *head_points_at;
 	struct object_id head_oid;
@@ -593,13 +604,13 @@ static void snapshot_refs(struct snapshot *snap, int argc, const char **argv)
 	for (int i = 0; i < argc; i++) {
 		const char *arg = argv[i];
 		struct object_id oid;
-		if (!repo_get_oid(the_repository, arg, &oid)) {
+		if (!repo_get_oid(repo, arg, &oid)) {
 			struct reference ref = {
 				.name = arg,
 				.oid = &oid,
 			};
 
-			snapshot_ref(&ref, snap);
+			snapshot_ref(&ref, &data);
 			continue;
 		}
 		error(_("invalid parameter: expected sha1, got '%s'"), arg);
@@ -611,8 +622,8 @@ static void snapshot_refs(struct snapshot *snap, int argc, const char **argv)
 		return;
 	}
 
-	refs_for_each_ref_ext(get_main_ref_store(the_repository),
-			      snapshot_ref, snap, &opts);
+	refs_for_each_ref_ext(get_main_ref_store(repo),
+			      snapshot_ref, &data, &opts);
 
 	worktrees = get_worktrees();
 	for (p = worktrees; *p; p++) {
@@ -621,7 +632,7 @@ static void snapshot_refs(struct snapshot *snap, int argc, const char **argv)
 
 		strbuf_worktree_ref(wt, &refname, "HEAD");
 
-		head_points_at = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
+		head_points_at = refs_resolve_ref_unsafe(get_main_ref_store(repo),
 							 refname.buf, 0, &head_oid, NULL);
 
 		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)
 				.oid = &head_oid,
 			};
 
-			snapshot_ref(&ref, snap);
+			snapshot_ref(&ref, &data);
 		}
 		strbuf_release(&refname);
 
@@ -1039,7 +1050,7 @@ int cmd_fsck(int argc,
 	 * objects. We can still walk over new objects that are added during the
 	 * execution of fsck but won't miss any objects that were reachable.
 	 */
-	snapshot_refs(&snap, argc, argv);
+	snapshot_refs(repo, &snap, argc, argv);
 
 	/* Ensure we get a "fresh" view of the odb */
 	odb_reprepare(repo->objects);
-- 
2.53.0.1118.gaef5881109.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 9 of 17 in “fsck: drop use of `the_repository`”
  1. 00/12 fsck: drop use of `the_repository`Patrick Steinhardt, Mar 23, 2026
  2. 01/12 fetch-pack: move fsck options into function scopePatrick Steinhardt, Mar 23, 2026
  3. Junio C HamanoMar 23, 2026
  4. 02/12 fsck: initialize fsck options via a functionPatrick Steinhardt, Mar 23, 2026
  5. Junio C HamanoMar 23, 2026
  6. 03/12 fsck: store repository in fsck optionsPatrick Steinhardt, Mar 23, 2026
  7. 04/12 fsck: drop USE_THE_REPOSITORYPatrick Steinhardt, Mar 23, 2026
  8. 05/12 builtin/fsck: fix trivial dependence on `the_repository`Patrick Steinhardt, Mar 23, 2026
  9. 06/12 builtin/fsck: stop using `the_repository` when snapshotting refsPatrick Steinhardt, Mar 23, 2026
  10. 07/12 builtin/fsck: stop using `the_repository` when checking refsPatrick Steinhardt, Mar 23, 2026
  11. 08/12 builtin/fsck: stop using `the_repository` when checking reflogsPatrick Steinhardt, Mar 23, 2026
  12. 09/12 builtin/fsck: stop using `the_repository` with loose objectsPatrick Steinhardt, Mar 23, 2026
  13. 10/12 builtin/fsck: stop using `the_repository` when checking packed objectsPatrick Steinhardt, Mar 23, 2026
  14. 11/12 builtin/fsck: stop using `the_repository` when marking objectsPatrick Steinhardt, Mar 23, 2026
  15. 12/12 builtin/fsck: stop using `the_repository` in error reportingPatrick Steinhardt, Mar 23, 2026
  16. Junio C HamanoMar 31, 2026
  17. Patrick SteinhardtMar 31, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.