summaryrefslogtreecommitdiff
path: root/object-file.c
diff options
context:
space:
mode:
authorbrian m. carlson <sandals@crustytoothpaste.net>2021-04-26 01:02:55 (GMT)
committerJunio C Hamano <gitster@pobox.com>2021-04-27 07:31:38 (GMT)
commit5a6dce70d7bb12ee2bc7926254c5b6741b91ac5f (patch)
treea4350b3f781b36cda38fc645a5eb54b161eb4536 /object-file.c
parent0e5e2284f1267293e56e8948143f6cd04c74e2ed (diff)
downloadgit-5a6dce70d7bb12ee2bc7926254c5b6741b91ac5f.zip
git-5a6dce70d7bb12ee2bc7926254c5b6741b91ac5f.tar.gz
git-5a6dce70d7bb12ee2bc7926254c5b6741b91ac5f.tar.bz2
hash: set, copy, and use algo field in struct object_id
Now that struct object_id has an algorithm field, we should populate it. This will allow us to handle object IDs in any supported algorithm and distinguish between them. Ensure that the field is written whenever we write an object ID by storing it explicitly every time we write an object. Set values for the empty blob and tree values as well. In addition, use the algorithm field to compare object IDs. Note that because we zero-initialize struct object_id in many places throughout the codebase, we default to the default algorithm in cases where the algorithm field is zero rather than explicitly initialize all of those locations. This leads to a branch on every comparison, but the alternative is to compare the entire buffer each time and padding the buffer for SHA-1. That alternative ranges up to 3.9% worse than this approach on the perf t0001, t1450, and t1451. 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.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/object-file.c b/object-file.c
index 1d8c82f..d4ba0c4 100644
--- a/object-file.c
+++ b/object-file.c
@@ -57,16 +57,20 @@
const struct object_id null_oid;
static const struct object_id empty_tree_oid = {
- EMPTY_TREE_SHA1_BIN_LITERAL
+ .hash = EMPTY_TREE_SHA1_BIN_LITERAL,
+ .algo = GIT_HASH_SHA1,
};
static const struct object_id empty_blob_oid = {
- EMPTY_BLOB_SHA1_BIN_LITERAL
+ .hash = EMPTY_BLOB_SHA1_BIN_LITERAL,
+ .algo = GIT_HASH_SHA1,
};
static const struct object_id empty_tree_oid_sha256 = {
- EMPTY_TREE_SHA256_BIN_LITERAL
+ .hash = EMPTY_TREE_SHA256_BIN_LITERAL,
+ .algo = GIT_HASH_SHA256,
};
static const struct object_id empty_blob_oid_sha256 = {
- EMPTY_BLOB_SHA256_BIN_LITERAL
+ .hash = EMPTY_BLOB_SHA256_BIN_LITERAL,
+ .algo = GIT_HASH_SHA256,
};
static void git_hash_sha1_init(git_hash_ctx *ctx)
@@ -93,6 +97,7 @@ 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);
+ oid->algo = GIT_HASH_SHA1;
}
@@ -124,6 +129,7 @@ static void git_hash_sha256_final_oid(struct object_id *oid, git_hash_ctx *ctx)
* but keep it in case we extend the hash size again.
*/
memset(oid->hash + GIT_SHA256_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA256_RAWSZ);
+ oid->algo = GIT_HASH_SHA256;
}
static void git_hash_unknown_init(git_hash_ctx *ctx)
@@ -2340,6 +2346,7 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
if (namelen == the_hash_algo->hexsz - 2 &&
!hex_to_bytes(oid.hash + 1, de->d_name,
the_hash_algo->rawsz - 1)) {
+ oid_set_algo(&oid, the_hash_algo);
if (obj_cb) {
r = obj_cb(&oid, path->buf, data);
if (r)