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

[PATCH v2 05/13] odb: refactor `odb_find_source()` to yield dirs

From
Patrick Steinhardt <ps@pks.im>
Date
Oct 8, 2026, 08:35 UTC
Message-ID
<20261008-pks-odb-move-alternates-v2-5-b47e8189baa5@pks.im>
In-Reply-To
<20261008-pks-odb-move-alternates-v2-0-b47e8189baa5@pks.im>

Similar as in the preceding commit, `odb_find_source()` can be used to yield a single source by its object directory. This function is also specific to the "files" backend once alternates are an implementation detail thereof.

Refactor it to be specific to the "files" backend and return an `struct odb_files_dir` to prepare for moving alternates into the "files" source.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/commit-graph.c     | 26 ++++++++++++++++----------
 builtin/multi-pack-index.c | 42 +++++++++++++++++++++---------------------
 odb.c                      | 26 --------------------------
 odb.h                      |  9 ---------
 odb/source-files.c         | 26 ++++++++++++++++++++++++++
 odb/source-files.h         |  6 ++++++
 t/helper/test-read-midx.c  |  8 ++++----
 7 files changed, 73 insertions(+), 70 deletions(-)
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index a986f08a94..ff244c866f 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -9,6 +9,7 @@
 #include "commit-graph.h"
 #include "odb.h"
 #include "odb/source.h"
+#include "odb/source-files.h"
 #include "progress.h"
 #include "replace-object.h"
 #include "strbuf.h"
