Skip to content

Commit 430883a

Browse files
committed
Merge branch 'ab/object-file-api-updates'
Object-file API shuffling. * ab/object-file-api-updates: object-file API: pass an enum to read_object_with_reference() object-file.c: add a literal version of write_object_file_prepare() object-file API: have hash_object_file() take "enum object_type" object API: rename hash_object_file_literally() to write_*() object-file API: split up and simplify check_object_signature() object API users + docs: check <0, not !0 with check_object_signature() object API docs: move check_object_signature() docs to cache.h object API: correct "buf" v.s. "map" mismatch in *.c and *.h object-file API: have write_object_file() take "enum object_type" object-file API: add a format_object_header() function object-file API: return "void", not "int" from hash_object_file() object-file.c: split up declaration of unrelated variables
2 parents 8d1ae40 + 6aea6ba commit 430883a

36 files changed

+202
-137
lines changed

apply.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3164,7 +3164,7 @@ static int apply_binary(struct apply_state *state,
31643164
* See if the old one matches what the patch
31653165
* applies to.
31663166
*/
3167-
hash_object_file(the_hash_algo, img->buf, img->len, blob_type,
3167+
hash_object_file(the_hash_algo, img->buf, img->len, OBJ_BLOB,
31683168
&oid);
31693169
if (strcmp(oid_to_hex(&oid), patch->old_oid_prefix))
31703170
return error(_("the patch applies to '%s' (%s), "
@@ -3210,7 +3210,7 @@ static int apply_binary(struct apply_state *state,
32103210
name);
32113211

32123212
/* verify that the result matches */
3213-
hash_object_file(the_hash_algo, img->buf, img->len, blob_type,
3213+
hash_object_file(the_hash_algo, img->buf, img->len, OBJ_BLOB,
32143214
&oid);
32153215
if (strcmp(oid_to_hex(&oid), patch->new_oid_prefix))
32163216
return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
@@ -3599,7 +3599,7 @@ static int try_threeway(struct apply_state *state,
35993599

36003600
/* Preimage the patch was prepared for */
36013601
if (patch->is_new)
3602-
write_object_file("", 0, blob_type, &pre_oid);
3602+
write_object_file("", 0, OBJ_BLOB, &pre_oid);
36033603
else if (get_oid(patch->old_oid_prefix, &pre_oid) ||
36043604
read_blob_object(&buf, &pre_oid, patch->old_mode))
36053605
return error(_("repository lacks the necessary blob to perform 3-way merge."));
@@ -3615,7 +3615,7 @@ static int try_threeway(struct apply_state *state,
36153615
return -1;
36163616
}
36173617
/* post_oid is theirs */
3618-
write_object_file(tmp_image.buf, tmp_image.len, blob_type, &post_oid);
3618+
write_object_file(tmp_image.buf, tmp_image.len, OBJ_BLOB, &post_oid);
36193619
clear_image(&tmp_image);
36203620

36213621
/* our_oid is ours */
@@ -3628,7 +3628,7 @@ static int try_threeway(struct apply_state *state,
36283628
return error(_("cannot read the current contents of '%s'"),
36293629
patch->old_name);
36303630
}
3631-
write_object_file(tmp_image.buf, tmp_image.len, blob_type, &our_oid);
3631+
write_object_file(tmp_image.buf, tmp_image.len, OBJ_BLOB, &our_oid);
36323632
clear_image(&tmp_image);
36333633

36343634
/* in-core three-way merge between post and our using pre as base */
@@ -4328,7 +4328,7 @@ static int add_index_file(struct apply_state *state,
43284328
}
43294329
fill_stat_cache_info(state->repo->index, ce, &st);
43304330
}
4331-
if (write_object_file(buf, size, blob_type, &ce->oid) < 0) {
4331+
if (write_object_file(buf, size, OBJ_BLOB, &ce->oid) < 0) {
43324332
discard_cache_entry(ce);
43334333
return error(_("unable to create backing store "
43344334
"for newly created file %s"), path);

builtin/cat-file.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
156156
break;
157157

158158
case 0:
159-
if (type_from_string(exp_type) == OBJ_BLOB) {
159+
{
160+
enum object_type exp_type_id = type_from_string(exp_type);
161+
162+
if (exp_type_id == OBJ_BLOB) {
160163
struct object_id blob_oid;
161164
if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) {
162165
char *buffer = read_object_file(&oid, &type,
@@ -178,10 +181,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
178181
* fall-back to the usual case.
179182
*/
180183
}
181-
buf = read_object_with_reference(the_repository,
182-
&oid, exp_type, &size, NULL);
184+
buf = read_object_with_reference(the_repository, &oid,
185+
exp_type_id, &size, NULL);
183186
break;
184-
187+
}
185188
default:
186189
die("git cat-file: unknown option: %s", exp_type);
187190
}

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ static int checkout_merged(int pos, const struct checkout *state,
303303
* (it also writes the merge result to the object database even
304304
* when it may contain conflicts).
305305
*/
306-
if (write_object_file(result_buf.ptr, result_buf.size, blob_type, &oid))
306+
if (write_object_file(result_buf.ptr, result_buf.size, OBJ_BLOB, &oid))
307307
die(_("Unable to add merge result for '%s'"), path);
308308
free(result_buf.ptr);
309309
ce = make_transient_cache_entry(mode, &oid, path, 2, ce_mem_pool);

builtin/fast-export.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ static void export_blob(const struct object_id *oid)
300300
if (!buf)
301301
die("could not read blob %s", oid_to_hex(oid));
302302
if (check_object_signature(the_repository, oid, buf, size,
303-
type_name(type), NULL) < 0)
303+
type) < 0)
304304
die("oid mismatch in blob %s", oid_to_hex(oid));
305305
object = parse_object_buffer(the_repository, oid, type,
306306
size, buf, &eaten);

builtin/fast-import.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -944,8 +944,8 @@ static int store_object(
944944
git_hash_ctx c;
945945
git_zstream s;
946946

947-
hdrlen = xsnprintf((char *)hdr, sizeof(hdr), "%s %lu",
948-
type_name(type), (unsigned long)dat->len) + 1;
947+
hdrlen = format_object_header((char *)hdr, sizeof(hdr), type,
948+
dat->len);
949949
the_hash_algo->init_fn(&c);
950950
the_hash_algo->update_fn(&c, hdr, hdrlen);
951951
the_hash_algo->update_fn(&c, dat->buf, dat->len);
@@ -1098,7 +1098,7 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
10981098
hashfile_checkpoint(pack_file, &checkpoint);
10991099
offset = checkpoint.offset;
11001100

1101-
hdrlen = xsnprintf((char *)out_buf, out_sz, "blob %" PRIuMAX, len) + 1;
1101+
hdrlen = format_object_header((char *)out_buf, out_sz, OBJ_BLOB, len);
11021102

11031103
the_hash_algo->init_fn(&c);
11041104
the_hash_algo->update_fn(&c, out_buf, hdrlen);
@@ -2490,7 +2490,7 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa
24902490
unsigned long size;
24912491
char *buf = read_object_with_reference(the_repository,
24922492
&commit_oid,
2493-
commit_type, &size,
2493+
OBJ_COMMIT, &size,
24942494
&commit_oid);
24952495
if (!buf || size < the_hash_algo->hexsz + 6)
24962496
die("Not a valid commit: %s", p);
@@ -2562,7 +2562,7 @@ static void parse_from_existing(struct branch *b)
25622562
char *buf;
25632563

25642564
buf = read_object_with_reference(the_repository,
2565-
&b->oid, commit_type, &size,
2565+
&b->oid, OBJ_COMMIT, &size,
25662566
&b->oid);
25672567
parse_from_commit(b, buf, size);
25682568
free(buf);
@@ -2658,7 +2658,7 @@ static struct hash_list *parse_merge(unsigned int *count)
26582658
unsigned long size;
26592659
char *buf = read_object_with_reference(the_repository,
26602660
&n->oid,
2661-
commit_type,
2661+
OBJ_COMMIT,
26622662
&size, &n->oid);
26632663
if (!buf || size < the_hash_algo->hexsz + 6)
26642664
die("Not a valid commit: %s", from);

builtin/grep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ static int grep_submodule(struct grep_opt *opt,
484484
object_type = oid_object_info(subrepo, oid, NULL);
485485
obj_read_unlock();
486486
data = read_object_with_reference(subrepo,
487-
oid, tree_type,
487+
oid, OBJ_TREE,
488488
&size, NULL);
489489
if (!data)
490490
die(_("unable to read tree (%s)"), oid_to_hex(oid));
@@ -653,7 +653,7 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
653653
int hit, len;
654654

655655
data = read_object_with_reference(opt->repo,
656-
&obj->oid, tree_type,
656+
&obj->oid, OBJ_TREE,
657657
&size, NULL);
658658
if (!data)
659659
die(_("unable to read tree (%s)"), oid_to_hex(&obj->oid));

builtin/hash-object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static int hash_literally(struct object_id *oid, int fd, const char *type, unsig
2525
if (strbuf_read(&buf, fd, 4096) < 0)
2626
ret = -1;
2727
else
28-
ret = hash_object_file_literally(buf.buf, buf.len, type, oid,
28+
ret = write_object_file_literally(buf.buf, buf.len, type, oid,
2929
flags);
3030
strbuf_release(&buf);
3131
return ret;

builtin/index-pack.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,7 @@ static void *unpack_entry_data(off_t offset, unsigned long size,
453453
int hdrlen;
454454

455455
if (!is_delta_type(type)) {
456-
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX,
457-
type_name(type),(uintmax_t)size) + 1;
456+
hdrlen = format_object_header(hdr, sizeof(hdr), type, size);
458457
the_hash_algo->init_fn(&c);
459458
the_hash_algo->update_fn(&c, hdr, hdrlen);
460459
} else
@@ -975,7 +974,7 @@ static struct base_data *resolve_delta(struct object_entry *delta_obj,
975974
if (!result_data)
976975
bad_object(delta_obj->idx.offset, _("failed to apply delta"));
977976
hash_object_file(the_hash_algo, result_data, result_size,
978-
type_name(delta_obj->real_type), &delta_obj->idx.oid);
977+
delta_obj->real_type, &delta_obj->idx.oid);
979978
sha1_object(result_data, NULL, result_size, delta_obj->real_type,
980979
&delta_obj->idx.oid);
981980

@@ -1419,9 +1418,8 @@ static void fix_unresolved_deltas(struct hashfile *f)
14191418
if (!data)
14201419
continue;
14211420

1422-
if (check_object_signature(the_repository, &d->oid,
1423-
data, size,
1424-
type_name(type), NULL))
1421+
if (check_object_signature(the_repository, &d->oid, data, size,
1422+
type) < 0)
14251423
die(_("local object %s is corrupt"), oid_to_hex(&d->oid));
14261424

14271425
/*

builtin/mktag.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ static int verify_object_in_tag(struct object_id *tagged_oid, int *tagged_type)
6161
type_name(*tagged_type), type_name(type));
6262

6363
repl = lookup_replace_object(the_repository, tagged_oid);
64-
ret = check_object_signature(the_repository, repl,
65-
buffer, size, type_name(*tagged_type),
66-
NULL);
64+
ret = check_object_signature(the_repository, repl, buffer, size,
65+
*tagged_type);
6766
free(buffer);
6867

6968
return ret;
@@ -97,10 +96,10 @@ int cmd_mktag(int argc, const char **argv, const char *prefix)
9796
&tagged_oid, &tagged_type))
9897
die(_("tag on stdin did not pass our strict fsck check"));
9998

100-
if (verify_object_in_tag(&tagged_oid, &tagged_type))
99+
if (verify_object_in_tag(&tagged_oid, &tagged_type) < 0)
101100
die(_("tag on stdin did not refer to a valid object"));
102101

103-
if (write_object_file(buf.buf, buf.len, tag_type, &result) < 0)
102+
if (write_object_file(buf.buf, buf.len, OBJ_TAG, &result) < 0)
104103
die(_("unable to write tag file"));
105104

106105
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/pack-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,7 @@ static void add_preferred_base(struct object_id *oid)
18021802
return;
18031803

18041804
data = read_object_with_reference(the_repository, oid,
1805-
tree_type, &size, &tree_oid);
1805+
OBJ_TREE, &size, &tree_oid);
18061806
if (!data)
18071807
return;
18081808

builtin/receive-pack.c

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

751751
already_done = 1;
752-
if (write_object_file(push_cert.buf, push_cert.len, "blob",
752+
if (write_object_file(push_cert.buf, push_cert.len, OBJ_BLOB,
753753
&push_cert_oid))
754754
oidclr(&push_cert_oid);
755755

builtin/replace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ static int check_one_mergetag(struct commit *commit,
409409
int i;
410410

411411
hash_object_file(the_hash_algo, extra->value, extra->len,
412-
type_name(OBJ_TAG), &tag_oid);
412+
OBJ_TAG, &tag_oid);
413413
tag = lookup_tag(the_repository, &tag_oid);
414414
if (!tag)
415415
return error(_("bad mergetag in commit '%s'"), ref);
@@ -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
@@ -239,7 +239,7 @@ static int build_tag_object(struct strbuf *buf, int sign, struct object_id *resu
239239
{
240240
if (sign && do_sign(buf) < 0)
241241
return error(_("unable to sign the tag"));
242-
if (write_object_file(buf->buf, buf->len, tag_type, result) < 0)
242+
if (write_object_file(buf->buf, buf->len, OBJ_TAG, result) < 0)
243243
return error(_("unable to write tag file"));
244244
return 0;
245245
}

builtin/unpack-objects.c

Lines changed: 4 additions & 4 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);
@@ -266,7 +266,7 @@ static void write_object(unsigned nr, enum object_type type,
266266
} else {
267267
struct object *obj;
268268
int eaten;
269-
hash_object_file(the_hash_algo, buf, size, type_name(type),
269+
hash_object_file(the_hash_algo, buf, size, type,
270270
&obj_list[nr].oid);
271271
added_object(nr, type, buf, size);
272272
obj = parse_object_buffer(the_repository, &obj_list[nr].oid,

bulk-checkin.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ static int deflate_to_pack(struct bulk_checkin_state *state,
220220
if (seekback == (off_t) -1)
221221
return error("cannot find the current offset");
222222

223-
header_len = xsnprintf((char *)obuf, sizeof(obuf), "%s %" PRIuMAX,
224-
type_name(type), (uintmax_t)size) + 1;
223+
header_len = format_object_header((char *)obuf, sizeof(obuf),
224+
type, size);
225225
the_hash_algo->init_fn(&ctx);
226226
the_hash_algo->update_fn(&ctx, obuf, header_len);
227227

cache-tree.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,15 +432,15 @@ static int update_one(struct cache_tree *it,
432432
if (repair) {
433433
struct object_id oid;
434434
hash_object_file(the_hash_algo, buffer.buf, buffer.len,
435-
tree_type, &oid);
435+
OBJ_TREE, &oid);
436436
if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
437437
oidcpy(&it->oid, &oid);
438438
else
439439
to_invalidate = 1;
440440
} else if (dryrun) {
441441
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,
442+
OBJ_TREE, &it->oid);
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);
@@ -948,7 +948,7 @@ static int verify_one(struct repository *r,
948948
strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
949949
strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
950950
}
951-
hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, tree_type,
951+
hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE,
952952
&new_oid);
953953
if (!oideq(&new_oid, &it->oid))
954954
BUG("cache-tree for path %.*s does not match. "

0 commit comments

Comments
 (0)