gix_object/commit/
mod.rs

1use std::ops::Range;
2
3use bstr::{BStr, BString, ByteSlice};
4use winnow::prelude::*;
5
6use crate::parse::parse_signature;
7use crate::{Commit, CommitRef, TagRef};
8
9/// The well-known field name for gpg signatures.
10pub const SIGNATURE_FIELD_NAME: &str = "gpgsig";
11
12mod decode;
13///
14pub mod message;
15
16/// A parsed commit message that assumes a title separated from the body by two consecutive newlines.
17///
18/// Titles can have any amount of whitespace
19#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
20#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
21pub struct MessageRef<'a> {
22    /// The title of the commit, as separated from the body with two consecutive newlines. The newlines are not included.
23    #[cfg_attr(feature = "serde", serde(borrow))]
24    pub title: &'a BStr,
25    /// All bytes not consumed by the title, excluding the separating newlines.
26    ///
27    /// The body is `None` if there was now title separation or the body was empty after the separator.
28    pub body: Option<&'a BStr>,
29}
30
31/// The raw commit data, parseable by [`CommitRef`] or [`Commit`], which was fed into a program to produce a signature.
32///
33/// See [`extract_signature()`](crate::CommitRefIter::signature()) for how to obtain it.
34// TODO: implement `std::io::Read` to avoid allocations
35#[derive(PartialEq, Eq, Debug, Hash, Clone)]
36#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
37pub struct SignedData<'a> {
38    /// The raw commit data that includes the signature.
39    data: &'a [u8],
40    /// The byte range at which we find the signature. All but the signature is the data that was signed.
41    signature_range: Range<usize>,
42}
43
44impl SignedData<'_> {
45    /// Convenience method to obtain a copy of the signed data.
46    pub fn to_bstring(&self) -> BString {
47        let mut buf = BString::from(&self.data[..self.signature_range.start]);
48        buf.extend_from_slice(&self.data[self.signature_range.end..]);
49        buf
50    }
51}
52
53impl From<SignedData<'_>> for BString {
54    fn from(value: SignedData<'_>) -> Self {
55        value.to_bstring()
56    }
57}
58
59///
60pub mod ref_iter;
61
62mod write;
63
64/// Lifecycle
65impl<'a> CommitRef<'a> {
66    /// Deserialize a commit from the given `data` bytes while avoiding most allocations.
67    pub fn from_bytes(mut data: &'a [u8]) -> Result<CommitRef<'a>, crate::decode::Error> {
68        let input = &mut data;
69        match decode::commit.parse_next(input) {
70            Ok(tag) => Ok(tag),
71            Err(err) => Err(crate::decode::Error::with_err(err, input)),
72        }
73    }
74}
75
76/// Access
77impl<'a> CommitRef<'a> {
78    /// Return the `tree` fields hash digest.
79    pub fn tree(&self) -> gix_hash::ObjectId {
80        gix_hash::ObjectId::from_hex(self.tree).expect("prior validation of tree hash during parsing")
81    }
82
83    /// Returns an iterator of parent object ids
84    pub fn parents(&self) -> impl Iterator<Item = gix_hash::ObjectId> + '_ {
85        self.parents
86            .iter()
87            .map(|hex_hash| gix_hash::ObjectId::from_hex(hex_hash).expect("prior validation of hashes during parsing"))
88    }
89
90    /// Returns a convenient iterator over all extra headers.
91    pub fn extra_headers(&self) -> ExtraHeaders<impl Iterator<Item = (&BStr, &BStr)>> {
92        ExtraHeaders::new(self.extra_headers.iter().map(|(k, v)| (*k, v.as_ref())))
93    }
94
95    /// Return the author, with whitespace trimmed.
96    ///
97    /// This is different from the `author` field which may contain whitespace.
98    pub fn author(&self) -> Result<gix_actor::SignatureRef<'a>, crate::decode::Error> {
99        parse_signature(self.author).map(|signature| signature.trim())
100    }
101
102    /// Return the committer, with whitespace trimmed.
103    ///
104    /// This is different from the `committer` field which may contain whitespace.
105    pub fn committer(&self) -> Result<gix_actor::SignatureRef<'a>, crate::decode::Error> {
106        parse_signature(self.committer).map(|signature| signature.trim())
107    }
108
109    /// Returns a partially parsed message from which more information can be derived.
110    pub fn message(&self) -> MessageRef<'a> {
111        MessageRef::from_bytes(self.message)
112    }
113
114    /// Returns the time at which this commit was created, or a default time if it could not be parsed.
115    pub fn time(&self) -> Result<gix_date::Time, crate::decode::Error> {
116        parse_signature(self.committer).map(|signature| signature.time().unwrap_or_default())
117    }
118}
119
120/// Conversion
121impl CommitRef<'_> {
122    /// Copy all fields of this instance into a fully owned commit, consuming this instance.
123    pub fn into_owned(self) -> Result<Commit, crate::decode::Error> {
124        self.try_into()
125    }
126
127    /// Copy all fields of this instance into a fully owned commit, internally cloning this instance.
128    pub fn to_owned(self) -> Result<Commit, crate::decode::Error> {
129        self.try_into()
130    }
131}
132
133impl Commit {
134    /// Returns a convenient iterator over all extra headers.
135    pub fn extra_headers(&self) -> ExtraHeaders<impl Iterator<Item = (&BStr, &BStr)>> {
136        ExtraHeaders::new(self.extra_headers.iter().map(|(k, v)| (k.as_bstr(), v.as_bstr())))
137    }
138}
139
140/// An iterator over extra headers in [owned][crate::Commit] and [borrowed][crate::CommitRef] commits.
141pub struct ExtraHeaders<I> {
142    inner: I,
143}
144
145/// Instantiation and convenience.
146impl<'a, I> ExtraHeaders<I>
147where
148    I: Iterator<Item = (&'a BStr, &'a BStr)>,
149{
150    /// Create a new instance from an iterator over tuples of (name, value) pairs.
151    pub fn new(iter: I) -> Self {
152        ExtraHeaders { inner: iter }
153    }
154
155    /// Find the _value_ of the _first_ header with the given `name`.
156    pub fn find(mut self, name: &str) -> Option<&'a BStr> {
157        self.inner
158            .find_map(move |(k, v)| if k == name.as_bytes().as_bstr() { Some(v) } else { None })
159    }
160
161    /// Find the entry index with the given name, or return `None` if unavailable.
162    pub fn find_pos(self, name: &str) -> Option<usize> {
163        self.inner
164            .enumerate()
165            .find_map(|(pos, (field, _value))| (field == name).then_some(pos))
166    }
167
168    /// Return an iterator over all _values_ of headers with the given `name`.
169    pub fn find_all(self, name: &'a str) -> impl Iterator<Item = &'a BStr> {
170        self.inner
171            .filter_map(move |(k, v)| if k == name.as_bytes().as_bstr() { Some(v) } else { None })
172    }
173
174    /// Return an iterator over all git mergetags.
175    ///
176    /// A merge tag is a tag object embedded within the respective header field of a commit, making
177    /// it a child object of sorts.
178    pub fn mergetags(self) -> impl Iterator<Item = Result<TagRef<'a>, crate::decode::Error>> {
179        self.find_all("mergetag").map(|b| TagRef::from_bytes(b))
180    }
181
182    /// Return the cryptographic signature provided by gpg/pgp verbatim.
183    pub fn pgp_signature(self) -> Option<&'a BStr> {
184        self.find(SIGNATURE_FIELD_NAME)
185    }
186}