-
-
Notifications
You must be signed in to change notification settings - Fork 408
Add ObjectId::Sha256 and Kind::Sha256
#2292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Byron
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems it's totally possible to start a new review per commit, sorry for that.
Generally, I think this kind of 'manual' testing is perfectly fine. Ideally we reach the same test-coverage for SHA256 as for SHA1, while maybe even covering more ground or doing some cleanup. This crate is one of the oldest, and it probably shows.
| #[test] | ||
| fn it_detects_equality() { | ||
| #[cfg(feature = "sha256")] | ||
| fn it_detects_inequality_sha256() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be interesting to see inequality between hashes of different kinds. Of course we know this will always be true, but… it's to be sure? Maybe make both have the same prefix, so an incorrect implementation could get it wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes total sense! Are you suggesting a test such as the following one?
#[test]
#[cfg(all(feature = "sha1", feature = "sha256"))]
fn it_detects_inequality_sha1_and_sha256() {
let prefix_sha1 = gix_hash::Prefix::new(&hex_to_id("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"), 7).unwrap();
let prefix_sha256 = gix_hash::Prefix::new(
&hex_to_id("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
7,
)
.unwrap();
assert_eq!(prefix_sha256.cmp(&prefix_sha1), Ordering::Greater);
assert_eq!(prefix_sha1.to_string(), "bbbbbbb");
assert_eq!(prefix_sha256.to_string(), "aaaaaaa");
}
Prefix has #[derive(Ord, PartialOrd)], so this is what the default implementation gives us.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that would be it.
We'd probably also want to ensure we have a test that turns both on at the same time then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not entirely sure I understand what you mean by “a test that turns both on at the same time”. Can you elaborate? :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I meant a test that runs if both feature toggles are enabled so both hashes are available.
Byron
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And my second comment, this should have been part of the 'review' above.

This is a draft that adds
ObjectId::Sha256andKind::Sha256togix-hash. I’m opening this to get early feedback, in particular from a CI run.Some remarks:
empty_blob_sha256andempty_tree_sha256intooidas the comment saysoidis related toSha1, but I’m unsure how to best resolve this tension.SHA256,SHA-256,Sha256.ObjectId::null_sha1hardcodes 20. Should this beSIZE_OF_SHA1_DIGEST?oid::null_sha1usesSIZE_OF_SHA1_DIGEST.Kind::len_in_hexandKind::len_in_bytesalso hardcode values.ObjectIdwould probably benefit from being rephrased. See e. g.ObjectId::new_sha1orObjectId::from_20_bytes.Sources for the hardcoded values related to SHA-256:
https://github.com/git/git/blob/e85ae279b0d58edc2f4c3fd5ac391b51e1223985/hash.h
https://github.com/git/git/blob/e85ae279b0d58edc2f4c3fd5ac391b51e1223985/hash.c