summaryrefslogtreecommitdiff
path: root/object-file.c
diff options
context:
space:
mode:
authorbrian m. carlson <sandals@crustytoothpaste.net>2021-04-26 01:02:52 (GMT)
committerJunio C Hamano <gitster@pobox.com>2021-04-27 07:31:38 (GMT)
commitab795f0d77aec52496557eb69a5fc9e4f60ea1fd (patch)
tree78a5e3451fac3e53b12364f4f8c5c854da318e84 /object-file.c
parentc3b4e4ee366b8de9802fa752d0a81be92e690e17 (diff)
downloadgit-ab795f0d77aec52496557eb69a5fc9e4f60ea1fd.zip
git-ab795f0d77aec52496557eb69a5fc9e4f60ea1fd.tar.gz
git-ab795f0d77aec52496557eb69a5fc9e4f60ea1fd.tar.bz2
hash: add a function to finalize object IDs
To avoid the penalty of having to branch in hash comparison functions, we'll want to always compare the full hash member in a struct object_id, which will require that SHA-1 object IDs be zero-padded. To do so, add a function which finalizes a hash context and writes it into an object ID that performs this padding. Move the definition of struct object_id and the constant definitions higher up so we they are available for us to use. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'object-file.c')
-rw-r--r--object-file.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/object-file.c b/object-file.c
index 624af40..ef1eb98 100644
--- a/object-file.c
+++ b/object-file.c
@@ -89,6 +89,12 @@ static void git_hash_sha1_final(unsigned char *hash, git_hash_ctx *ctx)
git_SHA1_Final(hash, &ctx->sha1);
}
+static void git_hash_sha1_final_oid(struct object_id *oid, git_hash_ctx *ctx)
+{
+ git_SHA1_Final(oid->hash, &ctx->sha1);
+ memset(oid->hash + GIT_SHA1_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA1_RAWSZ);
+}
+
static void git_hash_sha256_init(git_hash_ctx *ctx)
{
@@ -110,6 +116,16 @@ static void git_hash_sha256_final(unsigned char *hash, git_hash_ctx *ctx)
git_SHA256_Final(hash, &ctx->sha256);
}
+static void git_hash_sha256_final_oid(struct object_id *oid, git_hash_ctx *ctx)
+{
+ git_SHA256_Final(oid->hash, &ctx->sha256);
+ /*
+ * This currently does nothing, so the compiler should optimize it out,
+ * but keep it in case we extend the hash size again.
+ */
+ memset(oid->hash + GIT_SHA256_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA256_RAWSZ);
+}
+
static void git_hash_unknown_init(git_hash_ctx *ctx)
{
BUG("trying to init unknown hash");
@@ -130,6 +146,12 @@ static void git_hash_unknown_final(unsigned char *hash, git_hash_ctx *ctx)
BUG("trying to finalize unknown hash");
}
+static void git_hash_unknown_final_oid(struct object_id *oid, git_hash_ctx *ctx)
+{
+ BUG("trying to finalize unknown hash");
+}
+
+
const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
{
NULL,
@@ -141,6 +163,7 @@ const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
git_hash_unknown_clone,
git_hash_unknown_update,
git_hash_unknown_final,
+ git_hash_unknown_final_oid,
NULL,
NULL,
},
@@ -155,6 +178,7 @@ const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
git_hash_sha1_clone,
git_hash_sha1_update,
git_hash_sha1_final,
+ git_hash_sha1_final_oid,
&empty_tree_oid,
&empty_blob_oid,
},
@@ -169,6 +193,7 @@ const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
git_hash_sha256_clone,
git_hash_sha256_update,
git_hash_sha256_final,
+ git_hash_sha256_final_oid,
&empty_tree_oid_sha256,
&empty_blob_oid_sha256,
}