1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::borrow::Cow;
use crate::{
bstr::{BStr, BString, ByteSlice, ByteVec},
commit::MessageRef,
CommitRef,
};
pub mod body;
mod decode;
impl<'a> CommitRef<'a> {
pub fn message_summary(&self) -> Cow<'a, BStr> {
summary(self.message)
}
pub fn message_trailers(&self) -> body::Trailers<'a> {
BodyRef::from_bytes(self.message).trailers()
}
}
impl<'a> MessageRef<'a> {
pub fn from_bytes(input: &'a [u8]) -> Self {
let (title, body) = decode::message(input);
MessageRef { title, body }
}
pub fn summary(&self) -> Cow<'a, BStr> {
summary(self.title)
}
pub fn body(&self) -> Option<BodyRef<'a>> {
self.body.map(|b| BodyRef::from_bytes(b))
}
}
pub(crate) fn summary(message: &BStr) -> Cow<'_, BStr> {
let message = message.trim();
match message.find_byte(b'\n') {
Some(mut pos) => {
let mut out = BString::default();
let mut previous_pos = None;
loop {
if let Some(previous_pos) = previous_pos {
if previous_pos + 1 == pos {
let len_after_trim = out.trim_end().len();
out.resize(len_after_trim, 0);
break out.into();
}
}
let message_to_newline = &message[previous_pos.map(|p| p + 1).unwrap_or(0)..pos];
if let Some(pos_before_whitespace) = message_to_newline.rfind_not_byteset(b"\t\n\x0C\r ") {
out.extend_from_slice(&message_to_newline[..pos_before_whitespace + 1]);
}
out.push_byte(b' ');
previous_pos = Some(pos);
match message.get(pos + 1..).and_then(|i| i.find_byte(b'\n')) {
Some(next_nl_pos) => pos += next_nl_pos + 1,
None => {
if let Some(slice) = message.get((pos + 1)..) {
out.extend_from_slice(slice);
}
break out.into();
}
}
}
}
None => message.as_bstr().into(),
}
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
pub struct BodyRef<'a> {
body_without_trailer: &'a BStr,
start_of_trailer: &'a [u8],
}