]> git.ipfire.org Git - thirdparty/git.git/commitdiff
object-file API: have write_object_file() take "enum object_type"
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Fri, 4 Feb 2022 23:48:26 +0000 (00:48 +0100)
committerJunio C Hamano <gitster@pobox.com>
Sat, 26 Feb 2022 01:16:31 +0000 (17:16 -0800)
Change the write_object_file() function to take an "enum object_type"
instead of a "const char *type". Its callers either passed
{commit,tree,blob,tag}_type and can pass the corresponding OBJ_* type
instead, or were hardcoding strings like "blob".

This avoids the back & forth fragility where the callers of
write_object_file() would have the enum type, and convert it
themselves via type_name(). We do have to now do that conversion
ourselves before calling write_object_file_prepare(), but those
codepaths will be similarly adjusted in subsequent commits.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
19 files changed:
apply.c
builtin/checkout.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
merge-recursive.c
notes-cache.c
notes.c
object-file.c
object-store.h
read-cache.c

diff --git a/apply.c b/apply.c
index 7ffadc3b17a314095e213ad31ceda83a9915afb2..e82a7c30f01c5aa297ec3ccc3f684d3fc165cc22 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -3589,7 +3589,7 @@ static int try_threeway(struct apply_state *state,
 
        /* Preimage the patch was prepared for */
        if (patch->is_new)
-               write_object_file("", 0, blob_type, &pre_oid);
+               write_object_file("", 0, OBJ_BLOB, &pre_oid);
        else if (get_oid(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."));
@@ -3605,7 +3605,7 @@ static int try_threeway(struct apply_state *state,
                return -1;
        }
        /* post_oid is theirs */
-       write_object_file(tmp_image.buf, tmp_image.len, blob_type, &post_oid);
+       write_object_file(tmp_image.buf, tmp_image.len, OBJ_BLOB, &post_oid);
        clear_image(&tmp_image);
 
        /* our_oid is ours */
@@ -3618,7 +3618,7 @@ 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, tmp_image.len, blob_type, &our_oid);
+       write_object_file(tmp_image.buf, tmp_image.len, OBJ_BLOB, &our_oid);
        clear_image(&tmp_image);
 
        /* in-core three-way merge between post and our using pre as base */
@@ -4346,7 +4346,7 @@ static int add_index_file(struct apply_state *state,
                        }
                        fill_stat_cache_info(state->repo->index, ce, &st);
                }
-               if (write_object_file(buf, size, blob_type, &ce->oid) < 0) {
+               if (write_object_file(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 cc804ba8e1e6cdb8748eabc7e91575ac2ee53a4a..24cc235939fd85f24789f06c52ce53860cdc8bb1 100644 (file)
@@ -298,7 +298,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, blob_type, &oid))
+       if (write_object_file(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 3b2dbbb37e6fe1afc11af79aae774ca526496413..96a3686af531d4d37ea2789e7ff99e31cb083eb1 100644 (file)
@@ -100,7 +100,7 @@ int cmd_mktag(int argc, const char **argv, const char *prefix)
        if (verify_object_in_tag(&tagged_oid, &tagged_type))
                die(_("tag on stdin did not refer to a valid object"));
 
-       if (write_object_file(buf.buf, buf.len, tag_type, &result) < 0)
+       if (write_object_file(buf.buf, buf.len, OBJ_TAG, &result) < 0)
                die(_("unable to write tag file"));
 
        strbuf_release(&buf);
index ae78ca1c02981a6d846027d7ce01484e5e3a1851..a1213aaa9619b79cea0c8baa90659668e73a438c 100644 (file)
@@ -58,7 +58,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, tree_type, oid);
+       write_object_file(buf.buf, buf.len, OBJ_TREE, oid);
        strbuf_release(&buf);
 }
 
index 05d60483e8261d86a1295512c46a7e8cebe42469..4cc84dfa7686d34ca3ec53f0819616544a235a55 100644 (file)
@@ -199,7 +199,7 @@ 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, blob_type, oid)) {
+       if (write_object_file(d->buf.buf, d->buf.len, OBJ_BLOB, oid)) {
                int status = die_message(_("unable to write note object"));
 
                if (d->edit_path)
index 9f4a0b816cf9b6acd077a10a728a83e4048b9b5e..231d576927b493241d7c6ab48a85c05efa87779e 100644 (file)
@@ -762,7 +762,7 @@ 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, "blob",
+               if (write_object_file(push_cert.buf, push_cert.len, OBJ_BLOB,
                                      &push_cert_oid))
                        oidclr(&push_cert_oid);
 
index 6ff1734d5879d36c994d0ab4b82b95e60a348750..995ca683302a1539028cfde5ae7a543ff206f638 100644 (file)
@@ -474,7 +474,7 @@ static int create_graft(int argc, const char **argv, int force, int gentle)
                return -1;
        }
 
-       if (write_object_file(buf.buf, buf.len, commit_type, &new_oid)) {
+       if (write_object_file(buf.buf, buf.len, OBJ_COMMIT, &new_oid)) {
                strbuf_release(&buf);
                return error(_("could not write replacement commit for: '%s'"),
                             old_ref);
index 134b3f1edf06246aed1eed1e5890a27fff22a3bf..31e6b155c04084fc5a61065f6bf5d8360bb542a6 100644 (file)
@@ -238,7 +238,7 @@ static int build_tag_object(struct strbuf *buf, int sign, struct object_id *resu
 {
        if (sign && do_sign(buf) < 0)
                return error(_("unable to sign the tag"));
-       if (write_object_file(buf->buf, buf->len, tag_type, result) < 0)
+       if (write_object_file(buf->buf, buf->len, OBJ_TAG, result) < 0)
                return error(_("unable to write tag file"));
        return 0;
 }
index 4a9466295ba10de5752cca92fa08b1a28461777b..808041551fdf58e07c4af6837ff0bce2bad10022 100644 (file)
@@ -177,7 +177,7 @@ 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,
-                             type_name(obj->type), &oid) < 0)
+                             obj->type, &oid) < 0)
                die("failed to write object %s", oid_to_hex(&obj->oid));
        obj->flags |= FLAG_WRITTEN;
 }
