Skip to content

Commit c80d226

Browse files
avargitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b04cdea commit c80d226

19 files changed

+33
-33
lines changed

apply.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3589,7 +3589,7 @@ static int try_threeway(struct apply_state *state,
35893589

35903590
/* Preimage the patch was prepared for */
35913591
if (patch->is_new)
3592-
write_object_file("", 0, blob_type, &pre_oid);
3592+
write_object_file("", 0, OBJ_BLOB, &pre_oid);
35933593
else if (get_oid(patch->old_oid_prefix, &pre_oid) ||
35943594
read_blob_object(&buf, &pre_oid, patch->old_mode))
35953595
return error(_("repository lacks the necessary blob to perform 3-way merge."));
@@ -3605,7 +3605,7 @@ static int try_threeway(struct apply_state *state,
36053605
return -1;
36063606
}
36073607
/* 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);
36093609
clear_image(&tmp_image);
36103610

36113611
/* our_oid is ours */
@@ -3618,7 +3618,7 @@ static int try_threeway(struct apply_state *state,
36183618
return error(_("cannot read the current contents of '%s'"),
36193619
patch->old_name);
36203620
}
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);
36223622
clear_image(&tmp_image);
36233623

36243624
/* 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,
43464346
}
43474347
fill_stat_cache_info(state->repo->index, ce, &st);
43484348
}
4349-
if (write_object_file(buf, size, blob_type, &ce->oid) < 0) {
4349+
if (write_object_file(buf, size, OBJ_BLOB, &ce->oid) < 0) {
43504350
discard_cache_entry(ce);
43514351
return error(_("unable to create backing store "
43524352
"for newly created file %s"), path);

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ static int checkout_merged(int pos, const struct checkout *state,
298298
* (it also writes the merge result to the object database even
299299
* when it may contain conflicts).
300300
*/
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))
302302
die(_("Unable to add merge result for '%s'"), path);
303303
free(result_buf.ptr);
304304
ce = make_transient_cache_entry(mode, &oid, path, 2, ce_mem_pool);

builtin/mktag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ int cmd_mktag(int argc, const char **argv, const char *prefix)
100100
if (verify_object_in_tag(&tagged_oid, &tagged_type))
101101
die(_("tag on stdin did not refer to a valid object"));
102102

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)
104104
die(_("unable to write tag file"));
105105

106106
strbuf_release(&buf);

builtin/mktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static void write_tree(struct object_id *oid)
5858
strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz);
5959
}
6060

61-
write_object_file(buf.buf, buf.len, tree_type, oid);
61+
write_object_file(buf.buf, buf.len, OBJ_TREE, oid);
6262
strbuf_release(&buf);
6363
}
6464

builtin/notes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
199199

200200
static void write_note_data(struct note_data *d, struct object_id *oid)
201201
{
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)) {
203203
int status = die_message(_("unable to write note object"));
204204

205205
if (d->edit_path)

builtin/receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ static void prepare_push_cert_sha1(struct child_process *proc)
762762
int bogs /* beginning_of_gpg_sig */;
763763

764764
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,
766766
&push_cert_oid))
767767
oidclr(&push_cert_oid);
768768

builtin/replace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ static int create_graft(int argc, const char **argv, int force, int gentle)
474474
return -1;
475475
}
476476

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)) {
478478
strbuf_release(&buf);
479479
return error(_("could not write replacement commit for: '%s'"),
480480
old_ref);

builtin/tag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ static int build_tag_object(struct strbuf *buf, int sign, struct object_id *resu
238238
{
239239
if (sign && do_sign(buf) < 0)
240240
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)
242242
return error(_("unable to write tag file"));
243243
return 0;
244244
}

builtin/unpack-objects.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf)
177177
struct object_id oid;
178178

