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