1use bstr::{BStr, ByteSlice};
2
3use crate::{Commit, CommitRef, TagRef};
4
5mod decode;
6pub mod message;
8
9#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
13#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
14pub struct MessageRef<'a> {
15 #[cfg_attr(feature = "serde1", serde(borrow))]
17 pub title: &'a BStr,
18 pub body: Option<&'a BStr>,
22}
23
24pub mod ref_iter;
26
27mod write;
28
29impl<'a> CommitRef<'a> {
30 pub fn from_bytes(data: &'a [u8]) -> Result<CommitRef<'a>, crate::decode::Error> {
32 decode::commit(data).map(|(_, t)| t).map_err(crate::decode::Error::from)
33 }
34 pub fn tree(&self) -> git_hash::ObjectId {
36 git_hash::ObjectId::from_hex(self.tree).expect("prior validation of tree hash during parsing")
37 }
38
39 pub fn parents(&self) -> impl Iterator<Item = git_hash::ObjectId> + '_ {
41 self.parents
42 .iter()
43 .map(|hex_hash| git_hash::ObjectId::from_hex(hex_hash).expect("prior validation of hashes during parsing"))
44 }
45
46 pub fn extra_headers(&self) -> crate::commit::ExtraHeaders<impl Iterator<Item = (&BStr, &BStr)>> {
48 crate::commit::ExtraHeaders::new(self.extra_headers.iter().map(|(k, v)| (*k, v.as_ref())))
49 }
50
51 pub fn author(&self) -> git_actor::SignatureRef<'a> {
55 self.author.trim()
56 }
57
58 pub fn committer(&self) -> git_actor::SignatureRef<'a> {
62 self.committer.trim()
63 }
64
65 pub fn message(&self) -> MessageRef<'a> {
67 MessageRef::from_bytes(self.message)
68 }
69
70 pub fn time(&self) -> git_actor::Time {
72 self.committer.time
73 }
74}
75
76impl Commit {
77 pub fn extra_headers(&self) -> ExtraHeaders<impl Iterator<Item = (&BStr, &BStr)>> {
79 ExtraHeaders::new(self.extra_headers.iter().map(|(k, v)| (k.as_bstr(), v.as_bstr())))
80 }
81}
82
83pub struct ExtraHeaders<I> {
85 inner: I,
86}
87
88impl<'a, I> ExtraHeaders<I>
90where
91 I: Iterator<Item = (&'a BStr, &'a BStr)>,
92{
93 pub fn new(iter: I) -> Self {
95 ExtraHeaders { inner: iter }
96 }
97 pub fn find(mut self, name: &str) -> Option<&'a BStr> {
99 self.inner
100 .find_map(move |(k, v)| if k == name.as_bytes().as_bstr() { Some(v) } else { None })
101 }
102 pub fn find_all(self, name: &'a str) -> impl Iterator<Item = &'a BStr> {
104 self.inner
105 .filter_map(move |(k, v)| if k == name.as_bytes().as_bstr() { Some(v) } else { None })
106 }
107 pub fn mergetags(self) -> impl Iterator<Item = Result<TagRef<'a>, crate::decode::Error>> {
112 self.find_all("mergetag").map(|b| TagRef::from_bytes(b))
113 }
114
115 pub fn pgp_signature(self) -> Option<&'a BStr> {
117 self.find("gpgsig")
118 }
119}