object-file.c: add a literal version of write_object_file_prepare()
Split off a *_literally() variant of the write_object_file_prepare() function. To do this create a new "hash_object_body()" static helper. We now defer the type_name() call until the very last moment in format_object_header() for those callers that aren't "hash-object --literally". Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committed
Feb 5, 2022 at 00:48 UTC
2bbb28a3ee2f7252de02f5d0db4da79090b4f8fc
1 file changed
+29
-10
object-file.c
+29
-10
@@ -1784,21 +1784,40 @@ void *read_object_with_reference(struct repository *r,
1784
}
1785
}
1786
1787
+static void hash_object_body(const struct git_hash_algo *algo, git_hash_ctx *c,
1788
+ const void *buf, unsigned long len,
1789
+ struct object_id *oid,
1790
+ char *hdr, int *hdrlen)
1791
+{
1792
+ algo->init_fn(c);
1793
+ algo->update_fn(c, hdr, *hdrlen);
1794
+ algo->update_fn(c, buf, len);
1795
+ algo->final_oid_fn(oid, c);
1796
+}
1797
+
1798
static void write_object_file_prepare(const struct git_hash_algo *algo,
1799
const void *buf, unsigned long len,
1789
- const char *type, struct object_id *oid,
1800
+ enum object_type type, struct object_id *oid,
1801
char *hdr, int *hdrlen)
1802
{
1803
git_hash_ctx c;
1804
1805
/* Generate the header */
1795
- *hdrlen = format_object_header_literally(hdr, *hdrlen, type, len);
1806
+ *hdrlen = format_object_header(hdr, *hdrlen, type, len);
1807
1808
/* Sha1.. */
1798
- algo->init_fn(&c);
1799
- algo->update_fn(&c, hdr, *hdrlen);
1800
- algo->update_fn(&c, buf, len);
1801
- algo->final_oid_fn(oid, &c);
1809
+ hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen);
1810
+}
1811
+
1812
+static void write_object_file_prepare_literally(const struct git_hash_algo *algo,
1813
+ const void *buf, unsigned long len,
1814
+ const char *type, struct object_id *oid,
1815
+ char *hdr, int *hdrlen)
1816
+{
1817
+ git_hash_ctx c;
1818
+
1819
+ *hdrlen = format_object_header_literally(hdr, *hdrlen, type, len);
1820
+ hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen);
1821
}
1822
1823
/*
@@ -1858,7 +1877,7 @@ static void hash_object_file_literally(const struct git_hash_algo *algo,
1877
char hdr[MAX_HEADER_LEN];
1878
int hdrlen = sizeof(hdr);
1879
1861
- write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
1880
+ write_object_file_prepare_literally(algo, buf, len, type, oid, hdr, &hdrlen);
1881
}
1882
1883
void hash_object_file(const struct git_hash_algo *algo, const void *buf,
@@ -2029,7 +2048,7 @@ int write_object_file_flags(const void *buf, unsigned long len,
2048
/* Normally if we have it in the pack then we do not bother writing
2049
* it out into .git/objects/??/?{38} file.
2050
*/
2032
- write_object_file_prepare(the_hash_algo, buf, len, type_name(type), oid, hdr,
2051
+ write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr,
2052
&hdrlen);
2053
if (freshen_packed_object(oid) || freshen_loose_object(oid))
2054
return 0;
@@ -2046,8 +2065,8 @@ int write_object_file_literally(const void *buf, unsigned long len,
2065
/* type string, SP, %lu of the length plus NUL must fit this */
2066
hdrlen = strlen(type) + MAX_HEADER_LEN;
2067
header = xmalloc(hdrlen);
2049
- write_object_file_prepare(the_hash_algo, buf, len, type, oid, header,
2050
- &hdrlen);
2068
+ write_object_file_prepare_literally(the_hash_algo, buf, len, type,
2069
+ oid, header, &hdrlen);
2070
2071
if (!(flags & HASH_WRITE_OBJECT))
2072
goto cleanup;