use crate::error::BridgeError;
use crate::gitobj::{GitObject, GitType};
use crate::reconstruct;
use mkit_core::object::Object;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShallowVerdict {
Verified,
Unsigned,
Failed,
}
pub fn shallow_verify(obj: &GitObject) -> Result<ShallowVerdict, BridgeError> {
let rec = match obj.gtype {
GitType::Commit => reconstruct::reconstruct_commit(&obj.body)?,
GitType::Tag => reconstruct::reconstruct_tag(&obj.body)?,
GitType::Blob | GitType::Tree => {
return Err(BridgeError::NotBridgeObject(
"shallow verification applies to commits and tags only".into(),
));
}
};
Ok(match rec.object {
Object::Commit(ref c) => {
if c.signature == [0u8; 64] {
ShallowVerdict::Unsigned
} else if mkit_core::sign::verify_commit(c).is_ok() {
ShallowVerdict::Verified
} else {
ShallowVerdict::Failed
}
}
Object::Tag(ref t) => {
if t.signature == [0u8; 64] {
ShallowVerdict::Unsigned
} else if mkit_core::sign::verify_tag(t).is_ok() {
ShallowVerdict::Verified
} else {
ShallowVerdict::Failed
}
}
_ => unreachable!("reconstruct_commit/tag return Commit/Tag"),
})
}