Skip to main content

gix_object/commit/message/
mod.rs

1use std::borrow::Cow;
2
3use crate::{
4    CommitRef,
5    bstr::{BStr, BString, ByteSlice, ByteVec},
6    commit::MessageRef,
7};
8
9///
10pub mod body;
11mod decode;
12
13impl<'a> CommitRef<'a> {
14    /// Return exactly the same message as [`MessageRef::summary()`].
15    pub fn message_summary(&self) -> Cow<'a, BStr> {
16        summary(self.message)
17    }
18
19    /// Return an iterator over message trailers as obtained from the last paragraph of the commit message.
20    /// Maybe empty.
21    pub fn message_trailers(&self) -> body::Trailers<'a> {
22        MessageRef::from_bytes(self.message)
23            .body()
24            .map_or(body::Trailers { cursor: &[] }, |body| body.trailers())
25    }
26}
27
28/// Convenience methods
29impl<'a> CommitRef<'a> {
30    /// Get an iterator over all `Signed-off-by` trailers in the commit message.
31    /// This is useful for finding who signed off on the commit.
32    pub fn signed_off_by_trailers(&self) -> impl Iterator<Item = body::TrailerRef<'a>> {
33        self.message_trailers().signed_off_by()
34    }
35
36    /// Get an iterator over `Co-authored-by` trailers in the commit message.
37    /// This is useful for squashed commits that contain multiple authors.
38    pub fn co_authored_by_trailers(&self) -> impl Iterator<Item = body::TrailerRef<'a>> {
39        self.message_trailers().co_authored_by()
40    }
41
42    /// Get an iterator over `Assisted-by` trailers in the commit message.
43    /// This is useful for identifying agents that assisted with a commit.
44    pub fn assisted_by_trailers(&self) -> impl Iterator<Item = body::TrailerRef<'a>> {
45        self.message_trailers().assisted_by()
46    }
47
48    /// Get all authors mentioned in `Signed-off-by` and `Co-authored-by` trailers.
49    /// This is useful for squashed commits that contain multiple authors.
50    /// Returns a Vec of author strings that can include both signers and co-authors.
51    pub fn author_trailers(&self) -> impl Iterator<Item = body::TrailerRef<'a>> {
52        self.message_trailers().authors()
53    }
54
55    /// Get an iterator over all attribution-related trailers
56    /// (`Signed-off-by,` `Co-authored-by`, `Assisted-by`, `Acked-by`, `Reviewed-by`, `Tested-by`).
57    /// This provides a comprehensive view of everyone who contributed to or reviewed the commit.
58    /// Note that the same name may occur multiple times, it's not a unified list.
59    pub fn attribution_trailers(&self) -> impl Iterator<Item = body::TrailerRef<'a>> {
60        self.message_trailers().attributions()
61    }
62}
63
64impl<'a> MessageRef<'a> {
65    /// Parse the given `input` as a message.
66    ///
67    /// Note that this cannot fail as everything will be interpreted as title if there is no body separator.
68    pub fn from_bytes(input: &'a [u8]) -> Self {
69        let (title, body) = decode::message_title_and_body(input);
70        MessageRef { title, body }
71    }
72
73    /// Produce a short commit summary for the message title.
74    ///
75    /// This means the following
76    ///
77    /// * Take the subject line which is delimited by two newlines (\n\n)
78    /// * transform intermediate consecutive whitespace including \r into one space
79    ///
80    /// The resulting summary will have folded whitespace before a newline into spaces and stopped that process
81    /// once two consecutive newlines are encountered.
82    pub fn summary(&self) -> Cow<'a, BStr> {
83        summary(self.title)
84    }
85
86    /// Further parse the body into non-trailer and trailers, which can be iterated from the returned [`BodyRef`].
87    pub fn body(&self) -> Option<BodyRef<'a>> {
88        self.body.map(|b| BodyRef::from_bytes(b))
89    }
90}
91
92pub(crate) fn summary(message: &BStr) -> Cow<'_, BStr> {
93    let message = message.trim();
94    match message.find_byte(b'\n') {
95        Some(mut pos) => {
96            let mut out = BString::default();
97            let mut previous_pos = None;
98            loop {
99                if let Some(previous_pos) = previous_pos {
100                    if previous_pos + 1 == pos {
101                        let len_after_trim = out.trim_end().len();
102                        out.resize(len_after_trim, 0);
103                        break out.into();
104                    }
105                }
106                let message_to_newline = &message[previous_pos.map_or(0, |p| p + 1)..pos];
107
108                if let Some(pos_before_whitespace) = message_to_newline.rfind_not_byteset(b"\t\n\x0C\r ") {
109                    out.extend_from_slice(&message_to_newline[..=pos_before_whitespace]);
110                }
111                out.push_byte(b' ');
112                previous_pos = Some(pos);
113                match message.get(pos + 1..).and_then(|i| i.find_byte(b'\n')) {
114                    Some(next_nl_pos) => pos += next_nl_pos + 1,
115                    None => {
116                        if let Some(slice) = message.get((pos + 1)..) {
117                            out.extend_from_slice(slice);
118                        }
119                        break out.into();
120                    }
121                }
122            }
123        }
124        None => message.as_bstr().into(),
125    }
126}
127
128/// A reference to a message body, further parsed to only contain the non-trailer parts.
129///
130/// See [git-interpret-trailers](https://git-scm.com/docs/git-interpret-trailers) for more information
131/// on what constitutes trailers and not that this implementation is only good for typical sign-off footer or key-value parsing.
132///
133/// Note that we only parse trailers from the bottom of the body.
134#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
135pub struct BodyRef<'a> {
136    body: &'a BStr,
137    trailer_start: usize,
138}