gix_object/commit/message/
mod.rs

1use std::borrow::Cow;
2
3use crate::{
4    bstr::{BStr, BString, ByteSlice, ByteVec},
5    commit::MessageRef,
6    CommitRef,
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    /// May be empty.
21    pub fn message_trailers(&self) -> body::Trailers<'a> {
22        BodyRef::from_bytes(self.message).trailers()
23    }
24}
25
26impl<'a> MessageRef<'a> {
27    /// Parse the given `input` as message.
28    ///
29    /// Note that this cannot fail as everything will be interpreted as title if there is no body separator.
30    pub fn from_bytes(input: &'a [u8]) -> Self {
31        let (title, body) = decode::message(input);
32        MessageRef { title, body }
33    }
34
35    /// Produce a short commit summary for the message title.
36    ///
37    /// This means the following
38    ///
39    /// * Take the subject line which is delimited by two newlines (\n\n)
40    /// * transform intermediate consecutive whitespace including \r into one space
41    ///
42    /// The resulting summary will have folded whitespace before a newline into spaces and stopped that process
43    /// once two consecutive newlines are encountered.
44    pub fn summary(&self) -> Cow<'a, BStr> {
45        summary(self.title)
46    }
47
48    /// Further parse the body into into non-trailer and trailers, which can be iterated from the returned [`BodyRef`].
49    pub fn body(&self) -> Option<BodyRef<'a>> {
50        self.body.map(|b| BodyRef::from_bytes(b))
51    }
52}
53
54pub(crate) fn summary(message: &BStr) -> Cow<'_, BStr> {
55    let message = message.trim();
56    match message.find_byte(b'\n') {
57        Some(mut pos) => {
58            let mut out = BString::default();
59            let mut previous_pos = None;
60            loop {
61                if let Some(previous_pos) = previous_pos {
62                    if previous_pos + 1 == pos {
63                        let len_after_trim = out.trim_end().len();
64                        out.resize(len_after_trim, 0);
65                        break out.into();
66                    }
67                }
68                let message_to_newline = &message[previous_pos.map_or(0, |p| p + 1)..pos];
69
70                if let Some(pos_before_whitespace) = message_to_newline.rfind_not_byteset(b"\t\n\x0C\r ") {
71                    out.extend_from_slice(&message_to_newline[..=pos_before_whitespace]);
72                }
73                out.push_byte(b' ');
74                previous_pos = Some(pos);
75                match message.get(pos + 1..).and_then(|i| i.find_byte(b'\n')) {
76                    Some(next_nl_pos) => pos += next_nl_pos + 1,
77                    None => {
78                        if let Some(slice) = message.get((pos + 1)..) {
79                            out.extend_from_slice(slice);
80                        }
81                        break out.into();
82                    }
83                }
84            }
85        }
86        None => message.as_bstr().into(),
87    }
88}
89
90/// A reference to a message body, further parsed to only contain the non-trailer parts.
91///
92/// See [git-interpret-trailers](https://git-scm.com/docs/git-interpret-trailers) for more information
93/// on what constitutes trailers and not that this implementation is only good for typical sign-off footer or key-value parsing.
94///
95/// Note that we only parse trailers from the bottom of the body.
96#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
97pub struct BodyRef<'a> {
98    body_without_trailer: &'a BStr,
99    start_of_trailer: &'a [u8],
100}