]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb: introduce `odb_write_object()`
authorPatrick Steinhardt <ps@pks.im>
Thu, 17 Jul 2025 04:56:35 +0000 (06:56 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 17 Jul 2025 05:16:15 +0000 (22:16 -0700)
We do not have a backend-agnostic way to write objects into an object
database. While there is `write_object_file()`, this function is rather
specific to the loose object format.

Introduce `odb_write_object()` to plug this gap. For now, this function
is a simple wrapper around `write_object_file()` and doesn't even use
the passed-in object database yet. This will change in subsequent
commits, where `write_object_file()` is converted so that it works on
top of an `odb_source`. `odb_write_object()` will then become
responsible for deciding which source an object shall be written to.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
21 files changed:
apply.c
builtin/checkout.c
builtin/merge-file.c
builtin/mktag.c
builtin/mktree.c
builtin/notes.c
builtin/receive-pack.c
builtin/replace.c
builtin/tag.c
builtin/unpack-objects.c
cache-tree.c
commit.c
match-trees.c
merge-ort.c
notes-cache.c
notes.c
object-file.c
object-file.h
odb.c
odb.h
read-cache.c

diff --git a/apply.c b/apply.c
index a6836692d0ce087519f4473daa04a3d45032a237..ffb9d9f76d62d721f69626f6d6e178d53a392c62 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -3621,7 +3621,7 @@ static int try_threeway(struct apply_state *state,
 
        /* Preimage the patch was prepared for */
        if (patch->is_new)
-               write_object_file("", 0, OBJ_BLOB, &pre_oid);
+               odb_write_object(the_repository->objects, "", 0, OBJ_BLOB, &pre_oid);
        else if (repo_get_oid(the_repository, patch->old_oid_prefix, &pre_oid) ||
                 read_blob_object(&buf, &pre_oid, patch->old_mode))
                return error(_("repository lacks the necessary blob to perform 3-way merge."));
@@ -3637,7 +3637,8 @@ static int try_threeway(struct apply_state *state,
                return -1;
        }
        /* post_oid is theirs */
-       write_object_file(tmp_image.buf.buf, tmp_image.buf.len, OBJ_BLOB, &post_oid);
+       odb_write_object(the_repository->objects, tmp_image.buf.buf,
+                        tmp_image.buf.len, OBJ_BLOB, &post_oid);
        image_clear(&tmp_image);
 
        /* our_oid is ours */
@@ -3650,7 +3651,8 @@ static int try_threeway(struct apply_state *state,
                        return error(_("cannot read the current contents of '%s'"),
                                     patch->old_name);
        }
-       write_object_file(tmp_image.buf.buf, tmp_image.buf.len, OBJ_BLOB, &our_oid);
+       odb_write_object(the_repository->objects, tmp_image.buf.buf,
+                        tmp_image.buf.len, OBJ_BLOB, &our_oid);
        image_clear(&tmp_image);
 
        /* in-core three-way merge between post and our using pre as base */
@@ -4360,7 +4362,8 @@ static int add_index_file(struct apply_state *state,
                        }
                        fill_stat_cache_info(state->repo->index, ce, &st);
                }
-               if (write_object_file(buf, size, OBJ_BLOB, &ce->oid) < 0) {
+               if (odb_write_object(the_repository->objects, buf, size,
+                                    OBJ_BLOB, &ce->oid) < 0) {
                        discard_cache_entry(ce);
                        return error(_("unable to create backing store "
                                       "for newly created file %s"), path);
index 0a90b86a72913f2fb381159c7940ef4fe5e84b36..f95eb64ffb3d83229d82985f373f6fb7ae814901 100644 (file)
@@ -320,7 +320,7 @@ static int checkout_merged(int pos, const struct checkout *state,
         * (it also writes the merge result to the object database even
         * when it may contain conflicts).
         */
-       if (write_object_file(result_buf.ptr, result_buf.size, OBJ_BLOB, &oid))
+       if (odb_write_object(the_repository->objects, result_buf.ptr, result_buf.size, OBJ_BLOB, &oid))
                die(_("Unable to add merge result for '%s'"), path);
        free(result_buf.ptr);
        ce = make_transient_cache_entry(mode, &oid, path, 2, ce_mem_pool);
index 9464f2756299ec253673980c766e01783d5edd89..b8b25a14e6dc5ecac380cf8411da7c8a10b40a65 100644 (file)
@@ -155,7 +155,8 @@ int cmd_merge_file(int argc,
                if (object_id && !to_stdout) {
                        struct object_id oid;
                        if (result.size) {
-                               if (write_object_file(result.ptr, result.size, OBJ_BLOB, &oid) < 0)
+                               if (odb_write_object(the_repository->objects, result.ptr,
+                                                    result.size, OBJ_BLOB, &oid) < 0)
                                        ret = error(_("Could not write object file"));
                        } else {
                                oidcpy(&oid, the_hash_algo->empty_blob);
index 27e649736cf48c29437d5c65cd5a079f586bcab5..12552bbb217d1fcd2d5ebc4552ceab997d8169fe 100644 (file)
@@ -106,7 +106,7 @@ int cmd_mktag(int argc,
        if (verify_object_in_tag(&tagged_oid, &tagged_type) < 0)
                die(_("tag on stdin did not refer to a valid object"));
 
-       if (write_object_file(buf.buf, buf.len, OBJ_TAG, &result) < 0)
+       if (odb_write_object(the_repository->objects, buf.buf, buf.len, OBJ_TAG, &result) < 0)
                die(_("unable to write tag file"));
 
        strbuf_release(&buf);
index 81df7f6099fa4e4c98290402948e42398fed5dfb..12772303f504f3a27a404d96fd6867a4fdecc068 100644 (file)
@@ -63,7 +63,7 @@ static void write_tree(struct object_id *oid)
                strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz);
        }
 
-       write_object_file(buf.buf, buf.len, OBJ_TREE, oid);
+       odb_write_object(the_repository->objects, buf.buf, buf.len, OBJ_TREE, oid);
        strbuf_release(&buf);
 }
 
index a9529b1696ae14174580795fbd71e5285b028eb5..a3580b4aa3dfa15cb197fa7277d6560a1bf456c7 100644 (file)
@@ -229,7 +229,8 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 
 static void write_note_data(struct note_data *d, struct object_id *oid)
 {
-       if (write_object_file(d->buf.buf, d->buf.len, OBJ_BLOB, oid)) {
+       if (odb_write_object(the_repository->objects, d->buf.buf,
+                            d->buf.len, OBJ_BLOB, oid)) {
                int status = die_message(_("unable to write note object"));
 
                if (d->edit_path)
index dd1d1446e756baed2a35d5a010118c695272c105..bd9baf81e566add603069a327fdd649fa07dad9b 100644 (file)
@@ -760,8 +760,8 @@ static void prepare_push_cert_sha1(struct child_process *proc)
                int bogs /* beginning_of_gpg_sig */;
 
                already_done = 1;
-               if (write_object_file(push_cert.buf, push_cert.len, OBJ_BLOB,
-                                     &push_cert_oid))
+               if (odb_write_object(the_repository->objects, push_cert.buf,
+                                    push_cert.len, OBJ_BLOB, &push_cert_oid))
                        oidclr(&push_cert_oid, the_repository->hash_algo);
 
                memset(&sigcheck, '\0', sizeof(sigcheck));
index 5ff2ab723cbd58eca7b9b263deff156fcee38639..7c46d05ec15b3ba5a4b8ce090de4156867273978 100644 (file)
@@ -488,7 +488,8 @@ static int create_graft(int argc, const char **argv, int force, int gentle)
                return -1;
        }
 
-       if (write_object_file(buf.buf, buf.len, OBJ_COMMIT, &new_oid)) {
+       if (odb_write_object(the_repository->objects, buf.buf,
+                            buf.len, OBJ_COMMIT, &new_oid)) {
                strbuf_release(&buf);
                return error(_("could not write replacement commit for: '%s'"),
                             old_ref);
index 46cbf892e34224ad2e8557ee2c20d55bb1e14a5a..8fbe9e7be04a68313ab93641da21669984cf7f39 100644 (file)
@@ -271,8 +271,8 @@ static int build_tag_object(struct strbuf *buf, int sign, struct object_id *resu
        struct object_id *compat_oid = NULL, compat_oid_buf;
        if (sign && do_sign(buf, &compat_oid, &compat_oid_buf) < 0)
                return error(_("unable to sign the tag"));
-       if (write_object_file_flags(buf->buf, buf->len, OBJ_TAG, result,
-                                   compat_oid, 0) < 0)
+       if (odb_write_object_ext(the_repository->objects, buf->buf,
+                                buf->len, OBJ_TAG, result, compat_oid, 0) < 0)
                return error(_("unable to write tag file"));
        return 0;
 }
index a69d59eb50c90251a98430c7b3d069a3b9d0a552..1a4fbef36f898a4872500579088c2df34adb4c3e 100644 (file)
@@ -204,8 +204,8 @@ static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf)
 {
        struct object_id oid;
 
-       if (write_object_file(obj_buf->buffer, obj_buf->size,
-                             obj->type, &oid) < 0)
+       if (odb_write_object(the_repository->objects, obj_buf->buffer, obj_buf->size,
+                            obj->type, &oid) < 0)
                die("failed to write object %s", oid_to_hex(&obj->oid));
        obj->flags |= FLAG_WRITTEN;
 }
@@ -272,16 +272,16 @@ static void write_object(unsigned nr, enum object_type type,
                         void *buf, unsigned long size)
 {
        if (!strict) {
-               if (write_object_file(buf, size, type,
-                                     &obj_list[nr].oid) < 0)
+               if (odb_write_object(the_repository->objects, buf, size, type,
+                                    &obj_list[nr].oid) < 0)
                        die("failed to write object");
                added_object(nr, type, buf, size);
                free(buf);
                obj_list[nr].obj = NULL;
        } else if (type == OBJ_BLOB) {
                struct blob *blob;
-               if (write_object_file(buf, size, type,
-                                     &obj_list[nr].oid) < 0)
+               if (odb_write_object(the_repository->objects, buf, size, type,
+                                    &obj_list[nr].oid) < 0)
                        die("failed to write object");
                added_object(nr, type, buf, size);
                free(buf);
index a4bc14ad15c8a077733630960b8f08cb40ae0c4a..66ef2becbe01a4bfe2d41de91d97389487c82f64 100644 (file)
@@ -456,9 +456,8 @@ static int update_one(struct cache_tree *it,
        } else if (dryrun) {
                hash_object_file(the_hash_algo, buffer.buf, buffer.len,
                                 OBJ_TREE, &it->oid);
-       } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE,
-                                          &it->oid, NULL, flags & WRITE_TREE_SILENT
-                                          ? WRITE_OBJECT_FILE_SILENT : 0)) {
+       } else if (odb_write_object_ext(the_repository->objects, buffer.buf, buffer.len, OBJ_TREE,
+                                       &it->oid, NULL, flags & WRITE_TREE_SILENT ? WRITE_OBJECT_SILENT : 0)) {
                strbuf_release(&buffer);
                return -1;
        }
index 15115125c3612c045b3be2f68a94aae4bad1fc4a..bcc9aea55f65e9d9393a82b7c363f30738acd659 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -1797,8 +1797,8 @@ int commit_tree_extended(const char *msg, size_t msg_len,
                compat_oid = &compat_oid_buf;
        }
 
-       result = write_object_file_flags(buffer.buf, buffer.len, OBJ_COMMIT,
-                                        ret, compat_oid, 0);
+       result = odb_write_object_ext(the_repository->objects, buffer.buf, buffer.len,
+                                     OBJ_COMMIT, ret, compat_oid, 0);
 out:
        free(parent_buf);
        strbuf_release(&buffer);
index 5a8a5c39b04ab9f0397ec36282d35ffb79b66dca..4216933d06b16386d68ca6006acf0df41e82b9ff 100644 (file)
@@ -246,7 +246,7 @@ static int splice_tree(struct repository *r,
                rewrite_with = oid2;
        }
        hashcpy(rewrite_here, rewrite_with->hash, r->hash_algo);
-       status = write_object_file(buf, sz, OBJ_TREE, result);
+       status = odb_write_object(r->objects, buf, sz, OBJ_TREE, result);
        free(buf);
        return status;
 }
index 473ff61e36e9f7bad64d24c91880a4829f1ee391..535ef3efc6fb3393da40cc41cdb3dff99a6e2856 100644 (file)
@@ -2216,8 +2216,8 @@ static int handle_content_merge(struct merge_options *opt,
                }
 
                if (!ret && record_object &&
-                   write_object_file(result_buf.ptr, result_buf.size,
-                                     OBJ_BLOB, &result->oid)) {
+                   odb_write_object(the_repository->objects, result_buf.ptr, result_buf.size,
+                                    OBJ_BLOB, &result->oid)) {
                        path_msg(opt, ERROR_OBJECT_WRITE_FAILED, 0,
                                 pathnames[0], pathnames[1], pathnames[2], NULL,
                                 _("error: unable to add %s to database"), path);
@@ -3772,7 +3772,8 @@ static int write_tree(struct object_id *result_oid,
        }
 
        /* Write this object file out, and record in result_oid */
-       if (write_object_file(buf.buf, buf.len, OBJ_TREE, result_oid))
+       if (odb_write_object(the_repository->objects, buf.buf,
+                            buf.len, OBJ_TREE, result_oid))
                ret = -1;
        strbuf_release(&buf);
        return ret;
index dd56feed6e8cfe339f9869723c56c0f9b396fcdd..bf5bb1f6c13a13f0e7d3348337525c966ddb2730 100644 (file)
@@ -98,7 +98,8 @@ int notes_cache_put(struct notes_cache *c, struct object_id *key_oid,
 {
        struct object_id value_oid;
 
-       if (write_object_file(data, size, OBJ_BLOB, &value_oid) < 0)
+       if (odb_write_object(the_repository->objects, data,
+                            size, OBJ_BLOB, &value_oid) < 0)
                return -1;
        return add_note(&c->tree, key_oid, &value_oid, NULL);
 }
diff --git a/notes.c b/notes.c
index 97b995f3f2da6f154337b4672af6f67438ae0f2a..7596c0df9a12b4c57a478b4f086be6998e8a717d 100644 (file)
--- a/notes.c
+++ b/notes.c
@@ -682,7 +682,8 @@ static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
                ret = tree_write_stack_finish_subtree(n);
                if (ret)
                        return ret;
-               ret = write_object_file(n->buf.buf, n->buf.len, OBJ_TREE, &s);
+               ret = odb_write_object(the_repository->objects, n->buf.buf,
+                                      n->buf.len, OBJ_TREE, &s);
                if (ret)
                        return ret;
                strbuf_release(&n->buf);
@@ -847,7 +848,8 @@ int combine_notes_concatenate(struct object_id *cur_oid,
        free(new_msg);
 
        /* create a new blob object from buf */
-       ret = write_object_file(buf, buf_len, OBJ_BLOB, cur_oid);
+       ret = odb_write_object(the_repository->objects, buf,
+                              buf_len, OBJ_BLOB, cur_oid);
        free(buf);
        return ret;
 }
@@ -927,7 +929,8 @@ int combine_notes_cat_sort_uniq(struct object_id *cur_oid,
                                 string_list_join_lines_helper, &buf))
                goto out;
 
-       ret = write_object_file(buf.buf, buf.len, OBJ_BLOB, cur_oid);
+       ret = odb_write_object(the_repository->objects, buf.buf,
+                              buf.len, OBJ_BLOB, cur_oid);
 
 out:
        strbuf_release(&buf);
@@ -1215,7 +1218,8 @@ int write_notes_tree(struct notes_tree *t, struct object_id *result)
        ret = for_each_note(t, flags, write_each_note, &cb_data) ||
              write_each_non_note_until(NULL, &cb_data) ||
              tree_write_stack_finish_subtree(&root) ||
-             write_object_file(root.buf.buf, root.buf.len, OBJ_TREE, result);
+             odb_write_object(the_repository->objects, root.buf.buf,
+                              root.buf.len, OBJ_TREE, result);
        strbuf_release(&root.buf);
        return ret;
 }
index a9248760a2682216c0ef53014fcdb2cb9f2aed59..84ece01337ec1f480ba93ae03655a9d632065c4c 100644 (file)
@@ -755,7 +755,7 @@ static int start_loose_object_common(struct strbuf *tmp_file,
 
        fd = create_tmpfile(tmp_file, filename);
        if (fd < 0) {
-               if (flags & WRITE_OBJECT_FILE_SILENT)
+               if (flags & WRITE_OBJECT_SILENT)
                        return -1;
                else if (errno == EACCES)
                        return error(_("insufficient permission for adding "
@@ -887,7 +887,7 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
                utb.actime = mtime;
                utb.modtime = mtime;
                if (utime(tmp_file.buf, &utb) < 0 &&
-                   !(flags & WRITE_OBJECT_FILE_SILENT))
+                   !(flags & WRITE_OBJECT_SILENT))
                        warning_errno(_("failed utime() on %s"), tmp_file.buf);
        }
 
@@ -1032,9 +1032,9 @@ cleanup:
        return err;
 }
 
-int write_object_file_flags(const void *buf, unsigned long len,
-                           enum object_type type, struct object_id *oid,
-                           struct object_id *compat_oid_in, unsigned flags)
+int write_object_file(const void *buf, unsigned long len,
+                     enum object_type type, struct object_id *oid,
+                     struct object_id *compat_oid_in, unsigned flags)
 {
        struct repository *repo = the_repository;
        const struct git_hash_algo *algo = repo->hash_algo;
@@ -1159,7 +1159,7 @@ static int index_mem(struct index_state *istate,
        }
 
        if (write_object)
-               ret = write_object_file(buf, size, type, oid);
+               ret = odb_write_object(istate->repo->objects, buf, size, type, oid);
        else
                hash_object_file(istate->repo->hash_algo, buf, size, type, oid);
 
@@ -1184,8 +1184,8 @@ static int index_stream_convert_blob(struct index_state *istate,
                                 get_conv_flags(flags));
 
        if (write_object)
-               ret = write_object_file(sbuf.buf, sbuf.len, OBJ_BLOB,
-                                       oid);
+               ret = odb_write_object(istate->repo->objects, sbuf.buf, sbuf.len, OBJ_BLOB,
+                                      oid);
        else
                hash_object_file(istate->repo->hash_algo, sbuf.buf, sbuf.len, OBJ_BLOB,
                                 oid);
@@ -1287,7 +1287,7 @@ int index_path(struct index_state *istate, struct object_id *oid,
                if (!(flags & INDEX_WRITE_OBJECT))
                        hash_object_file(istate->repo->hash_algo, sb.buf, sb.len,
                                         OBJ_BLOB, oid);
-               else if (write_object_file(sb.buf, sb.len, OBJ_BLOB, oid))
+               else if (odb_write_object(the_repository->objects, sb.buf, sb.len, OBJ_BLOB, oid))
                        rc = error(_("%s: failed to insert into database"), path);
                strbuf_release(&sb);
                break;
index 370139e0762f1ba0c96cff73f9c6d61475f96ab8..8ee24b7d8f307d9e8e2bae8231735ba96f3d00a9 100644 (file)
@@ -157,29 +157,9 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
 struct object_info;
 int parse_loose_header(const char *hdr, struct object_info *oi);
 
-enum {
-       /*
-        * By default, `write_object_file()` does not actually write
-        * anything into the object store, but only computes the object ID.
-        * This flag changes that so that the object will be written as a loose
-        * object and persisted.
-        */
-       WRITE_OBJECT_FILE_PERSIST = (1 << 0),
-
-       /*
-        * Do not print an error in case something gose wrong.
-        */
-       WRITE_OBJECT_FILE_SILENT = (1 << 1),
-};
-
-int write_object_file_flags(const void *buf, unsigned long len,
-                           enum object_type type, struct object_id *oid,
-                           struct object_id *compat_oid_in, unsigned flags);
-static inline int write_object_file(const void *buf, unsigned long len,
-                                   enum object_type type, struct object_id *oid)
-{
-       return write_object_file_flags(buf, len, type, oid, NULL, 0);
-}
+int write_object_file(const void *buf, unsigned long len,
+                     enum object_type type, struct object_id *oid,
+                     struct object_id *compat_oid_in, unsigned flags);
 
 struct input_stream {
        const void *(*read)(struct input_stream *, unsigned long *len);
diff --git a/odb.c b/odb.c
index 1f48a0448e398ac8577b4867a356269f65f673ea..519df2fa49786302fbcded434ec1427f8ab310d5 100644 (file)
--- a/odb.c
+++ b/odb.c
@@ -980,6 +980,16 @@ void odb_assert_oid_type(struct object_database *odb,
                    type_name(expect));
 }
 
+int odb_write_object_ext(struct object_database *odb UNUSED,
+                        const void *buf, unsigned long len,
+                        enum object_type type,
+                        struct object_id *oid,
+                        struct object_id *compat_oid,
+                        unsigned flags)
+{
+       return write_object_file(buf, len, type, oid, compat_oid, flags);
+}
+
 struct object_database *odb_new(struct repository *repo)
 {
        struct object_database *o = xmalloc(sizeof(*o));
diff --git a/odb.h b/odb.h
index e922f256802a0c93392bd97087fc80eec0908794..03422068888ad8bc9e891b4fe5fc2a65b8b1a26b 100644 (file)
--- a/odb.h
+++ b/odb.h
@@ -437,6 +437,44 @@ enum for_each_object_flags {
        FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS = (1<<4),
 };
 
+enum {
+       /*
+        * By default, `odb_write_object()` does not actually write anything
+        * into the object store, but only computes the object ID. This flag
+        * changes that so that the object will be written as a loose object
+        * and persisted.
+        */
+       WRITE_OBJECT_PERSIST = (1 << 0),
+
+       /*
+        * Do not print an error in case something goes wrong.
+        */
+       WRITE_OBJECT_SILENT = (1 << 1),
+};
+
+/*
+ * Write an object into the object database. The object is being written into
+ * the local alternate of the repository. If provided, the converted object ID
+ * as well as the compatibility object ID are written to the respective
+ * pointers.
+ *
+ * Returns 0 on success, a negative error code otherwise.
+ */
+int odb_write_object_ext(struct object_database *odb,
+                        const void *buf, unsigned long len,
+                        enum object_type type,
+                        struct object_id *oid,
+                        struct object_id *compat_oid,
+                        unsigned flags);
+
+static inline int odb_write_object(struct object_database *odb,
+                                  const void *buf, unsigned long len,
+                                  enum object_type type,
+                                  struct object_id *oid)
+{
+       return odb_write_object_ext(odb, buf, len, type, oid, NULL, 0);
+}
+
 /* Compatibility wrappers, to be removed once Git 2.51 has been released. */
 #include "repository.h"
 
index 531d87e790529fef78e3ed2bf633d3c0421d659a..be17ca7f5868974dfc5eac18eaf1e0a2af168b50 100644 (file)
@@ -690,7 +690,7 @@ static struct cache_entry *create_alias_ce(struct index_state *istate,
 void set_object_name_for_intent_to_add_entry(struct cache_entry *ce)
 {
        struct object_id oid;
-       if (write_object_file("", 0, OBJ_BLOB, &oid))
+       if (odb_write_object(the_repository->objects, "", 0, OBJ_BLOB, &oid))
                die(_("cannot create an empty blob in the object database"));
        oidcpy(&ce->oid, &oid);
 }