summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>2022-02-04 23:48:33 (GMT)
committerJunio C Hamano <gitster@pobox.com>2022-02-26 01:16:32 (GMT)
commit2bbb28a3ee2f7252de02f5d0db4da79090b4f8fc (patch)
treefafc4041d7a15e23b86b2505ae29e6292ecae47a
parent44439c1c5827480f68b37c3cc38f257eaeb3ed2c (diff)
downloadgit-2bbb28a3ee2f7252de02f5d0db4da79090b4f8fc.zip
git-2bbb28a3ee2f7252de02f5d0db4da79090b4f8fc.tar.gz
git-2bbb28a3ee2f7252de02f5d0db4da79090b4f8fc.tar.bz2
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>
-rw-r--r--object-file.c39
1 files changed, 29 insertions, 10 deletions
diff --git a/object-file.c b/object-file.c
index b1cb78c..c75c12d 100644
--- a/object-file.c
+++ b/object-file.c
@@ -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;