Skip to content

Commit 665ff82

Browse files
committed
migrate hashing API users to fallible versions
1 parent 4658d8a commit 665ff82

File tree

32 files changed

+115
-84
lines changed

32 files changed

+115
-84
lines changed

gix-commitgraph/src/file/verify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl File {
155155
let data_len_without_trailer = self.data.len() - self.hash_len;
156156
let mut hasher = gix_hash::hasher(self.object_hash());
157157
hasher.update(&self.data[..data_len_without_trailer]);
158-
let actual = hasher.finalize();
158+
let actual = hasher.try_finalize()?;
159159
actual.verify(self.checksum())?;
160160
Ok(actual)
161161
}

gix-diff/tests/diff/blob/pipeline.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub(crate) mod convert_to_diffable {
7272

7373
let mut db = ObjectDb::default();
7474
let b_content = "b-content";
75-
let id = db.insert(b_content);
75+
let id = db.insert(b_content)?;
7676

7777
let out = filter.convert_to_diffable(
7878
&id,
@@ -129,7 +129,7 @@ pub(crate) mod convert_to_diffable {
129129
assert_eq!(buf.len(), 0, "it should avoid querying that data in the first place");
130130

131131
let mut db = ObjectDb::default();
132-
let id = db.insert(large_content);
132+
let id = db.insert(large_content)?;
133133
let out = filter.convert_to_diffable(
134134
&id,
135135
EntryKind::Blob,
@@ -211,7 +211,7 @@ pub(crate) mod convert_to_diffable {
211211
drop(tmp);
212212

213213
let mut db = ObjectDb::default();
214-
let id = db.insert(large_content);
214+
let id = db.insert(large_content)?;
215215

216216
let out = filter.convert_to_diffable(
217217
&id,
@@ -397,7 +397,7 @@ pub(crate) mod convert_to_diffable {
397397

398398
let mut db = ObjectDb::default();
399399
let b_content = "b-content\n";
400-
let id = db.insert(b_content);
400+
let id = db.insert(b_content)?;
401401

402402
let out = filter.convert_to_diffable(
403403
&id,
@@ -416,7 +416,7 @@ pub(crate) mod convert_to_diffable {
416416

417417
let mut db = ObjectDb::default();
418418
let b_content = "b\n";
419-
let id = db.insert(b_content);
419+
let id = db.insert(b_content)?;
420420
let out = filter.convert_to_diffable(
421421
&id,
422422
EntryKind::Blob,
@@ -490,7 +490,7 @@ pub(crate) mod convert_to_diffable {
490490

491491
let mut db = ObjectDb::default();
492492
let b_content = "b-co\0ntent\n";
493-
let id = db.insert(b_content);
493+
let id = db.insert(b_content)?;
494494

495495
let out = filter.convert_to_diffable(
496496
&id,
@@ -509,7 +509,7 @@ pub(crate) mod convert_to_diffable {
509509

510510
let platform = attributes.at_entry("c", None, &gix_object::find::Never)?;
511511

512-
let id = db.insert("b");
512+
let id = db.insert("b")?;
513513
let out = filter.convert_to_diffable(
514514
&id,
515515
EntryKind::Blob,
@@ -638,7 +638,7 @@ pub(crate) mod convert_to_diffable {
638638
assert_eq!(out.data, Some(pipeline::Data::Buffer { is_derived: false }));
639639
assert_eq!(buf.as_bstr(), "a\n", "unconditionally use git according to mode");
640640

641-
let id = db.insert("a\n");
641+
let id = db.insert("a\n")?;
642642
for mode in worktree_modes {
643643
let out = filter.convert_to_diffable(
644644
&id,
@@ -714,7 +714,7 @@ pub(crate) mod convert_to_diffable {
714714
assert_eq!(buf.len(), 0, "always cleared");
715715

716716
buf.push(1);
717-
let id = db.insert("link-target");
717+
let id = db.insert("link-target")?;
718718
let out = filter.convert_to_diffable(
719719
&id,
720720
EntryKind::Link,
@@ -761,7 +761,7 @@ pub(crate) mod convert_to_diffable {
761761
assert_eq!(buf.len(), 0, "it's always cleared before any potential use");
762762
}
763763

764-
let id = db.insert("b\n");
764+
let id = db.insert("b\n")?;
765765
for mode in all_modes {
766766
buf.push(1);
767767
let out = filter.convert_to_diffable(
@@ -814,7 +814,7 @@ pub(crate) mod convert_to_diffable {
814814
);
815815
}
816816

817-
let id = db.insert("c\n");
817+
let id = db.insert("c\n")?;
818818
for mode in worktree_modes {
819819
let out = filter.convert_to_diffable(
820820
&id,
@@ -863,7 +863,7 @@ pub(crate) mod convert_to_diffable {
863863
assert_eq!(buf.len(), 0);
864864
}
865865

866-
let id = db.insert("unset\n");
866+
let id = db.insert("unset\n")?;
867867
for mode in all_modes {
868868
let out = filter.convert_to_diffable(
869869
&id,
@@ -890,7 +890,7 @@ pub(crate) mod convert_to_diffable {
890890
}
891891

892892
let platform = attributes.at_entry("d", None, &gix_object::find::Never)?;
893-
let id = db.insert("d-in-db");
893+
let id = db.insert("d-in-db")?;
894894
for mode in worktree_modes {
895895
let out = filter.convert_to_diffable(
896896
&null,
@@ -959,7 +959,7 @@ pub(crate) mod convert_to_diffable {
959959
"no text filter, so git conversion was applied for worktree source"
960960
);
961961

962-
let id = db.insert("e-in-db");
962+
let id = db.insert("e-in-db")?;
963963
let out = filter.convert_to_diffable(
964964
&id,
965965
EntryKind::Blob,

gix-diff/tests/diff/blob/platform.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn resources_of_worktree_and_odb_and_check_link() -> crate::Result {
3030

3131
let mut db = ObjectDb::default();
3232
let a_content = "a-content";
33-
let id = db.insert(a_content);
33+
let id = db.insert(a_content)?;
3434
platform.set_resource(
3535
id,
3636
EntryKind::BlobExecutable,
@@ -194,7 +194,7 @@ fn diff_binary() -> crate::Result {
194194

195195
let mut db = ObjectDb::default();
196196
let a_content = "b";
197-
let id = db.insert(a_content);
197+
let id = db.insert(a_content)?;
198198
platform.set_resource(id, EntryKind::Blob, "b".into(), ResourceKind::NewOrDestination, &db)?;
199199

200200
let out = platform.prepare_diff()?;
@@ -235,7 +235,7 @@ fn diff_performed_despite_external_command() -> crate::Result {
235235

236236
let mut db = ObjectDb::default();
237237
let a_content = "b";
238-
let id = db.insert(a_content);
238+
let id = db.insert(a_content)?;
239239
platform.set_resource(id, EntryKind::Blob, "b".into(), ResourceKind::NewOrDestination, &db)?;
240240

241241
let out = platform.prepare_diff()?;
@@ -277,7 +277,7 @@ fn diff_skipped_due_to_external_command_and_enabled_option() -> crate::Result {
277277

278278
let mut db = ObjectDb::default();
279279
let a_content = "b";
280-
let id = db.insert(a_content);
280+
let id = db.insert(a_content)?;
281281
platform.set_resource(id, EntryKind::Blob, "b".into(), ResourceKind::NewOrDestination, &db)?;
282282

283283
let out = platform.prepare_diff()?;

gix-diff/tests/diff/main.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ mod util {
5151

5252
impl ObjectDb {
5353
/// Insert `data` and return its hash. That can be used to find it again.
54-
pub fn insert(&mut self, data: &str) -> gix_hash::ObjectId {
55-
let id = gix_object::compute_hash(gix_hash::Kind::Sha1, gix_object::Kind::Blob, data.as_bytes());
54+
pub fn insert(&mut self, data: &str) -> Result<gix_hash::ObjectId, Error> {
55+
let id = gix_object::try_compute_hash(gix_hash::Kind::Sha1, gix_object::Kind::Blob, data.as_bytes())?;
5656
self.data_by_id.insert(id, data.into());
57-
id
57+
Ok(id)
5858
}
5959
}
6060
}

gix-diff/tests/diff/rewrites/tracker.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn copy_by_similarity_reports_limit_if_encountered() -> crate::Result {
9292
(Change::addition(), "a-cpy-2", "a"),
9393
(Change::modification(), "d", "ab"),
9494
],
95-
);
95+
)?;
9696

9797
let mut calls = 0;
9898
let out = util::assert_emit_with_objects(
@@ -145,7 +145,7 @@ fn copy_by_id() -> crate::Result {
145145
(Change::addition(), "a-cpy-2", "a"),
146146
(Change::modification(), "d", "a"),
147147
],
148-
);
148+
)?;
149149

150150
let mut calls = 0;
151151
let out = util::assert_emit_with_objects(
@@ -218,7 +218,7 @@ fn copy_by_id_search_in_all_sources() -> crate::Result {
218218
(Change::addition(), "a-cpy-1", "a"),
219219
(Change::addition(), "a-cpy-2", "a"),
220220
],
221-
);
221+
)?;
222222

223223
let mut calls = 0;
224224
let content_id = hex_to_id("2e65efe2a145dda7ee51d1741299f848e5bf752e");
@@ -299,7 +299,7 @@ fn copy_by_50_percent_similarity() -> crate::Result {
299299
(Change::addition(), "a-cpy-2", "a\nc"),
300300
(Change::modification(), "d", "a"),
301301
],
302-
);
302+
)?;
303303

304304
let mut calls = 0;
305305
let out = util::assert_emit_with_objects(
@@ -377,7 +377,7 @@ fn copy_by_id_in_additions_only() -> crate::Result {
377377
(Change::modification(), "a", "a"),
378378
(Change::modification(), "a-cpy-1", "a"),
379379
],
380-
);
380+
)?;
381381

382382
let mut calls = 0;
383383
let out = util::assert_emit_with_objects(
@@ -429,7 +429,7 @@ fn rename_by_similarity_reports_limit_if_encountered() -> crate::Result {
429429
(Change::addition(), "b", "firt\nsecond\n"),
430430
(Change::addition(), "c", "second\nunrelated\n"),
431431
],
432-
);
432+
)?;
433433

434434
let mut calls = 0;
435435
let out = util::assert_emit_with_objects(
@@ -475,7 +475,7 @@ fn rename_by_50_percent_similarity() -> crate::Result {
475475
(Change::addition(), "b", "firt\nsecond\n"),
476476
(Change::addition(), "c", "second\nunrelated\n"),
477477
],
478-
);
478+
)?;
479479

480480
let mut calls = 0;
481481
let out = util::assert_emit_with_objects(
@@ -596,7 +596,7 @@ fn directory_renames_by_id_can_fail_gracefully() -> crate::Result {
596596
(Change::deletion(), "a", "first\nsecond\n"),
597597
(Change::addition(), "b", "firt\nsecond\n"),
598598
],
599-
);
599+
)?;
600600

601601
let mut calls = 0;
602602
let out = util::assert_emit_with_objects(
@@ -803,16 +803,16 @@ mod util {
803803
pub fn add_retained_blobs<'a>(
804804
tracker: &mut rewrites::Tracker<Change>,
805805
blobs: impl IntoIterator<Item = (Change, &'a str, &'a str)>,
806-
) -> ObjectDb {
806+
) -> crate::Result<ObjectDb> {
807807
let mut db = ObjectDb::default();
808808
for (mut change, location, data) in blobs {
809-
change.id = db.insert(data);
809+
change.id = db.insert(data)?;
810810
assert!(
811811
tracker.try_push_change(change, location.into()).is_none(),
812812
"input changes must be tracked"
813813
);
814814
}
815-
db
815+
Ok(db)
816816
}
817817

818818
pub fn assert_emit(

gix-filter/src/ident.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn apply(src: &[u8], object_hash: gix_hash::Kind, buf: &mut Vec<u8>) -> Resu
6868
while let Some(pos) = src[ofs..].find(b"$Id$") {
6969
let id = match id {
7070
None => {
71-
let new_id = gix_object::compute_hash(object_hash, gix_object::Kind::Blob, src);
71+
let new_id = gix_object::try_compute_hash(object_hash, gix_object::Kind::Blob, src)?;
7272
id = new_id.into();
7373
clear_and_set_capacity(buf, src.len() + HASH_LEN)?; // pre-allocate for one ID
7474
new_id

gix-hash/src/hasher/io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub fn bytes_with_hasher(
7676
}
7777
}
7878

79-
let id = hasher.finalize();
79+
let id = hasher.try_finalize()?;
8080
progress.show_throughput(start);
8181
Ok(id)
8282
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn decode(data: &[u8], object_hash: gix_hash::Kind) -> Result<Option<usize>,
4444
last_chunk = Some(chunk);
4545
}
4646

47-
if hasher.finalize().verify(checksum).is_err() {
47+
if hasher.try_finalize()?.verify(checksum).is_err() {
4848
return Ok(None);
4949
}
5050
// The last-to-this chunk ends where ours starts

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

+1-1
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.finalize().as_slice())?;
26+
out.write_all(hasher.try_finalize()?.as_slice())?;
2727

2828
Ok(())
2929
}

gix-index/src/file/write.rs

+1-1
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, hasher.hash.finalize())
34+
(version, hasher.hash.try_finalize()?)
3535
};
3636
out.write_all(hash.as_slice())?;
3737
Ok((version, hash))

gix-merge/tests/merge/blob/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ mod util {
4343

4444
impl ObjectDb {
4545
/// Insert `data` and return its hash. That can be used to find it again.
46-
pub fn insert(&mut self, data: &str) -> gix_hash::ObjectId {
47-
let id = gix_object::compute_hash(gix_hash::Kind::Sha1, gix_object::Kind::Blob, data.as_bytes());
46+
pub fn insert(&mut self, data: &str) -> Result<gix_hash::ObjectId, Error> {
47+
let id = gix_object::try_compute_hash(gix_hash::Kind::Sha1, gix_object::Kind::Blob, data.as_bytes())?;
4848
self.data_by_id.insert(id, data.into());
49-
id
49+
Ok(id)
5050
}
5151
}
5252
}

gix-merge/tests/merge/blob/pipeline.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn without_transformation() -> crate::Result {
6767

6868
let mut db = ObjectDb::default();
6969
let b_content = "b-content";
70-
let id = db.insert(b_content);
70+
let id = db.insert(b_content)?;
7171

7272
let out = filter.convert_to_mergeable(
7373
&id,
@@ -148,7 +148,7 @@ fn binary_below_large_file_threshold() -> crate::Result {
148148
assert_eq!(buf.as_bstr(), binary_content);
149149

150150
let mut db = ObjectDb::default();
151-
let id = db.insert(binary_content);
151+
let id = db.insert(binary_content)?;
152152
let out = filter.convert_to_mergeable(
153153
&id,
154154
EntryKind::Blob,
@@ -203,7 +203,7 @@ fn above_large_file_threshold() -> crate::Result {
203203

204204
drop(tmp);
205205
let mut db = ObjectDb::default();
206-
let id = db.insert(large_content);
206+
let id = db.insert(large_content)?;
207207

208208
let out = filter.convert_to_mergeable(
209209
&id,
@@ -352,7 +352,7 @@ fn worktree_filter() -> crate::Result {
352352
"worktree files need to be converted back to what's stored in Git"
353353
);
354354

355-
let id = db.insert(a_content);
355+
let id = db.insert(a_content)?;
356356
let out = filter.convert_to_mergeable(
357357
&id,
358358
EntryKind::Blob,
@@ -385,7 +385,7 @@ fn worktree_filter() -> crate::Result {
385385
drop(tmp);
386386

387387
let b_content = "b-content\n";
388-
let id = db.insert(b_content);
388+
let id = db.insert(b_content)?;
389389

390390
let out = filter.convert_to_mergeable(
391391
&id,
@@ -403,7 +403,7 @@ fn worktree_filter() -> crate::Result {
403403

404404
let mut db = ObjectDb::default();
405405
let b_content = "b-content\r\n";
406-
let id = db.insert(b_content);
406+
let id = db.insert(b_content)?;
407407
let out = filter.convert_to_mergeable(
408408
&id,
409409
EntryKind::Blob,

0 commit comments

Comments
 (0)