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

diff --git a/odb.c b/odb.c
index c9f42c5afd0b4f8c03a260a3cf3f7af9ce872dbe..5eb60063dcc6f3437ad8d8b5f0e3e6563efdb2cc 100644 (file)
--- a/odb.c
+++ b/odb.c
@@ -1005,8 +1005,8 @@ int odb_write_object_ext(struct object_database *odb,
                         struct object_id *compat_oid,
                         unsigned flags)
 {
-       return odb_source_loose_write_object(odb->sources, buf, len, type,
-                                            oid, compat_oid, flags);
+       return odb_source_write_object(odb->sources, buf, len, type,
+                                      oid, compat_oid, flags);
 }
 
 int odb_write_object_stream(struct object_database *odb,
index a6447909e01bbb3baec6218c4c6fb40eb951dd90..67c2aff659c47efe3abb0271267e888f4aa2bce5 100644 (file)
@@ -98,6 +98,17 @@ static int odb_source_files_freshen_object(struct odb_source *source,
        return 0;
 }
 
+static int odb_source_files_write_object(struct odb_source *source,
+                                        const void *buf, unsigned long len,
+                                        enum object_type type,
+                                        struct object_id *oid,
+                                        struct object_id *compat_oid,
+                                        unsigned flags)
+{
+       return odb_source_loose_write_object(source, buf, len, type,
+                                            oid, compat_oid, flags);
+}
+
 struct odb_source_files *odb_source_files_new(struct object_database *odb,
                                              const char *path,
                                              bool local)
@@ -116,6 +127,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
        files->base.read_object_stream = odb_source_files_read_object_stream;
        files->base.for_each_object = odb_source_files_for_each_object;
        files->base.freshen_object = odb_source_files_freshen_object;
+       files->base.write_object = odb_source_files_write_object;
 
        /*
         * Ideally, we would only ever store absolute paths in the source. This
index 7f2ecf420bd44c81313a245dd781337431b787c5..c959e962f6cd894bd30c28c39f60acf666965c65 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef ODB_SOURCE_H
 #define ODB_SOURCE_H
 
+#include "object.h"
+
 enum odb_source_type {
        /*
         * The "unknown" type, which should never be in use. This type mostly
@@ -198,6 +200,24 @@ struct odb_source {
         */
        int (*freshen_object)(struct odb_source *source,
                              const struct object_id *oid);
+
+       /*
+        * This callback is expected to persist the given object into the
+        * object source. In case the object already exists it shall be
+        * freshened.
+        *
+        * The flags field is a combination of `WRITE_OBJECT` flags.
+        *
+        * The resulting object ID (and optionally the compatibility object ID)
+        * shall be written into the out pointers. The callback is expected to
+        * return 0 on success, a negative error code otherwise.
+        */
+       int (*write_object)(struct odb_source *source,
+                           const void *buf, unsigned long len,
+                           enum object_type type,
+                           struct object_id *oid,
+                           struct object_id *compat_oid,
+                           unsigned flags);
 };
 
 /*
@@ -320,4 +340,20 @@ static inline int odb_source_freshen_object(struct odb_source *source,
        return source->freshen_object(source, oid);
 }
 
+/*
+ * Write an object into the object database source. Returns 0 on success, a
+ * negative error code otherwise. Populates the given out pointers for the
+ * object ID and the compatibility object ID, if non-NULL.
+ */
+static inline int odb_source_write_object(struct odb_source *source,
+                                         const void *buf, unsigned long len,
+                                         enum object_type type,
+                                         struct object_id *oid,
+                                         struct object_id *compat_oid,
+                                         unsigned flags)
+{
+       return source->write_object(source, buf, len, type, oid,
+                                   compat_oid, flags);
+}
+
 #endif