gix_object/commit/
mod.rs

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