| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | use topcoat::{ |
| 17 | Result, |
| 18 | context::Cx, |
| 19 | icon::{icon, iconify::iconify_icon}, |
| 20 | router::{error::RouterErrorExt, page, query_params}, |
| 21 | view::{View, attributes, component, view}, |
| 22 | }; |
| 23 | |
| 24 | use crate::{ |
| 25 | application::{RefPage, RepoView, list_branches, list_tags, port::ArchiveFormat}, |
| 26 | components::badge::{BadgeVariant, badge}, |
| 27 | domain::{BranchRow, RefName, TagRow}, |
| 28 | }; |
| 29 | |
| 30 | use super::{ |
| 31 | archive::archive_url, |
| 32 | browse::{ago, empty_repo, encode, log_url, timestamp, tree_url}, |
| 33 | context::{current_actor, memberships, orgs, queries, repos, server_error, timed_out}, |
| 34 | layout::wide, |
| 35 | paging::{Pages, list_pages, requested_page, showing}, |
| 36 | pulls::new_pull_url, |
| 37 | repo::{Tab, clone_url_for, repo_for, repo_header}, |
| 38 | }; |
| 39 | |
| 40 | |
| 41 | #[query_params(error = bad_request)] |
| 42 | struct PageQuery { |
| 43 | page: Option<String>, |
| 44 | } |
| 45 | |
| 46 | |
| 47 | pub(super) fn branches_url(handle: &str, name: &str) -> String { |
| 48 | format!("/{handle}/repos/{name}/branches") |
| 49 | } |
| 50 | |
| 51 | |
| 52 | pub(super) fn tags_url(handle: &str, name: &str) -> String { |
| 53 | format!("/{handle}/repos/{name}/tags") |
| 54 | } |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | fn compare_url(handle: &str, name: &str, base: &RefName, head: &RefName) -> String { |
| 66 | format!( |
| 67 | "/{handle}/repos/{name}/compare/{}...{}", |
| 68 | encode(base.as_str(), false), |
| 69 | encode(head.as_str(), false) |
| 70 | ) |
| 71 | } |
| 72 | |
| 73 | #[page("/{handle}/repos/{name}/branches")] |
| 74 | async fn branches_page(cx: &Cx) -> Result { |
| 75 | let repo = repo_for(cx).await?; |
| 76 | let clone = clone_url_for(cx, &repo); |
| 77 | let other = tags_url(repo.handle.as_str(), repo.name.as_str()); |
| 78 | |
| 79 | let listed = list_branches( |
| 80 | &repo.handle, |
| 81 | &repo.name, |
| 82 | Some(requested_page( |
| 83 | query_params::<PageQuery>(cx)?.page.as_deref(), |
| 84 | )), |
| 85 | ¤t_actor(cx).await?, |
| 86 | &orgs(cx), |
| 87 | &memberships(cx), |
| 88 | &repos(cx), |
| 89 | &queries(cx), |
| 90 | ) |
| 91 | .await; |
| 92 | |
| 93 | let listed: RefPage<BranchRow> = match listed { |
| 94 | Ok(page) => page.ok_or_not_found()?, |
| 95 | Err(error) if timed_out(&error) => { |
| 96 | return view! { |
| 97 | wide( |
| 98 | repo_header(repo: &repo, rev: "", active: Tab::Code) |
| 99 | heading( |
| 100 | title: "Branches", |
| 101 | count: None, |
| 102 | other: "Tags", |
| 103 | href: other.as_str(), |
| 104 | ) |
| 105 | took_too_long(what: "branches") |
| 106 | ) |
| 107 | }; |
| 108 | } |
| 109 | Err(error) => return Err(server_error(error)), |
| 110 | }; |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | let default = listed |
| 116 | .rows |
| 117 | .items |
| 118 | .iter() |
| 119 | .find(|row| row.is_default) |
| 120 | .map(|row| row.name.clone()); |
| 121 | |
| 122 | let at = default.as_ref().map(RefName::as_str).unwrap_or_default(); |
| 123 | let sentence = showing( |
| 124 | listed.rows.offset(), |
| 125 | listed.rows.items.len(), |
| 126 | listed.rows.total, |
| 127 | "branches", |
| 128 | ); |
| 129 | let pages = Pages::numbered(listed.rows.page, listed.rows.pages, |page| { |
| 130 | format!( |
| 131 | "{}?page={page}", |
| 132 | branches_url(repo.handle.as_str(), repo.name.as_str()) |
| 133 | ) |
| 134 | }); |
| 135 | |
| 136 | view! { |
| 137 | wide( |
| 138 | repo_header(repo: &repo, rev: at, active: Tab::Code) |
| 139 | heading( |
| 140 | title: "Branches", |
| 141 | count: Some(listed.rows.total), |
| 142 | other: "Tags", |
| 143 | href: other.as_str(), |
| 144 | ) |
| 145 | |
| 146 | if listed.repo_is_empty { |
| 147 | empty_repo(url: clone.as_str()) |
| 148 | } else if listed.rows.items.is_empty() { |
| 149 | nothing_here("No branches. Every commit in this repository is reachable only by tag or by sha.") |
| 150 | } else { |
| 151 | <ul class="divide-y divide-border overflow-hidden rounded-lg border border-border"> |
| 152 | for row in &listed.rows.items { |
| 153 | <li> |
| 154 | branch_row(repo: &repo, row: row, default: default.as_ref()) |
| 155 | </li> |
| 156 | } |
| 157 | </ul> |
| 158 | list_pages(summary: sentence.as_str(), pages: &pages) |
| 159 | } |
| 160 | ) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | #[component] |
| 175 | async fn branch_row(repo: &RepoView, row: &BranchRow, default: Option<&RefName>) -> Result { |
| 176 | let handle = repo.handle.as_str(); |
| 177 | let name = repo.name.as_str(); |
| 178 | let tree = tree_url(handle, name, &row.name, &crate::domain::RepoPath::root()); |
| 179 | let log = log_url(handle, name, row.name.as_str()); |
| 180 | |
| 181 | view! { |
| 182 | <div class="flex items-center gap-3 px-4 py-2 text-sm"> |
| 183 | <a href=(tree.as_str()) class="max-w-[45%] shrink-0 truncate font-mono hover:underline"> |
| 184 | (row.name.as_str()) |
| 185 | </a> |
| 186 | if row.is_default { |
| 187 | badge(variant: BadgeVariant::Outline, "default") |
| 188 | } |
| 189 | |
| 190 | |
| 191 | |
| 192 | <a |
| 193 | href=(log.as_str()) |
| 194 | class="hidden min-w-0 flex-1 truncate text-muted-foreground hover:text-foreground sm:block" |
| 195 | >(&row.summary)</a> |
| 196 | |
| 197 | <span class="ml-auto flex shrink-0 items-center gap-3 text-xs text-muted-foreground sm:ml-0"> |
| 198 | <code class="font-mono">(row.commit.short())</code> |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | <span class="sm:w-24 sm:text-right" title=(timestamp(row.committed_at))> |
| 204 | (ago(row.committed_at)) |
| 205 | </span> |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | <span class="hidden w-36 text-right sm:block"> |
| 218 | match default { |
| 219 | Some(base) if base != &row.name => { |
| 220 | <a |
| 221 | href=(compare_url(handle, name, base, &row.name)) |
| 222 | class="hover:text-foreground" |
| 223 | >"Compare"</a> |
| 224 | if repo.viewer_is_owner { |
| 225 | <span class="px-1.5">"·"</span> |
| 226 | <a |
| 227 | href=(new_pull_url( |
| 228 | handle, |
| 229 | name, |
| 230 | row.name.as_str(), |
| 231 | base.as_str(), |
| 232 | )) |
| 233 | class="hover:text-foreground" |
| 234 | >"Pull request"</a> |
| 235 | } |
| 236 | } |
| 237 | _ => "", |
| 238 | } |
| 239 | </span> |
| 240 | </span> |
| 241 | </div> |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | #[page("/{handle}/repos/{name}/tags")] |
| 246 | async fn tags_page(cx: &Cx) -> Result { |
| 247 | let repo = repo_for(cx).await?; |
| 248 | let clone = clone_url_for(cx, &repo); |
| 249 | let other = branches_url(repo.handle.as_str(), repo.name.as_str()); |
| 250 | |
| 251 | let listed = list_tags( |
| 252 | &repo.handle, |
| 253 | &repo.name, |
| 254 | Some(requested_page( |
| 255 | query_params::<PageQuery>(cx)?.page.as_deref(), |
| 256 | )), |
| 257 | ¤t_actor(cx).await?, |
| 258 | &orgs(cx), |
| 259 | &memberships(cx), |
| 260 | &repos(cx), |
| 261 | &queries(cx), |
| 262 | ) |
| 263 | .await; |
| 264 | |
| 265 | let listed: RefPage<TagRow> = match listed { |
| 266 | Ok(page) => page.ok_or_not_found()?, |
| 267 | Err(error) if timed_out(&error) => { |
| 268 | return view! { |
| 269 | wide( |
| 270 | repo_header(repo: &repo, rev: "", active: Tab::Code) |
| 271 | heading( |
| 272 | title: "Tags", |
| 273 | count: None, |
| 274 | other: "Branches", |
| 275 | href: other.as_str(), |
| 276 | ) |
| 277 | took_too_long(what: "tags") |
| 278 | ) |
| 279 | }; |
| 280 | } |
| 281 | Err(error) => return Err(server_error(error)), |
| 282 | }; |
| 283 | |
| 284 | let sentence = showing( |
| 285 | listed.rows.offset(), |
| 286 | listed.rows.items.len(), |
| 287 | listed.rows.total, |
| 288 | "tags", |
| 289 | ); |
| 290 | let pages = Pages::numbered(listed.rows.page, listed.rows.pages, |page| { |
| 291 | format!( |
| 292 | "{}?page={page}", |
| 293 | tags_url(repo.handle.as_str(), repo.name.as_str()) |
| 294 | ) |
| 295 | }); |
| 296 | |
| 297 | view! { |
| 298 | wide( |
| 299 | |
| 300 | repo_header(repo: &repo, rev: "", active: Tab::Code) |
| 301 | heading( |
| 302 | title: "Tags", |
| 303 | count: Some(listed.rows.total), |
| 304 | other: "Branches", |
| 305 | href: other.as_str(), |
| 306 | ) |
| 307 | |
| 308 | if listed.repo_is_empty { |
| 309 | empty_repo(url: clone.as_str()) |
| 310 | } else if listed.rows.items.is_empty() { |
| 311 | nothing_here( |
| 312 | "No tags yet. Push one with " |
| 313 | <code class="font-mono text-foreground">"git push --tags"</code> |
| 314 | " to see it here." |
| 315 | ) |
| 316 | } else { |
| 317 | <ul class="divide-y divide-border overflow-hidden rounded-lg border border-border"> |
| 318 | for row in &listed.rows.items { |
| 319 | <li>tag_row(repo: &repo, row: row)</li> |
| 320 | } |
| 321 | </ul> |
| 322 | list_pages(summary: sentence.as_str(), pages: &pages) |
| 323 | } |
| 324 | ) |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | #[component] |
| 338 | async fn tag_row(repo: &RepoView, row: &TagRow) -> Result { |
| 339 | let handle = repo.handle.as_str(); |
| 340 | let name = repo.name.as_str(); |
| 341 | let tree = tree_url(handle, name, &row.name, &crate::domain::RepoPath::root()); |
| 342 | let log = log_url(handle, name, row.name.as_str()); |
| 343 | |
| 344 | view! { |
| 345 | <div class="flex items-center gap-3 px-4 py-2 text-sm"> |
| 346 | <span class="shrink-0 text-muted-foreground"> |
| 347 | icon(data: iconify_icon!("feather:tag"), attrs: attributes! { class="size-3.5" }) |
| 348 | </span> |
| 349 | <a href=(tree.as_str()) class="max-w-[45%] shrink-0 truncate font-mono hover:underline"> |
| 350 | (row.name.as_str()) |
| 351 | </a> |
| 352 | |
| 353 | |
| 354 | if row.annotated { |
| 355 | badge(variant: BadgeVariant::Outline, "annotated") |
| 356 | } |
| 357 | |
| 358 | match &row.message { |
| 359 | Some(message) => <span |
| 360 | class="hidden min-w-0 flex-1 truncate text-muted-foreground sm:block" |
| 361 | >(message)</span>, |
| 362 | None => <span class="hidden flex-1 sm:block"></span>, |
| 363 | } |
| 364 | |
| 365 | <span class="ml-auto flex shrink-0 items-center gap-3 text-xs text-muted-foreground sm:ml-0"> |
| 366 | // The commit the tag names, peeled through the tag object — the log at |
| 367 | // the tag is where it can be seen in context. |
| 368 | <a href=(log.as_str()) class="font-mono hover:text-foreground"> |
| 369 | (row.commit.short()) |
| 370 | </a> |
| 371 | <span class="sm:w-24 sm:text-right" title=(timestamp(row.created_at))> |
| 372 | (ago(row.created_at)) |
| 373 | </span> |
| 374 | // A fixed slot on the right, like Compare on the branches row, so a |
| 375 | // column of tags keeps its dates in line. Hidden below `sm` with the |
| 376 | // message: the row keeps what identifies a tag rather than overflowing, |
| 377 | // and the archive is still one tap away through the tag's own tree. |
| 378 | <span class="hidden shrink-0 items-center gap-1.5 sm:inline-flex"> |
| 379 | <a |
| 380 | href=(archive_url(handle, name, row.name.as_str(), ArchiveFormat::Zip)) |
| 381 | class="font-mono hover:text-foreground hover:underline" |
| 382 | >"zip"</a> |
| 383 | "·" |
| 384 | <a |
| 385 | href=(archive_url(handle, name, row.name.as_str(), ArchiveFormat::TarGz)) |
| 386 | class="font-mono hover:text-foreground hover:underline" |
| 387 | >"tar.gz"</a> |
| 388 | </span> |
| 389 | </span> |
| 390 | </div> |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /// The line above the list: what this page is, how many, and the other one. |
| 395 | /// |
| 396 | /// The cross-link is the whole navigation between the two pages. Both live under the |
| 397 | /// Code tab, so the strip cannot say which of them you are on, and having arrived from |
| 398 | /// one count the visitor usually wants the other. |
| 399 | #[component] |
| 400 | async fn heading(title: &str, count: Option<usize>, other: &str, href: &str) -> Result { |
| 401 | view! { |
| 402 | <div class="mb-2 flex items-baseline gap-2.5"> |
| 403 | <h2 class="text-sm font-medium">(title)</h2> |
| 404 | match count { |
| 405 | Some(count) => <span class="font-mono text-xs text-muted-foreground"> |
| 406 | (count.to_string()) |
| 407 | </span>, |
| 408 | None => "", |
| 409 | } |
| 410 | <a href=(href) class="ml-auto inline-flex items-center gap-1.5 text-xs text-muted-foreground hover:text-foreground"> |
| 411 | (other) |
| 412 | icon(data: iconify_icon!("feather:arrow-right"), attrs: attributes! { |
| 413 | class="size-3.5" |
| 414 | }) |
| 415 | </a> |
| 416 | </div> |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /// A repository that has history but nothing of this kind in it. |
| 421 | /// |
| 422 | /// Distinct from an empty repository, which gets the push snippet: this one is not |
| 423 | /// missing a setup step, so telling it how to push would be answering a question it did |
| 424 | /// not ask. |
| 425 | #[component] |
| 426 | async fn nothing_here(#[default] child: View) -> Result { |
| 427 | view! { |
| 428 | <p class="rounded-lg border border-border px-4 py-10 text-center text-sm text-muted-foreground"> |
| 429 | (child) |
| 430 | </p> |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /// What a read that ran out of time looks like. |
| 435 | /// |
| 436 | /// A page state rather than a 500: the repository is fine, and the honest thing to say |
| 437 | /// is that this one question took too long — with reloading offered, because on a |
| 438 | /// repository this large it may well not the second time. |
| 439 | #[component] |
| 440 | async fn took_too_long(what: &str) -> Result { |
| 441 | view! { |
| 442 | <div class="rounded-lg border border-border px-4 py-10 text-center"> |
| 443 | <p class="text-sm text-muted-foreground"> |
| 444 | "Listing this repository's " (what) " took too long." |
| 445 | </p> |
| 446 | <p class="mt-1 text-xs text-muted-foreground"> |
| 447 | "Nothing is wrong with the repository — reading it simply ran out of time. Try again." |
| 448 | </p> |
| 449 | </div> |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | #[cfg(test)] |
| 454 | mod tests { |
| 455 | use super::*; |
| 456 | |
| 457 | fn rev(value: &str) -> RefName { |
| 458 | RefName::new(value).expect("valid revision") |
| 459 | } |
| 460 | |
| 461 | #[test] |
| 462 | fn the_pages_live_under_the_repository() { |
| 463 | assert_eq!(branches_url("ada", "steid"), "/ada/repos/steid/branches"); |
| 464 | assert_eq!(tags_url("ada", "steid"), "/ada/repos/steid/tags"); |
| 465 | } |
| 466 | |
| 467 | #[test] |
| 468 | fn a_comparison_names_the_default_branch_first() { |
| 469 | assert_eq!( |
| 470 | compare_url("ada", "steid", &rev("main"), &rev("spike")), |
| 471 | "/ada/repos/steid/compare/main...spike" |
| 472 | ); |
| 473 | } |
| 474 | |
| 475 | #[test] |
| 476 | fn a_tag_row_offers_both_archive_formats_at_that_tag() { |
| 477 | assert_eq!( |
| 478 | archive_url("ada", "steid", "v0.2.0", ArchiveFormat::Zip), |
| 479 | "/ada/repos/steid/archive/v0.2.0.zip" |
| 480 | ); |
| 481 | assert_eq!( |
| 482 | archive_url("ada", "steid", "release/2.0", ArchiveFormat::TarGz), |
| 483 | "/ada/repos/steid/archive/release%2F2.0.tar.gz" |
| 484 | ); |
| 485 | } |
| 486 | |
| 487 | #[test] |
| 488 | fn a_refs_slashes_are_encoded_so_the_separator_stays_the_only_one() { |
| 489 | // Otherwise `feature/login` would look like extra path segments, which is the |
| 490 | // same ambiguity `/-/` removes on a tree URL. |
| 491 | assert_eq!( |
| 492 | compare_url("ada", "steid", &rev("main"), &rev("feature/login")), |
| 493 | "/ada/repos/steid/compare/main...feature%2Flogin" |
| 494 | ); |
| 495 | } |
| 496 | } |