179179
if (write_object_file(obj_buf->buffer, obj_buf->size,
180-
type_name(obj->type), &oid) < 0)
180+
obj->type, &oid) < 0)
181181
die("failed to write object %s", oid_to_hex(&obj->oid));
182182
obj->flags |= FLAG_WRITTEN;
183183
}
@@ -243,15 +243,15 @@ static void write_object(unsigned nr, enum object_type type,
243243
void *buf, unsigned long size)
244244
{
245245
if (!strict) {
246-
if (write_object_file(buf, size, type_name(type),
246+
if (write_object_file(buf, size, type,
247247
&obj_list[nr].oid) < 0)
248248
die("failed to write object");
249249
added_object(nr, type, buf, size);
250250
free(buf);
251251
obj_list[nr].obj = NULL;
252252
} else if (type == OBJ_BLOB) {
253253
struct blob *blob;
254-
if (write_object_file(buf, size, type_name(type),
254+
if (write_object_file(buf, size, type,
255255
&obj_list[nr].oid) < 0)
256256
die("failed to write object");
257257
added_object(nr, type, buf, size);

cache-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ static int update_one(struct cache_tree *it,
440440
} else if (dryrun) {
441441
hash_object_file(the_hash_algo, buffer.buf, buffer.len,
442442
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,
444444
&it->oid, flags & WRITE_TREE_SILENT
445445
? HASH_SILENT : 0)) {
446446
strbuf_release(&buffer);

commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,7 @@ int commit_tree_extended(const char *msg, size_t msg_len,
15671567
goto out;
15681568
}
15691569

1570-
result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
1570+
result = write_object_file(buffer.buf, buffer.len, OBJ_COMMIT, ret);
15711571
out:
15721572
strbuf_release(&buffer);
15731573
return result;

match-trees.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ static int splice_tree(const struct object_id *oid1, const char *prefix,
235235
rewrite_with = oid2;
236236
}
237237
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);
239239
free(buf);
240240
return status;
241241
}

merge-ort.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,7 @@ static int handle_content_merge(struct merge_options *opt,
18881888

18891889
if (!ret &&
18901890
write_object_file(result_buf.ptr, result_buf.size,
1891-
blob_type, &result->oid))
1891+
OBJ_BLOB, &result->oid))
18921892
ret = err(opt, _("Unable to add %s to database"),
18931893
path);
18941894

@@ -3343,7 +3343,7 @@ static void write_tree(struct object_id *result_oid,
33433343
}
33443344

33453345
/* 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);
33473347
strbuf_release(&buf);
33483348
}
33493349

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,7 @@ static int merge_mode_and_contents(struct merge_options *opt,
13731373

13741374
if (!ret &&
13751375
write_object_file(result_buf.ptr, result_buf.size,
1376-
blob_type, &result->blob.oid))
1376+
OBJ_BLOB, &result->blob.oid))
13771377
ret = err(opt, _("Unable to add %s to database"),
13781378
a->path);
13791379

notes-cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ int notes_cache_put(struct notes_cache *c, struct object_id *key_oid,
9292
{
9393
struct object_id value_oid;
9494

95-
if (write_object_file(data, size, "blob", &value_oid) < 0)
95+
if (write_object_file(data, size, OBJ_BLOB, &value_oid) < 0)
9696
return -1;
9797
return add_note(&c->tree, key_oid, &value_oid, NULL);
9898
}

notes.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
675675
ret = tree_write_stack_finish_subtree(n);
676676
if (ret)
677677
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);
679679
if (ret)
680680
return ret;
681681
strbuf_release(&n->buf);
@@ -836,7 +836,7 @@ int combine_notes_concatenate(struct object_id *cur_oid,
836836
free(new_msg);
837837

838838
/* 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);
840840
free(buf);
841841
return ret;
842842
}
@@ -916,7 +916,7 @@ int combine_notes_cat_sort_uniq(struct object_id *cur_oid,
916916
string_list_join_lines_helper, &buf))
917917
goto out;
918918

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

921921
out:
922922
strbuf_release(&buf);
@@ -1192,7 +1192,7 @@ int write_notes_tree(struct notes_tree *t, struct object_id *result)
11921192
ret = for_each_note(t, flags, write_each_note, &cb_data) ||
11931193
write_each_non_note_until(NULL, &cb_data) ||
11941194
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);
11961196
strbuf_release(&root.buf);
11971197
return ret;
11981198
}

object-file.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,7 @@ static int freshen_packed_object(const struct object_id *oid)
20142014
}
20152015

20162016
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,
20182018
unsigned flags)
20192019
{
20202020
char hdr[MAX_HEADER_LEN];
@@ -2023,7 +2023,7 @@ int write_object_file_flags(const void *buf, unsigned long len,
20232023
/* Normally if we have it in the pack then we do not bother writing
20242024
* it out into .git/objects/??/?{38} file.
20252025
*/
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,
20272027
&hdrlen);
20282028
if (freshen_packed_object(oid) || freshen_loose_object(oid))
20292029
return 0;
@@ -2162,7 +2162,7 @@ static int index_mem(struct index_state *istate,
21622162
}
21632163

21642164
if (write_object)
2165-
ret = write_object_file(buf, size, type_name(type), oid);
2165+
ret = write_object_file(buf, size, type, oid);
21662166
else
21672167
hash_object_file(the_hash_algo, buf, size, type_name(type),
21682168
oid);
@@ -2189,7 +2189,7 @@ static int index_stream_convert_blob(struct index_state *istate,
21892189
get_conv_flags(flags));
21902190

21912191
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,
21932193
oid);
21942194
else
21952195
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,
23132313
if (!(flags & HASH_WRITE_OBJECT))
23142314
hash_object_file(the_hash_algo, sb.buf, sb.len,
23152315
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))
23172317
rc = error(_("%s: failed to insert into database"), path);
23182318
strbuf_release(&sb);
23192319
break;

object-store.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,10 @@ void hash_object_file(const struct git_hash_algo *algo, const void *buf,
250250
struct object_id *oid);
251251

252252
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,
254254
unsigned flags);
255255
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)
257257
{
258258
return write_object_file_flags(buf, len, type, oid, 0);
259259
}

read-cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ static struct cache_entry *create_alias_ce(struct index_state *istate,
735735
void set_object_name_for_intent_to_add_entry(struct cache_entry *ce)
736736
{
737737
struct object_id oid;
738-
if (write_object_file("", 0, blob_type, &oid))
738+
if (write_object_file("", 0, OBJ_BLOB, &oid))
739739
die(_("cannot create an empty blob in the object database"));
740740
oidcpy(&ce->oid, &oid);
741741
}

0 commit comments

Comments
 (0)