@matheusbsbs / matheus-bagliano / commits / 2dcde20e1c
Fork 0

sha1-file: pass git_hash_algo to hash_object_file()

Allow hash_object_file() to work on arbitrary repos by introducing a git_hash_algo parameter. Change callers which have a struct repository pointer in their scope to pass on the git_hash_algo from the said repo. For all other callers, pass on the_hash_algo, which was already being used internally at hash_object_file(). This functionality will be used in the following patch to make check_object_signature() be able to work on arbitrary repos (which, in turn, will be used to fix an inconsistency at object.c:parse_object()). 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 2dcde20e1c55fc2e3f9e9e6d48e93c39ec5661d2
11 files changed +36 -25
apply.c
+4 -2
@@ -3157,7 +3157,8 @@ 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(img->buf, img->len, blob_type, &oid);
3160 + hash_object_file(the_hash_algo, img->buf, img->len, blob_type,
3161 + &oid);
3162 if (strcmp(oid_to_hex(&oid), patch->old_oid_prefix))
3163 return error(_("the patch applies to '%s' (%s), "
3164 "which does not match the "
@@ -3202,7 +3203,8 @@ static int apply_binary(struct apply_state *state,
3203 name);
3204
3205 /* verify that the result matches */
3205 - hash_object_file(img->buf, img->len, blob_type, &oid);
3206 + hash_object_file(the_hash_algo, img->buf, img->len, blob_type,
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)"),
3210 name, patch->new_oid_prefix, oid_to_hex(&oid));
builtin/index-pack.c
+1 -1
@@ -949,7 +949,7 @@ static void resolve_delta(struct object_entry *delta_obj,
949 free(delta_data);
950 if (!result->data)
951 bad_object(delta_obj->idx.offset, _("failed to apply delta"));
952 - hash_object_file(result->data, result->size,
952 + hash_object_file(the_hash_algo, result->data, result->size,
953 type_name(delta_obj->real_type), &delta_obj->idx.oid);
954 sha1_object(result->data, NULL, result->size, delta_obj->real_type,
955 &delta_obj->idx.oid);
builtin/replace.c
+2 -1
@@ -409,7 +409,8 @@ static int check_one_mergetag(struct commit *commit,
409 struct tag *tag;
410 int i;
411
412 - hash_object_file(extra->value, extra->len, type_name(OBJ_TAG), &tag_oid);
412 + hash_object_file(the_hash_algo, extra->value, extra->len,
413 + type_name(OBJ_TAG), &tag_oid);
414 tag = lookup_tag(the_repository, &tag_oid);
415 if (!tag)
416 return error(_("bad mergetag in commit '%s'"), ref);
builtin/unpack-objects.c
+2 -1
@@ -265,7 +265,8 @@ static void write_object(unsigned nr, enum object_type type,
265 } else {
266 struct object *obj;
267 int eaten;
268 - hash_object_file(buf, size, type_name(type), &obj_list[nr].oid);
268 + hash_object_file(the_hash_algo, buf, size, type_name(type),
269 + &obj_list[nr].oid);
270 added_object(nr, type, buf, size);
271 obj = parse_object_buffer(the_repository, &obj_list[nr].oid,
272 type, size, buf,
cache-tree.c
+6 -3
@@ -407,13 +407,15 @@ static int update_one(struct cache_tree *it,
407
408 if (repair) {
409 struct object_id oid;
410 - hash_object_file(buffer.buf, buffer.len, tree_type, &oid);
410 + hash_object_file(the_hash_algo, buffer.buf, buffer.len,
411 + tree_type, &oid);
412 if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
413 oidcpy(&it->oid, &oid);
414 else
415 to_invalidate = 1;
416 } else if (dryrun) {
416 - hash_object_file(buffer.buf, buffer.len, tree_type, &it->oid);
417 + hash_object_file(the_hash_algo, buffer.buf, buffer.len,
418 + tree_type, &it->oid);
419 } else if (write_object_file(buffer.buf, buffer.len, tree_type,
420 &it->oid)) {
421 strbuf_release(&buffer);
@@ -828,7 +830,8 @@ static void verify_one(struct repository *r,
830 strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
831 strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
832 }
831 - hash_object_file(tree_buf.buf, tree_buf.len, tree_type, &new_oid);
833 + hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, tree_type,
834 + &new_oid);
835 if (!oideq(&new_oid, &it->oid))
836 BUG("cache-tree for path %.*s does not match. "
837 "Expected %s got %s", len, path->buf,
convert.c
+1 -1
@@ -1146,7 +1146,7 @@ static int ident_to_worktree(const char *src, size_t len,
1146 /* are we "faking" in place editing ? */
1147 if (src == buf->buf)
1148 to_free = strbuf_detach(buf, NULL);
1149 - hash_object_file(src, len, "blob", &oid);
1149 + hash_object_file(the_hash_algo, src, len, "blob", &oid);
1150
1151 strbuf_grow(buf, len + cnt * (the_hash_algo->hexsz + 3));
1152 for (;;) {
diffcore-rename.c
+2 -2
@@ -263,8 +263,8 @@ static unsigned int hash_filespec(struct repository *r,
263 if (!filespec->oid_valid) {
264 if (diff_populate_filespec(r, filespec, 0))
265 return 0;
266 - hash_object_file(filespec->data, filespec->size, "blob",
267 - &filespec->oid);
266 + hash_object_file(r->hash_algo, filespec->data, filespec->size,
267 + "blob", &filespec->oid);
268 }
269 return oidhash(&filespec->oid);
270 }
dir.c
+2 -2
@@ -1002,8 +1002,8 @@ static int add_patterns(const char *fname, const char *base, int baselen,
1002 oidcpy(&oid_stat->oid,
1003 &istate->cache[pos]->oid);
1004 else
1005 - hash_object_file(buf, size, "blob",
1006 - &oid_stat->oid);
1005 + hash_object_file(the_hash_algo, buf, size,
1006 + "blob", &oid_stat->oid);
1007 fill_stat_data(&oid_stat->stat, &st);
1008 oid_stat->valid = 1;
1009 }
log-tree.c
+2 -1
@@ -501,7 +501,8 @@ static int show_one_mergetag(struct commit *commit,
501 int status, nth;
502 size_t payload_size, gpg_message_offset;
503
504 - hash_object_file(extra->value, extra->len, type_name(OBJ_TAG), &oid);
504 + hash_object_file(the_hash_algo, extra->value, extra->len,
505 + type_name(OBJ_TAG), &oid);
506 tag = lookup_tag(the_repository, &oid);
507 if (!tag)
508 return -1; /* error message already given */
object-store.h
+3 -2
@@ -198,8 +198,9 @@ static inline void *repo_read_object_file(struct repository *r,
198 /* Read and unpack an object file into memory, write memory to an object file */
199 int oid_object_info(struct repository *r, const struct object_id *, unsigned long *);
200
201 -int hash_object_file(const void *buf, unsigned long len,
202 - const char *type, struct object_id *oid);
201 +int hash_object_file(const struct git_hash_algo *algo, const void *buf,
202 + unsigned long len, const char *type,
203 + struct object_id *oid);
204
205 int write_object_file(const void *buf, unsigned long len,
206 const char *type, struct object_id *oid);
sha1-file.c
+11 -9
@@ -982,7 +982,7 @@ int check_object_signature(const struct object_id *oid, void *map,
982 int hdrlen;
983
984 if (map) {
985 - hash_object_file(map, size, type, &real_oid);
985 + hash_object_file(the_hash_algo, map, size, type, &real_oid);
986 return !oideq(oid, &real_oid) ? -1 : 0;
987 }
988
@@ -1544,7 +1544,7 @@ int pretend_object_file(void *buf, unsigned long len, enum object_type type,
1544 {
1545 struct cached_object *co;
1546
1547 - hash_object_file(buf, len, type_name(type), oid);
1547 + hash_object_file(the_hash_algo, buf, len, type_name(type), oid);
1548 if (has_object_file(oid) || find_cached_object(oid))
1549 return 0;
1550 ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
@@ -1715,13 +1715,13 @@ static int write_buffer(int fd, const void *buf, size_t len)
1715 return 0;
1716 }
1717
1718 -int hash_object_file(const void *buf, unsigned long len, const char *type,
1718 +int hash_object_file(const struct git_hash_algo *algo, const void *buf,
1719 + unsigned long len, const char *type,
1720 struct object_id *oid)
1721 {
1722 char hdr[MAX_HEADER_LEN];
1723 int hdrlen = sizeof(hdr);
1723 - write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr,
1724 - &hdrlen);
1724 + write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
1725 return 0;
1726 }
1727
@@ -2008,7 +2008,8 @@ static int index_mem(struct index_state *istate,
2008 if (write_object)
2009 ret = write_object_file(buf, size, type_name(type), oid);
2010 else
2011 - ret = hash_object_file(buf, size, type_name(type), oid);
2011 + ret = hash_object_file(the_hash_algo, buf, size,
2012 + type_name(type), oid);
2013 if (re_allocated)
2014 free(buf);
2015 return ret;
@@ -2034,8 +2035,8 @@ static int index_stream_convert_blob(struct index_state *istate,
2035 ret = write_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB),
2036 oid);
2037 else
2037 - ret = hash_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB),
2038 - oid);
2038 + ret = hash_object_file(the_hash_algo, sbuf.buf, sbuf.len,
2039 + type_name(OBJ_BLOB), oid);
2040 strbuf_release(&sbuf);
2041 return ret;
2042 }
@@ -2153,7 +2154,8 @@ int index_path(struct index_state *istate, struct object_id *oid,
2154 if (strbuf_readlink(&sb, path, st->st_size))
2155 return error_errno("readlink(\"%s\")", path);
2156 if (!(flags & HASH_WRITE_OBJECT))
2156 - hash_object_file(sb.buf, sb.len, blob_type, oid);
2157 + hash_object_file(the_hash_algo, sb.buf, sb.len,
2158 + blob_type, oid);
2159 else if (write_object_file(sb.buf, sb.len, blob_type, oid))
2160 rc = error(_("%s: failed to insert into database"), path);
2161 strbuf_release(&sb);