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

[PATCH 15/17] odb/source: make `read_alternates()` function pluggable

From
Patrick Steinhardt <ps@pks.im>
Date
Feb 23, 2026, 16:18 UTC
Message-ID
<20260223-b4-pks-odb-source-pluggable-v1-15-253bac1db598@pks.im>
In-Reply-To
<20260223-b4-pks-odb-source-pluggable-v1-0-253bac1db598@pks.im>

Introduce a new callback function in `struct odb_source` to make the function pluggable.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 odb.c              | 26 ++++----------------------
 odb.h              |  5 +++++
 odb/source-files.c | 22 ++++++++++++++++++++++
 odb/source.h       | 29 +++++++++++++++++++++++++++++
 4 files changed, 60 insertions(+), 22 deletions(-)
diff --git a/odb.c b/odb.c
index f439de9db2..d9424cdfd0 100644
--- a/odb.c
+++ b/odb.c
@@ -131,10 +131,10 @@ static bool odb_is_source_usable(struct object_database *o, const char *path)
 	return usable;
 }
 
-static void parse_alternates(const char *string,
-			     int sep,
-			     const char *relative_base,
-			     struct strvec *out)
+void parse_alternates(const char *string,
+		      int sep,
+		      const char *relative_base,
+		      struct strvec *out)
 {
 	struct strbuf pathbuf = STRBUF_INIT;
 	struct strbuf buf = STRBUF_INIT;
@@ -198,24 +198,6 @@ static void parse_alternates(const char *string,
 	strbuf_release(&buf);
 }
 
-static void odb_source_read_alternates(struct odb_source *source,
-				       struct strvec *out)
-{
-	struct strbuf buf = STRBUF_INIT;
-	char *path;
-
-	path = xstrfmt("%s/info/alternates", source->path);
-	if (strbuf_read_file(&buf, path, 1024) < 0) {
-		warn_on_fopen_errors(path);
-		free(path);
-		return;
-	}
-	parse_alternates(buf.buf, '\n', source->path, out);
-
-	strbuf_release(&buf);
-	free(path);
-}
-
 static struct odb_source *odb_add_alternate_recursively(struct object_database *odb,
 							const char *source,
 							int depth)
diff --git a/odb.h b/odb.h
index 692d9029ef..86e0365c24 100644
--- a/odb.h
+++ b/odb.h
@@ -500,4 +500,9 @@ int odb_write_object_stream(struct object_database *odb,
 			    struct odb_write_stream *stream, size_t len,
 			    struct object_id *oid);
 
+void parse_alternates(const char *string,
+		      int sep,
+		      const char *relative_base,
+		      struct strvec *out);
+
 #endif /* ODB_H */
diff --git a/odb/source-files.c b/odb/source-files.c
index b8844f11b7..199c55cfa4 100644
--- a/odb/source-files.c
+++ b/odb/source-files.c
@@ -2,9 +2,11 @@
 #include "abspath.h"
 #include "chdir-notify.h"
 #include "object-file.h"
+#include "odb.h"
 #include "odb/source.h"
 #include "odb/source-files.h"
 #include "packfile.h"
+#include "strbuf.h"
 
 static void odb_source_files_reparent(const char *name UNUSED,
 				      const char *old_cwd,
@@ -117,6 +119,25 @@ static int odb_source_files_write_object_stream(struct odb_source *source,
 	return odb_source_loose_write_stream(source, stream, len, oid);
 }
 
+static int odb_source_files_read_alternates(struct odb_source *source,
+					    struct strvec *out)
+{
+	struct strbuf buf = STRBUF_INIT;
+	char *path;
+
+	path = xstrfmt("%s/info/alternates", source->path);
+	if (strbuf_read_file(&buf, path, 1024) < 0) {
+		warn_on_fopen_errors(path);
+		free(path);
+		return 0;
+	}
+	parse_alternates(buf.buf, '\n', source->path, out);
+
+	strbuf_release(&buf);
+	free(path);
+	return 0;
+}
+
 struct odb_source_files *odb_source_files_new(struct object_database *odb,
 					      const char *path,
 					      bool local)
@@ -137,6 +158,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
 	files->base.freshen_object = odb_source_files_freshen_object;
 	files->base.write_object = odb_source_files_write_object;
 	files->base.write_object_stream = odb_source_files_write_object_stream;
+	files->base.read_alternates = odb_source_files_read_alternates;
 
 	/*
 	 * Ideally, we would only ever store absolute paths in the source. This
diff --git a/odb/source.h b/odb/source.h
index ddce43eb20..14f5d56f68 100644
--- a/odb/source.h
+++ b/odb/source.h
@@ -55,6 +55,7 @@ struct object_id;
 struct object_info;
 struct odb_read_stream;
 struct odb_write_stream;
+struct strvec;
 
 /*
  * A callback function that can be used to iterate through objects. If given,
@@ -229,6 +230,20 @@ struct odb_source {
 	int (*write_object_stream)(struct odb_source *source,
 				   struct odb_write_stream *stream, size_t len,
 				   struct object_id *oid);
+
+	/*
+	 * This callback is expected to read the list of alternate object
+	 * database sources connected to it and write them into the `strvec`.
+	 *
+	 * The format is expected to follow the "objectStorage" extension
+	 * format with `(backend://)?payload` syntax. If the payload contains
+	 * paths, these paths must be resolved to absolute paths.
+	 *
+	 * The callback is expected to return 0 on success, a negative error
+	 * code otherwise.
+	 */
+	int (*read_alternates)(struct odb_source *source,
+			       struct strvec *out);
 };
 
 /*
@@ -379,4 +394,18 @@ static inline int odb_source_write_object_stream(struct odb_source *source,
 	return source->write_object_stream(source, stream, len, oid);
 }
 
+/*
+ * Read the list of alternative object database sources from the given backend
+ * and populate the `strvec` with them. The listing is not recursive -- that
+ * is, if any of the yielded alternate sources has alternates itself, those
+ * will not be yielded as part of this function call.
+ *
+ * Return 0 on success, a negative error code otherwise.
+ */
+static inline int odb_source_read_alternates(struct odb_source *source,
+					     struct strvec *out)
+{
+	return source->read_alternates(source, out);
+}
+
 #endif
-- 
2.53.0.536.g309c995771.dirty
Previous: Patrick SteinhardtNext: Justin Tobler
Message 46 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.