Commit pages show Co-authored-by co-authors next to the author and hide the trailer lines
Browse files at this commitf558fe8bb2724a7423ec14cd357a679b69db05d3Parent: 5d31584- feat
- commit pages show Co-authored-by co-authors next to the author and hide the trailer lines
5 changed files +254 -4
- crates/forge-git/src/lib.rs +1 -0
- crates/forge-git/src/trailers.rs +117 -0
- crates/forge-web/src/i18n/code.rs +16 -0
- crates/forge-web/src/pages/commit.rs +94 -4
- style/_repo.scss +26 -0
crates/forge-git/src/lib.rsmodified+1 -0
| @@ -17,6 +17,7 @@ pub mod search; | ||
| 17 | 17 | pub mod service; |
| 18 | 18 | pub mod setup; |
| 19 | 19 | pub mod tags; |
| 20 | +pub mod trailers; | |
| 20 | 21 | pub mod verify; |
| 21 | 22 | pub mod version; |
| 22 | 23 | pub mod wiki; |
crates/forge-git/src/trailers.rsadded+117 -0
| @@ -0,0 +1,117 @@ | ||
| 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 | + | |
| 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 | +/// Co-authors from the message's trailer block, in order, deduplicated by lowercase email. | |
| 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 | +/// The message with every `Co-authored-by:` line removed and trailing blank lines trimmed. | |
| 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(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. | |
| 39 | +fn 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. | |
| 44 | +fn 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 | + | |
| 51 | +fn 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. | |
| 56 | +fn 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`. | |
| 62 | +fn 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)] | |
| 82 | +mod 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 | +} |
crates/forge-web/src/i18n/code.rsmodified+16 -0
| @@ -89,6 +89,8 @@ pub enum K { | ||
| 89 | 89 | DetailsLink, |
| 90 | 90 | AuthorLabel, |
| 91 | 91 | CommitterLabel, |
| 92 | + CoAuthorAnd, | |
| 93 | + CoAuthorsLabel, | |
| 92 | 94 | RootCommit, |
| 93 | 95 | ParentLabel, |
| 94 | 96 | BrowseFiles, |
| @@ -293,6 +295,10 @@ pub fn t(lang: Lang, key: K) -> &'static str { | ||
| 293 | 295 | (AuthorLabel, Tr) => "Yazar ", |
| 294 | 296 | (CommitterLabel, En) => "Committer ", |
| 295 | 297 | (CommitterLabel, Tr) => "Commit eden ", |
| 298 | + (CoAuthorAnd, En) => " and ", | |
| 299 | + (CoAuthorAnd, Tr) => " ve ", | |
| 300 | + (CoAuthorsLabel, En) => "Co-authors", | |
| 301 | + (CoAuthorsLabel, Tr) => "Ortak yazarlar", | |
| 296 | 302 | (RootCommit, En) => "Root commit (no parents)", |
| 297 | 303 | (RootCommit, Tr) => "Kök commit (üst commit yok)", |
| 298 | 304 | (ParentLabel, En) => "Parent: ", |
| @@ -490,6 +496,14 @@ pub fn star_label(lang: Lang, starred: bool, stars: u64) -> String { | ||
| 490 | 496 | } |
| 491 | 497 | } |
| 492 | 498 | |
| 499 | +/// `+<n> others` for co-authors folded behind a disclosure. | |
| 500 | +pub fn co_author_others(lang: Lang, count: usize) -> String { | |
| 501 | + match lang { | |
| 502 | + Lang::En => format!("+{count} others"), | |
| 503 | + Lang::Tr => format!("+{count} kişi daha"), | |
| 504 | + } | |
| 505 | +} | |
| 506 | + | |
| 493 | 507 | /// `<n> watching` in English, `<n> izleyici` in Turkish. |
| 494 | 508 | pub fn watching_count(lang: Lang, watchers: u64) -> String { |
| 495 | 509 | match lang { |
| @@ -608,6 +622,8 @@ mod tests { | ||
| 608 | 622 | K::DetailsLink, |
| 609 | 623 | K::AuthorLabel, |
| 610 | 624 | K::CommitterLabel, |
| 625 | + K::CoAuthorAnd, | |
| 626 | + K::CoAuthorsLabel, | |
| 611 | 627 | K::RootCommit, |
| 612 | 628 | K::ParentLabel, |
| 613 | 629 | K::BrowseFiles, |
crates/forge-web/src/pages/commit.rsmodified+94 -4
| @@ -3,6 +3,7 @@ | ||
| 3 | 3 | use forge_core::tz::format_in_tz; |
| 4 | 4 | use forge_git::browse; |
| 5 | 5 | use forge_git::history; |
| 6 | +use forge_git::trailers; | |
| 6 | 7 | use topcoat::Result; |
| 7 | 8 | use topcoat::context::{Cx, app_context}; |
| 8 | 9 | use topcoat::router::error::{content_too_large, not_found}; |
| @@ -20,7 +21,7 @@ use crate::signatures; | ||
| 20 | 21 | use crate::state::AppState; |
| 21 | 22 | use crate::views::commit_status::{StatusLang, StatusSummary, checks_summary}; |
| 22 | 23 | use crate::views::diff::{DiffLang, DiffOpts, LIMITS, diff_section}; |
| 23 | -use crate::views::relative_time; | |
| 24 | +use crate::views::{avatar_url, display_name, relative_time, user_href}; | |
| 24 | 25 | use crate::views::shell; |
| 25 | 26 | |
| 26 | 27 | path_param!(owner); |
| @@ -40,6 +41,16 @@ struct ParentLink { | ||
| 40 | 41 | short: String, |
| 41 | 42 | } |
| 42 | 43 | |
| 44 | +/// One `Co-authored-by` person: linked and avatar-shown when a verified user has the email. | |
| 45 | +struct CoAuthorView { | |
| 46 | + name: String, | |
| 47 | + email: String, | |
| 48 | + href: Option<String>, | |
| 49 | + avatar: Option<String>, | |
| 50 | + /// Whether a comma separates this one from the entry before it. | |
| 51 | + sep: bool, | |
| 52 | +} | |
| 53 | + | |
| 43 | 54 | fn is_hex_prefix(s: &str) -> bool { |
| 44 | 55 | (4..=64).contains(&s.len()) && s.bytes().all(|b| b.is_ascii_hexdigit()) |
| 45 | 56 | } |
| @@ -155,13 +166,52 @@ async fn commit_page(cx: &Cx) -> Result<impl View> { | ||
| 155 | 166 | }) |
| 156 | 167 | .collect(); |
| 157 | 168 | let is_root = parents.is_empty(); |
| 158 | - let has_body = !info.message.trim().is_empty(); | |
| 169 | + let body = trailers::strip_co_authors(&info.message); | |
| 170 | + let has_body = !body.trim().is_empty(); | |
| 171 | + let mut coauthors: Vec<CoAuthorView> = Vec::new(); | |
| 172 | + for co in trailers::co_authors(&info.message) { | |
| 173 | + if co.email.eq_ignore_ascii_case(&info.author_email) { | |
| 174 | + continue; | |
| 175 | + } | |
| 176 | + let user = state | |
| 177 | + .store | |
| 178 | + .verified_user_by_email(&co.email) | |
| 179 | + .await | |
| 180 | + .ok() | |
| 181 | + .flatten(); | |
| 182 | + let (name, href, avatar) = match user { | |
| 183 | + Some(user) => ( | |
| 184 | + display_name(lang, &user.username), | |
| 185 | + Some(user_href(&user.username)), | |
| 186 | + Some(avatar_url(&user.username, user.avatar_hash.as_deref())), | |
| 187 | + ), | |
| 188 | + None => (co.name.clone(), None, None), | |
| 189 | + }; | |
| 190 | + let sep = !coauthors.is_empty(); | |
| 191 | + coauthors.push(CoAuthorView { | |
| 192 | + name, | |
| 193 | + email: co.email, | |
| 194 | + href, | |
| 195 | + avatar, | |
| 196 | + sep, | |
| 197 | + }); | |
| 198 | + } | |
| 199 | + // Three or more: the first is shown, the rest sit behind a "+N others" disclosure. | |
| 200 | + let has_coauthors = !coauthors.is_empty(); | |
| 201 | + let folded = coauthors.len() >= 3; | |
| 202 | + let rest: Vec<CoAuthorView> = if folded { | |
| 203 | + coauthors.split_off(1) | |
| 204 | + } else { | |
| 205 | + Vec::new() | |
| 206 | + }; | |
| 207 | + let others = g::co_author_others(lang, rest.len()); | |
| 208 | + let coauthors_label = g::t(lang, K::CoAuthorsLabel); | |
| 159 | 209 | let title = format!("{owner_name}/{repo_name}"); |
| 160 | 210 | let repo_href = format!("/{owner_name}/{repo_name}"); |
| 161 | 211 | let tree_href = format!("/{owner_name}/{repo_name}/tree/{}", info.oid); |
| 162 | 212 | let summary = emojify(&info.summary).into_owned(); |
| 163 | 213 | let full_oid = info.oid.clone(); |
| 164 | - let message = emojify(&info.message).into_owned(); | |
| 214 | + let message = emojify(&body).into_owned(); | |
| 165 | 215 | let statuses = state.store.statuses_for(&ctx.repo.id, &full_oid).await?; |
| 166 | 216 | let checks = (!statuses.is_empty()).then(|| StatusSummary::from_rows(&statuses)); |
| 167 | 217 | let sig = signatures::badges(state, &ctx.repo, std::slice::from_ref(&full_oid)) |
| @@ -193,7 +243,47 @@ async fn commit_page(cx: &Cx) -> Result<impl View> { | ||
| 193 | 243 | if let Some(summary) = checks { |
| 194 | 244 | checks_summary(summary: summary, lang: StatusLang(lang)) |
| 195 | 245 | } |
| 196 | - <p class="muted">(g::t(lang, K::AuthorLabel)) <strong>(author.name.as_str())</strong> " · " <time data-rel="true" datetime=(author.iso.as_str()) title=(author.absolute.as_str())>(author.relative.as_str())</time></p> | |
| 246 | + <div class="muted commit-author-line">(g::t(lang, K::AuthorLabel)) <strong>(author.name.as_str())</strong> | |
| 247 | + if has_coauthors { | |
| 248 | + (g::t(lang, K::CoAuthorAnd)) | |
| 249 | + for c in coauthors.iter() { | |
| 250 | + if c.sep { ", " } | |
| 251 | + if let Some(href) = c.href.as_ref() { | |
| 252 | + <a class="coauthor" href=(href.as_str())> | |
| 253 | + if let Some(src) = c.avatar.as_ref() { | |
| 254 | + <img class="avatar coauthor-avatar" src=(src.as_str()) alt="" width="16" height="16"> | |
| 255 | + } | |
| 256 | + (c.name.as_str()) | |
| 257 | + </a> | |
| 258 | + } else { | |
| 259 | + <span class="coauthor" title=(c.email.as_str())>(c.name.as_str())</span> | |
| 260 | + } | |
| 261 | + } | |
| 262 | + if folded { | |
| 263 | + ", " | |
| 264 | + <details class="coauthors-more"> | |
| 265 | + <summary>(others.as_str())</summary> | |
| 266 | + <ul class="coauthors-list" aria-label=(coauthors_label)> | |
| 267 | + for c in rest.iter() { | |
| 268 | + <li> | |
| 269 | + if let Some(href) = c.href.as_ref() { | |
| 270 | + <a class="coauthor" href=(href.as_str())> | |
| 271 | + if let Some(src) = c.avatar.as_ref() { | |
| 272 | + <img class="avatar coauthor-avatar" src=(src.as_str()) alt="" width="16" height="16"> | |
| 273 | + } | |
| 274 | + (c.name.as_str()) | |
| 275 | + </a> | |
| 276 | + } else { | |
| 277 | + <span class="coauthor" title=(c.email.as_str())>(c.name.as_str())</span> | |
| 278 | + } | |
| 279 | + </li> | |
| 280 | + } | |
| 281 | + </ul> | |
| 282 | + </details> | |
| 283 | + } | |
| 284 | + } | |
| 285 | + " · " <time data-rel="true" datetime=(author.iso.as_str()) title=(author.absolute.as_str())>(author.relative.as_str())</time> | |
| 286 | + </div> | |
| 197 | 287 | if committer_differs { |
| 198 | 288 | <p class="muted">(g::t(lang, K::CommitterLabel)) <strong>(committer.name.as_str())</strong> " · " <time data-rel="true" datetime=(committer.iso.as_str()) title=(committer.absolute.as_str())>(committer.relative.as_str())</time></p> |
| 199 | 289 | } |
style/_repo.scssmodified+26 -0
| @@ -929,6 +929,32 @@ | ||
| 929 | 929 | } |
| 930 | 930 | } |
| 931 | 931 | |
| 932 | +// Co-authors on the author line: small avatars, and the "+N others" disclosure inline. | |
| 933 | +.commit-author-line { | |
| 934 | + .coauthor-avatar { | |
| 935 | + width: 16px; | |
| 936 | + height: 16px; | |
| 937 | + margin-right: 0.3em; | |
| 938 | + vertical-align: -3px; | |
| 939 | + } | |
| 940 | + | |
| 941 | + .coauthors-more { | |
| 942 | + display: inline; | |
| 943 | + | |
| 944 | + > summary { | |
| 945 | + display: inline; | |
| 946 | + cursor: pointer; | |
| 947 | + color: var(--muted); | |
| 948 | + text-decoration: underline dotted; | |
| 949 | + } | |
| 950 | + | |
| 951 | + .coauthors-list { | |
| 952 | + margin: 6px 0 0; | |
| 953 | + padding-left: 1.2em; | |
| 954 | + } | |
| 955 | + } | |
| 956 | +} | |
| 957 | + | |
| 932 | 958 | // The full commit hash in its copy box: one line, truncated with an ellipsis when the screen is |
| 933 | 959 | // narrow, so the hash never breaks in the middle. The Copy button always copies the whole hash. |
| 934 | 960 | .commit-info .commit-full-oid { |