@@ -243,7 +243,7 @@ static void write_object(unsigned nr, enum object_type type,
                         void *buf, unsigned long size)
 {
        if (!strict) {
-               if (write_object_file(buf, size, type_name(type),
+               if (write_object_file(buf, size, type,
                                      &obj_list[nr].oid) < 0)
                        die("failed to write object");
                added_object(nr, type, buf, size);
@@ -251,7 +251,7 @@ static void write_object(unsigned nr, enum object_type type,
                obj_list[nr].obj = NULL;
        } else if (type == OBJ_BLOB) {
                struct blob *blob;
-               if (write_object_file(buf, size, type_name(type),
+               if (write_object_file(buf, size, type,
                                      &obj_list[nr].oid) < 0)
                        die("failed to write object");
                added_object(nr, type, buf, size);
index 65ca99336136f1f50695f06c6b404859b13cc0a3..fdbbd191d2261b39e05171c6c804984ea6b04e28 100644 (file)
@@ -440,7 +440,7 @@ static int update_one(struct cache_tree *it,
        } else if (dryrun) {
                hash_object_file(the_hash_algo, buffer.buf, buffer.len,
                                 tree_type, &it->oid);
-       } else if (write_object_file_flags(buffer.buf, buffer.len, tree_type,
+       } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE,
                                           &it->oid, flags & WRITE_TREE_SILENT
                                           ? HASH_SILENT : 0)) {
                strbuf_release(&buffer);
index a348f085b2b853d2f14d6848f3ff57edf85601b8..6cc0ba9c11b4a28b3dbd4cd707053eeb0756b22d 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -1567,7 +1567,7 @@ int commit_tree_extended(const char *msg, size_t msg_len,
                goto out;
        }
 
-       result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
+       result = write_object_file(buffer.buf, buffer.len, OBJ_COMMIT, ret);
 out:
        strbuf_release(&buffer);
        return result;
index df413989fa85cb007f3c7d88375283b6243687ca..49398e599fe3afdc88f481608fb58b0887b019e3 100644 (file)
@@ -235,7 +235,7 @@ static int splice_tree(const struct object_id *oid1, const char *prefix,
                rewrite_with = oid2;
        }
        hashcpy(rewrite_here, rewrite_with->hash);
-       status = write_object_file(buf, sz, tree_type, result);
+       status = write_object_file(buf, sz, OBJ_TREE, result);
        free(buf);
        return status;
 }
index c319797021938a9df635c8db146ac6555331c7db..d4ba2955f11548c8bc12a8428cd692c0f85afdd9 100644 (file)
@@ -1888,7 +1888,7 @@ static int handle_content_merge(struct merge_options *opt,
 
                if (!ret &&
                    write_object_file(result_buf.ptr, result_buf.size,
-                                     blob_type, &result->oid))
+                                     OBJ_BLOB, &result->oid))
                        ret = err(opt, _("Unable to add %s to database"),
                                  path);
 
@@ -3343,7 +3343,7 @@ static void write_tree(struct object_id *result_oid,
        }
 
        /* Write this object file out, and record in result_oid */
-       write_object_file(buf.buf, buf.len, tree_type, result_oid);
+       write_object_file(buf.buf, buf.len, OBJ_TREE, result_oid);
        strbuf_release(&buf);
 }
 
index d9457797dbb73bfed720ac9ca0b9bcf56575c62a..e0cbbf2d02231d0c44fde42c7e13d57c810560b5 100644 (file)
@@ -1373,7 +1373,7 @@ static int merge_mode_and_contents(struct merge_options *opt,
 
                        if (!ret &&
                            write_object_file(result_buf.ptr, result_buf.size,
-                                             blob_type, &result->blob.oid))
+                                             OBJ_BLOB, &result->blob.oid))
                                ret = err(opt, _("Unable to add %s to database"),
                                          a->path);
 
index 2473314d686858493c6730e895e7106e44a462f9..9dfd251a8151d4205297b5c27064a01acf448f69 100644 (file)
@@ -92,7 +92,7 @@ int notes_cache_put(struct notes_cache *c, struct object_id *key_oid,
 {
        struct object_id value_oid;
 
-       if (write_object_file(data, size, "blob", &value_oid) < 0)
+       if (write_object_file(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 f87dac40684030a70566a618ee3dbcedd97bc641..7452e71cc8dd289c7ace9361a6c6e090b8b113f9 100644 (file)
--- a/notes.c
+++ b/notes.c
@@ -675,7 +675,7 @@ 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, tree_type, &s);
+               ret = write_object_file(n->buf.buf, n->buf.len, OBJ_TREE, &s);
                if (ret)
                        return ret;
                strbuf_release(&n->buf);
@@ -836,7 +836,7 @@ 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, blob_type, cur_oid);
+       ret = write_object_file(buf, buf_len, OBJ_BLOB, cur_oid);
        free(buf);
        return ret;
 }
@@ -916,7 +916,7 @@ 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, blob_type, cur_oid);
+       ret = write_object_file(buf.buf, buf.len, OBJ_BLOB, cur_oid);
 
 out:
        strbuf_release(&buf);
@@ -1192,7 +1192,7 @@ 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, tree_type, result);
+             write_object_file(root.buf.buf, root.buf.len, OBJ_TREE, result);
        strbuf_release(&root.buf);
        return ret;
 }
index 3fcd46cf9ed8762859f676d24365b11dd9c078c7..59eb793e0ac59b2f8c4a1aaaa361212db661b9ff 100644 (file)
@@ -2014,7 +2014,7 @@ static int freshen_packed_object(const struct object_id *oid)
 }
 
 int write_object_file_flags(const void *buf, unsigned long len,
-                           const char *type, struct object_id *oid,
+                           enum object_type type, struct object_id *oid,
                            unsigned flags)
 {
        char hdr[MAX_HEADER_LEN];
@@ -2023,7 +2023,7 @@ int write_object_file_flags(const void *buf, unsigned long len,
        /* Normally if we have it in the pack then we do not bother writing
         * it out into .git/objects/??/?{38} file.
         */
-       write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr,
+       write_object_file_prepare(the_hash_algo, buf, len, type_name(type), oid, hdr,
                                  &hdrlen);
        if (freshen_packed_object(oid) || freshen_loose_object(oid))
                return 0;
@@ -2162,7 +2162,7 @@ static int index_mem(struct index_state *istate,
        }
 
        if (write_object)
-               ret = write_object_file(buf, size, type_name(type), oid);
+               ret = write_object_file(buf, size, type, oid);
        else
                hash_object_file(the_hash_algo, buf, size, type_name(type),
                                 oid);
@@ -2189,7 +2189,7 @@ 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, type_name(OBJ_BLOB),
+               ret = write_object_file(sbuf.buf, sbuf.len, OBJ_BLOB,
                                        oid);
        else
                hash_object_file(the_hash_algo, sbuf.buf, sbuf.len,
@@ -2313,7 +2313,7 @@ int index_path(struct index_state *istate, struct object_id *oid,
                if (!(flags & HASH_WRITE_OBJECT))
                        hash_object_file(the_hash_algo, sb.buf, sb.len,
                                         blob_type, oid);
-               else if (write_object_file(sb.buf, sb.len, blob_type, oid))
+               else if (write_object_file(sb.buf, sb.len, OBJ_BLOB, oid))
                        rc = error(_("%s: failed to insert into database"), path);
                strbuf_release(&sb);
                break;
index 8b4413d0ce70ff35dc71715ffbe1e45d2f3a680f..eab1e2a967e0874d64a42f025da24d9a9b473c59 100644 (file)
@@ -250,10 +250,10 @@ void hash_object_file(const struct git_hash_algo *algo, const void *buf,
                      struct object_id *oid);
 
 int write_object_file_flags(const void *buf, unsigned long len,
-                           const char *type, struct object_id *oid,
+                           enum object_type type, struct object_id *oid,
                            unsigned flags);
 static inline int write_object_file(const void *buf, unsigned long len,
-                                   const char *type, struct object_id *oid)
+                                   enum object_type type, struct object_id *oid)
 {
        return write_object_file_flags(buf, len, type, oid, 0);
 }
index cbe73f14e5e7efc63b20e80a2702fb5c0dea9a5d..b8ae30a0e71f173e79eab1d13a3578ff2f8fea60 100644 (file)
@@ -735,7 +735,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, blob_type, &oid))
+       if (write_object_file("", 0, OBJ_BLOB, &oid))
                die(_("cannot create an empty blob in the object database"));
        oidcpy(&ce->oid, &oid);
 }