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>
This commit is contained in:
Ævar Arnfjörð Bjarmason 2022-02-05 00:48:26 +01:00 коммит произвёл Junio C Hamano
Родитель b04cdea46c
Коммит c80d226a04
19 изменённых файлов: 33 добавлений и 33 удалений

Просмотреть файл

@ -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);

Просмотреть файл

@ -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);

Просмотреть файл

@ -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);

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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)

Просмотреть файл

@ -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);

Просмотреть файл

@ -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);

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -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);

Просмотреть файл

@ -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);

Просмотреть файл

@ -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;

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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);

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -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;

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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);
}