object-file API: have hash_object_file() take "enum object_type"
Change the hash_object_file() function to take an "enum object_type". Since a preceding commit all of its callers are passing either "{commit,tree,blob,tag}_type", or the result of a call to type_name(), the parse_object() caller that would pass NULL is now using stream_object_signature(). 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
44439c1c5827480f68b37c3cc38f257eaeb3ed2c
16 files changed
+41
-34
apply.c
+2
-2
@@ -3157,7 +3157,7 @@ static int apply_binary(struct apply_state *state,
3157
* See if the old one matches what the patch
3158
* applies to.
3159
*/
3160
- hash_object_file(the_hash_algo, img->buf, img->len, blob_type,
3160
+ hash_object_file(the_hash_algo, img->buf, img->len, OBJ_BLOB,
3161
&oid);
3162
if (strcmp(oid_to_hex(&oid), patch->old_oid_prefix))
3163
return error(_("the patch applies to '%s' (%s), "
@@ -3203,7 +3203,7 @@ static int apply_binary(struct apply_state *state,
3203
name);
3204
3205
/* verify that the result matches */
3206
- hash_object_file(the_hash_algo, img->buf, img->len, blob_type,
3206
+ hash_object_file(the_hash_algo, img->buf, img->len, OBJ_BLOB,
3207
&oid);
3208
if (strcmp(oid_to_hex(&oid), patch->new_oid_prefix))
3209
return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
builtin/fast-export.c
+1
-1
@@ -300,7 +300,7 @@ static void export_blob(const struct object_id *oid)
300
if (!buf)
301
die("could not read blob %s", oid_to_hex(oid));
302
if (check_object_signature(the_repository, oid, buf, size,
303
- type_name(type)) < 0)
303
+ type) < 0)
304
die("oid mismatch in blob %s", oid_to_hex(oid));
305
object = parse_object_buffer(the_repository, oid, type,
306
size, buf, &eaten);
builtin/index-pack.c
+2
-2
@@ -970,7 +970,7 @@ static struct base_data *resolve_delta(struct object_entry *delta_obj,
970
if (!result_data)
971
bad_object(delta_obj->idx.offset, _("failed to apply delta"));
972
hash_object_file(the_hash_algo, result_data, result_size,
973
- type_name(delta_obj->real_type), &delta_obj->idx.oid);
973
+ delta_obj->real_type, &delta_obj->idx.oid);
974
sha1_object(result_data, NULL, result_size, delta_obj->real_type,
975
&delta_obj->idx.oid);
976
@@ -1413,7 +1413,7 @@ static void fix_unresolved_deltas(struct hashfile *f)
1413
continue;
1414
1415
if (check_object_signature(the_repository, &d->oid, data, size,
1416
- type_name(type)) < 0)
1416
+ type) < 0)
1417
die(_("local object %s is corrupt"), oid_to_hex(&d->oid));
1418
1419
/*
builtin/mktag.c
+1
-1
@@ -62,7 +62,7 @@ static int verify_object_in_tag(struct object_id *tagged_oid, int *tagged_type)
62
63
repl = lookup_replace_object(the_repository, tagged_oid);
64
ret = check_object_signature(the_repository, repl, buffer, size,
65
- type_name(*tagged_type));
65
+ *tagged_type);
66
free(buffer);
67
68
return ret;
builtin/replace.c
+1
-1
@@ -409,7 +409,7 @@ static int check_one_mergetag(struct commit *commit,
409
int i;
410
411
hash_object_file(the_hash_algo, extra->value, extra->len,
412
- type_name(OBJ_TAG), &tag_oid);
412
+ OBJ_TAG, &tag_oid);
413
tag = lookup_tag(the_repository, &tag_oid);
414
if (!tag)
415
return error(_("bad mergetag in commit '%s'"), ref);
builtin/unpack-objects.c
+1
-1
@@ -266,7 +266,7 @@ static void write_object(unsigned nr, enum object_type type,
266
} else {
267
struct object *obj;
268
int eaten;
269
- hash_object_file(the_hash_algo, buf, size, type_name(type),
269
+ hash_object_file(the_hash_algo, buf, size, type,
270
&obj_list[nr].oid);
271
added_object(nr, type, buf, size);
272
obj = parse_object_buffer(the_repository, &obj_list[nr].oid,
cache-tree.c
+3
-3
@@ -432,14 +432,14 @@ static int update_one(struct cache_tree *it,
432
if (repair) {
433
struct object_id oid;
434
hash_object_file(the_hash_algo, buffer.buf, buffer.len,
435
- tree_type, &oid);
435
+ OBJ_TREE, &oid);
436
if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
437
oidcpy(&it->oid, &oid);
438
else
439
to_invalidate = 1;
440
} else if (dryrun) {
441
hash_object_file(the_hash_algo, buffer.buf, buffer.len,
442
- tree_type, &it->oid);
442
+ OBJ_TREE, &it->oid);
443
} else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE,
444
&it->oid, flags & WRITE_TREE_SILENT
445
? HASH_SILENT : 0)) {
@@ -948,7 +948,7 @@ static int verify_one(struct repository *r,
948
strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
949
strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
950
}
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,
952
&new_oid);
953
if (!oideq(&new_oid, &it->oid))
954
BUG("cache-tree for path %.*s does not match. "
cache.h
+2
-1
@@ -1327,7 +1327,8 @@ int parse_loose_header(const char *hdr, struct object_info *oi);
1327
* what we expected, but it might also indicate another error.
1328
*/
1329
int check_object_signature(struct repository *r, const struct object_id *oid,
1330
- void *buf, unsigned long size, const char *type);
1330
+ void *map, unsigned long size,
1331
+ enum object_type type);
1332
1333
/**
1334
* A streaming version of check_object_signature().
convert.c
+1
-1
@@ -1159,7 +1159,7 @@ static int ident_to_worktree(const char *src, size_t len,
1159
/* are we "faking" in place editing ? */
1160
if (src == buf->buf)
1161
to_free = strbuf_detach(buf, NULL);
1162
- hash_object_file(the_hash_algo, src, len, "blob", &oid);
1162
+ hash_object_file(the_hash_algo, src, len, OBJ_BLOB, &oid);
1163
1164
strbuf_grow(buf, len + cnt * (the_hash_algo->hexsz + 3));
1165
for (;;) {
diffcore-rename.c
+1
-1
@@ -261,7 +261,7 @@ static unsigned int hash_filespec(struct repository *r,
261
if (diff_populate_filespec(r, filespec, NULL))
262
return 0;
263
hash_object_file(r->hash_algo, filespec->data, filespec->size,
264
- "blob", &filespec->oid);
264
+ OBJ_BLOB, &filespec->oid);
265
}
266
return oidhash(&filespec->oid);
267
}
dir.c
+1
-1
@@ -1113,7 +1113,7 @@ static int add_patterns(const char *fname, const char *base, int baselen,
1113
&istate->cache[pos]->oid);
1114
else
1115
hash_object_file(the_hash_algo, buf, size,
1116
- "blob", &oid_stat->oid);
1116
+ OBJ_BLOB, &oid_stat->oid);
1117
fill_stat_data(&oid_stat->stat, &st);
1118
oid_stat->valid = 1;
1119
}
log-tree.c
+1
-1
@@ -561,7 +561,7 @@ static int show_one_mergetag(struct commit *commit,
561
struct strbuf signature = STRBUF_INIT;
562
563
hash_object_file(the_hash_algo, extra->value, extra->len,
564
- type_name(OBJ_TAG), &oid);
564
+ OBJ_TAG, &oid);
565
tag = lookup_tag(the_repository, &oid);
566
if (!tag)
567
return -1; /* error message already given */
object-file.c
+21
-14
@@ -1067,7 +1067,8 @@ int format_object_header(char *str, size_t size, enum object_type type,
1067
}
1068
1069
int check_object_signature(struct repository *r, const struct object_id *oid,
1070
- void *buf, unsigned long size, const char *type)
1070
+ void *buf, unsigned long size,
1071
+ enum object_type type)
1072
{
1073
struct object_id real_oid;
1074
@@ -1676,7 +1677,7 @@ int pretend_object_file(void *buf, unsigned long len, enum object_type type,
1677
{
1678
struct cached_object *co;
1679
1679
- hash_object_file(the_hash_algo, buf, len, type_name(type), oid);
1680
+ hash_object_file(the_hash_algo, buf, len, type, oid);
1681
if (has_object_file_with_flags(oid, OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT) ||
1682
find_cached_object(oid))
1683
return 0;
@@ -1850,15 +1851,23 @@ static int write_buffer(int fd, const void *buf, size_t len)
1851
return 0;
1852
}
1853
1853
-void hash_object_file(const struct git_hash_algo *algo, const void *buf,
1854
- unsigned long len, const char *type,
1855
- struct object_id *oid)
1854
+static void hash_object_file_literally(const struct git_hash_algo *algo,
1855
+ const void *buf, unsigned long len,
1856
+ const char *type, struct object_id *oid)
1857
{
1858
char hdr[MAX_HEADER_LEN];
1859
int hdrlen = sizeof(hdr);
1860
+
1861
write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
1862
}
1863
1864
+void hash_object_file(const struct git_hash_algo *algo, const void *buf,
1865
+ unsigned long len, enum object_type type,
1866
+ struct object_id *oid)
1867
+{
1868
+ hash_object_file_literally(algo, buf, len, type_name(type), oid);
1869
+}
1870
+
1871
/* Finalize a file on disk, and close it. */
1872
static void close_loose_object(int fd)
1873
{
@@ -2161,9 +2170,7 @@ static int index_mem(struct index_state *istate,
2170
if (write_object)
2171
ret = write_object_file(buf, size, type, oid);
2172
else
2164
- hash_object_file(the_hash_algo, buf, size, type_name(type),
2165
- oid);
2166
-
2173
+ hash_object_file(the_hash_algo, buf, size, type, oid);
2174
if (re_allocated)
2175
free(buf);
2176
return ret;
@@ -2189,8 +2196,8 @@ static int index_stream_convert_blob(struct index_state *istate,
2196
ret = write_object_file(sbuf.buf, sbuf.len, OBJ_BLOB,
2197
oid);
2198
else
2192
- hash_object_file(the_hash_algo, sbuf.buf, sbuf.len,
2193
- type_name(OBJ_BLOB), oid);
2199
+ hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, OBJ_BLOB,
2200
+ oid);
2201
strbuf_release(&sbuf);
2202
return ret;
2203
}
@@ -2309,7 +2316,7 @@ int index_path(struct index_state *istate, struct object_id *oid,
2316
return error_errno("readlink(\"%s\")", path);
2317
if (!(flags & HASH_WRITE_OBJECT))
2318
hash_object_file(the_hash_algo, sb.buf, sb.len,
2312
- blob_type, oid);
2319
+ OBJ_BLOB, oid);
2320
else if (write_object_file(sb.buf, sb.len, OBJ_BLOB, oid))
2321
rc = error(_("%s: failed to insert into database"), path);
2322
strbuf_release(&sb);
@@ -2614,9 +2621,9 @@ int read_loose_object(const char *path,
2621
git_inflate_end(&stream);
2622
goto out;
2623
}
2617
- hash_object_file(the_repository->hash_algo,
2618
- *contents, *size, oi->type_name->buf,
2619
- real_oid);
2624
+ hash_object_file_literally(the_repository->hash_algo,
2625
+ *contents, *size,
2626
+ oi->type_name->buf, real_oid);
2627
if (!oideq(expected_oid, real_oid))
2628
goto out;
2629
}
object-store.h
+1
-1
@@ -246,7 +246,7 @@ static inline void *repo_read_object_file(struct repository *r,
246
int oid_object_info(struct repository *r, const struct object_id *, unsigned long *);
247
248
void hash_object_file(const struct git_hash_algo *algo, const void *buf,
249
- unsigned long len, const char *type,
249
+ unsigned long len, enum object_type type,
250
struct object_id *oid);
251
252
int write_object_file_flags(const void *buf, unsigned long len,
object.c
+1
-2
@@ -289,8 +289,7 @@ struct object *parse_object(struct repository *r, const struct object_id *oid)
289
290
buffer = repo_read_object_file(r, oid, &type, &size);
291
if (buffer) {
292
- if (check_object_signature(r, repl, buffer, size,
293
- type_name(type)) < 0) {
292
+ if (check_object_signature(r, repl, buffer, size, type) < 0) {
293
free(buffer);
294
error(_("hash mismatch %s"), oid_to_hex(repl));
295
return NULL;
pack-check.c
+1
-1
@@ -143,7 +143,7 @@ static int verify_packfile(struct repository *r,
143
oid_to_hex(&oid), p->pack_name,
144
(uintmax_t)entries[i].offset);
145
else if (data && check_object_signature(r, &oid, data, size,
146
- type_name(type)) < 0)
146
+ type) < 0)
147
err = error("packed %s from %s is corrupt",
148
oid_to_hex(&oid), p->pack_name);
149
else if (!data && stream_object_signature(r, &oid) < 0)