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

0 commit comments

Comments
 (0)