|
1 |
| -use std::borrow::Cow; |
2 |
| - |
3 |
| -use bstr::{BStr, BString, ByteSlice, ByteVec}; |
4 |
| - |
5 | 1 | /// An empty array of a type usable with the `git::easy` API to help declaring no parents should be used
|
6 | 2 | pub const NO_PARENT_IDS: [git_hash::ObjectId; 0] = [];
|
7 |
| - |
8 |
| -/// Produce a short commit summary for the given `message`. |
9 |
| -/// |
10 |
| -/// This means the following |
11 |
| -/// |
12 |
| -/// * Take the subject line which is delimited by two newlines (\n\n) |
13 |
| -/// * transform intermediate consecutive whitespace including \r into one space |
14 |
| -/// |
15 |
| -/// The resulting summary will have folded whitespace before a newline into spaces and stopped that process |
16 |
| -/// once two consecutive newlines are encountered. |
17 |
| -pub fn summary(message: &BStr) -> Cow<'_, BStr> { |
18 |
| - let message = message.trim(); |
19 |
| - match message.find_byte(b'\n') { |
20 |
| - Some(mut pos) => { |
21 |
| - let mut out = BString::default(); |
22 |
| - let mut previous_pos = None; |
23 |
| - loop { |
24 |
| - if let Some(previous_pos) = previous_pos { |
25 |
| - if previous_pos + 1 == pos { |
26 |
| - let len_after_trim = out.trim_end().len(); |
27 |
| - out.resize(len_after_trim, 0); |
28 |
| - break out.into(); |
29 |
| - } |
30 |
| - } |
31 |
| - let message_to_newline = &message[previous_pos.map(|p| p + 1).unwrap_or(0)..pos]; |
32 |
| - |
33 |
| - if let Some(pos_before_whitespace) = message_to_newline.rfind_not_byteset(b"\t\n\x0C\r ") { |
34 |
| - out.extend_from_slice(&message_to_newline[..pos_before_whitespace + 1]); |
35 |
| - } |
36 |
| - out.push_byte(b' '); |
37 |
| - previous_pos = Some(pos); |
38 |
| - match message.get(pos + 1..).and_then(|i| i.find_byte(b'\n')) { |
39 |
| - Some(next_nl_pos) => pos += next_nl_pos + 1, |
40 |
| - None => { |
41 |
| - if let Some(slice) = message.get((pos + 1)..) { |
42 |
| - out.extend_from_slice(slice); |
43 |
| - } |
44 |
| - break out.into(); |
45 |
| - } |
46 |
| - } |
47 |
| - } |
48 |
| - } |
49 |
| - None => message.as_bstr().into(), |
50 |
| - } |
51 |
| -} |
0 commit comments