]> git.ipfire.org Git - thirdparty/git.git/commitdiff
object-file.c: add a literal version of write_object_file_prepare()
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Fri, 4 Feb 2022 23:48:33 +0000 (00:48 +0100)
committerJunio C Hamano <gitster@pobox.com>
Sat, 26 Feb 2022 01:16:32 +0000 (17:16 -0800)
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>
object-file.c

index b1cb78c44196ecb4514010b3d19b7462d252dbd9..c75c12dd0ba4d1cf404932a5288fc2957765b58a 100644 (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,
-                                     const char *type, struct object_id *oid,
+                                     enum object_type 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);
+       *hdrlen = format_object_header(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);
+}
+
+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;
+
+       *hdrlen = format_object_header_literally(hdr, *hdrlen, type, len);
+       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;