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>
This commit is contained in:
Matheus Tavares 2020-01-30 17:32:22 -03:00 committed by Junio C Hamano
parent 7ad5c44d9c
commit 2dcde20e1c
11 changed files with 36 additions and 25 deletions

View File

@ -3157,7 +3157,8 @@ static int apply_binary(struct apply_state *state,
* See if the old one matches what the patch * See if the old one matches what the patch
* applies to. * applies to.
*/ */
hash_object_file(img->buf, img->len, blob_type, &oid); hash_object_file(the_hash_algo, img->buf, img->len, blob_type,
&oid);
if (strcmp(oid_to_hex(&oid), patch->old_oid_prefix)) if (strcmp(oid_to_hex(&oid), patch->old_oid_prefix))
return error(_("the patch applies to '%s' (%s), " return error(_("the patch applies to '%s' (%s), "
"which does not match the " "which does not match the "
@ -3202,7 +3203,8 @@ static int apply_binary(struct apply_state *state,
name); name);
/* verify that the result matches */ /* verify that the result matches */
hash_object_file(img->buf, img->len, blob_type, &oid); hash_object_file(the_hash_algo, img->buf, img->len, blob_type,
&oid);
if (strcmp(oid_to_hex(&oid), patch->new_oid_prefix)) if (strcmp(oid_to_hex(&oid), patch->new_oid_prefix))
return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"), return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
name, patch->new_oid_prefix, oid_to_hex(&oid)); name, patch->new_oid_prefix, oid_to_hex(&oid));

View File

@ -949,7 +949,7 @@ static void resolve_delta(struct object_entry *delta_obj,
free(delta_data); free(delta_data);
if (!result->data) if (!result->data)
bad_object(delta_obj->idx.offset, _("failed to apply delta")); bad_object(delta_obj->idx.offset, _("failed to apply delta"));
hash_object_file(result->data, result->size, hash_object_file(the_hash_algo, result->data, result->size,
type_name(delta_obj->real_type), &delta_obj->idx.oid); type_name(delta_obj->real_type), &delta_obj->idx.oid);
sha1_object(result->data, NULL, result->size, delta_obj->real_type, sha1_object(result->data, NULL, result->size, delta_obj->real_type,
&delta_obj->idx.oid); &delta_obj->idx.oid);

View File

@ -409,7 +409,8 @@ static int check_one_mergetag(struct commit *commit,
struct tag *tag; struct tag *tag;
int i; int i;
hash_object_file(extra->value, extra->len, type_name(OBJ_TAG), &tag_oid); hash_object_file(the_hash_algo, extra->value, extra->len,
type_name(OBJ_TAG), &tag_oid);
tag = lookup_tag(the_repository, &tag_oid); tag = lookup_tag(the_repository, &tag_oid);
if (!tag) if (!tag)
return error(_("bad mergetag in commit '%s'"), ref); return error(_("bad mergetag in commit '%s'"), ref);

View File

@ -265,7 +265,8 @@ static void write_object(unsigned nr, enum object_type type,
} else { } else {
struct object *obj; struct object *obj;
int eaten; int eaten;
hash_object_file(buf, size, type_name(type), &obj_list[nr].oid); hash_object_file(the_hash_algo, buf, size, type_name(type),
&obj_list[nr].oid);
added_object(nr, type, buf, size); added_object(nr, type, buf, size);
obj = parse_object_buffer(the_repository, &obj_list[nr].oid, obj = parse_object_buffer(the_repository, &obj_list[nr].oid,
type, size, buf, type, size, buf,

View File

@ -407,13 +407,15 @@ static int update_one(struct cache_tree *it,
if (repair) { if (repair) {
struct object_id oid; struct object_id oid;
hash_object_file(buffer.buf, buffer.len, tree_type, &oid); hash_object_file(the_hash_algo, buffer.buf, buffer.len,
tree_type, &oid);
if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT)) if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
oidcpy(&it->oid, &oid); oidcpy(&it->oid, &oid);
else else
to_invalidate = 1; to_invalidate = 1;
} else if (dryrun) { } else if (dryrun) {
hash_object_file(buffer.buf, buffer.len, tree_type, &it->oid); hash_object_file(the_hash_algo, buffer.buf, buffer.len,
tree_type, &it->oid);
} else if (write_object_file(buffer.buf, buffer.len, tree_type, } else if (write_object_file(buffer.buf, buffer.len, tree_type,
&it->oid)) { &it->oid)) {
strbuf_release(&buffer); strbuf_release(&buffer);
@ -828,7 +830,8 @@ static void verify_one(struct repository *r,
strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0'); strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz); strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
} }
hash_object_file(tree_buf.buf, tree_buf.len, tree_type, &new_oid); hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, tree_type,
&new_oid);
if (!oideq(&new_oid, &it->oid)) if (!oideq(&new_oid, &it->oid))
BUG("cache-tree for path %.*s does not match. " BUG("cache-tree for path %.*s does not match. "
"Expected %s got %s", len, path->buf, "Expected %s got %s", len, path->buf,

View File

@ -1146,7 +1146,7 @@ static int ident_to_worktree(const char *src, size_t len,
/* are we "faking" in place editing ? */ /* are we "faking" in place editing ? */
if (src == buf->buf) if (src == buf->buf)
to_free = strbuf_detach(buf, NULL); to_free = strbuf_detach(buf, NULL);
hash_object_file(src, len, "blob", &oid); hash_object_file(the_hash_algo, src, len, "blob", &oid);
strbuf_grow(buf, len + cnt * (the_hash_algo->hexsz + 3)); strbuf_grow(buf, len + cnt * (the_hash_algo->hexsz + 3));
for (;;) { for (;;) {

View File

@ -263,8 +263,8 @@ static unsigned int hash_filespec(struct repository *r,
if (!filespec->oid_valid) { if (!filespec->oid_valid) {
if (diff_populate_filespec(r, filespec, 0)) if (diff_populate_filespec(r, filespec, 0))
return 0; return 0;
hash_object_file(filespec->data, filespec->size, "blob", hash_object_file(r->hash_algo, filespec->data, filespec->size,
&filespec->oid); "blob", &filespec->oid);
} }
return oidhash(&filespec->oid); return oidhash(&filespec->oid);
} }

4
dir.c
View File

@ -1002,8 +1002,8 @@ static int add_patterns(const char *fname, const char *base, int baselen,
oidcpy(&oid_stat->oid, oidcpy(&oid_stat->oid,
&istate->cache[pos]->oid); &istate->cache[pos]->oid);
else else
hash_object_file(buf, size, "blob", hash_object_file(the_hash_algo, buf, size,
&oid_stat->oid); "blob", &oid_stat->oid);
fill_stat_data(&oid_stat->stat, &st); fill_stat_data(&oid_stat->stat, &st);
oid_stat->valid = 1; oid_stat->valid = 1;
} }

View File

@ -501,7 +501,8 @@ static int show_one_mergetag(struct commit *commit,
int status, nth; int status, nth;
size_t payload_size, gpg_message_offset; size_t payload_size, gpg_message_offset;
hash_object_file(extra->value, extra->len, type_name(OBJ_TAG), &oid); hash_object_file(the_hash_algo, extra->value, extra->len,
type_name(OBJ_TAG), &oid);
tag = lookup_tag(the_repository, &oid); tag = lookup_tag(the_repository, &oid);
if (!tag) if (!tag)
return -1; /* error message already given */ return -1; /* error message already given */

View File

@ -198,8 +198,9 @@ static inline void *repo_read_object_file(struct repository *r,
/* Read and unpack an object file into memory, write memory to an object file */ /* Read and unpack an object file into memory, write memory to an object file */
int oid_object_info(struct repository *r, const struct object_id *, unsigned long *); int oid_object_info(struct repository *r, const struct object_id *, unsigned long *);
int hash_object_file(const void *buf, unsigned long len, int hash_object_file(const struct git_hash_algo *algo, const void *buf,
const char *type, struct object_id *oid); unsigned long len, const char *type,
struct object_id *oid);
int write_object_file(const void *buf, unsigned long len, int write_object_file(const void *buf, unsigned long len,
const char *type, struct object_id *oid); const char *type, struct object_id *oid);

View File

@ -982,7 +982,7 @@ int check_object_signature(const struct object_id *oid, void *map,
int hdrlen; int hdrlen;
if (map) { if (map) {
hash_object_file(map, size, type, &real_oid); hash_object_file(the_hash_algo, map, size, type, &real_oid);
return !oideq(oid, &real_oid) ? -1 : 0; return !oideq(oid, &real_oid) ? -1 : 0;
} }
@ -1544,7 +1544,7 @@ int pretend_object_file(void *buf, unsigned long len, enum object_type type,
{ {
struct cached_object *co; 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)) if (has_object_file(oid) || find_cached_object(oid))
return 0; return 0;
ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc); 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)
return 0; 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) struct object_id *oid)
{ {
char hdr[MAX_HEADER_LEN]; char hdr[MAX_HEADER_LEN];
int hdrlen = sizeof(hdr); int hdrlen = sizeof(hdr);
write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr, write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
&hdrlen);
return 0; return 0;
} }
@ -2008,7 +2008,8 @@ static int index_mem(struct index_state *istate,
if (write_object) if (write_object)
ret = write_object_file(buf, size, type_name(type), oid); ret = write_object_file(buf, size, type_name(type), oid);
else 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) if (re_allocated)
free(buf); free(buf);
return ret; return ret;
@ -2034,8 +2035,8 @@ static int index_stream_convert_blob(struct index_state *istate,
ret = write_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB), ret = write_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB),
oid); oid);
else else
ret = hash_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB), ret = hash_object_file(the_hash_algo, sbuf.buf, sbuf.len,
oid); type_name(OBJ_BLOB), oid);
strbuf_release(&sbuf); strbuf_release(&sbuf);
return ret; return ret;
} }
@ -2153,7 +2154,8 @@ int index_path(struct index_state *istate, struct object_id *oid,
if (strbuf_readlink(&sb, path, st->st_size)) if (strbuf_readlink(&sb, path, st->st_size))
return error_errno("readlink(\"%s\")", path); return error_errno("readlink(\"%s\")", path);
if (!(flags & HASH_WRITE_OBJECT)) 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)) else if (write_object_file(sb.buf, sb.len, blob_type, oid))
rc = error(_("%s: failed to insert into database"), path); rc = error(_("%s: failed to insert into database"), path);
strbuf_release(&sb); strbuf_release(&sb);