Skip to content

Commit b98d188

Browse files
matheustavaresgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2dcde20 commit b98d188

File tree

7 files changed

+27
-19
lines changed

7 files changed

+27
-19
lines changed

builtin/fast-export.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ static void export_blob(const struct object_id *oid)
293293
buf = read_object_file(oid, &type, &size);
294294
if (!buf)
295295
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)
297298
die("oid mismatch in blob %s", oid_to_hex(oid));
298299
object = parse_object_buffer(the_repository, oid, type,
299300
size, buf, &eaten);

builtin/index-pack.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,8 +1384,9 @@ static void fix_unresolved_deltas(struct hashfile *f)
13841384
if (!base_obj->data)
13851385
continue;
13861386

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)))
13891390
die(_("local object %s is corrupt"), oid_to_hex(&d->oid));
13901391
base_obj->obj = append_obj_to_pack(f, d->oid.hash,
13911392
base_obj->data, base_obj->size, type);

builtin/mktag.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ static int verify_object(const struct object_id *oid, const char *expected_type)
2929
const struct object_id *repl = lookup_replace_object(the_repository, oid);
3030

3131
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+
}
3437
free(buffer);
3538
}
3639
return ret;

cache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,8 @@ int git_open_cloexec(const char *name, int flags);
13631363
int unpack_loose_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz);
13641364
int parse_loose_header(const char *hdr, unsigned long *sizep);
13651365

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

13681369
int finalize_object_file(const char *tmpfile, const char *filename);
13691370

object.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ struct object *parse_object(struct repository *r, const struct object_id *oid)
262262
if ((obj && obj->type == OBJ_BLOB && repo_has_object_file(r, oid)) ||
263263
(!obj && repo_has_object_file(r, oid) &&
264264
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) {
266266
error(_("hash mismatch %s"), oid_to_hex(oid));
267267
return NULL;
268268
}
@@ -272,7 +272,8 @@ struct object *parse_object(struct repository *r, const struct object_id *oid)
272272

273273
buffer = repo_read_object_file(r, oid, &type, &size);
274274
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) {
276277
free(buffer);
277278
error(_("hash mismatch %s"), oid_to_hex(repl));
278279
return NULL;

pack-check.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static int verify_packfile(struct repository *r,
144144
err = error("cannot unpack %s from %s at offset %"PRIuMAX"",
145145
oid_to_hex(entries[i].oid.oid), p->pack_name,
146146
(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)))
148148
err = error("packed %s from %s is corrupt",
149149
oid_to_hex(entries[i].oid.oid), p->pack_name);
150150
else if (fn) {

sha1-file.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -971,8 +971,8 @@ void *xmmap(void *start, size_t length,
971971
* With "map" == NULL, try reading the object named with "oid" using
972972
* the streaming interface and rehash it to do the same.
973973
*/
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)
976976
{
977977
struct object_id real_oid;
978978
enum object_type obj_type;
@@ -982,20 +982,20 @@ int check_object_signature(const struct object_id *oid, void *map,
982982
int hdrlen;
983983

984984
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);
986986
return !oideq(oid, &real_oid) ? -1 : 0;
987987
}
988988

989-
st = open_istream(the_repository, oid, &obj_type, &size, NULL);
989+
st = open_istream(r, oid, &obj_type, &size, NULL);
990990
if (!st)
991991
return -1;
992992

993993
/* Generate the header */
994994
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(obj_type), (uintmax_t)size) + 1;
995995

996996
/* 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);
999999
for (;;) {
10001000
char buf[1024 * 16];
10011001
ssize_t readlen = read_istream(st, buf, sizeof(buf));
@@ -1006,9 +1006,9 @@ int check_object_signature(const struct object_id *oid, void *map,
10061006
}
10071007
if (!readlen)
10081008
break;
1009-
the_hash_algo->update_fn(&c, buf, readlen);
1009+
r->hash_algo->update_fn(&c, buf, readlen);
10101010
}
1011-
the_hash_algo->final_fn(real_oid.hash, &c);
1011+
r->hash_algo->final_fn(real_oid.hash, &c);
10121012
close_istream(st);
10131013
return !oideq(oid, &real_oid) ? -1 : 0;
10141014
}
@@ -2456,8 +2456,9 @@ int read_loose_object(const char *path,
24562456
git_inflate_end(&stream);
24572457
goto out;
24582458
}
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))) {
24612462
error(_("hash mismatch for %s (expected %s)"), path,
24622463
oid_to_hex(expected_oid));
24632464
free(*contents);

0 commit comments

Comments
 (0)