use std::cmp::Reverse; use std::path::PathBuf; use crate::state::AppState; use forge_core::Repo as RepoRow; use forge_git::browse::{self, CommitInfo, RefInfo, RefKind}; use topcoat::Result; use topcoat::context::{Cx, context}; use topcoat::router::{page, path_param}; use topcoat::view::{View, component, view}; use crate::i18n::code::{self as g, K}; use crate::i18n::{self, Lang}; use crate::pages::ref_live; use crate::pages::repo::repo_head; use crate::pages::repo_ctx::{RepoCtx, load_repo}; use crate::pages::tree::{LastCommit, last_commit_view}; use crate::views::commit_line::commit_line; use crate::views::{RefLink, now_unix, repo_href, shell}; path_param!(owner); path_param!(repo); pub async fn load_refs(path: PathBuf) -> Result> { Ok(tokio::task::spawn_blocking(move || browse::list_refs(&path)).await??) } pub fn ref_links( refs: &[RefInfo], current: &str, default_branch: &str, repo: &RepoRow, ) -> (Vec, Vec) { let mut sorted: Vec<&RefInfo> = refs.iter().collect(); sorted.sort_by(|a, b| { ( a.name != default_branch, Reverse(a.committer_time), a.name.as_str(), ) .cmp(&( b.name != default_branch, Reverse(b.committer_time), b.name.as_str(), )) }); let link = |r: &RefInfo| RefLink { name: r.name.clone(), href: repo_href(&repo.owner_name, &repo.name, "tree", &r.name), current: r.name == current, default: r.kind == RefKind::Branch && r.name == default_branch, updated: r.committer_time, }; let branches = sorted .iter() .filter(|r| r.kind == RefKind::Branch) .map(|r| link(r)) .collect(); let tags = sorted .iter() .filter(|r| r.kind == RefKind::Tag) .map(|r| link(r)) .collect(); (branches, tags) } pub struct RefRow { pub name: String, pub href: String, pub is_default: bool, pub last: Option, } pub async fn ref_rows( path: PathBuf, kind: RefKind, repo: &RepoRow, ) -> Result)>> { let rows = tokio::task::spawn_blocking(move || -> forge_git::Result<_> { let mut out = Vec::new(); for r in browse::list_refs(&path)? .into_iter() .filter(|r| r.kind == kind) { let commit = browse::read_commit(&path, &r.target)?; out.push((r, commit)); } Ok(out) }) .await??; let mut rows = rows; rows.sort_by(|(a, _), (b, _)| { (a.name != repo.default_branch, a.name.as_str()) .cmp(&(b.name != repo.default_branch, b.name.as_str())) }); Ok(rows) } #[component] async fn refs_table(cx: &Cx, rows: Vec, lang: Lang) -> Result { let empty = rows.is_empty(); Ok(view! { if empty {

(g::t(lang, K::NothingHere))

} else {
for row in rows.into_iter() { if let Some(last) = row.last {
(row.name) if row.is_default { (g::t(lang, K::DefaultBadge)) } commit_line(subject: last.summary.clone()) " ยท "
} else {
(row.name) if row.is_default { (g::t(lang, K::DefaultBadge)) }
} }
} }) } struct RefsPage { ctx: RepoCtx, rows: Vec, } async fn refs_data(cx: &Cx, kind: RefKind) -> Result { let owner = owner(cx); let name = repo(cx); let ctx = load_repo(cx, owner, name).await?; let now = now_unix(); let lang = i18n::lang(cx, ctx.viewer_user()); let rows = ref_rows(ctx.path.clone(), kind, &ctx.repo) .await? .into_iter() .map(|(info, commit)| RefRow { href: repo_href(&ctx.repo.owner_name, &ctx.repo.name, "tree", &info.name), is_default: kind == RefKind::Branch && info.name == ctx.repo.default_branch, last: commit .as_ref() .map(|c| last_commit_view(lang, &ctx.repo, c, now, ctx.viewer_tz())), name: info.name, }) .collect(); Ok(RefsPage { ctx, rows }) } #[page("/{owner}/{repo}/tags")] async fn tags_page(cx: &Cx) -> Result { let page = refs_data(cx, RefKind::Tag).await?; let ctx = page.ctx; let rows = page.rows; let lang = i18n::lang(cx, ctx.viewer_user()); let title = format!( "{}/{} \u{b7} {}", ctx.repo.owner_name, ctx.repo.name, g::t(lang, K::TagsTitle) ); let mut header = ctx.head(context::(cx), "tags").await?; header.live = ref_live::live_url(&ctx.repo.owner_name, &ctx.repo.name); Ok(view! { shell(title: title.as_str(), viewer: ctx.viewer.as_ref(), repo_head(head: header) refs_table(rows: rows, lang: lang) ) }) }