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

[PATCH v2 01/17] odb: split `struct odb_source` into separate header

From
Patrick Steinhardt <ps@pks.im>
Date
Mar 5, 2026, 14:19 UTC
Message-ID
<20260305-b4-pks-odb-source-pluggable-v2-1-3290bfd1f444@pks.im>
In-Reply-To
<20260305-b4-pks-odb-source-pluggable-v2-0-3290bfd1f444@pks.im>

Subsequent commits will expand the `struct odb_source` to become a generic interface for accessing an object database source. As part of these refactorings we'll add a set of function pointers that will significantly expand the structure overall.

Prepare for this by splitting out the `struct odb_source` into a separate header. This keeps the high-level object database interface detached from the low-level object database sources.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 Makefile     |  1 +
 meson.build  |  1 +
 odb.c        | 25 -------------------------
 odb.h        | 45 +--------------------------------------------
 odb/source.c | 28 ++++++++++++++++++++++++++++
 odb/source.h | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 91 insertions(+), 69 deletions(-)
diff --git a/Makefile b/Makefile
index 47ed9fa7fd..116358e484 100644
--- a/Makefile
+++ b/Makefile
@@ -1214,6 +1214,7 @@ LIB_OBJS += object-file.o
 LIB_OBJS += object-name.o
 LIB_OBJS += object.o
 LIB_OBJS += odb.o
+LIB_OBJS += odb/source.o
 LIB_OBJS += odb/streaming.o
 LIB_OBJS += oid-array.o
 LIB_OBJS += oidmap.o
diff --git a/meson.build b/meson.build
index 3a1d12caa4..1018af17c3 100644
--- a/meson.build
+++ b/meson.build
@@ -397,6 +397,7 @@ libgit_sources = [
   'object-name.c',
   'object.c',
   'odb.c',
+  'odb/source.c',
   'odb/streaming.c',
   'oid-array.c',
   'oidmap.c',
diff --git a/odb.c b/odb.c
index 776de5356c..d318482d47 100644
--- a/odb.c
+++ b/odb.c
@@ -217,23 +217,6 @@ static void odb_source_read_alternates(struct odb_source *source,
 	free(path);
 }
 
-
-static struct odb_source *odb_source_new(struct object_database *odb,
-					 const char *path,
-					 bool local)
-{
-	struct odb_source *source;
-
-	CALLOC_ARRAY(source, 1);
-	source->odb = odb;
-	source->local = local;
-	source->path = xstrdup(path);
-	source->loose = odb_source_loose_new(source);
-	source->packfiles = packfile_store_new(source);
-
-	return source;
-}
-
 static struct odb_source *odb_add_alternate_recursively(struct object_database *odb,
 							const char *source,
 							int depth)
@@ -373,14 +356,6 @@ struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
 	return source->next;
 }
 
-static void odb_source_free(struct odb_source *source)
-{
-	free(source->path);
-	odb_source_loose_free(source->loose);
-	packfile_store_free(source->packfiles);
-	free(source);
-}
-
 void odb_restore_primary_source(struct object_database *odb,
 				struct odb_source *restore_source,
 				const char *old_path)
diff --git a/odb.h b/odb.h
index 68b8ec2289..e13b5b7c44 100644
--- a/odb.h
+++ b/odb.h
@@ -3,6 +3,7 @@
 
 #include "hashmap.h"
 #include "object.h"
+#include "odb/source.h"
 #include "oidset.h"
 #include "oidmap.h"
 #include "string-list.h"
@@ -30,50 +31,6 @@ extern int fetch_if_missing;
  */
 char *compute_alternate_path(const char *path, struct strbuf *err);
 
-/*
- * The source is the part of the object database that stores the actual
- * objects. It thus encapsulates the logic to read and write the specific
- * on-disk format. An object database can have multiple sources:
- *
- *   - The primary source, which is typically located in "$GIT_DIR/objects".
- *     This is where new objects are usually written to.
- *
- *   - Alternate sources, which are configured via "objects/info/alternates" or
- *     via the GIT_ALTERNATE_OBJECT_DIRECTORIES environment variable. These
- *     alternate sources are only used to read objects.
- */
-struct odb_source {
-	struct odb_source *next;
-
-	/* Object database that owns this object source. */
-	struct object_database *odb;
-
-	/* Private state for loose objects. */
-	struct odb_source_loose *loose;
-
-	/* Should only be accessed directly by packfile.c and midx.c. */
-	struct packfile_store *packfiles;
-
-	/*
-	 * Figure out whether this is the local source of the owning
-	 * repository, which would typically be its ".git/objects" directory.
-	 * This local object directory is usually where objects would be
-	 * written to.
-	 */
-	bool local;
-
-	/*
-	 * This object store is ephemeral, so there is no need to fsync.
-	 */
-	int will_destroy;
-
-	/*
-	 * Path to the source. If this is a relative path, it is relative to
-	 * the current working directory.
-	 */
-	char *path;
-};
-
 struct packed_git;
 struct packfile_store;
 struct cached_object_entry;
