1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use crate::{hash::Write as HashWrite, loose};
use git_object::{borrowed, owned};
use std::io;

/// Returned by [`loose::Object::verify_checksum()`]
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]
pub enum Error {
    #[error("reading of object failed")]
    Io(#[from] io::Error),
    #[error("Decoding of object failed")]
    Decode(#[from] super::decode::Error),
    #[error("Object expected to have id {desired}, but actual id was {actual}")]
    ChecksumMismatch { desired: owned::Id, actual: owned::Id },
}

impl loose::Object {
    /// Generate the git hash of this object, reading it in the process, and compare it with the given `desired` [Id][borrowed::Id].
    ///
    /// Returns an error with the actual id if the hashes don't match.
    pub fn verify_checksum(&mut self, desired: borrowed::Id<'_>) -> Result<(), Error> {
        let mut sink = HashWrite::new(io::sink(), desired.kind());
        let (kind, size) = (self.kind, self.size);
        let mut reader = self.stream()?;

        loose::object::header::encode(kind, size as u64, &mut sink).expect("hash to always work");
        io::copy(&mut reader, &mut sink)?;

        let actual = owned::Id::from(sink.hash.digest());
        if desired != actual.to_borrowed() {
            return Err(Error::ChecksumMismatch {
                desired: desired.into(),
                actual,
            });
        }
        Ok(())
    }
}