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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use smallvec::SmallVec;
use std::borrow::Cow;
use crate::{immutable::object, BStr};
mod decode;
pub mod iter;
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Commit<'a> {
#[cfg_attr(feature = "serde1", serde(borrow))]
pub tree: &'a BStr,
pub parents: SmallVec<[&'a BStr; 2]>,
pub author: git_actor::immutable::Signature<'a>,
pub committer: git_actor::immutable::Signature<'a>,
pub encoding: Option<&'a BStr>,
pub message: &'a BStr,
pub extra_headers: Vec<(&'a BStr, Cow<'a, BStr>)>,
}
impl<'a> Commit<'a> {
pub fn from_bytes(data: &'a [u8]) -> Result<Commit<'a>, object::decode::Error> {
decode::commit(data)
.map(|(_, t)| t)
.map_err(object::decode::Error::from)
}
pub fn tree(&self) -> git_hash::ObjectId {
git_hash::ObjectId::from_hex(self.tree).expect("prior validation of tree hash during parsing")
}
pub fn parents(&self) -> impl Iterator<Item = git_hash::ObjectId> + '_ {
self.parents
.iter()
.map(|hex_hash| git_hash::ObjectId::from_hex(hex_hash).expect("prior validation of hashes during parsing"))
}
pub fn extra_headers(&self) -> crate::commit::ExtraHeaders<impl Iterator<Item = (&BStr, &BStr)>> {
crate::commit::ExtraHeaders::new(self.extra_headers.iter().map(|(k, v)| (*k, v.as_ref())))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn size_of_commit() {
assert_eq!(
std::mem::size_of::<Commit<'_>>(),
216,
"the size of an immutable commit shouldn't change unnoticed"
);
}
}