diff --git a/odb/source.c b/odb/source.c
new file mode 100644
index 0000000000..7fc89806f9
--- /dev/null
+++ b/odb/source.c
@@ -0,0 +1,28 @@
+#include "git-compat-util.h"
+#include "object-file.h"
+#include "odb/source.h"
+#include "packfile.h"
+
+struct odb_source *odb_source_new(struct object_database *odb,
+				  const char *path,
+				  bool local)
+{
+	struct odb_source *source;
+
+	CALLOC_ARRAY(source, 1);
+	source->odb = odb;
+	source->local = local;
+	source->path = xstrdup(path);
+	source->loose = odb_source_loose_new(source);
+	source->packfiles = packfile_store_new(source);
+
+	return source;
+}
+
+void odb_source_free(struct odb_source *source)
+{
+	free(source->path);
+	odb_source_loose_free(source->loose);
+	packfile_store_free(source->packfiles);
+	free(source);
+}
diff --git a/odb/source.h b/odb/source.h
new file mode 100644
index 0000000000..391d6d1e38
--- /dev/null
+++ b/odb/source.h
@@ -0,0 +1,60 @@
+#ifndef ODB_SOURCE_H
+#define ODB_SOURCE_H
+
+/*
+ * The source is the part of the object database that stores the actual
+ * objects. It thus encapsulates the logic to read and write the specific
+ * on-disk format. An object database can have multiple sources:
+ *
+ *   - The primary source, which is typically located in "$GIT_DIR/objects".
+ *     This is where new objects are usually written to.
+ *
+ *   - Alternate sources, which are configured via "objects/info/alternates" or
+ *     via the GIT_ALTERNATE_OBJECT_DIRECTORIES environment variable. These
+ *     alternate sources are only used to read objects.
+ */
+struct odb_source {
+	struct odb_source *next;
+
+	/* Object database that owns this object source. */
+	struct object_database *odb;
+
+	/* Private state for loose objects. */
+	struct odb_source_loose *loose;
+
+	/* Should only be accessed directly by packfile.c and midx.c. */
+	struct packfile_store *packfiles;
+
+	/*
+	 * Figure out whether this is the local source of the owning
+	 * repository, which would typically be its ".git/objects" directory.
+	 * This local object directory is usually where objects would be
+	 * written to.
+	 */
+	bool local;
+
+	/*
+	 * This object store is ephemeral, so there is no need to fsync.
+	 */
+	int will_destroy;
+
+	/*
+	 * Path to the source. If this is a relative path, it is relative to
+	 * the current working directory.
+	 */
+	char *path;
+};
+
+/*
+ * Allocate and initialize a new source for the given object database located
+ * at `path`. `local` indicates whether or not the source is the local and thus
+ * primary object source of the object database.
+ */
+struct odb_source *odb_source_new(struct object_database *odb,
+				  const char *path,
+				  bool local);
+
+/* Free the object database source, releasing all associated resources. */
+void odb_source_free(struct odb_source *source);
+
+#endif
-- 
2.53.0.797.g7842e34a66.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 58 of 77 in “odb: make object database sources pluggable”
  1. 00/17 odb: make object database sources pluggablePatrick Steinhardt, Feb 23, 2026
  2. 01/17 odb: split `struct odb_source` into separate headerPatrick Steinhardt, Feb 23, 2026
  3. Justin ToblerMar 4, 2026
  4. Patrick SteinhardtMar 5, 2026
  5. Justin ToblerMar 5, 2026
  6. 02/17 odb: introduce "files" sourcePatrick Steinhardt, Feb 23, 2026
  7. Justin ToblerMar 4, 2026
  8. Patrick SteinhardtMar 5, 2026
  9. Karthik NayakMar 5, 2026
  10. 03/17 odb: embed base source in the "files" backendPatrick Steinhardt, Feb 23, 2026
  11. Justin ToblerMar 4, 2026
  12. Patrick SteinhardtMar 5, 2026
  13. Justin ToblerMar 5, 2026
  14. Karthik NayakMar 5, 2026
  15. Patrick SteinhardtMar 5, 2026
  16. 04/17 odb: move reparenting logic into respective subsystemsPatrick Steinhardt, Feb 23, 2026
  17. Justin ToblerMar 4, 2026
  18. Patrick SteinhardtMar 5, 2026
  19. 05/17 odb/source: introduce source type for robustnessPatrick Steinhardt, Feb 23, 2026
  20. Justin ToblerMar 4, 2026
  21. Patrick SteinhardtMar 5, 2026
  22. Karthik NayakMar 5, 2026
  23. 06/17 odb/source: make `free()` function pluggablePatrick Steinhardt, Feb 23, 2026
  24. Justin ToblerMar 4, 2026
  25. 07/17 odb/source: make `reprepare()` function pluggablePatrick Steinhardt, Feb 23, 2026
  26. Justin ToblerMar 4, 2026
  27. Patrick SteinhardtMar 5, 2026
  28. 08/17 odb/source: make `close()` function pluggablePatrick Steinhardt, Feb 23, 2026
  29. Justin ToblerMar 4, 2026
  30. Patrick SteinhardtMar 5, 2026
  31. Justin ToblerMar 5, 2026
  32. Karthik NayakMar 5, 2026
  33. Patrick SteinhardtMar 5, 2026
  34. 09/17 odb/source: make `read_object_info()` function pluggablePatrick Steinhardt, Feb 23, 2026
  35. Justin ToblerMar 4, 2026
  36. 10/17 odb/source: make `read_object_stream()` function pluggablePatrick Steinhardt, Feb 23, 2026
  37. Karthik NayakMar 5, 2026
  38. Patrick SteinhardtMar 5, 2026
  39. 11/17 odb/source: make `for_each_object()` function pluggablePatrick Steinhardt, Feb 23, 2026
  40. Karthik NayakMar 5, 2026
  41. Karthik NayakMar 5, 2026
  42. Patrick SteinhardtMar 5, 2026
  43. 12/17 odb/source: make `freshen_object()` function pluggablePatrick Steinhardt, Feb 23, 2026
  44. 13/17 odb/source: make `write_object()` function pluggablePatrick Steinhardt, Feb 23, 2026
  45. 14/17 odb/source: make `write_object_stream()` function pluggablePatrick Steinhardt, Feb 23, 2026
  46. 15/17 odb/source: make `read_alternates()` function pluggablePatrick Steinhardt, Feb 23, 2026
  47. Justin ToblerMar 4, 2026
  48. Patrick SteinhardtMar 5, 2026
  49. 16/17 odb/source: make `write_alternate()` function pluggablePatrick Steinhardt, Feb 23, 2026
  50. 17/17 odb/source: make `begin_transaction()` function pluggablePatrick Steinhardt, Feb 23, 2026
  51. Justin ToblerMar 4, 2026
  52. Patrick SteinhardtMar 5, 2026
  53. Patrick SteinhardtFeb 23, 2026
  54. Junio C HamanoFeb 23, 2026
  55. Patrick SteinhardtFeb 24, 2026
  56. Karthik NayakMar 5, 2026
  57. 00/17 odb: make object database sources pluggablePatrick Steinhardt, Mar 5, 2026
  58. 01/17 odb: split `struct odb_source` into separate headerPatrick Steinhardt, Mar 5, 2026
  59. 02/17 odb: introduce "files" sourcePatrick Steinhardt, Mar 5, 2026
  60. 03/17 odb: embed base source in the "files" backendPatrick Steinhardt, Mar 5, 2026
  61. 04/17 odb: move reparenting logic into respective subsystemsPatrick Steinhardt, Mar 5, 2026
  62. 05/17 odb/source: introduce source type for robustnessPatrick Steinhardt, Mar 5, 2026
  63. 06/17 odb/source: make `free()` function pluggablePatrick Steinhardt, Mar 5, 2026
  64. 07/17 odb/source: make `reprepare()` function pluggablePatrick Steinhardt, Mar 5, 2026
  65. 08/17 odb/source: make `close()` function pluggablePatrick Steinhardt, Mar 5, 2026
  66. 09/17 odb/source: make `read_object_info()` function pluggablePatrick Steinhardt, Mar 5, 2026
  67. 10/17 odb/source: make `read_object_stream()` function pluggablePatrick Steinhardt, Mar 5, 2026
  68. 11/17 odb/source: make `for_each_object()` function pluggablePatrick Steinhardt, Mar 5, 2026
  69. 12/17 odb/source: make `freshen_object()` function pluggablePatrick Steinhardt, Mar 5, 2026
  70. 13/17 odb/source: make `write_object()` function pluggablePatrick Steinhardt, Mar 5, 2026
  71. 14/17 odb/source: make `write_object_stream()` function pluggablePatrick Steinhardt, Mar 5, 2026
  72. 15/17 odb/source: make `read_alternates()` function pluggablePatrick Steinhardt, Mar 5, 2026
  73. 16/17 odb/source: make `write_alternate()` function pluggablePatrick Steinhardt, Mar 5, 2026
  74. 17/17 odb/source: make `begin_transaction()` function pluggablePatrick Steinhardt, Mar 5, 2026
  75. Justin ToblerMar 5, 2026
  76. Junio C HamanoMar 5, 2026
  77. Patrick SteinhardtMar 10, 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.