]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb/source: make `begin_transaction()` function pluggable
authorPatrick Steinhardt <ps@pks.im>
Thu, 5 Mar 2026 14:19:57 +0000 (15:19 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 5 Mar 2026 19:45:17 +0000 (11:45 -0800)
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>
odb/source-files.c
odb/source.h

index c32cd67b2650258dfb448333be0779cc643bcfa3..14cb9adecadd99fcbbed7ad5601a5196e6787ff7 100644 (file)
@@ -122,6 +122,16 @@ 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_begin_transaction(struct odb_source *source,
+                                             struct odb_transaction **out)
+{
+       struct odb_transaction *tx = odb_transaction_files_begin(source);
+       if (!tx)
+               return -1;
+       *out = tx;
+       return 0;
+}
+
 static int odb_source_files_read_alternates(struct odb_source *source,
                                            struct strvec *out)
 {
@@ -213,6 +223,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.begin_transaction = odb_source_files_begin_transaction;
        files->base.read_alternates = odb_source_files_read_alternates;
        files->base.write_alternate = odb_source_files_write_alternate;
 
index ee540630d293d80bc972295551f2944ab763e951..caac5581495ecead5b14d95acfe95329a47c6418 100644 (file)
@@ -53,6 +53,7 @@ enum object_info_flags {
 struct object_id;
 struct object_info;
 struct odb_read_stream;
+struct odb_transaction;
 struct odb_write_stream;
 struct strvec;
 
@@ -233,6 +234,19 @@ struct odb_source {
                                   struct odb_write_stream *stream, size_t len,
                                   struct object_id *oid);
 
+       /*
+        * This callback is expected to create a new transaction that can be
+        * used to write objects to. The objects shall only be persisted into
+        * the object database when the transcation's commit function is
+        * called. Otherwise, the objects shall be discarded.
+        *
+        * Returns 0 on success, in which case the `*out` pointer will have
+        * been populated with the object database transaction. Returns a
+        * negative error code otherwise.
+        */
+       int (*begin_transaction)(struct odb_source *source,
+                                struct odb_transaction **out);
+
        /*
         * This callback is expected to read the list of alternate object
         * 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,
        return source->write_alternate(source, alternate);
 }
 
+/*
+ * Create a new transaction that can be used to write objects into a temporary
+ * staging area. The objects will only be persisted when the transaction is
+ * committed.
+ *
+ * Returns 0 on success, a negative error code otherwise.
+ */
+static inline int odb_source_begin_transaction(struct odb_source *source,
+                                              struct odb_transaction **out)
+{
+       return source->begin_transaction(source, out);
+}
+
 #endif