@@ -24,7 +24,8 @@ pub trait Diff: Send + Sync {
2424 ) -> Result < Option < Self :: Output > , E > ;
2525}
2626
27- /// compares to blobs by comparing their size and oid very fast
27+ /// compares to blobs by comparing their size and oid, only looks at the file if
28+ /// the size matches, therefore very fast
2829pub struct Fast ;
2930
3031impl Diff for Fast {
@@ -46,20 +47,17 @@ impl Diff for Fast {
4647 }
4748 let blob = blob. read ( ) ?;
4849 let header = loose_header ( gix_object:: Kind :: Blob , blob. len ( ) ) ;
49- match entry. id {
50- ObjectId :: Sha1 ( entry_hash) => {
51- let mut file_hash = hash:: Sha1 :: default ( ) ;
52- file_hash. update ( & header) ;
53- file_hash. update ( blob) ;
54- let file_hash = file_hash. digest ( ) ;
55- Ok ( ( entry_hash != file_hash) . then_some ( ( ) ) )
56- }
57- }
50+ let mut hasher = hash:: hasher ( entry. id . kind ( ) ) ;
51+ hasher. update ( & header) ;
52+ hasher. update ( blob) ;
53+ let file_hash: ObjectId = hasher. digest ( ) . into ( ) ;
54+ Ok ( ( entry. id != file_hash) . then_some ( ( ) ) )
5855 }
5956}
6057
61- /// compares to blobs by comparing their oid
62- /// Same as [`FastEq`] but always
58+ /// Compares files to blobs by comparing their oids. Same as [`Fast`] but does
59+ /// not contain a fast path for files with mismatched files and therefore always
60+ /// returns an OID that can be reused later
6361pub struct Hash ;
6462
6563impl Diff for Hash {
@@ -74,14 +72,10 @@ impl Diff for Hash {
7472 ) -> Result < Option < Self :: Output > , E > {
7573 let blob = blob. read ( ) ?;
7674 let header = loose_header ( gix_object:: Kind :: Blob , blob. len ( ) ) ;
77- match entry. id {
78- ObjectId :: Sha1 ( entry_hash) => {
79- let mut file_hash = hash:: Sha1 :: default ( ) ;
80- file_hash. update ( & header) ;
81- file_hash. update ( blob) ;
82- let file_hash = file_hash. digest ( ) ;
83- Ok ( ( entry_hash != file_hash) . then_some ( ObjectId :: Sha1 ( file_hash) ) )
84- }
85- }
75+ let mut hasher = hash:: hasher ( entry. id . kind ( ) ) ;
76+ hasher. update ( & header) ;
77+ hasher. update ( blob) ;
78+ let file_hash: ObjectId = hasher. digest ( ) . into ( ) ;
79+ Ok ( ( entry. id != file_hash) . then_some ( file_hash) )
8680 }
8781}
0 commit comments