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()

gix-pack/src/data/input/bytes_to_entries.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ where
177177
}
178178

179179
if let Some(hash) = self.hash.take() {
180-
let actual_id = gix_hash::ObjectId::from(hash.digest());
180+
let actual_id = hash.digest();
181181
if self.mode == input::Mode::Restore {
182182
id = actual_id;
183183
}
@@ -191,7 +191,7 @@ where
191191
Some(id)
192192
} else if self.mode == input::Mode::Restore {
193193
let hash = self.hash.clone().expect("in restore mode a hash is set");
194-
Some(gix_hash::ObjectId::from(hash.digest()))
194+
Some(hash.digest())
195195
} else {
196196
None
197197
})

gix-pack/src/data/output/bytes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ where
122122
}
123123
None => {
124124
let digest = self.output.hash.clone().digest();
125-
self.output.inner.write_all(&digest[..])?;
126-
self.written += digest.len() as u64;
125+
self.output.inner.write_all(digest.as_slice())?;
126+
self.written += digest.as_slice().len() as u64;
127127
self.output.inner.flush()?;
128128
self.is_done = true;
129-
self.trailer = Some(gix_hash::ObjectId::from(digest));
129+
self.trailer = Some(digest);
130130
}
131131
}
132132
Ok(self.written - previous_written)

gix-pack/src/index/encode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ mod function {
137137

138138
let bytes_written_without_trailer = out.bytes;
139139
let out = out.inner.into_inner()?;
140-
let index_hash: gix_hash::ObjectId = out.hash.digest().into();
140+
let index_hash = out.hash.digest();
141141
out.inner.write_all(index_hash.as_slice())?;
142142
out.inner.flush()?;
143143

gix-pack/src/index/write/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl crate::index::File {
227227
let header = crate::data::header::encode(pack_version, 0);
228228
let mut hasher = gix_hash::hasher(object_hash);
229229
hasher.update(&header);
230-
gix_hash::ObjectId::from(hasher.digest())
230+
hasher.digest()
231231
}
232232
None => return Err(Error::IteratorInvariantTrailer),
233233
};

gix-pack/src/multi_index/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl multi_index::File {
214214
}
215215

216216
// write trailing checksum
217-
let multi_index_checksum: gix_hash::ObjectId = out.inner.hash.digest().into();
217+
let multi_index_checksum = out.inner.hash.digest();
218218
out.inner.inner.write_all(multi_index_checksum.as_slice())?;
219219
out.progress.show_throughput(write_start);
220220

gix-pack/src/verify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn checksum_on_disk_or_mmap(
5252
hasher.update(&data[..data_len_without_trailer]);
5353
progress.inc_by(data_len_without_trailer);
5454
progress.show_throughput(start);
55-
gix_hash::ObjectId::from(hasher.digest())
55+
hasher.digest()
5656
}
5757
};
5858

0 commit comments

Comments
 (0)