forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·9.4 MiB·37e985d·Tahinli·7 minutes ago·363 commits
| 1 | use std::cmp::Reverse; |
| 2 | use std::path::PathBuf; |
| 3 | |
| 4 | use crate::state::AppState; |
| 5 | use forge_core::Repo as RepoRow; |
| 6 | use forge_git::browse::{self, CommitInfo, RefInfo, RefKind}; |
| 7 | use topcoat::Result; |
| 8 | use topcoat::context::{Cx, context}; |
| 9 | use topcoat::router::{page, path_param}; |
| 10 | use topcoat::view::{View, component, view}; |
| 11 | |
| 12 | use crate::i18n::code::{self as g, K}; |
| 13 | use crate::i18n::{self, Lang}; |
| 14 | use crate::pages::ref_live; |
| 15 | use crate::pages::repo::repo_head; |
| 16 | use crate::pages::repo_ctx::{RepoCtx, load_repo}; |
| 17 | use crate::pages::tree::{LastCommit, last_commit_view}; |
| 18 | use crate::views::commit_line::commit_line; |
| 19 | use crate::views::{RefLink, now_unix, repo_href, shell}; |
| 20 | |
| 21 | path_param!(owner); |
| 22 | path_param!(repo); |
| 23 | |
| 24 | pub async fn load_refs(path: PathBuf) -> Result<Vec<RefInfo>> { |
| 25 | Ok(tokio::task::spawn_blocking(move || browse::list_refs(&path)).await??) |
| 26 | } |
| 27 | |
| 28 | pub fn ref_links( |
| 29 | refs: &[RefInfo], |
| 30 | current: &str, |
| 31 | default_branch: &str, |
| 32 | repo: &RepoRow, |
| 33 | ) -> (Vec<RefLink>, Vec<RefLink>) { |
| 34 | let mut sorted: Vec<&RefInfo> = refs.iter().collect(); |
| 35 | sorted.sort_by(|a, b| { |
| 36 | ( |
| 37 | a.name != default_branch, |
| 38 | Reverse(a.committer_time), |
| 39 | a.name.as_str(), |
| 40 | ) |
| 41 | .cmp(&( |
| 42 | b.name != default_branch, |
| 43 | Reverse(b.committer_time), |
| 44 | b.name.as_str(), |
| 45 | )) |
| 46 | }); |
| 47 | let link = |r: &RefInfo| RefLink { |
| 48 | name: r.name.clone(), |
| 49 | href: repo_href(&repo.owner_name, &repo.name, "tree", &r.name), |
| 50 | current: r.name == current, |
| 51 | default: r.kind == RefKind::Branch && r.name == default_branch, |
| 52 | updated: r.committer_time, |
| 53 | }; |
| 54 | let branches = sorted |
| 55 | .iter() |
| 56 | .filter(|r| r.kind == RefKind::Branch) |
| 57 | .map(|r| link(r)) |
| 58 | .collect(); |
| 59 | let tags = sorted |
| 60 | .iter() |
| 61 | .filter(|r| r.kind == RefKind::Tag) |
| 62 | .map(|r| link(r)) |
| 63 | .collect(); |
| 64 | (branches, tags) |
| 65 | } |
| 66 | |
| 67 | pub struct RefRow { |
| 68 | pub name: String, |
| 69 | pub href: String, |
| 70 | pub is_default: bool, |
| 71 | pub last: Option<LastCommit>, |
| 72 | } |
| 73 | |
| 74 | pub async fn ref_rows( |
| 75 | path: PathBuf, |
| 76 | kind: RefKind, |
| 77 | repo: &RepoRow, |
| 78 | ) -> Result<Vec<(RefInfo, Option<CommitInfo>)>> { |
| 79 | let rows = tokio::task::spawn_blocking(move || -> forge_git::Result<_> { |
| 80 | let mut out = Vec::new(); |
| 81 | for r in browse::list_refs(&path)? |
| 82 | .into_iter() |
| 83 | .filter(|r| r.kind == kind) |
| 84 | { |
| 85 | let commit = browse::read_commit(&path, &r.target)?; |
| 86 | out.push((r, commit)); |
| 87 | } |
| 88 | Ok(out) |
| 89 | }) |
| 90 | .await??; |
| 91 | let mut rows = rows; |
| 92 | rows.sort_by(|(a, _), (b, _)| { |
| 93 | (a.name != repo.default_branch, a.name.as_str()) |
| 94 | .cmp(&(b.name != repo.default_branch, b.name.as_str())) |
| 95 | }); |
| 96 | Ok(rows) |
| 97 | } |
| 98 | |
| 99 | #[component] |
| 100 | async fn refs_table(cx: &Cx, rows: Vec<RefRow>, lang: Lang) -> Result<impl View> { |
| 101 | let empty = rows.is_empty(); |
| 102 | Ok(view! { |
| 103 | if empty { |
| 104 | <p class="state">(g::t(lang, K::NothingHere))</p> |
| 105 | } else { |
| 106 | <div class="sheet"> |
| 107 | for row in rows.into_iter() { |
| 108 | if let Some(last) = row.last { |
| 109 | <div class="line" data-kb-row=""> |
| 110 | <a class="name" href=(row.href)>(row.name)</a> |
| 111 | if row.is_default { |
| 112 | <span class="hint">(g::t(lang, K::DefaultBadge))</span> |
| 113 | } |
| 114 | <span class="val"><a href=(last.href)>commit_line(subject: last.summary.clone())</a> " · " <time datetime=(last.iso.clone()) title=(last.absolute)>(last.age)</time></span> |
| 115 | </div> |
| 116 | } else { |
| 117 | <div class="line" data-kb-row=""> |
| 118 | <a class="name" href=(row.href)>(row.name)</a> |
| 119 | if row.is_default { |
| 120 | <span class="hint">(g::t(lang, K::DefaultBadge))</span> |
| 121 | } |
| 122 | </div> |
| 123 | } |
| 124 | } |
| 125 | </div> |
| 126 | } |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | struct RefsPage { |
| 131 | ctx: RepoCtx, |
| 132 | rows: Vec<RefRow>, |
| 133 | } |
| 134 | |
| 135 | async fn refs_data(cx: &Cx, kind: RefKind) -> Result<RefsPage> { |
| 136 | let owner = owner(cx); |
| 137 | let name = repo(cx); |
| 138 | let ctx = load_repo(cx, owner, name).await?; |
| 139 | let now = now_unix(); |
| 140 | let lang = i18n::lang(cx, ctx.viewer_user()); |
| 141 | let rows = ref_rows(ctx.path.clone(), kind, &ctx.repo) |
| 142 | .await? |
| 143 | .into_iter() |
| 144 | .map(|(info, commit)| RefRow { |
| 145 | href: repo_href(&ctx.repo.owner_name, &ctx.repo.name, "tree", &info.name), |
| 146 | is_default: kind == RefKind::Branch && info.name == ctx.repo.default_branch, |
| 147 | last: commit |
| 148 | .as_ref() |
| 149 | .map(|c| last_commit_view(lang, &ctx.repo, c, now, ctx.viewer_tz())), |
| 150 | name: info.name, |
| 151 | }) |
| 152 | .collect(); |
| 153 | Ok(RefsPage { ctx, rows }) |
| 154 | } |
| 155 | |
| 156 | #[page("/{owner}/{repo}/tags")] |
| 157 | async fn tags_page(cx: &Cx) -> Result<impl View> { |
| 158 | let page = refs_data(cx, RefKind::Tag).await?; |
| 159 | let ctx = page.ctx; |
| 160 | let rows = page.rows; |
| 161 | let lang = i18n::lang(cx, ctx.viewer_user()); |
| 162 | let title = format!( |
| 163 | "{}/{} \u{b7} {}", |
| 164 | ctx.repo.owner_name, |
| 165 | ctx.repo.name, |
| 166 | g::t(lang, K::TagsTitle) |
| 167 | ); |
| 168 | let mut header = ctx.head(context::<AppState>(cx), "tags").await?; |
| 169 | header.live = ref_live::live_url(&ctx.repo.owner_name, &ctx.repo.name); |
| 170 | Ok(view! { |
| 171 | shell(title: title.as_str(), viewer: ctx.viewer.as_ref(), |
| 172 | repo_head(head: header) |
| 173 | refs_table(rows: rows, lang: lang) |
| 174 | ) |
| 175 | }) |
| 176 | } |