odb/source: make `write_object_stream()` 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
fc7fb0ef3575091bbc00d6bf8345c2da0c386052
3 files changed
+38
-1
odb.c
+1
-1
@@ -1013,7 +1013,7 @@ int odb_write_object_stream(struct object_database *odb,
1013
struct odb_write_stream *stream, size_t len,
1014
struct object_id *oid)
1015
{
1016
- return odb_source_loose_write_stream(odb->sources, stream, len, oid);
1016
+ return odb_source_write_object_stream(odb->sources, stream, len, oid);
1017
}
1018
1019
struct object_database *odb_new(struct repository *repo,
odb/source-files.c
+9
@@ -109,6 +109,14 @@ static int odb_source_files_write_object(struct odb_source *source,
109
oid, compat_oid, flags);
110
}
111
112
+static int odb_source_files_write_object_stream(struct odb_source *source,
113
+ struct odb_write_stream *stream,
114
+ size_t len,
115
+ struct object_id *oid)
116
+{
117
+ return odb_source_loose_write_stream(source, stream, len, oid);
118
+}
119
+
120
struct odb_source_files *odb_source_files_new(struct object_database *odb,
121
const char *path,
122
bool local)
@@ -128,6 +136,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
136
files->base.for_each_object = odb_source_files_for_each_object;
137
files->base.freshen_object = odb_source_files_freshen_object;
138
files->base.write_object = odb_source_files_write_object;
139
+ files->base.write_object_stream = odb_source_files_write_object_stream;
140
141
/*
142
* Ideally, we would only ever store absolute paths in the source. This
odb/source.h
+28
@@ -53,6 +53,7 @@ enum object_info_flags {
53
struct object_id;
54
struct object_info;
55
struct odb_read_stream;
56
+struct odb_write_stream;
57
58
/*
59
* A callback function that can be used to iterate through objects. If given,
@@ -218,6 +219,18 @@ struct odb_source {
219
struct object_id *oid,
220
struct object_id *compat_oid,
221
unsigned flags);
222
+
223
+ /*
224
+ * This callback is expected to persist the given object stream into
225
+ * the object source.
226
+ *
227
+ * The resulting object ID shall be written into the out pointer. The
228
+ * callback is expected to return 0 on success, a negative error code
229
+ * otherwise.
230
+ */
231
+ int (*write_object_stream)(struct odb_source *source,
232
+ struct odb_write_stream *stream, size_t len,
233
+ struct object_id *oid);
234
};
235
236
/*
@@ -356,4 +369,19 @@ static inline int odb_source_write_object(struct odb_source *source,
369
compat_oid, flags);
370
}
371
372
+/*
373
+ * Write an object into the object database source via a stream. The overall
374
+ * length of the object must be known in advance.
375
+ *
376
+ * Return 0 on success, a negative error code otherwise. Populates the given
377
+ * out pointer for the object ID.
378
+ */
379
+static inline int odb_source_write_object_stream(struct odb_source *source,
380
+ struct odb_write_stream *stream,
381
+ size_t len,
382
+ struct object_id *oid)
383
+{
384
+ return source->write_object_stream(source, stream, len, oid);
385
+}
386
+
387
#endif