Skip to content

Commit fef386e

Browse files
committed
change!: make the hashing API return ObjectId
The vast majority of users of this API immediately converted the result to `ObjectId`, so this eliminates a lot of cruft across the tree. It also has a nicer `Debug` and `Display` instance than `[u8; 20]`, and should theoretically make supporting the hash function transition easier, although I suspect further API changes will be required for that anyway. I wasn’t sure whether this would be a good change, as not every digest identifies an entry in the Git object database, but even many of the existing uses for non‐object digests across the tree used the `ObjectId` API anyway. Perhaps it would be best to have a separate non‐alias `Digest` type that `ObjectId` wraps, but this seems like the pragmatic choice for now that sticks with current practice.
1 parent f0e9ee1 commit fef386e

File tree

16 files changed

+21
-21
lines changed

16 files changed

+21
-21
lines changed

gix-commitgraph/src/file/verify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl File {
147147
let data_len_without_trailer = self.data.len() - self.hash_len;
148148
let mut hasher = gix_hash::hasher(self.object_hash());
149149
hasher.update(&self.data[..data_len_without_trailer]);
150-
let actual = gix_hash::ObjectId::from_bytes_or_panic(hasher.digest().as_ref());
150+
let actual = hasher.digest();
151151

152152
let expected = self.checksum();
153153
if actual == expected {

gix-hash/src/hasher/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub fn bytes_with_hasher(
6666
}
6767
}
6868

69-
let id = crate::ObjectId::from(hasher.digest());
69+
let id = hasher.digest();
7070
progress.show_throughput(start);
7171
Ok(id)
7272
}

gix-hash/src/hasher/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ impl Hasher {
77
pub fn update(&mut self, bytes: &[u8]) {
88
self.0.update(bytes);
99
}
10-
/// Finalize the hash and produce a digest.
11-
pub fn digest(self) -> gix_features::hash::Digest {
12-
self.0.digest()
10+
/// Finalize the hash and produce an object ID.
11+
pub fn digest(self) -> crate::ObjectId {
12+
self.0.digest().into()
1313
}
1414
}
1515

gix-hash/tests/object_id/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ mod sha1 {
5050
fn hash_contents(s: &[u8]) -> ObjectId {
5151
let mut hasher = hasher(Kind::Sha1);
5252
hasher.update(s);
53-
ObjectId::Sha1(hasher.digest())
53+
hasher.digest()
5454
}
5555

5656
#[test]

gix-index/src/extension/end_of_index_entry/decode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn decode(data: &[u8], object_hash: gix_hash::Kind) -> Option<usize> {
4141
last_chunk = Some(chunk);
4242
}
4343

44-
if hasher.digest() != checksum {
44+
if hasher.digest().as_slice() != checksum {
4545
return None;
4646
}
4747
// The last-to-this chunk ends where ours starts

gix-index/src/extension/end_of_index_entry/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn write_to(
2323
hasher.update(&signature);
2424
hasher.update(&size.to_be_bytes());
2525
}
26-
out.write_all(&hasher.digest())?;
26+
out.write_all(hasher.digest().as_slice())?;
2727

2828
Ok(())
2929
}

gix-index/src/file/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl File {
3131
let mut hasher = hasher::io::Write::new(&mut out, self.state.object_hash);
3232
let out: &mut dyn std::io::Write = &mut hasher;
3333
let version = self.state.write_to(out, options)?;
34-
(version, gix_hash::ObjectId::from(hasher.hash.digest()))
34+
(version, hasher.hash.digest())
3535
};
3636
out.write_all(hash.as_slice())?;
3737
Ok((version, hash))

gix-object/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ fn object_hasher(hash_kind: gix_hash::Kind, object_kind: Kind, object_size: u64)
409409
pub fn compute_hash(hash_kind: gix_hash::Kind, object_kind: Kind, data: &[u8]) -> gix_hash::ObjectId {
410410
let mut hasher = object_hasher(hash_kind, object_kind, data.len() as u64);
411411
hasher.update(data);
412-
hasher.digest().into()
412+
hasher.digest()
413413
}
414414

415415
/// A function to compute a hash of kind `hash_kind` for an object of `object_kind` and its data read from `stream`

gix-odb/src/sink.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ impl gix_object::Write for Sink {
5353
c.reset();
5454
}
5555

56-
Ok(hasher.digest().into())
56+
Ok(hasher.digest())
5757
}
5858
}

gix-odb/src/store_impls/loose/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl Store {
130130
&self,
131131
hasher::io::Write { hash, inner: file }: hasher::io::Write<CompressedTempfile>,
132132
) -> Result<gix_hash::ObjectId, Error> {
133-
let id = gix_hash::ObjectId::from(hash.digest());
133+
let id = hash.digest();
134134
let object_path = loose::hash_path(&id, self.path.clone());
135135
let object_dir = object_path
136136
.parent()

0 commit comments

Comments
 (0)