From f558fe8bb2724a7423ec14cd357a679b69db05d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 27 Oct 2026 05:44:14 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20commit=20pages=20show=20Co-?= =?UTF-8?q?authored-by=20co-authors=20next=20to=20the=20author=20and=20hid?= =?UTF-8?q?e=20the=20trailer=20lines?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/forge-git/src/lib.rs | 1 + crates/forge-git/src/trailers.rs | 117 +++++++++++++++++++++++++++ crates/forge-web/src/i18n/code.rs | 16 ++++ crates/forge-web/src/pages/commit.rs | 98 +++++++++++++++++++++- style/_repo.scss | 26 ++++++ 5 files changed, 254 insertions(+), 4 deletions(-) create mode 100644 crates/forge-git/src/trailers.rs diff --git a/crates/forge-git/src/lib.rs b/crates/forge-git/src/lib.rs index 4e84731..e99bf64 100644 --- a/crates/forge-git/src/lib.rs +++ b/crates/forge-git/src/lib.rs @@ -17,6 +17,7 @@ pub mod search; pub mod service; pub mod setup; pub mod tags; +pub mod trailers; pub mod verify; pub mod version; pub mod wiki; diff --git a/crates/forge-git/src/trailers.rs b/crates/forge-git/src/trailers.rs new file mode 100644 index 0000000..9a01795 --- /dev/null +++ b/crates/forge-git/src/trailers.rs @@ -0,0 +1,117 @@ +//! `Co-authored-by:` trailers in a commit message. +//! +//! Trailers live in the message's last paragraph (the text after its last blank line). + +use std::collections::HashSet; + +const KEY: &str = "Co-authored-by"; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct CoAuthor { + pub name: String, + pub email: String, +} + +/// Co-authors from the message's trailer block, in order, deduplicated by lowercase email. +pub fn co_authors(message: &str) -> Vec { + let lines = lines(message); + let mut seen = HashSet::new(); + trailer_lines(&lines) + .iter() + .filter_map(|line| parse(line)) + .filter(|author| seen.insert(author.email.to_lowercase())) + .collect() +} + +/// The message with every `Co-authored-by:` line removed and trailing blank lines trimmed. +pub fn strip_co_authors(message: &str) -> String { + let lines = lines(message); + let start = last_paragraph_start(&lines); + let kept: Vec<&str> = lines[..start] + .iter() + .copied() + .chain(lines[start..].iter().copied().filter(|line| !is_trailer(line))) + .collect(); + kept.join("\n").trim_end().to_owned() +} + +/// The message's lines with trailing whitespace removed from the end of the whole message. +fn lines(message: &str) -> Vec<&str> { + message.trim_end().lines().collect() +} + +/// Index of the first line of the last paragraph; 0 when there is no blank line. +fn last_paragraph_start(lines: &[&str]) -> usize { + lines + .iter() + .rposition(|line| line.trim().is_empty()) + .map_or(0, |i| i + 1) +} + +fn trailer_lines<'a>(lines: &[&'a str]) -> Vec<&'a str> { + lines[last_paragraph_start(lines)..].to_vec() +} + +/// Whether a line's key is `Co-authored-by`, in any letter case. +fn is_trailer(line: &str) -> bool { + line.split_once(':') + .is_some_and(|(key, _)| key.trim().eq_ignore_ascii_case(KEY)) +} + +/// Parses `Co-authored-by: Name `. Lines without a non-empty `` give `None`. +fn parse(line: &str) -> Option { + let (key, value) = line.trim().split_once(':')?; + if !key.trim().eq_ignore_ascii_case(KEY) { + return None; + } + let value = value.trim(); + let open = value.find('<')?; + let close = open + value[open..].find('>')?; + let email = value[open + 1..close].trim(); + if email.is_empty() { + return None; + } + let name = value[..open].trim(); + Some(CoAuthor { + name: if name.is_empty() { email } else { name }.to_owned(), + email: email.to_owned(), + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn co_authors_from_the_trailer_block_only() { + let msg = "Co-authored-by: Old \n\nSubject\n\nBody\n\n\ + co-authored-by: Ann \n\ + CO-AUTHORED-BY: Bob \n\ + Co-authored-by: Ann Again \n\ + Co-authored-by: No Email\n\ + Co-authored-by: Empty <>\n"; + let got = co_authors(msg); + assert_eq!( + got, + vec![ + CoAuthor { + name: "Ann".into(), + email: "ANN@x.io".into() + }, + CoAuthor { + name: "Bob".into(), + email: "bob@x.io".into() + }, + ] + ); + } + + #[test] + fn strip_removes_trailers_in_last_paragraph() { + let msg = "Subject\n\nCo-authored-by: Mid \nKept\n\nSigned-off-by: Me\nCo-authored-by: Ann \n\n\n"; + assert_eq!( + strip_co_authors(msg), + "Subject\n\nCo-authored-by: Mid \nKept\n\nSigned-off-by: Me" + ); + } +} diff --git a/crates/forge-web/src/i18n/code.rs b/crates/forge-web/src/i18n/code.rs index 6e2a4c7..141aad9 100644 --- a/crates/forge-web/src/i18n/code.rs +++ b/crates/forge-web/src/i18n/code.rs @@ -89,6 +89,8 @@ pub enum K { DetailsLink, AuthorLabel, CommitterLabel, + CoAuthorAnd, + CoAuthorsLabel, RootCommit, ParentLabel, BrowseFiles, @@ -293,6 +295,10 @@ pub fn t(lang: Lang, key: K) -> &'static str { (AuthorLabel, Tr) => "Yazar ", (CommitterLabel, En) => "Committer ", (CommitterLabel, Tr) => "Commit eden ", + (CoAuthorAnd, En) => " and ", + (CoAuthorAnd, Tr) => " ve ", + (CoAuthorsLabel, En) => "Co-authors", + (CoAuthorsLabel, Tr) => "Ortak yazarlar", (RootCommit, En) => "Root commit (no parents)", (RootCommit, Tr) => "Kök commit (üst commit yok)", (ParentLabel, En) => "Parent: ", @@ -490,6 +496,14 @@ pub fn star_label(lang: Lang, starred: bool, stars: u64) -> String { } } +/// `+ others` for co-authors folded behind a disclosure. +pub fn co_author_others(lang: Lang, count: usize) -> String { + match lang { + Lang::En => format!("+{count} others"), + Lang::Tr => format!("+{count} kişi daha"), + } +} + /// ` watching` in English, ` izleyici` in Turkish. pub fn watching_count(lang: Lang, watchers: u64) -> String { match lang { @@ -608,6 +622,8 @@ mod tests { K::DetailsLink, K::AuthorLabel, K::CommitterLabel, + K::CoAuthorAnd, + K::CoAuthorsLabel, K::RootCommit, K::ParentLabel, K::BrowseFiles, diff --git a/crates/forge-web/src/pages/commit.rs b/crates/forge-web/src/pages/commit.rs index 185ec47..5b28ab7 100644 --- a/crates/forge-web/src/pages/commit.rs +++ b/crates/forge-web/src/pages/commit.rs @@ -3,6 +3,7 @@ use forge_core::tz::format_in_tz; use forge_git::browse; use forge_git::history; +use forge_git::trailers; use topcoat::Result; use topcoat::context::{Cx, app_context}; use topcoat::router::error::{content_too_large, not_found}; @@ -20,7 +21,7 @@ use crate::signatures; use crate::state::AppState; use crate::views::commit_status::{StatusLang, StatusSummary, checks_summary}; use crate::views::diff::{DiffLang, DiffOpts, LIMITS, diff_section}; -use crate::views::relative_time; +use crate::views::{avatar_url, display_name, relative_time, user_href}; use crate::views::shell; path_param!(owner); @@ -40,6 +41,16 @@ struct ParentLink { short: String, } +/// One `Co-authored-by` person: linked and avatar-shown when a verified user has the email. +struct CoAuthorView { + name: String, + email: String, + href: Option, + avatar: Option, + /// Whether a comma separates this one from the entry before it. + sep: bool, +} + fn is_hex_prefix(s: &str) -> bool { (4..=64).contains(&s.len()) && s.bytes().all(|b| b.is_ascii_hexdigit()) } @@ -155,13 +166,52 @@ async fn commit_page(cx: &Cx) -> Result { }) .collect(); let is_root = parents.is_empty(); - let has_body = !info.message.trim().is_empty(); + let body = trailers::strip_co_authors(&info.message); + let has_body = !body.trim().is_empty(); + let mut coauthors: Vec = Vec::new(); + for co in trailers::co_authors(&info.message) { + if co.email.eq_ignore_ascii_case(&info.author_email) { + continue; + } + let user = state + .store + .verified_user_by_email(&co.email) + .await + .ok() + .flatten(); + let (name, href, avatar) = match user { + Some(user) => ( + display_name(lang, &user.username), + Some(user_href(&user.username)), + Some(avatar_url(&user.username, user.avatar_hash.as_deref())), + ), + None => (co.name.clone(), None, None), + }; + let sep = !coauthors.is_empty(); + coauthors.push(CoAuthorView { + name, + email: co.email, + href, + avatar, + sep, + }); + } + // Three or more: the first is shown, the rest sit behind a "+N others" disclosure. + let has_coauthors = !coauthors.is_empty(); + let folded = coauthors.len() >= 3; + let rest: Vec = if folded { + coauthors.split_off(1) + } else { + Vec::new() + }; + let others = g::co_author_others(lang, rest.len()); + let coauthors_label = g::t(lang, K::CoAuthorsLabel); let title = format!("{owner_name}/{repo_name}"); let repo_href = format!("/{owner_name}/{repo_name}"); let tree_href = format!("/{owner_name}/{repo_name}/tree/{}", info.oid); let summary = emojify(&info.summary).into_owned(); let full_oid = info.oid.clone(); - let message = emojify(&info.message).into_owned(); + let message = emojify(&body).into_owned(); let statuses = state.store.statuses_for(&ctx.repo.id, &full_oid).await?; let checks = (!statuses.is_empty()).then(|| StatusSummary::from_rows(&statuses)); let sig = signatures::badges(state, &ctx.repo, std::slice::from_ref(&full_oid)) @@ -193,7 +243,47 @@ async fn commit_page(cx: &Cx) -> Result { if let Some(summary) = checks { checks_summary(summary: summary, lang: StatusLang(lang)) } -

