forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.010.5 MiB8e56ffcTahinli1 month ago262 commits

trailers.rs117 lines, 3.6 KiBHistoryRawDownload
1//! `Co-authored-by:` trailers in a commit message.
2//!
3//! Trailers live in the message's last paragraph (the text after its last blank line).
4
5use std::collections::HashSet;
6
7const KEY: &str = "Co-authored-by";
8
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub struct CoAuthor {
11 pub name: String,
12 pub email: String,
13}
14
15/// Co-authors from the message's trailer block, in order, deduplicated by lowercase email.
16pub fn co_authors(message: &str) -> Vec<CoAuthor> {
17 let lines = lines(message);
18 let mut seen = HashSet::new();
19 trailer_lines(&lines)
20 .iter()
21 .filter_map(|line| parse(line))
22 .filter(|author| seen.insert(author.email.to_lowercase()))
23 .collect()
24}
25
26/// The message with every `Co-authored-by:` line removed and trailing blank lines trimmed.
27pub fn strip_co_authors(message: &str) -> String {
28 let lines = lines(message);
29 let start = last_paragraph_start(&lines);
30 let kept: Vec<&str> = lines[..start]
31 .iter()
32 .copied()
33 .chain(lines[start..].iter().copied().filter(|line| !is_trailer(line)))
34 .collect();
35 kept.join("\n").trim_end().to_owned()
36}
37
38/// The message's lines with trailing whitespace removed from the end of the whole message.
39fn lines(message: &str) -> Vec<&str> {
40 message.trim_end().lines().collect()
41}
42
43/// Index of the first line of the last paragraph; 0 when there is no blank line.
44fn last_paragraph_start(lines: &[&str]) -> usize {
45 lines
46 .iter()
47 .rposition(|line| line.trim().is_empty())
48 .map_or(0, |i| i + 1)
49}
50
51fn trailer_lines<'a>(lines: &[&'a str]) -> Vec<&'a str> {
52 lines[last_paragraph_start(lines)..].to_vec()
53}
54
55/// Whether a line's key is `Co-authored-by`, in any letter case.
56fn is_trailer(line: &str) -> bool {
57 line.split_once(':')
58 .is_some_and(|(key, _)| key.trim().eq_ignore_ascii_case(KEY))
59}
60
61/// Parses `Co-authored-by: Name <email>`. Lines without a non-empty `<email>` give `None`.
62fn parse(line: &str) -> Option<CoAuthor> {
63 let (key, value) = line.trim().split_once(':')?;
64 if !key.trim().eq_ignore_ascii_case(KEY) {
65 return None;
66 }
67 let value = value.trim();
68 let open = value.find('<')?;
69 let close = open + value[open..].find('>')?;
70 let email = value[open + 1..close].trim();
71 if email.is_empty() {
72 return None;
73 }
74 let name = value[..open].trim();
75 Some(CoAuthor {
76 name: if name.is_empty() { email } else { name }.to_owned(),
77 email: email.to_owned(),
78 })
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn co_authors_from_the_trailer_block_only() {
87 let msg = "Co-authored-by: Old <old@x.io>\n\nSubject\n\nBody\n\n\
88 co-authored-by: Ann <ANN@x.io>\n\
89 CO-AUTHORED-BY: Bob <bob@x.io> \n\
90 Co-authored-by: Ann Again <ann@x.io>\n\
91 Co-authored-by: No Email\n\
92 Co-authored-by: Empty <>\n";
93 let got = co_authors(msg);
94 assert_eq!(
95 got,
96 vec![
97 CoAuthor {
98 name: "Ann".into(),
99 email: "ANN@x.io".into()
100 },
101 CoAuthor {
102 name: "Bob".into(),
103 email: "bob@x.io".into()
104 },
105 ]
106 );
107 }
108
109 #[test]
110 fn strip_removes_trailers_in_last_paragraph() {
111 let msg = "Subject\n\nCo-authored-by: Mid <mid@x.io>\nKept\n\nSigned-off-by: Me\nCo-authored-by: Ann <ann@x.io>\n\n\n";
112 assert_eq!(
113 strip_co_authors(msg),
114 "Subject\n\nCo-authored-by: Mid <mid@x.io>\nKept\n\nSigned-off-by: Me"
115 );
116 }
117}