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