@matheusbsbs / matheus-bagliano / commits / b98d188581
Fork 0

sha1-file: allow check_object_signature() to handle any repo

Some callers of check_object_signature() can work on arbitrary repositories, but the repo does not get passed to this function. Instead, the_repository is always used internally. To fix possible inconsistencies, allow the function to receive a struct repository and make those callers pass on the repo being handled. Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matheus Tavares committed Jan 30, 2020 at 17:32 UTC b98d18858187eb926912d5199533a6d2a14d5007
7 files changed +27 -19
builtin/fast-export.c
+2 -1
@@ -293,7 +293,8 @@ static void export_blob(const struct object_id *oid)
293 buf = read_object_file(oid, &type, &size);
294 if (!buf)
295 die("could not read blob %s", oid_to_hex(oid));
296 - if (check_object_signature(oid, buf, size, type_name(type)) < 0)
296 + if (check_object_signature(the_repository, oid, buf, size,
297 + type_name(type)) < 0)
298 die("oid mismatch in blob %s", oid_to_hex(oid));
299 object = parse_object_buffer(the_repository, oid, type,
300 size, buf, &eaten);
builtin/index-pack.c
+3 -2
@@ -1384,8 +1384,9 @@ static void fix_unresolved_deltas(struct hashfile *f)
1384 if (!base_obj->data)
1385 continue;
1386
1387 - if (check_object_signature(&d->oid, base_obj->data,
1388 - base_obj->size, type_name(type)))
1387 + if (check_object_signature(the_repository, &d->oid,
1388 + base_obj->data, base_obj->size,
1389 + type_name(type)))
1390 die(_("local object %s is corrupt"), oid_to_hex(&d->oid));
1391 base_obj->obj = append_obj_to_pack(f, d->oid.hash,
1392 base_obj->data, base_obj->size, type);
builtin/mktag.c
+5 -2
@@ -29,8 +29,11 @@ static int verify_object(const struct object_id *oid, const char *expected_type)
29 const struct object_id *repl = lookup_replace_object(the_repository, oid);
30
31 if (buffer) {
32 - if (type == type_from_string(expected_type))
33 - ret = check_object_signature(repl, buffer, size, expected_type);
32 + if (type == type_from_string(expected_type)) {
33 + ret = check_object_signature(the_repository, repl,
34 + buffer, size,
35 + expected_type);
36 + }
37 free(buffer);
38 }
39 return ret;
cache.h
+2 -1
@@ -1363,7 +1363,8 @@ int git_open_cloexec(const char *name, int flags);
1363 int unpack_loose_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz);
1364 int parse_loose_header(const char *hdr, unsigned long *sizep);
1365
1366 -int check_object_signature(const struct object_id *oid, void *buf, unsigned long size, const char *type);
1366 +int check_object_signature(struct repository *r, const struct object_id *oid,
1367 + void *buf, unsigned long size, const char *type);
1368
1369 int finalize_object_file(const char *tmpfile, const char *filename);
1370
object.c
+3 -2
@@ -262,7 +262,7 @@ struct object *parse_object(struct repository *r, const struct object_id *oid)
262 if ((obj && obj->type == OBJ_BLOB && repo_has_object_file(r, oid)) ||
263 (!obj && repo_has_object_file(r, oid) &&
264 oid_object_info(r, oid, NULL) == OBJ_BLOB)) {
265 - if (check_object_signature(repl, NULL, 0, NULL) < 0) {
265 + if (check_object_signature(r, repl, NULL, 0, NULL) < 0) {
266 error(_("hash mismatch %s"), oid_to_hex(oid));
267 return NULL;
268 }
@@ -272,7 +272,8 @@ struct object *parse_object(struct repository *r, const struct object_id *oid)
272
273 buffer = repo_read_object_file(r, oid, &type, &size);
274 if (buffer) {
275 - if (check_object_signature(repl, buffer, size, type_name(type)) < 0) {
275 + if (check_object_signature(r, repl, buffer, size,
276 + type_name(type)) < 0) {
277 free(buffer);
278 error(_("hash mismatch %s"), oid_to_hex(repl));
279 return NULL;
pack-check.c
+1 -1
@@ -144,7 +144,7 @@ static int verify_packfile(struct repository *r,
144 err = error("cannot unpack %s from %s at offset %"PRIuMAX"",
145 oid_to_hex(entries[i].oid.oid), p->pack_name,
146 (uintmax_t)entries[i].offset);
147 - else if (check_object_signature(entries[i].oid.oid, data, size, type_name(type)))
147 + else if (check_object_signature(r, entries[i].oid.oid, data, size, type_name(type)))
148 err = error("packed %s from %s is corrupt",
149 oid_to_hex(entries[i].oid.oid), p->pack_name);
150 else if (fn) {
sha1-file.c
+11 -10
@@ -971,8 +971,8 @@ void *xmmap(void *start, size_t length,
971 * With "map" == NULL, try reading the object named with "oid" using
972 * the streaming interface and rehash it to do the same.
973 */
974 -int check_object_signature(const struct object_id *oid, void *map,
975 - unsigned long size, const char *type)
974 +int check_object_signature(struct repository *r, const struct object_id *oid,
975 + void *map, unsigned long size, const char *type)
976 {
977 struct object_id real_oid;
978 enum object_type obj_type;
@@ -982,11 +982,11 @@ int check_object_signature(const struct object_id *oid, void *map,
982 int hdrlen;
983
984 if (map) {
985 - hash_object_file(the_hash_algo, map, size, type, &real_oid);
985 + hash_object_file(r->hash_algo, map, size, type, &real_oid);
986 return !oideq(oid, &real_oid) ? -1 : 0;
987 }
988
989 - st = open_istream(the_repository, oid, &obj_type, &size, NULL);
989 + st = open_istream(r, oid, &obj_type, &size, NULL);
990 if (!st)
991 return -1;
992
@@ -994,8 +994,8 @@ int check_object_signature(const struct object_id *oid, void *map,
994 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(obj_type), (uintmax_t)size) + 1;
995
996 /* Sha1.. */
997 - the_hash_algo->init_fn(&c);
998 - the_hash_algo->update_fn(&c, hdr, hdrlen);
997 + r->hash_algo->init_fn(&c);
998 + r->hash_algo->update_fn(&c, hdr, hdrlen);
999 for (;;) {
1000 char buf[1024 * 16];
1001 ssize_t readlen = read_istream(st, buf, sizeof(buf));
@@ -1006,9 +1006,9 @@ int check_object_signature(const struct object_id *oid, void *map,
1006 }
1007 if (!readlen)
1008 break;
1009 - the_hash_algo->update_fn(&c, buf, readlen);
1009 + r->hash_algo->update_fn(&c, buf, readlen);
1010 }
1011 - the_hash_algo->final_fn(real_oid.hash, &c);
1011 + r->hash_algo->final_fn(real_oid.hash, &c);
1012 close_istream(st);
1013 return !oideq(oid, &real_oid) ? -1 : 0;
1014 }
@@ -2456,8 +2456,9 @@ int read_loose_object(const char *path,
2456 git_inflate_end(&stream);
2457 goto out;
2458 }
2459 - if (check_object_signature(expected_oid, *contents,
2460 - *size, type_name(*type))) {
2459 + if (check_object_signature(the_repository, expected_oid,
2460 + *contents, *size,
2461 + type_name(*type))) {
2462 error(_("hash mismatch for %s (expected %s)"), path,
2463 oid_to_hex(expected_oid));
2464 free(*contents);