odb/source: make `begin_transaction()` 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 d6fc6fe6f8b74e663d6013f830b535f50bfc1414
2 files changed +38
odb/source-files.c
+11
@@ -122,6 +122,16 @@ static int odb_source_files_write_object_stream(struct odb_source *source,
122 return odb_source_loose_write_stream(source, stream, len, oid);
123 }
124
125 +static int odb_source_files_begin_transaction(struct odb_source *source,
126 + struct odb_transaction **out)
127 +{
128 + struct odb_transaction *tx = odb_transaction_files_begin(source);
129 + if (!tx)
130 + return -1;
131 + *out = tx;
132 + return 0;
133 +}
134 +
135 static int odb_source_files_read_alternates(struct odb_source *source,
136 struct strvec *out)
137 {
@@ -213,6 +223,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
223 files->base.freshen_object = odb_source_files_freshen_object;
224 files->base.write_object = odb_source_files_write_object;
225 files->base.write_object_stream = odb_source_files_write_object_stream;
226 + files->base.begin_transaction = odb_source_files_begin_transaction;
227 files->base.read_alternates = odb_source_files_read_alternates;
228 files->base.write_alternate = odb_source_files_write_alternate;
229
odb/source.h
+27
@@ -53,6 +53,7 @@ enum object_info_flags {
53 struct object_id;
54 struct object_info;
55 struct odb_read_stream;
56 +struct odb_transaction;
57 struct odb_write_stream;
58 struct strvec;
59
@@ -233,6 +234,19 @@ struct odb_source {
234 struct odb_write_stream *stream, size_t len,
235 struct object_id *oid);
236
237 + /*
238 + * This callback is expected to create a new transaction that can be
239 + * used to write objects to. The objects shall only be persisted into
240 + * the object database when the transcation's commit function is
241 + * called. Otherwise, the objects shall be discarded.
242 + *
243 + * Returns 0 on success, in which case the `*out` pointer will have
244 + * been populated with the object database transaction. Returns a
245 + * negative error code otherwise.
246 + */
247 + int (*begin_transaction)(struct odb_source *source,
248 + struct odb_transaction **out);
249 +
250 /*
251 * This callback is expected to read the list of alternate object
252 * database sources connected to it and write them into the `strvec`.
@@ -438,4 +452,17 @@ static inline int odb_source_write_alternate(struct odb_source *source,
452 return source->write_alternate(source, alternate);
453 }
454
455 +/*
456 + * Create a new transaction that can be used to write objects into a temporary
457 + * staging area. The objects will only be persisted when the transaction is
458 + * committed.
459 + *
460 + * Returns 0 on success, a negative error code otherwise.
461 + */
462 +static inline int odb_source_begin_transaction(struct odb_source *source,
463 + struct odb_transaction **out)
464 +{
465 + return source->begin_transaction(source, out);
466 +}
467 +
468 #endif