1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-08 02:46:08 +02:00

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>
This commit is contained in:
Ævar Arnfjörð Bjarmason 2022-02-05 00:48:33 +01:00 committed by Junio C Hamano
parent 44439c1c58
commit 2bbb28a3ee

View File

@ -1784,21 +1784,40 @@ void *read_object_with_reference(struct repository *r,
}
}
static void hash_object_body(const struct git_hash_algo *algo, git_hash_ctx *c,
const void *buf, unsigned long len,
struct object_id *oid,
char *hdr, int *hdrlen)
{
algo->init_fn(c);
algo->update_fn(c, hdr, *hdrlen);
algo->update_fn(c, buf, len);
algo->final_oid_fn(oid, c);
}
static void write_object_file_prepare(const struct git_hash_algo *algo,
const void *buf, unsigned long len,
enum object_type type, struct object_id *oid,
char *hdr, int *hdrlen)
{
git_hash_ctx c;
/* Generate the header */
*hdrlen = format_object_header(hdr, *hdrlen, type, len);
/* Sha1.. */
hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen);
}
static void write_object_file_prepare_literally(const struct git_hash_algo *algo,
const void *buf, unsigned long len,
const char *type, struct object_id *oid,
char *hdr, int *hdrlen)
{
git_hash_ctx c;
/* Generate the header */
*hdrlen = format_object_header_literally(hdr, *hdrlen, type, len);
/* Sha1.. */
algo->init_fn(&c);
algo->update_fn(&c, hdr, *hdrlen);
algo->update_fn(&c, buf, len);
algo->final_oid_fn(oid, &c);
hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen);
}
/*
@ -1858,7 +1877,7 @@ static void hash_object_file_literally(const struct git_hash_algo *algo,
char hdr[MAX_HEADER_LEN];
int hdrlen = sizeof(hdr);
write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
write_object_file_prepare_literally(algo, buf, len, type, oid, hdr, &hdrlen);
}
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,
/* Normally if we have it in the pack then we do not bother writing
* it out into .git/objects/??/?{38} file.
*/
write_object_file_prepare(the_hash_algo, buf, len, type_name(type), oid, hdr,
write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr,
&hdrlen);
if (freshen_packed_object(oid) || freshen_loose_object(oid))
return 0;
@ -2046,8 +2065,8 @@ int write_object_file_literally(const void *buf, unsigned long len,
/* type string, SP, %lu of the length plus NUL must fit this */
hdrlen = strlen(type) + MAX_HEADER_LEN;
header = xmalloc(hdrlen);
write_object_file_prepare(the_hash_algo, buf, len, type, oid, header,
&hdrlen);
write_object_file_prepare_literally(the_hash_algo, buf, len, type,
oid, header, &hdrlen);
if (!(flags & HASH_WRITE_OBJECT))
goto cleanup;