odb/source: make `write_object()` function pluggable

Introduce a new callback function in `struct odb_source` to make the function pluggable. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 5, 2026 at 15:19 UTC 6e76c3ab69f9b30530d50550a2feb7d8bda935db
3 files changed +50 -2
odb.c
+2 -2
@@ -1005,8 +1005,8 @@ int odb_write_object_ext(struct object_database *odb,
1005 struct object_id *compat_oid,
1006 unsigned flags)
1007 {
1008 - return odb_source_loose_write_object(odb->sources, buf, len, type,
1009 - oid, compat_oid, flags);
1008 + return odb_source_write_object(odb->sources, buf, len, type,
1009 + oid, compat_oid, flags);
1010 }
1011
1012 int odb_write_object_stream(struct object_database *odb,
odb/source-files.c
+12
@@ -98,6 +98,17 @@ static int odb_source_files_freshen_object(struct odb_source *source,
98 return 0;
99 }
100
101 +static int odb_source_files_write_object(struct odb_source *source,
102 + const void *buf, unsigned long len,
103 + enum object_type type,
104 + struct object_id *oid,
105 + struct object_id *compat_oid,
106 + unsigned flags)
107 +{
108 + return odb_source_loose_write_object(source, buf, len, type,
109 + oid, compat_oid, flags);
110 +}
111 +
112 struct odb_source_files *odb_source_files_new(struct object_database *odb,
113 const char *path,
114 bool local)
@@ -116,6 +127,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
127 files->base.read_object_stream = odb_source_files_read_object_stream;
128 files->base.for_each_object = odb_source_files_for_each_object;
129 files->base.freshen_object = odb_source_files_freshen_object;
130 + files->base.write_object = odb_source_files_write_object;
131
132 /*
133 * Ideally, we would only ever store absolute paths in the source. This
odb/source.h
+36
@@ -1,6 +1,8 @@
1 #ifndef ODB_SOURCE_H
2 #define ODB_SOURCE_H
3
4 +#include "object.h"
5 +
6 enum odb_source_type {
7 /*
8 * The "unknown" type, which should never be in use. This type mostly
@@ -198,6 +200,24 @@ struct odb_source {
200 */
201 int (*freshen_object)(struct odb_source *source,
202 const struct object_id *oid);
203 +
204 + /*
205 + * This callback is expected to persist the given object into the
206 + * object source. In case the object already exists it shall be
207 + * freshened.
208 + *
209 + * The flags field is a combination of `WRITE_OBJECT` flags.
210 + *
211 + * The resulting object ID (and optionally the compatibility object ID)
212 + * shall be written into the out pointers. The callback is expected to
213 + * return 0 on success, a negative error code otherwise.
214 + */
215 + int (*write_object)(struct odb_source *source,
216 + const void *buf, unsigned long len,
217 + enum object_type type,
218 + struct object_id *oid,
219 + struct object_id *compat_oid,
220 + unsigned flags);
221 };
222
223 /*
@@ -320,4 +340,20 @@ static inline int odb_source_freshen_object(struct odb_source *source,
340 return source->freshen_object(source, oid);
341 }
342
343 +/*
344 + * Write an object into the object database source. Returns 0 on success, a
345 + * negative error code otherwise. Populates the given out pointers for the
346 + * object ID and the compatibility object ID, if non-NULL.
347 + */
348 +static inline int odb_source_write_object(struct odb_source *source,
349 + const void *buf, unsigned long len,
350 + enum object_type type,
351 + struct object_id *oid,
352 + struct object_id *compat_oid,
353 + unsigned flags)
354 +{
355 + return source->write_object(source, buf, len, type, oid,
356 + compat_oid, flags);
357 +}
358 +
359 #endif