|
| 1 | +use gix_features::hash; |
| 2 | +use gix_hash::ObjectId; |
| 3 | +use gix_index as index; |
| 4 | +use gix_object::encode::loose_header; |
| 5 | +use index::Entry; |
| 6 | + |
| 7 | +/// |
| 8 | +pub trait LazyBlob<'a, E> { |
| 9 | + /// |
| 10 | + fn read(self) -> Result<&'a [u8], E>; |
| 11 | +} |
| 12 | + |
| 13 | +/// |
| 14 | +pub trait Diff: Send + Sync { |
| 15 | + /// |
| 16 | + type Output; |
| 17 | + /// |
| 18 | + fn content_changed<'a, E>( |
| 19 | + &self, |
| 20 | + entry: &'a Entry, |
| 21 | + blob_size: usize, |
| 22 | + blob: impl LazyBlob<'a, E>, |
| 23 | + resolve_oid: impl FnMut(gix_hash::ObjectId) -> Result<&'a [u8], E>, |
| 24 | + ) -> Result<Option<Self::Output>, E>; |
| 25 | +} |
| 26 | + |
| 27 | +/// compares to blobs by comparing their size and oid very fast |
| 28 | +pub struct Fast; |
| 29 | + |
| 30 | +impl Diff for Fast { |
| 31 | + type Output = (); |
| 32 | + |
| 33 | + fn content_changed<'a, E>( |
| 34 | + &self, |
| 35 | + entry: &'a Entry, |
| 36 | + blob_size: usize, |
| 37 | + blob: impl LazyBlob<'a, E>, |
| 38 | + _resolve_oid: impl FnMut(gix_hash::ObjectId) -> Result<&'a [u8], E>, |
| 39 | + ) -> Result<Option<Self::Output>, E> { |
| 40 | + // make sure to account for racily smudged entries here |
| 41 | + // so that they don't always keep showing up as modified even |
| 42 | + // after their contents have changed again (to a potentially unmodified state) |
| 43 | + // that means that we want to ignore stat.size == 0 for non_empty_blobs |
| 44 | + if entry.stat.size as usize != blob_size && (entry.id.is_empty_blob() || entry.stat.size != 0) { |
| 45 | + return Ok(Some(())); |
| 46 | + } |
| 47 | + let blob = blob.read()?; |
| 48 | + 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 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/// compares to blobs by comparing their oid |
| 62 | +/// Same as [`FastEq`] but always |
| 63 | +pub struct Hash; |
| 64 | + |
| 65 | +impl Diff for Hash { |
| 66 | + type Output = ObjectId; |
| 67 | + |
| 68 | + fn content_changed<'a, E>( |
| 69 | + &self, |
| 70 | + entry: &'a Entry, |
| 71 | + _blob_size: usize, |
| 72 | + blob: impl LazyBlob<'a, E>, |
| 73 | + _resolve_oid: impl FnMut(gix_hash::ObjectId) -> Result<&'a [u8], E>, |
| 74 | + ) -> Result<Option<Self::Output>, E> { |
| 75 | + let blob = blob.read()?; |
| 76 | + 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 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments