forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.010.5 MiB0c29382Tahinli1 month ago279 commits

trailers.rs122 lines, 3.7 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(
34 lines[start..]
35 .iter()
36 .copied()
37 .filter(|line| !is_trailer(line)),
38 )
39 .collect();
40 kept.join("\n").trim_end().to_owned()
41}
42
43/// The message's lines with trailing whitespace removed from the end of the whole message.
44fn lines(message: &str) -> Vec<&str> {
45 message.trim_end().lines().collect()
46}
47
48/// Index of the first line of the last paragraph; 0 when there is no blank line.
49fn last_paragraph_start(lines: &[&str]) -> usize {
50 lines
51 .iter()
52 .rposition(|line| line.trim().is_empty())
53 .map_or(0, |i| i + 1)
54}
55
56fn trailer_lines<'a>(lines: &[&'a str]) -> Vec<&'a str> {
57 lines[last_paragraph_start(lines)..].to_vec()
58}
59
60/// Whether a line's key is `Co-authored-by`, in any letter case.
61fn is_trailer(line: &str) -> bool {
62 line.split_once(':')
63 .is_some_and(|(key, _)| key.trim().eq_ignore_ascii_case(KEY))
64}
65
66/// Parses `Co-authored-by: Name <email>`. Lines without a non-empty `<email>` give `None`.
67fn parse(line: &str) -> Option<CoAuthor> {
68 let (key, value) = line.trim().split_once(':')?;
69 if !key.trim().eq_ignore_ascii_case(KEY) {
70 return None;
71 }
72 let value = value.trim();
73 let open = value.find('<')?;
74 let close = open + value[open..].find('>')?;
75 let email = value[open + 1..close].trim();
76 if email.is_empty() {
77 return None;
78 }
79 let name = value[..open].trim();
80 Some(CoAuthor {
81 name: if name.is_empty() { email } else { name }.to_owned(),
82 email: email.to_owned(),
83 })
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn co_authors_from_the_trailer_block_only() {
92 let msg = "Co-authored-by: Old <old@x.io>\n\nSubject\n\nBody\n\n\
93 co-authored-by: Ann <ANN@x.io>\n\
94 CO-AUTHORED-BY: Bob <bob@x.io> \n\
95 Co-authored-by: Ann Again <ann@x.io>\n\
96 Co-authored-by: No Email\n\
97 Co-authored-by: Empty <>\n";
98 let got = co_authors(msg);
99 assert_eq!(
100 got,
101 vec![
102 CoAuthor {
103 name: "Ann".into(),
104 email: "ANN@x.io".into()
105 },
106 CoAuthor {
107 name: "Bob".into(),
108 email: "bob@x.io".into()
109 },
110 ]
111 );
112 }
113
114 #[test]
115 fn strip_removes_trailers_in_last_paragraph() {
116 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";
117 assert_eq!(
118 strip_co_authors(msg),
119 "Subject\n\nCo-authored-by: Mid <mid@x.io>\nKept\n\nSigned-off-by: Me"
120 );
121 }
122}