]> git.ipfire.org Git - thirdparty/git.git/commitdiff
sha1-file: pass git_hash_algo to hash_object_file()
authorMatheus Tavares <matheus.bernardino@usp.br>
Thu, 30 Jan 2020 20:32:22 +0000 (17:32 -0300)
committerJunio C Hamano <gitster@pobox.com>
Fri, 31 Jan 2020 18:45:39 +0000 (10:45 -0800)
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>
apply.c
builtin/index-pack.c
builtin/replace.c
builtin/unpack-objects.c
cache-tree.c
convert.c
diffcore-rename.c
dir.c
log-tree.c
object-store.h
sha1-file.c

diff --git a/apply.c b/apply.c
index fab44322c5f27952842b6bcfe48bd5e5f8a3c739..bdc008fae2ad77197a859d64196bf91161cf8d67 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -3157,7 +3157,8 @@ static int apply_binary(struct apply_state *state,
                 * See if the old one matches what the patch
                 * 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))
                        return error(_("the patch applies to '%s' (%s), "
                                       "which does not match the "
@@ -3202,7 +3203,8 @@ static int apply_binary(struct apply_state *state,
                                     name);
 
                /* 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))
                        return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
                                name, patch->new_oid_prefix, oid_to_hex(&oid));
index 7a08da8401078d70224748b1ea4b8983696f600d..0183610a7686cb9b85937e921839ab1cf28999ac 100644 (file)
@@ -949,7 +949,7 @@ static void resolve_delta(struct object_entry *delta_obj,
        free(delta_data);
        if (!result->data)
                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);
        sha1_object(result->data, NULL, result->size, delta_obj->real_type,
                    &delta_obj->idx.oid);
index bd92dc63b96b93476270d03d278aadfd7d3bb686..b36d17a657f3e62ae4abc450b616e9c7398377c9 100644 (file)
@@ -409,7 +409,8 @@ static int check_one_mergetag(struct commit *commit,
        struct tag *tag;
        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);
        if (!tag)
                return error(_("bad mergetag in commit '%s'"), ref);
index 91009646678cc46d234f3e7eae77005510852edd..dd4a75e030d219dfb3e5e0eeb59683c14f02fdf0 100644 (file)
@@ -265,7 +265,8 @@ static void write_object(unsigned nr, enum object_type type,
        } else {
                struct object *obj;
                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);
                obj = parse_object_buffer(the_repository, &obj_list[nr].oid,
                                          type, size, buf,
index 8c51a1575169c3ee5965d240fc6f8c860c114abe..a537a806c16e0352e4347a6bd9473457c78377bf 100644 (file)
@@ -407,13 +407,15 @@ static int update_one(struct cache_tree *it,
 
        if (repair) {
                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))
                        oidcpy(&it->oid, &oid);
                else
                        to_invalidate = 1;
        } 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,
                                     &it->oid)) {
                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_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))
                BUG("cache-tree for path %.*s does not match. "
                    "Expected %s got %s", len, path->buf,
index 797e0bd0b14cbd665a638cf0a22ca7f9f3df04d3..33507ee0a943e59b1af50576039ea8770251a2d0 100644 (file)
--- a/convert.c
+++ b/convert.c
@@ -1146,7 +1146,7 @@ static int ident_to_worktree(const char *src, size_t len,
        /* are we "faking" in place editing ? */
        if (src == buf->buf)
                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));
        for (;;) {
index 531d7adeafe0e218de2a56a9dbb8b4156d2d4921..e189f407af3a8cc0155cd7a1146c9daacfe304a8 100644 (file)
@@ -263,8 +263,8 @@ static unsigned int hash_filespec(struct repository *r,
        if (!filespec->oid_valid) {
                if (diff_populate_filespec(r, filespec, 0))
                        return 0;
-               hash_object_file(filespec->data, filespec->size, "blob",
-                                &filespec->oid);
+               hash_object_file(r->hash_algo, filespec->data, filespec->size,
+                                "blob", &filespec->oid);
        }
        return oidhash(&filespec->oid);
 }
diff --git a/dir.c b/dir.c
index 7d255227b130d7729747fe1a86c8efc04eee9f7e..512c5d7ea6d45e380a3e9f94d22af8820302a7b8 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -1002,8 +1002,8 @@ static int add_patterns(const char *fname, const char *base, int baselen,
                                oidcpy(&oid_stat->oid,
                                       &istate->cache[pos]->oid);
                        else
-                               hash_object_file(buf, size, "blob",
-                                                &oid_stat->oid);
+                               hash_object_file(the_hash_algo, buf, size,
+                                                "blob", &oid_stat->oid);
                        fill_stat_data(&oid_stat->stat, &st);
                        oid_stat->valid = 1;
                }
index 4e32638de8adc2ad28aaedd16f44281b2fbc9482..cae38dcc6625e542f3e7c637dcd5a75a9bc52670 100644 (file)
@@ -501,7 +501,8 @@ static int show_one_mergetag(struct commit *commit,
        int status, nth;
        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);
        if (!tag)
                return -1; /* error message already given */
index 55ee63935073e2b2790d82e52399aaa1638f9e8f..bb19f4aa9973a6c54d4bb73e609730db7f01e160 100644 (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 */
 int oid_object_info(struct repository *r, const struct object_id *, unsigned long *);
 
-int hash_object_file(const void *buf, unsigned long len,
-                    const char *type, struct object_id *oid);
+int hash_object_file(const struct git_hash_algo *algo, const void *buf,
+                    unsigned long len, const char *type,
+                    struct object_id *oid);
 
 int write_object_file(const void *buf, unsigned long len,
                      const char *type, struct object_id *oid);
index 1ca3f9f2b024cb07c4c01e2e18b41fe9cb0e68d9..46a79e7614d6c2348df409dfed142223c02811e6 100644 (file)
@@ -982,7 +982,7 @@ 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(the_hash_algo, map, size, type, &real_oid);
                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;
 
-       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);
@@ -1715,13 +1715,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(the_hash_algo, buf, len, type, oid, hdr,
-                                 &hdrlen);
+       write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
        return 0;
 }
 
@@ -2008,7 +2008,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;
@@ -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),
                                        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;
 }
@@ -2153,7 +2154,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);