@matheusbsbs / matheus-bagliano / commits / 7ad5c44d9c
Fork 0

sha1-file: pass git_hash_algo to write_object_file_prepare()

Allow write_object_file_prepare() to receive arbitrary 'struct git_hash_algo's instead of always using the_hash_algo. The added parameter will be used in the next commit to make hash_object_file() be able to work with arbitrary git_hash_algo's, as well. 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 7ad5c44d9ce559703245d389aa71c4445dc9359c
1 file changed +12 -8
sha1-file.c
+12 -8
@@ -1648,7 +1648,8 @@ void *read_object_with_reference(struct repository *r,
1648 }
1649 }
1650
1651 -static void write_object_file_prepare(const void *buf, unsigned long len,
1651 +static void write_object_file_prepare(const struct git_hash_algo *algo,
1652 + const void *buf, unsigned long len,
1653 const char *type, struct object_id *oid,
1654 char *hdr, int *hdrlen)
1655 {
@@ -1658,10 +1659,10 @@ static void write_object_file_prepare(const void *buf, unsigned long len,
1659 *hdrlen = xsnprintf(hdr, *hdrlen, "%s %"PRIuMAX , type, (uintmax_t)len)+1;
1660
1661 /* Sha1.. */
1661 - the_hash_algo->init_fn(&c);
1662 - the_hash_algo->update_fn(&c, hdr, *hdrlen);
1663 - the_hash_algo->update_fn(&c, buf, len);
1664 - the_hash_algo->final_fn(oid->hash, &c);
1662 + algo->init_fn(&c);
1663 + algo->update_fn(&c, hdr, *hdrlen);
1664 + algo->update_fn(&c, buf, len);
1665 + algo->final_fn(oid->hash, &c);
1666 }
1667
1668 /*
@@ -1719,7 +1720,8 @@ int hash_object_file(const void *buf, unsigned long len, const char *type,
1720 {
1721 char hdr[MAX_HEADER_LEN];
1722 int hdrlen = sizeof(hdr);
1722 - write_object_file_prepare(buf, len, type, oid, hdr, &hdrlen);
1723 + write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr,
1724 + &hdrlen);
1725 return 0;
1726 }
1727
@@ -1877,7 +1879,8 @@ int write_object_file(const void *buf, unsigned long len, const char *type,
1879 /* Normally if we have it in the pack then we do not bother writing
1880 * it out into .git/objects/??/?{38} file.
1881 */
1880 - write_object_file_prepare(buf, len, type, oid, hdr, &hdrlen);
1882 + write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr,
1883 + &hdrlen);
1884 if (freshen_packed_object(oid) || freshen_loose_object(oid))
1885 return 0;
1886 return write_loose_object(oid, hdr, hdrlen, buf, len, 0);
@@ -1893,7 +1896,8 @@ int hash_object_file_literally(const void *buf, unsigned long len,
1896 /* type string, SP, %lu of the length plus NUL must fit this */
1897 hdrlen = strlen(type) + MAX_HEADER_LEN;
1898 header = xmalloc(hdrlen);
1896 - write_object_file_prepare(buf, len, type, oid, header, &hdrlen);
1899 + write_object_file_prepare(the_hash_algo, buf, len, type, oid, header,
1900 + &hdrlen);
1901
1902 if (!(flags & HASH_WRITE_OBJECT))
1903 goto cleanup;