diff options
author | Junio C Hamano <gitster@pobox.com> | 2020-02-14 12:54:22 -0800 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2020-02-14 12:54:22 -0800 |
commit | 78e67cda42a440f216fc4cbfd8a9e190c5eece5a (patch) | |
tree | 6e1f8349af8dc8ba249a6fd9f4367ef8e983a55d /sha1-file.c | |
parent | df04a316176d5ba44422c2c6b0cb1a9fc31901e5 (diff) | |
parent | b98d18858187eb926912d5199533a6d2a14d5007 (diff) | |
download | git-78e67cda42a440f216fc4cbfd8a9e190c5eece5a.tar.gz |
Merge branch 'mt/use-passed-repo-more-in-funcs'
Some codepaths were given a repository instance as a parameter to
work in the repository, but passed the_repository instance to its
callees, which has been cleaned up (somewhat).
* mt/use-passed-repo-more-in-funcs:
sha1-file: allow check_object_signature() to handle any repo
sha1-file: pass git_hash_algo to hash_object_file()
sha1-file: pass git_hash_algo to write_object_file_prepare()
streaming: allow open_istream() to handle any repo
pack-check: use given repo's hash_algo at verify_packfile()
cache-tree: use given repo's hash_algo at verify_one()
diff: make diff_populate_filespec() honor its repo argument
Diffstat (limited to 'sha1-file.c')
-rw-r--r-- | sha1-file.c | 55 |
1 files changed, 31 insertions, 24 deletions
diff --git a/sha1-file.c b/sha1-file.c index 6575d6f1fc..d785de8a85 100644 --- a/sha1-file.c +++ b/sha1-file.c @@ -971,8 +971,8 @@ void *xmmap(void *start, size_t length, * With "map" == NULL, try reading the object named with "oid" using * the streaming interface and rehash it to do the same. */ -int check_object_signature(const struct object_id *oid, void *map, - unsigned long size, const char *type) +int check_object_signature(struct repository *r, const struct object_id *oid, + void *map, unsigned long size, const char *type) { struct object_id real_oid; enum object_type obj_type; @@ -982,11 +982,11 @@ int check_object_signature(const struct object_id *oid, void *map, int hdrlen; if (map) { - hash_object_file(map, size, type, &real_oid); + hash_object_file(r->hash_algo, map, size, type, &real_oid); return !oideq(oid, &real_oid) ? -1 : 0; } - st = open_istream(oid, &obj_type, &size, NULL); + st = open_istream(r, oid, &obj_type, &size, NULL); if (!st) return -1; @@ -994,8 +994,8 @@ int check_object_signature(const struct object_id *oid, void *map, hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(obj_type), (uintmax_t)size) + 1; /* Sha1.. */ - the_hash_algo->init_fn(&c); - the_hash_algo->update_fn(&c, hdr, hdrlen); + r->hash_algo->init_fn(&c); + r->hash_algo->update_fn(&c, hdr, hdrlen); for (;;) { char buf[1024 * 16]; ssize_t readlen = read_istream(st, buf, sizeof(buf)); @@ -1006,9 +1006,9 @@ int check_object_signature(const struct object_id *oid, void *map, } if (!readlen) break; - the_hash_algo->update_fn(&c, buf, readlen); + r->hash_algo->update_fn(&c, buf, readlen); } - the_hash_algo->final_fn(real_oid.hash, &c); + r->hash_algo->final_fn(real_oid.hash, &c); close_istream(st); return !oideq(oid, &real_oid) ? -1 : 0; } @@ -1588,7 +1588,7 @@ int pretend_object_file(void *buf, unsigned long len, enum object_type type, { struct cached_object *co; - hash_object_file(buf, len, type_name(type), oid); + hash_object_file(the_hash_algo, buf, len, type_name(type), oid); if (has_object_file(oid) || find_cached_object(oid)) return 0; ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc); @@ -1694,7 +1694,8 @@ void *read_object_with_reference(struct repository *r, } } -static void write_object_file_prepare(const void *buf, unsigned long len, +static void write_object_file_prepare(const struct git_hash_algo *algo, + const void *buf, unsigned long len, const char *type, struct object_id *oid, char *hdr, int *hdrlen) { @@ -1704,10 +1705,10 @@ static void write_object_file_prepare(const void *buf, unsigned long len, *hdrlen = xsnprintf(hdr, *hdrlen, "%s %"PRIuMAX , type, (uintmax_t)len)+1; /* Sha1.. */ - the_hash_algo->init_fn(&c); - the_hash_algo->update_fn(&c, hdr, *hdrlen); - the_hash_algo->update_fn(&c, buf, len); - the_hash_algo->final_fn(oid->hash, &c); + algo->init_fn(&c); + algo->update_fn(&c, hdr, *hdrlen); + algo->update_fn(&c, buf, len); + algo->final_fn(oid->hash, &c); } /* @@ -1760,12 +1761,13 @@ static int write_buffer(int fd, const void *buf, size_t len) return 0; } -int hash_object_file(const void *buf, unsigned long len, const char *type, +int hash_object_file(const struct git_hash_algo *algo, const void *buf, + unsigned long len, const char *type, struct object_id *oid) { char hdr[MAX_HEADER_LEN]; int hdrlen = sizeof(hdr); - write_object_file_prepare(buf, len, type, oid, hdr, &hdrlen); + write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen); return 0; } @@ -1923,7 +1925,8 @@ int write_object_file(const void *buf, unsigned long len, const char *type, /* Normally if we have it in the pack then we do not bother writing * it out into .git/objects/??/?{38} file. */ - write_object_file_prepare(buf, len, type, oid, hdr, &hdrlen); + write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr, + &hdrlen); if (freshen_packed_object(oid) || freshen_loose_object(oid)) return 0; return write_loose_object(oid, hdr, hdrlen, buf, len, 0); @@ -1939,7 +1942,8 @@ int hash_object_file_literally(const void *buf, unsigned long len, /* type string, SP, %lu of the length plus NUL must fit this */ hdrlen = strlen(type) + MAX_HEADER_LEN; header = xmalloc(hdrlen); - write_object_file_prepare(buf, len, type, oid, header, &hdrlen); + write_object_file_prepare(the_hash_algo, buf, len, type, oid, header, + &hdrlen); if (!(flags & HASH_WRITE_OBJECT)) goto cleanup; @@ -2049,7 +2053,8 @@ static int index_mem(struct index_state *istate, if (write_object) ret = write_object_file(buf, size, type_name(type), oid); else - ret = hash_object_file(buf, size, type_name(type), oid); + ret = hash_object_file(the_hash_algo, buf, size, + type_name(type), oid); if (re_allocated) free(buf); return ret; @@ -2075,8 +2080,8 @@ static int index_stream_convert_blob(struct index_state *istate, ret = write_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB), oid); else - ret = hash_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB), - oid); + ret = hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, + type_name(OBJ_BLOB), oid); strbuf_release(&sbuf); return ret; } @@ -2194,7 +2199,8 @@ int index_path(struct index_state *istate, struct object_id *oid, if (strbuf_readlink(&sb, path, st->st_size)) return error_errno("readlink(\"%s\")", path); if (!(flags & HASH_WRITE_OBJECT)) - hash_object_file(sb.buf, sb.len, blob_type, oid); + hash_object_file(the_hash_algo, sb.buf, sb.len, + blob_type, oid); else if (write_object_file(sb.buf, sb.len, blob_type, oid)) rc = error(_("%s: failed to insert into database"), path); strbuf_release(&sb); @@ -2495,8 +2501,9 @@ int read_loose_object(const char *path, git_inflate_end(&stream); goto out; } - if (check_object_signature(expected_oid, *contents, - *size, type_name(*type))) { + if (check_object_signature(the_repository, expected_oid, + *contents, *size, + type_name(*type))) { error(_("hash mismatch for %s (expected %s)"), path, oid_to_hex(expected_oid)); free(*contents); |