@@ -68,7 +69,7 @@ static int graph_verify(int argc, const char **argv, const char *prefix,
 			struct repository *repo UNUSED)
 {
 	struct commit_graph *graph = NULL;
-	struct odb_source *source = NULL;
+	struct odb_files_dir *dir = NULL;
 	char *graph_name;
 	char *chain_name;
 	enum { OPENED_NONE, OPENED_GRAPH, OPENED_CHAIN } opened = OPENED_NONE;
@@ -103,9 +104,12 @@ static int graph_verify(int argc, const char **argv, const char *prefix,
 	if (opts.progress)
 		flags |= COMMIT_GRAPH_WRITE_PROGRESS;
 
-	source = odb_find_source_or_die(the_repository->objects, opts.obj_dir);
-	graph_name = get_commit_graph_filename(source->path);
-	chain_name = get_commit_graph_chain_filename(source->path);
+	dir = odb_source_files_find_dir(the_repository->objects, opts.obj_dir);
+	if (!dir)
+		die(_("could not find object directory matching %s"), opts.obj_dir);
+
+	graph_name = get_commit_graph_filename(dir->abspath);
+	chain_name = get_commit_graph_chain_filename(dir->abspath);
 	if (open_commit_graph(graph_name, &fd, &st))
 		opened = OPENED_GRAPH;
 	else if (errno != ENOENT)
@@ -123,7 +127,7 @@ static int graph_verify(int argc, const char **argv, const char *prefix,
 	if (opened == OPENED_NONE)
 		return 0;
 	else if (opened == OPENED_GRAPH)
-		graph = load_commit_graph_one_fd_st(the_repository, source->path, fd, &st);
+		graph = load_commit_graph_one_fd_st(the_repository, dir->abspath, fd, &st);
 	else
 		graph = load_commit_graph_chain_fd_st(the_repository->objects, fd, &st,
 						      &incomplete_chain);
@@ -226,7 +230,7 @@ static int graph_write(int argc, const char **argv, const char *prefix,
 	struct string_list pack_indexes = STRING_LIST_INIT_DUP;
 	struct strbuf buf = STRBUF_INIT;
 	struct oidset commits = OIDSET_INIT;
-	struct odb_source *source = NULL;
+	struct odb_files_dir *dir = NULL;
 	int result = 0;
 	enum commit_graph_write_flags flags = 0;
 	struct progress *progress = NULL;
@@ -294,10 +298,12 @@ static int graph_write(int argc, const char **argv, const char *prefix,
 	    git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
 		flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
 
-	source = odb_find_source_or_die(the_repository->objects, opts.obj_dir);
+	dir = odb_source_files_find_dir(the_repository->objects, opts.obj_dir);
+	if (!dir)
+		die(_("could not find object directory matching %s"), opts.obj_dir);
 
 	if (opts.reachable) {
-		if (write_commit_graph_reachable(the_repository, source->path, flags, &write_opts))
+		if (write_commit_graph_reachable(the_repository, dir->abspath, flags, &write_opts))
 			result = 1;
 		goto cleanup;
 	}
@@ -306,7 +312,7 @@ static int graph_write(int argc, const char **argv, const char *prefix,
 		struct strbuf packname = STRBUF_INIT;
 		size_t dirlen;
 
-		strbuf_addf(&packname, "%s/pack/", source->path);
+		strbuf_addf(&packname, "%s/pack/", dir->abspath);
 		dirlen = packname.len;
 
 		while (strbuf_getline(&buf, stdin) != EOF) {
@@ -334,7 +340,7 @@ static int graph_write(int argc, const char **argv, const char *prefix,
 		stop_progress(&progress);
 	}
 
-	if (write_commit_graph(the_repository, source->path,
+	if (write_commit_graph(the_repository, dir->abspath,
 			       opts.stdin_packs ? &pack_indexes : NULL,
 			       opts.stdin_commits ? &commits : NULL,
 			       flags,
diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index a170ec80b9..fc8b494996 100644
--- a/builtin/multi-pack-index.c
+++ b/builtin/multi-pack-index.c
@@ -86,13 +86,13 @@ static int parse_object_dir(const struct option *opt, const char *arg,
 	return 0;
 }
 
-static struct odb_source_files *handle_object_dir_option(struct repository *repo)
+static struct odb_source_packed *handle_object_dir_option(struct repository *repo)
 {
-	struct odb_source *source = odb_find_source(repo->objects, opts.object_dir);
-	if (!source)
+	struct odb_files_dir *dir = odb_source_files_find_dir(repo->objects, opts.object_dir);
+	if (!dir)
 		die(_("object directory is not an alternate of the current repository: '%s'"),
 		    opts.object_dir);
-	return odb_source_files_downcast(source);
+	return dir->packed;
 }
 
 static struct option common_opts[] = {
@@ -169,7 +169,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 			     N_("refs snapshot for selecting bitmap commits")),
 		OPT_END(),
 	};
-	struct odb_source_files *source;
+	struct odb_source_packed *packed_source;
 	int ret;
 
 	opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE;
@@ -204,7 +204,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 				   options);
 	}
 
-	source = handle_object_dir_option(repo);
+	packed_source = handle_object_dir_option(repo);
 
 	FREE_AND_NULL(options);
 
@@ -213,7 +213,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 
 		read_packs_from_stdin(&packs);
 
-		ret = write_midx_file_only(source->dirs->packed, &packs,
+		ret = write_midx_file_only(packed_source, &packs,
 					   opts.preferred_pack,
 					   opts.refs_snapshot,
 					   opts.incremental_base, opts.flags);
@@ -225,7 +225,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 
 	}
 
-	ret = write_midx_file(source->dirs->packed, opts.preferred_pack,
+	ret = write_midx_file(packed_source, opts.preferred_pack,
 			      opts.refs_snapshot, opts.flags);
 
 	free(opts.refs_snapshot);
@@ -239,7 +239,7 @@ static int cmd_multi_pack_index_compact(int argc, const char **argv,
 	struct multi_pack_index *m, *cur;
 	struct multi_pack_index *from_midx = NULL;
 	struct multi_pack_index *to_midx = NULL;
-	struct odb_source_files *source;
+	struct odb_source_packed *packed_source;
 	int ret;
 
 	struct option *options;
@@ -280,11 +280,11 @@ static int cmd_multi_pack_index_compact(int argc, const char **argv,
 				   options);
 	}
 
-	source = handle_object_dir_option(the_repository);
+	packed_source = handle_object_dir_option(the_repository);
 
 	FREE_AND_NULL(options);
 
-	m = get_multi_pack_index(source->dirs->packed);
+	m = get_multi_pack_index(packed_source);
 
 	for (cur = m; cur && !(from_midx && to_midx); cur = cur->base_midx) {
 		const char *midx_csum = midx_get_checksum_hex(cur);
@@ -307,7 +307,7 @@ static int cmd_multi_pack_index_compact(int argc, const char **argv,
 			die(_("MIDX %s must be an ancestor of %s"), argv[0], argv[1]);
 	}
 
-	ret = write_midx_file_compact(source->dirs->packed, from_midx, to_midx,
+	ret = write_midx_file_compact(packed_source, from_midx, to_midx,
 				      opts.incremental_base, opts.flags);
 
 	return ret;
@@ -321,7 +321,7 @@ static int cmd_multi_pack_index_verify(int argc, const char **argv,
 	static struct option builtin_multi_pack_index_verify_options[] = {
 		OPT_END(),
 	};
-	struct odb_source_files *source;
+	struct odb_source_packed *packed_source;
 
 	options = add_common_options(builtin_multi_pack_index_verify_options);
 
@@ -335,11 +335,11 @@ static int cmd_multi_pack_index_verify(int argc, const char **argv,
 	if (argc)
 		usage_with_options(builtin_multi_pack_index_verify_usage,
 				   options);
-	source = handle_object_dir_option(the_repository);
+	packed_source = handle_object_dir_option(the_repository);
 
 	FREE_AND_NULL(options);
 
-	return verify_midx_file(source->dirs->packed, opts.flags);
+	return verify_midx_file(packed_source, opts.flags);
 }
 
 static int cmd_multi_pack_index_expire(int argc, const char **argv,
@@ -350,7 +350,7 @@ static int cmd_multi_pack_index_expire(int argc, const char **argv,
 	static struct option builtin_multi_pack_index_expire_options[] = {
 		OPT_END(),
 	};
-	struct odb_source_files *source;
+	struct odb_source_packed *packed_source;
 
 	options = add_common_options(builtin_multi_pack_index_expire_options);
 
@@ -364,11 +364,11 @@ static int cmd_multi_pack_index_expire(int argc, const char **argv,
 	if (argc)
 		usage_with_options(builtin_multi_pack_index_expire_usage,
 				   options);
-	source = handle_object_dir_option(the_repository);
+	packed_source = handle_object_dir_option(the_repository);
 
 	FREE_AND_NULL(options);
 
-	return expire_midx_packs(source->dirs->packed, opts.flags);
+	return expire_midx_packs(packed_source, opts.flags);
 }
 
 static int cmd_multi_pack_index_repack(int argc, const char **argv,
@@ -381,7 +381,7 @@ static int cmd_multi_pack_index_repack(int argc, const char **argv,
 		  N_("during repack, collect pack-files of smaller size into a batch that is larger than this size")),
 		OPT_END(),
 	};
-	struct odb_source_files *source;
+	struct odb_source_packed *packed_source;
 
 	options = add_common_options(builtin_multi_pack_index_repack_options);
 
@@ -396,11 +396,11 @@ static int cmd_multi_pack_index_repack(int argc, const char **argv,
 	if (argc)
 		usage_with_options(builtin_multi_pack_index_repack_usage,
 				   options);
-	source = handle_object_dir_option(the_repository);
+	packed_source = handle_object_dir_option(the_repository);
 
 	FREE_AND_NULL(options);
 
-	return midx_repack(source->dirs->packed, (size_t)opts.batch_size, opts.flags);
+	return midx_repack(packed_source, (size_t)opts.batch_size, opts.flags);
 }
 
 int cmd_multi_pack_index(int argc,
diff --git a/odb.c b/odb.c
index 9b70859c23..1dc8647159 100644
--- a/odb.c
+++ b/odb.c
@@ -348,32 +348,6 @@ char *compute_alternate_path(const char *path, struct strbuf *err)
 	return ref_git;
 }
 
-struct odb_source *odb_find_source(struct object_database *odb, const char *obj_dir)
-{
-	struct odb_source *source;
-	char *obj_dir_real = real_pathdup(obj_dir, 1);
-	struct strbuf odb_path_real = STRBUF_INIT;
-
-	for (source = odb->sources; source; source = source->next) {
-		strbuf_realpath(&odb_path_real, source->path, 1);
-		if (!strcmp(obj_dir_real, odb_path_real.buf))
-			break;
-	}
-
-	free(obj_dir_real);
-	strbuf_release(&odb_path_real);
-
-	return source;
-}
-
-struct odb_source *odb_find_source_or_die(struct object_database *odb, const char *obj_dir)
-{
-	struct odb_source *source = odb_find_source(odb, obj_dir);
-	if (!source)
-		die(_("could not find object directory matching %s"), obj_dir);
-	return source;
-}
-
 static void fill_alternate_refs_command(struct repository *repo,
 					struct child_process *cmd,
 					const char *repo_path)
diff --git a/odb.h b/odb.h
index 3715351bb3..5c86572b5d 100644
--- a/odb.h
+++ b/odb.h
@@ -226,15 +226,6 @@ struct odb_fsck_options {
  */
 int odb_fsck(struct object_database *odb, struct odb_fsck_options *opts);
 
-/*
- * Find source by its object directory path. Returns a `NULL` pointer in case
- * the source could not be found.
- */
-struct odb_source *odb_find_source(struct object_database *odb, const char *obj_dir);
-
-/* Same as `odb_find_source()`, but dies in case the source doesn't exist. */
-struct odb_source *odb_find_source_or_die(struct object_database *odb, const char *obj_dir);
-
 /*
  * Replace the current writable object directory with the specified temporary
  * object directory and return the newly installed primary source. The former
diff --git a/odb/source-files.c b/odb/source-files.c
index 1f4cedaffa..e5e43b1543 100644
--- a/odb/source-files.c
+++ b/odb/source-files.c
@@ -967,6 +967,32 @@ static int odb_source_files_fsck(struct odb_source *source,
 	return ret;
 }
 
+struct odb_files_dir *odb_source_files_find_dir(struct object_database *odb, const char *obj_dir)
+{
+	char *obj_dir_real = real_pathdup(obj_dir, 1);
+	struct strbuf odb_path_real = STRBUF_INIT;
+	struct odb_files_dir *dir = NULL;
+	struct odb_source *source;
+
+	for (source = odb->sources; source; source = source->next) {
+		struct odb_source_files *files;
+
+		if (source->type != ODB_SOURCE_FILES)
+			continue;
+		files = odb_source_files_downcast(source);
+
+		strbuf_realpath(&odb_path_real, files->dirs->abspath, 1);
+		if (!strcmp(obj_dir_real, odb_path_real.buf)) {
+			dir = files->dirs;
+			break;
+		}
+	}
+
+	free(obj_dir_real);
+	strbuf_release(&odb_path_real);
+	return dir;
+}
+
 struct odb_source_files *odb_source_files_new(struct object_database *odb,
 					      const char *path,
 					      bool local)
diff --git a/odb/source-files.h b/odb/source-files.h
index 7f465853b1..77f4d842e0 100644
--- a/odb/source-files.h
+++ b/odb/source-files.h
@@ -72,4 +72,10 @@ static inline struct odb_source_files *odb_source_files_downcast(struct odb_sour
 	return container_of(source, struct odb_source_files, base);
 }
 
+/*
+ * Find "files" directory by its object directory path. Returns a `NULL`
+ * pointer in case the object directory could not be found.
+ */
+struct odb_files_dir *odb_source_files_find_dir(struct object_database *odb, const char *obj_dir);
+
 #endif
diff --git a/t/helper/test-read-midx.c b/t/helper/test-read-midx.c
index 3f4bafff61..88a79fdfc8 100644
--- a/t/helper/test-read-midx.c
+++ b/t/helper/test-read-midx.c
@@ -16,13 +16,13 @@ static struct multi_pack_index *setup_midx(const char *object_dir,
 					   struct odb_source_packed **out)
 {
 	struct odb_source_packed *packed;
-	struct odb_source *source;
+	struct odb_files_dir *dir;
 
 	setup_git_directory(the_repository);
 
-	source = odb_find_source(the_repository->objects, object_dir);
-	if (source) {
-		packed = odb_source_files_downcast(source)->dirs->packed;
+	dir = odb_source_files_find_dir(the_repository->objects, object_dir);
+	if (dir) {
+		packed = dir->packed;
 	} else {
 		packed = odb_source_packed_new(the_repository->objects,
 					       object_dir, false);
-- 
2.56.0.406.ga2d225a756.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 32 of 41 in “odb/source-files: move alternates into the backend”
  1. 00/13 odb/source-files: move alternates into the backendPatrick Steinhardt, Oct 2, 2026
  2. 01/13 commit-graph: require resolved packfile paths for `stdin_packs`Patrick Steinhardt, Oct 2, 2026
  3. Karthik NayakOct 5, 2026
  4. Patrick SteinhardtOct 6, 2026
  5. 02/13 commit-graph: stop depending on `struct odb_source`Patrick Steinhardt, Oct 2, 2026
  6. Karthik NayakOct 5, 2026
  7. Patrick SteinhardtOct 6, 2026
  8. Karthik NayakOct 6, 2026
  9. 03/13 odb/source-files: introduce `struct odb_files_dir`Patrick Steinhardt, Oct 2, 2026
  10. 04/13 odb: refactor `odb_for_each_alternate()` to yield dirsPatrick Steinhardt, Oct 2, 2026
  11. Karthik NayakOct 6, 2026
  12. 05/13 odb: refactor `odb_find_source()` to yield dirsPatrick Steinhardt, Oct 2, 2026
  13. 06/13 odb/source-files: add the ability to have multiple object dirsPatrick Steinhardt, Oct 2, 2026
  14. 07/13 tmp-objdir: absorb logic to set and restore primary sourcesPatrick Steinhardt, Oct 2, 2026
  15. 08/13 tmp-objdir: manage quarantine as an object directoryPatrick Steinhardt, Oct 2, 2026
  16. Karthik NayakOct 6, 2026
  17. Karthik NayakOct 6, 2026
  18. 09/13 tmp-objdir: replace primary source at creation timePatrick Steinhardt, Oct 2, 2026
  19. 10/13 odb/source: make `will_destroy` an implementation detailPatrick Steinhardt, Oct 2, 2026
  20. 11/13 odb/source-files: extract reading alternatesPatrick Steinhardt, Oct 2, 2026
  21. 12/13 odb/source-files: move alternates into the backendPatrick Steinhardt, Oct 2, 2026
  22. Karthik NayakOct 6, 2026
  23. Patrick SteinhardtOct 7, 2026
  24. 13/13 odb/source: drop `read_alternates` callbackPatrick Steinhardt, Oct 2, 2026
  25. Karthik NayakOct 6, 2026
  26. Patrick SteinhardtOct 7, 2026
  27. 00/13 odb/source-files: move alternates into the backendPatrick Steinhardt, Oct 8, 2026
  28. 01/13 commit-graph: require resolved packfile paths for `stdin_packs`Patrick Steinhardt, Oct 8, 2026
  29. 02/13 commit-graph: stop depending on `struct odb_source`Patrick Steinhardt, Oct 8, 2026
  30. 03/13 odb/source-files: introduce `struct odb_files_dir`Patrick Steinhardt, Oct 8, 2026
  31. 04/13 odb: refactor `odb_for_each_alternate()` to yield dirsPatrick Steinhardt, Oct 8, 2026
  32. 05/13 odb: refactor `odb_find_source()` to yield dirsPatrick Steinhardt, Oct 8, 2026
  33. 06/13 odb/source-files: add the ability to have multiple object dirsPatrick Steinhardt, Oct 8, 2026
  34. 07/13 tmp-objdir: absorb logic to set and restore primary sourcesPatrick Steinhardt, Oct 8, 2026
  35. 08/13 tmp-objdir: manage quarantine as an object directoryPatrick Steinhardt, Oct 8, 2026
  36. 09/13 tmp-objdir: replace primary source at creation timePatrick Steinhardt, Oct 8, 2026
  37. 10/13 odb/source: make `will_destroy` an implementation detailPatrick Steinhardt, Oct 8, 2026
  38. 11/13 odb/source-files: extract reading alternatesPatrick Steinhardt, Oct 8, 2026
  39. 12/13 odb/source-files: move alternates into the backendPatrick Steinhardt, Oct 8, 2026
  40. 13/13 odb/source: drop `read_alternates` callbackPatrick Steinhardt, Oct 8, 2026
  41. Karthik NayakOct 8, 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.