| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::collections::HashSet; |
| 6 | |
| 7 | const KEY: &str = "Co-authored-by"; |
| 8 | |
| 9 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 10 | pub struct CoAuthor { |
| 11 | pub name: String, |
| 12 | pub email: String, |
| 13 | } |
| 14 | |
| 15 | |
| 16 | pub 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 | |
| 27 | pub 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 | |
| 44 | fn lines(message: &str) -> Vec<&str> { |
| 45 | message.trim_end().lines().collect() |
| 46 | } |
| 47 | |
| 48 | |
| 49 | fn 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 | |
| 56 | fn trailer_lines<'a>(lines: &[&'a str]) -> Vec<&'a str> { |
| 57 | lines[last_paragraph_start(lines)..].to_vec() |
| 58 | } |
| 59 | |
| 60 | |
| 61 | fn is_trailer(line: &str) -> bool { |
| 62 | line.split_once(':') |
| 63 | .is_some_and(|(key, _)| key.trim().eq_ignore_ascii_case(KEY)) |
| 64 | } |
| 65 | |
| 66 | |
| 67 | fn 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)] |
| 87 | mod 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 | } |