object-file API: have write_object_file() take "enum object_type"

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>

Ævar Arnfjörð Bjarmason committed Feb 5, 2022 at 00:48 UTC c80d226a046170b1c8dd82ef72a27373ddd5880e
19 files changed +33 -33
apply.c
+4 -4
@@ -3589,7 +3589,7 @@ static int try_threeway(struct apply_state *state,
3589
3590 /* Preimage the patch was prepared for */
3591 if (patch->is_new)
3592 - write_object_file("", 0, blob_type, &pre_oid);
3592 + write_object_file("", 0, OBJ_BLOB, &pre_oid);
3593 else if (get_oid(patch->old_oid_prefix, &pre_oid) ||
3594 read_blob_object(&buf, &pre_oid, patch->old_mode))
3595 return error(_("repository lacks the necessary blob to perform 3-way merge."));
@@ -3605,7 +3605,7 @@ static int try_threeway(struct apply_state *state,
3605 return -1;
3606 }
3607 /* post_oid is theirs */
3608 - write_object_file(tmp_image.buf, tmp_image.len, blob_type, &post_oid);
3608 + write_object_file(tmp_image.buf, tmp_image.len, OBJ_BLOB, &post_oid);
3609 clear_image(&tmp_image);
3610
3611 /* our_oid is ours */
@@ -3618,7 +3618,7 @@ static int try_threeway(struct apply_state *state,
3618 return error(_("cannot read the current contents of '%s'"),
3619 patch->old_name);
3620 }
3621 - write_object_file(tmp_image.buf, tmp_image.len, blob_type, &our_oid);
3621 + write_object_file(tmp_image.buf, tmp_image.len, OBJ_BLOB, &our_oid);
3622 clear_image(&tmp_image);
3623
3624 /* 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,
4346 }
4347 fill_stat_cache_info(state->repo->index, ce, &st);
4348 }
4349 - if (write_object_file(buf, size, blob_type, &ce->oid) < 0) {
4349 + if (write_object_file(buf, size, OBJ_BLOB, &ce->oid) < 0) {
4350 discard_cache_entry(ce);
4351 return error(_("unable to create backing store "
4352 "for newly created file %s"), path);
builtin/checkout.c
+1 -1
@@ -298,7 +298,7 @@ static int checkout_merged(int pos, const struct checkout *state,
298 * (it also writes the merge result to the object database even
299 * when it may contain conflicts).
300 */
301 - if (write_object_file(result_buf.ptr, result_buf.size, blob_type, &oid))
301 + if (write_object_file(result_buf.ptr, result_buf.size, OBJ_BLOB, &oid))
302 die(_("Unable to add merge result for '%s'"), path);
303 free(result_buf.ptr);
304 ce = make_transient_cache_entry(mode, &oid, path, 2, ce_mem_pool);
builtin/mktag.c
+1 -1
@@ -100,7 +100,7 @@ int cmd_mktag(int argc, const char **argv, const char *prefix)
100 if (verify_object_in_tag(&tagged_oid, &tagged_type))
101 die(_("tag on stdin did not refer to a valid object"));
102
103 - if (write_object_file(buf.buf, buf.len, tag_type, &result) < 0)
103 + if (write_object_file(buf.buf, buf.len, OBJ_TAG, &result) < 0)
104 die(_("unable to write tag file"));
105
106 strbuf_release(&buf);
builtin/mktree.c
+1 -1
@@ -58,7 +58,7 @@ static void write_tree(struct object_id *oid)
58 strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz);
59 }
60
61 - write_object_file(buf.buf, buf.len, tree_type, oid);
61 + write_object_file(buf.buf, buf.len, OBJ_TREE, oid);
62 strbuf_release(&buf);
63 }
64
builtin/notes.c
+1 -1
@@ -199,7 +199,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
199
200 static void write_note_data(struct note_data *d, struct object_id *oid)
201 {
202 - if (write_object_file(d->buf.buf, d->buf.len, blob_type, oid)) {
202 + if (write_object_file(d->buf.buf, d->buf.len, OBJ_BLOB, oid)) {
203 int status = die_message(_("unable to write note object"));
204
205 if (d->edit_path)
builtin/receive-pack.c
+1 -1
@@ -762,7 +762,7 @@ static void prepare_push_cert_sha1(struct child_process *proc)
762 int bogs /* beginning_of_gpg_sig */;
763
764 already_done = 1;
765 - if (write_object_file(push_cert.buf, push_cert.len, "blob",
765 + if (write_object_file(push_cert.buf, push_cert.len, OBJ_BLOB,
766 &push_cert_oid))
767 oidclr(&push_cert_oid);
768
builtin/replace.c
+1 -1
@@ -474,7 +474,7 @@ static int create_graft(int argc, const char **argv, int force, int gentle)
474 return -1;
475 }
476
477 - if (write_object_file(buf.buf, buf.len, commit_type, &new_oid)) {
477 + if (write_object_file(buf.buf, buf.len, OBJ_COMMIT, &new_oid)) {
478 strbuf_release(&buf);
479 return error(_("could not write replacement commit for: '%s'"),
480 old_ref);
builtin/tag.c
+1 -1
@@ -238,7 +238,7 @@ static int build_tag_object(struct strbuf *buf, int sign, struct object_id *resu
238 {
239 if (sign && do_sign(buf) < 0)
240 return error(_("unable to sign the tag"));
241 - if (write_object_file(buf->buf, buf->len, tag_type, result) < 0)
241 + if (write_object_file(buf->buf, buf->len, OBJ_TAG, result) < 0)
242 return error(_("unable to write tag file"));
243 return 0;
244 }
builtin/unpack-objects.c
+3 -3
@@ -177,7 +177,7 @@ static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf)
177 struct object_id oid;
178
179 if (write_object_file(obj_buf->buffer, obj_buf->size,
180 - type_name(obj->type), &oid) < 0)
180 + obj->type, &oid) < 0)
181 die("failed to write object %s", oid_to_hex(&obj->oid));
182 obj->flags |= FLAG_WRITTEN;
183 }
@@ -243,7 +243,7 @@ static void write_object(unsigned nr, enum object_type type,
243 void *buf, unsigned long size)
244 {
245 if (!strict) {
246 - if (write_object_file(buf, size, type_name(type),
246 + if (write_object_file(buf, size, type,
247 &obj_list[nr].oid) < 0)
248 die("failed to write object");
249 added_object(nr, type, buf, size);
@@ -251,7 +251,7 @@ static void write_object(unsigned nr, enum object_type type,
251 obj_list[nr].obj = NULL;
252 } else if (type == OBJ_BLOB) {
253 struct blob *blob;
254 - if (write_object_file(buf, size, type_name(type),
254 + if (write_object_file(buf, size, type,
255 &obj_list[nr].oid) < 0)
256 die("failed to write object");
257 added_object(nr, type, buf, size);
cache-tree.c
+1 -1
@@ -440,7 +440,7 @@ static int update_one(struct cache_tree *it,
440 } else if (dryrun) {
441 hash_object_file(the_hash_algo, buffer.buf, buffer.len,
442 tree_type, &it->oid);
443 - } else if (write_object_file_flags(buffer.buf, buffer.len, tree_type,
443 + } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE,
444 &it->oid, flags & WRITE_TREE_SILENT
445 ? HASH_SILENT : 0)) {
446 strbuf_release(&buffer);
commit.c
+1 -1
@@ -1567,7 +1567,7 @@ int commit_tree_extended(const char *msg, size_t msg_len,
1567 goto out;
1568 }
1569
1570 - result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
1570 + result = write_object_file(buffer.buf, buffer.len, OBJ_COMMIT, ret);
1571 out:
1572 strbuf_release(&buffer);
1573 return result;
match-trees.c
+1 -1
@@ -235,7 +235,7 @@ static int splice_tree(const struct object_id *oid1, const char *prefix,
235 rewrite_with = oid2;
236 }
237 hashcpy(rewrite_here, rewrite_with->hash);
238 - status = write_object_file(buf, sz, tree_type, result);
238 + status = write_object_file(buf, sz, OBJ_TREE, result);
239 free(buf);
240 return status;
241 }
merge-ort.c
+2 -2
@@ -1888,7 +1888,7 @@ static int handle_content_merge(struct merge_options *opt,
1888
1889 if (!ret &&
1890 write_object_file(result_buf.ptr, result_buf.size,
1891 - blob_type, &result->oid))
1891 + OBJ_BLOB, &result->oid))
1892 ret = err(opt, _("Unable to add %s to database"),
1893 path);
1894
@@ -3343,7 +3343,7 @@ static void write_tree(struct object_id *result_oid,
3343 }
3344
3345 /* Write this object file out, and record in result_oid */
3346 - write_object_file(buf.buf, buf.len, tree_type, result_oid);
3346 + write_object_file(buf.buf, buf.len, OBJ_TREE, result_oid);
3347 strbuf_release(&buf);
3348 }
3349
merge-recursive.c
+1 -1
@@ -1373,7 +1373,7 @@ static int merge_mode_and_contents(struct merge_options *opt,
1373
1374 if (!ret &&
1375 write_object_file(result_buf.ptr, result_buf.size,
1376 - blob_type, &result->blob.oid))
1376 + OBJ_BLOB, &result->blob.oid))
1377 ret = err(opt, _("Unable to add %s to database"),
1378 a->path);
1379
notes-cache.c
+1 -1
@@ -92,7 +92,7 @@ int notes_cache_put(struct notes_cache *c, struct object_id *key_oid,
92 {
93 struct object_id value_oid;
94
95 - if (write_object_file(data, size, "blob", &value_oid) < 0)
95 + if (write_object_file(data, size, OBJ_BLOB, &value_oid) < 0)
96 return -1;
97 return add_note(&c->tree, key_oid, &value_oid, NULL);
98 }
notes.c
+4 -4
@@ -675,7 +675,7 @@ static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
675 ret = tree_write_stack_finish_subtree(n);
676 if (ret)
677 return ret;
678 - ret = write_object_file(n->buf.buf, n->buf.len, tree_type, &s);
678 + ret = write_object_file(n->buf.buf, n->buf.len, OBJ_TREE, &s);
679 if (ret)
680 return ret;
681 strbuf_release(&n->buf);
@@ -836,7 +836,7 @@ int combine_notes_concatenate(struct object_id *cur_oid,
836 free(new_msg);
837
838 /* create a new blob object from buf */
839 - ret = write_object_file(buf, buf_len, blob_type, cur_oid);
839 + ret = write_object_file(buf, buf_len, OBJ_BLOB, cur_oid);
840 free(buf);
841 return ret;
842 }
@@ -916,7 +916,7 @@ int combine_notes_cat_sort_uniq(struct object_id *cur_oid,
916 string_list_join_lines_helper, &buf))
917 goto out;
918
919 - ret = write_object_file(buf.buf, buf.len, blob_type, cur_oid);
919 + ret = write_object_file(buf.buf, buf.len, OBJ_BLOB, cur_oid);
920
921 out:
922 strbuf_release(&buf);
@@ -1192,7 +1192,7 @@ int write_notes_tree(struct notes_tree *t, struct object_id *result)
1192 ret = for_each_note(t, flags, write_each_note, &cb_data) ||
1193 write_each_non_note_until(NULL, &cb_data) ||
1194 tree_write_stack_finish_subtree(&root) ||
1195 - write_object_file(root.buf.buf, root.buf.len, tree_type, result);
1195 + write_object_file(root.buf.buf, root.buf.len, OBJ_TREE, result);
1196 strbuf_release(&root.buf);
1197 return ret;
1198 }
object-file.c
+5 -5
@@ -2014,7 +2014,7 @@ static int freshen_packed_object(const struct object_id *oid)
2014 }
2015
2016 int write_object_file_flags(const void *buf, unsigned long len,
2017 - const char *type, struct object_id *oid,
2017 + enum object_type type, struct object_id *oid,
2018 unsigned flags)
2019 {
2020 char hdr[MAX_HEADER_LEN];
@@ -2023,7 +2023,7 @@ int write_object_file_flags(const void *buf, unsigned long len,
2023 /* Normally if we have it in the pack then we do not bother writing
2024 * it out into .git/objects/??/?{38} file.
2025 */
2026 - write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr,
2026 + write_object_file_prepare(the_hash_algo, buf, len, type_name(type), oid, hdr,
2027 &hdrlen);
2028 if (freshen_packed_object(oid) || freshen_loose_object(oid))
2029 return 0;
@@ -2162,7 +2162,7 @@ static int index_mem(struct index_state *istate,
2162 }
2163
2164 if (write_object)
2165 - ret = write_object_file(buf, size, type_name(type), oid);
2165 + ret = write_object_file(buf, size, type, oid);
2166 else
2167 hash_object_file(the_hash_algo, buf, size, type_name(type),
2168 oid);
@@ -2189,7 +2189,7 @@ static int index_stream_convert_blob(struct index_state *istate,
2189 get_conv_flags(flags));
2190
2191 if (write_object)
2192 - ret = write_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB),
2192 + ret = write_object_file(sbuf.buf, sbuf.len, OBJ_BLOB,
2193 oid);
2194 else
2195 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,
2313 if (!(flags & HASH_WRITE_OBJECT))
2314 hash_object_file(the_hash_algo, sb.buf, sb.len,
2315 blob_type, oid);
2316 - else if (write_object_file(sb.buf, sb.len, blob_type, oid))
2316 + else if (write_object_file(sb.buf, sb.len, OBJ_BLOB, oid))
2317 rc = error(_("%s: failed to insert into database"), path);
2318 strbuf_release(&sb);
2319 break;
object-store.h
+2 -2
@@ -250,10 +250,10 @@ void hash_object_file(const struct git_hash_algo *algo, const void *buf,
250 struct object_id *oid);
251
252 int write_object_file_flags(const void *buf, unsigned long len,
253 - const char *type, struct object_id *oid,
253 + enum object_type type, struct object_id *oid,
254 unsigned flags);
255 static inline int write_object_file(const void *buf, unsigned long len,
256 - const char *type, struct object_id *oid)
256 + enum object_type type, struct object_id *oid)
257 {
258 return write_object_file_flags(buf, len, type, oid, 0);
259 }
read-cache.c
+1 -1
@@ -735,7 +735,7 @@ static struct cache_entry *create_alias_ce(struct index_state *istate,
735 void set_object_name_for_intent_to_add_entry(struct cache_entry *ce)
736 {
737 struct object_id oid;
738 - if (write_object_file("", 0, blob_type, &oid))
738 + if (write_object_file("", 0, OBJ_BLOB, &oid))
739 die(_("cannot create an empty blob in the object database"));
740 oidcpy(&ce->oid, &oid);
741 }