(g::t(lang, K::AuthorLabel)) (author.name.as_str()) " · "

+
(g::t(lang, K::AuthorLabel)) (author.name.as_str()) + if has_coauthors { + (g::t(lang, K::CoAuthorAnd)) + for c in coauthors.iter() { + if c.sep { ", " } + if let Some(href) = c.href.as_ref() { + + if let Some(src) = c.avatar.as_ref() { + + } + (c.name.as_str()) + + } else { + (c.name.as_str()) + } + } + if folded { + ", " +
+ (others.as_str()) + +
+ } + } + " · " +
if committer_differs {

(g::t(lang, K::CommitterLabel)) (committer.name.as_str()) " · "

} diff --git a/style/_repo.scss b/style/_repo.scss index 2cc2220..d24842b 100644 --- a/style/_repo.scss +++ b/style/_repo.scss @@ -929,6 +929,32 @@ } } +// Co-authors on the author line: small avatars, and the "+N others" disclosure inline. +.commit-author-line { + .coauthor-avatar { + width: 16px; + height: 16px; + margin-right: 0.3em; + vertical-align: -3px; + } + + .coauthors-more { + display: inline; + + > summary { + display: inline; + cursor: pointer; + color: var(--muted); + text-decoration: underline dotted; + } + + .coauthors-list { + margin: 6px 0 0; + padding-left: 1.2em; + } + } +} + // The full commit hash in its copy box: one line, truncated with an ellipsis when the screen is // narrow, so the hash never breaks in the middle. The Copy button always copies the whole hash. .commit-info .commit-full-oid {