forge
Explore
Log in

Don't you dare go Hollow

Commit pages show Co-authored-by co-authors next to the author and hide the trailer lines

Browse files at this commit

f558fe8bb2724a7423ec14cd357a679b69db05d3Ahmet Kaan GümüşParent: 5d31584
Some checks failed
Show checks
  • feat
    • commit pages show Co-authored-by co-authors next to the author and hide the trailer lines
Show full message
✨ feat: commit pages show Co-authored-by co-authors next to the author and hide the trailer lines
crates/forge-git/src/lib.rsmodified+1 -0
@@ -17,6 +17,7 @@ pub mod search;
1717 pub mod service;
1818 pub mod setup;
1919 pub mod tags;
20+pub mod trailers;
2021 pub mod verify;
2122 pub mod version;
2223 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 {
8989 DetailsLink,
9090 AuthorLabel,
9191 CommitterLabel,
92+ CoAuthorAnd,
93+ CoAuthorsLabel,
9294 RootCommit,
9395 ParentLabel,
9496 BrowseFiles,
@@ -293,6 +295,10 @@ pub fn t(lang: Lang, key: K) -> &'static str {
293295 (AuthorLabel, Tr) => "Yazar ",
294296 (CommitterLabel, En) => "Committer ",
295297 (CommitterLabel, Tr) => "Commit eden ",
298+ (CoAuthorAnd, En) => " and ",
299+ (CoAuthorAnd, Tr) => " ve ",
300+ (CoAuthorsLabel, En) => "Co-authors",
301+ (CoAuthorsLabel, Tr) => "Ortak yazarlar",
296302 (RootCommit, En) => "Root commit (no parents)",
297303 (RootCommit, Tr) => "Kök commit (üst commit yok)",
298304 (ParentLabel, En) => "Parent: ",
@@ -490,6 +496,14 @@ pub fn star_label(lang: Lang, starred: bool, stars: u64) -> String {
490496 }
491497 }
492498
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+
493507 /// `<n> watching` in English, `<n> izleyici` in Turkish.
494508 pub fn watching_count(lang: Lang, watchers: u64) -> String {
495509 match lang {
@@ -608,6 +622,8 @@ mod tests {
608622 K::DetailsLink,
609623 K::AuthorLabel,
610624 K::CommitterLabel,
625+ K::CoAuthorAnd,
626+ K::CoAuthorsLabel,
611627 K::RootCommit,
612628 K::ParentLabel,
613629 K::BrowseFiles,
crates/forge-web/src/pages/commit.rsmodified+94 -4
@@ -3,6 +3,7 @@
33 use forge_core::tz::format_in_tz;
44 use forge_git::browse;
55 use forge_git::history;
6+use forge_git::trailers;
67 use topcoat::Result;
78 use topcoat::context::{Cx, app_context};
89 use topcoat::router::error::{content_too_large, not_found};
@@ -20,7 +21,7 @@ use crate::signatures;
2021 use crate::state::AppState;
2122 use crate::views::commit_status::{StatusLang, StatusSummary, checks_summary};
2223 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};
2425 use crate::views::shell;
2526
2627 path_param!(owner);
@@ -40,6 +41,16 @@ struct ParentLink {
4041 short: String,
4142 }
4243
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+
4354 fn is_hex_prefix(s: &str) -> bool {
4455 (4..=64).contains(&s.len()) && s.bytes().all(|b| b.is_ascii_hexdigit())
4556 }
@@ -155,13 +166,52 @@ async fn commit_page(cx: &Cx) -> Result<impl View> {
155166 })
156167 .collect();
157168 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);
159209 let title = format!("{owner_name}/{repo_name}");
160210 let repo_href = format!("/{owner_name}/{repo_name}");
161211 let tree_href = format!("/{owner_name}/{repo_name}/tree/{}", info.oid);
162212 let summary = emojify(&info.summary).into_owned();
163213 let full_oid = info.oid.clone();
164- let message = emojify(&info.message).into_owned();
214+ let message = emojify(&body).into_owned();
165215 let statuses = state.store.statuses_for(&ctx.repo.id, &full_oid).await?;
166216 let checks = (!statuses.is_empty()).then(|| StatusSummary::from_rows(&statuses));
167217 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> {
193243 if let Some(summary) = checks {
194244 checks_summary(summary: summary, lang: StatusLang(lang))
195245 }
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>
197287 if committer_differs {
198288 <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>
199289 }
style/_repo.scssmodified+26 -0
@@ -929,6 +929,32 @@
929929 }
930930 }
931931
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+
932958 // The full commit hash in its copy box: one line, truncated with an ellipsis when the screen is
933959 // narrow, so the hash never breaks in the middle. The Copy button always copies the whole hash.
934960 .commit-info .commit-full-oid {