| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | use std::collections::HashMap; |
| 23 | |
| 24 | use topcoat::{ |
| 25 | Result, |
| 26 | context::Cx, |
| 27 | icon::{icon, iconify::iconify_icon}, |
| 28 | router::{ |
| 29 | Body, Response, StatusCode, |
| 30 | error::{RouterErrorExt, not_found}, |
| 31 | header::{CONTENT_DISPOSITION, CONTENT_TYPE}, |
| 32 | page, path_param, query_params, route, |
| 33 | }, |
| 34 | view::{Unescaped, attributes, component, view}, |
| 35 | }; |
| 36 | |
| 37 | use crate::{ |
| 38 | application::{ |
| 39 | Browsed, EntryDetail, FileView, RepoView, Stores, |
| 40 | browse::resolve_relative, |
| 41 | browse::{RawFile, RefList, list_refs, read_raw_file}, |
| 42 | browse_repo, repo_log, |
| 43 | }, |
| 44 | components::badge::{BadgeVariant, badge}, |
| 45 | domain::{CommitSummary, EntryCommit, EntryKind, RefName, RepoPath, TreeEntry}, |
| 46 | infrastructure::highlight::{Source, SourceLine, language_of}, |
| 47 | }; |
| 48 | |
| 49 | use super::{ |
| 50 | atom::{commits_feed_path, feed_link}, |
| 51 | blame::{FileTab, blame_url, view_toggle}, |
| 52 | commit::commit_url, |
| 53 | context::{current_actor, highlights, memberships, orgs, queries, repos, server_error}, |
| 54 | format::{ago, size_of, timestamp}, |
| 55 | layout::wide, |
| 56 | markdown, |
| 57 | paging::{Pages, list_pages, requested_page, showing}, |
| 58 | refs::{branches_url, tags_url}, |
| 59 | repo::{Tab, clone_url_for, repo_for, repo_header, split_fragment}, |
| 60 | search::search_form, |
| 61 | urls::{encode, log_url, path_log_url, raw_url, tree_url}, |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | #[path_param] |
| 66 | struct Rev(str); |
| 67 | |
| 68 | |
| 69 | #[path_param] |
| 70 | struct Path(str); |
| 71 | |
| 72 | |
| 73 | |
| 74 | #[query_params(error = bad_request)] |
| 75 | pub(super) struct PageQuery { |
| 76 | pub page: Option<String>, |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | #[query_params(error = bad_request)] |
| 86 | struct ViewQuery { |
| 87 | view: Option<String>, |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | fn rev_param(cx: &Cx) -> Result<RefName> { |
| 95 | Ok(RefName::new(path_param::<Rev>(cx)).map_err(|_| not_found())?) |
| 96 | } |
| 97 | |
| 98 | |
| 99 | fn path_arg(cx: &Cx) -> Result<RepoPath> { |
| 100 | Ok(RepoPath::new(path_param::<Path>(cx)).map_err(|_| not_found())?) |
| 101 | } |
| 102 | |
| 103 | #[page("/{handle}/repos/{name}/tree/{rev}")] |
| 104 | async fn tree_root_page(cx: &Cx) -> Result { |
| 105 | let rev = rev_param(cx)?; |
| 106 | |
| 107 | view! { browsing(rev: Some(rev), path: RepoPath::root(), show_source: false) } |
| 108 | } |
| 109 | |
| 110 | #[page("/{handle}/repos/{name}/tree/{rev}/-/{*path}")] |
| 111 | async fn tree_path_page(cx: &Cx) -> Result { |
| 112 | let rev = rev_param(cx)?; |
| 113 | let path = path_arg(cx)?; |
| 114 | let show_source = wants_source(cx)?; |
| 115 | |
| 116 | view! { browsing(rev: Some(rev), path: path, show_source: show_source) } |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | fn wants_source(cx: &Cx) -> Result<bool> { |
| 124 | Ok(query_params::<ViewQuery>(cx)?.view.as_deref() == Some("source")) |
| 125 | } |
| 126 | |
| 127 | #[page("/{handle}/repos/{name}/log")] |
| 128 | async fn log_page(_cx: &Cx) -> Result { |
| 129 | view! { history(rev: None) } |
| 130 | } |
| 131 | |
| 132 | #[page("/{handle}/repos/{name}/log/{rev}")] |
| 133 | async fn log_rev_page(cx: &Cx) -> Result { |
| 134 | let rev = rev_param(cx)?; |
| 135 | |
| 136 | view! { history(rev: Some(rev)) } |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | #[route(GET "/{handle}/repos/{name}/raw/{rev}/-/{*path}")] |
| 148 | async fn raw_page(cx: &Cx) -> Result<Response<Body>> { |
| 149 | let rev = rev_param(cx)?; |
| 150 | let path = path_arg(cx)?; |
| 151 | let repo = repo_for(cx).await?; |
| 152 | |
| 153 | let raw = read_raw_file( |
| 154 | &repo.handle, |
| 155 | &repo.name, |
| 156 | Some(&rev), |
| 157 | &path, |
| 158 | ¤t_actor(cx).await?, |
| 159 | &Stores::new(&orgs(cx), &memberships(cx), &repos(cx)), |
| 160 | &queries(cx), |
| 161 | ) |
| 162 | .await |
| 163 | .map_err(server_error)? |
| 164 | .ok_or_not_found()?; |
| 165 | |
| 166 | match raw { |
| 167 | RawFile::Ready { name, content } => raw_response(&name, content), |
| 168 | |
| 169 | |
| 170 | RawFile::TooLarge { size } => Response::builder() |
| 171 | .status(StatusCode::PAYLOAD_TOO_LARGE) |
| 172 | .header(CONTENT_TYPE, "text/plain; charset=utf-8") |
| 173 | .header(NOSNIFF.0, NOSNIFF.1) |
| 174 | .body(Body::from(format!( |
| 175 | "This file is {}, which is larger than this instance serves raw. Clone the repository to read it.\n", |
| 176 | size_of(size) |
| 177 | ))) |
| 178 | .map_err(server_error), |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | pub(super) async fn browsed_at( |
| 186 | cx: &Cx, |
| 187 | repo: &RepoView, |
| 188 | rev: Option<&RefName>, |
| 189 | path: &RepoPath, |
| 190 | ) -> Result<Browsed> { |
| 191 | Ok(browse_repo( |
| 192 | &repo.handle, |
| 193 | &repo.name, |
| 194 | rev, |
| 195 | path, |
| 196 | ¤t_actor(cx).await?, |
| 197 | &Stores::new(&orgs(cx), &memberships(cx), &repos(cx)), |
| 198 | &queries(cx), |
| 199 | ) |
| 200 | .await |
| 201 | .map_err(server_error)? |
| 202 | .ok_or_not_found()?) |
| 203 | } |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | pub(super) async fn refs_for(cx: &Cx, repo: &RepoView) -> Result<RefList> { |
| 213 | Ok(list_refs( |
| 214 | &repo.handle, |
| 215 | &repo.name, |
| 216 | ¤t_actor(cx).await?, |
| 217 | &Stores::new(&orgs(cx), &memberships(cx), &repos(cx)), |
| 218 | &queries(cx), |
| 219 | ) |
| 220 | .await |
| 221 | .map_err(server_error)? |
| 222 | .ok_or_not_found()?) |
| 223 | } |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | #[component] |
| 231 | async fn browsing(cx: &Cx, rev: Option<RefName>, path: RepoPath, show_source: bool) -> Result { |
| 232 | let repo = repo_for(cx).await?; |
| 233 | let browsed = browsed_at(cx, &repo, rev.as_ref(), &path).await?; |
| 234 | let clone = clone_url_for(cx, &repo); |
| 235 | |
| 236 | |
| 237 | let refs = match browsed { |
| 238 | Browsed::Empty => RefList::default(), |
| 239 | _ => refs_for(cx, &repo).await?, |
| 240 | }; |
| 241 | |
| 242 | let at = browsed_rev(&browsed); |
| 243 | let switch = Switch::Tree(&path); |
| 244 | let handle = repo.handle.as_str(); |
| 245 | let name = repo.name.as_str(); |
| 246 | let known = RefName::new(at).is_ok_and(|at| refs.contains(&at)); |
| 247 | let branches = ref_links(handle, name, &refs.branches, at, &switch); |
| 248 | let tags = ref_links(handle, name, &refs.tags, at, &switch); |
| 249 | |
| 250 | view! { |
| 251 | wide( |
| 252 | repo_header( |
| 253 | repo: &repo, |
| 254 | rev: at, |
| 255 | active: Tab::Code, |
| 256 | rev_switcher(current: at, known: known, branches: &branches, tags: &tags) |
| 257 | ) |
| 258 | |
| 259 | match &browsed { |
| 260 | Browsed::Empty => empty_repo(url: clone.as_str()), |
| 261 | Browsed::Directory { rev, path, entries, history: last_commits, details } => directory( |
| 262 | handle: repo.handle.as_str(), |
| 263 | name: repo.name.as_str(), |
| 264 | rev: rev, |
| 265 | path: path, |
| 266 | entries: entries, |
| 267 | listing: Listing::of(last_commits, details), |
| 268 | ), |
| 269 | Browsed::File { rev, path, file } => blob( |
| 270 | handle: repo.handle.as_str(), |
| 271 | name: repo.name.as_str(), |
| 272 | rev: rev, |
| 273 | path: path, |
| 274 | file: file, |
| 275 | show_source: show_source, |
| 276 | ), |
| 277 | } |
| 278 | ) |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | |
| 283 | #[component] |
| 284 | async fn history(cx: &Cx, rev: Option<RefName>) -> Result { |
| 285 | let repo = repo_for(cx).await?; |
| 286 | |
| 287 | let log = repo_log( |
| 288 | &repo.handle, |
| 289 | &repo.name, |
| 290 | rev.as_ref(), |
| 291 | requested_page(query_params::<PageQuery>(cx)?.page.as_deref()), |
| 292 | ¤t_actor(cx).await?, |
| 293 | &Stores::new(&orgs(cx), &memberships(cx), &repos(cx)), |
| 294 | &queries(cx), |
| 295 | ) |
| 296 | .await |
| 297 | .map_err(server_error)? |
| 298 | .ok_or_not_found()?; |
| 299 | |
| 300 | let refs = refs_for(cx, &repo).await?; |
| 301 | let at = rev.as_ref().map(RefName::as_str).unwrap_or_default(); |
| 302 | let handle = repo.handle.as_str(); |
| 303 | let name = repo.name.as_str(); |
| 304 | let known = RefName::new(at).is_ok_and(|at| refs.contains(&at)); |
| 305 | let branches = ref_links(handle, name, &refs.branches, at, &Switch::Log); |
| 306 | let tags = ref_links(handle, name, &refs.tags, at, &Switch::Log); |
| 307 | |
| 308 | |
| 309 | |
| 310 | let feed = match at.is_empty() { |
| 311 | true => commits_feed_path(handle, name), |
| 312 | false => format!( |
| 313 | "{}?rev={}", |
| 314 | commits_feed_path(handle, name), |
| 315 | encode(at, false) |
| 316 | ), |
| 317 | }; |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | let sentence = showing(log.offset(), log.items.len(), log.total, "commits"); |
| 323 | let pages = Pages::numbered(log.page, log.pages, |page| { |
| 324 | format!("{}?page={page}", log_url(handle, name, at)) |
| 325 | }); |
| 326 | |
| 327 | view! { |
| 328 | wide( |
| 329 | repo_header( |
| 330 | repo: &repo, |
| 331 | rev: at, |
| 332 | active: Tab::Commits, |
| 333 | <span class="flex items-center gap-3"> |
| 334 | feed_link(href: feed.as_str()) |
| 335 | // At the default branch the URL names no revision and `repo_log` does |
| 336 | // not report the one it resolved, so the switcher opens with nothing |
| 337 | // marked current rather than guessing. Noted in `plans/current.md`. |
| 338 | rev_switcher(current: at, known: known, branches: &branches, tags: &tags) |
| 339 | </span> |
| 340 | ) |
| 341 | commit_log(handle: handle, name: name, commits: &log.items) |
| 342 | list_pages(summary: sentence.as_str(), pages: &pages) |
| 343 | ) |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /// The revision a browse landed on, for display. Empty when there is none. |
| 348 | pub(super) fn browsed_rev(browsed: &Browsed) -> &str { |
| 349 | match browsed { |
| 350 | Browsed::Empty => "", |
| 351 | Browsed::Directory { rev, .. } | Browsed::File { rev, .. } => rev.as_str(), |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // --- Serving bytes ---------------------------------------------------------------- |
| 356 | |
| 357 | /// The header that stops a browser second-guessing a `Content-Type`. |
| 358 | /// |
| 359 | /// A pair rather than a constant string so the name is written once; `http` has no |
| 360 | /// constant for it. |
| 361 | const NOSNIFF: (&str, &str) = ("x-content-type-options", "nosniff"); |
| 362 | |
| 363 | /// What a raw file is served as. |
| 364 | /// |
| 365 | /// **Never the file's own type, and never guessed from its extension.** Steid serves |
| 366 | /// repository contents from the same origin as the application, so a file the origin |
| 367 | /// labels `text/html` runs *as this site*: it reads the session cookie, calls Steid's |
| 368 | /// own endpoints as the viewer, and rewrites the page around it. Somebody pushing |
| 369 | /// `evil.html` would then have stored XSS on every visitor who followed a link to it. |
| 370 | /// The same holds for SVG (scriptable), XML, and anything a browser will render. |
| 371 | /// |
| 372 | /// Today only the repository's owner can push, so the only person who could attack a |
| 373 | /// viewer is the person whose site it is. Milestone 7 adds other users and this endpoint |
| 374 | /// will outlive that assumption, so the policy is written for the world where the bytes |
| 375 | /// are hostile. |
| 376 | /// |
| 377 | /// Four headers, each closing a different door: |
| 378 | /// |
| 379 | /// - `application/octet-stream` — a type no browser renders. Not `text/plain`, which |
| 380 | /// *is* rendered, and which older browsers have been talked into sniffing as HTML. |
| 381 | /// - `nosniff` — without it a browser is free to ignore the type above and decide from |
| 382 | /// the content, which is exactly the guess this policy refuses to make. |
| 383 | /// - `Content-Disposition: attachment` — the file is saved, not shown, so nothing it |
| 384 | /// contains is ever parsed in this origin's context. It also stops a same-origin |
| 385 | /// `<iframe>` from rendering it. |
| 386 | /// - `Content-Security-Policy: default-src 'none'; sandbox` — belt and braces for the |
| 387 | /// case where one of the above is wrong or unsupported. Nothing in the response may |
| 388 | /// load, run, or navigate. |
| 389 | /// |
| 390 | /// The cost is that a raw URL downloads rather than displays. That is the correct trade |
| 391 | /// for a forge serving other people's bytes, and it is what `curl` wants anyway. |
| 392 | fn raw_response(name: &str, content: Vec<u8>) -> Result<Response<Body>> { |
| 393 | Response::builder() |
| 394 | .header(CONTENT_TYPE, "application/octet-stream") |
| 395 | .header(NOSNIFF.0, NOSNIFF.1) |
| 396 | .header(CONTENT_DISPOSITION, disposition(name)) |
| 397 | .header("content-security-policy", "default-src 'none'; sandbox") |
| 398 | .body(Body::from(content)) |
| 399 | .map_err(server_error) |
| 400 | } |
| 401 | |
| 402 | /// The `Content-Disposition` for a downloaded file. |
| 403 | /// |
| 404 | /// Two filenames, per RFC 6266: a plain `filename` every client understands, and a |
| 405 | /// `filename*` carrying the real name as percent-encoded UTF-8 for those that do. The |
| 406 | /// plain one is reduced to characters that cannot end the quoted string or be read as a |
| 407 | /// header of their own — a name is repository content, so a quote or a newline in it |
| 408 | /// would otherwise be header injection. |
| 409 | fn disposition(name: &str) -> String { |
| 410 | let mut safe = String::with_capacity(name.len()); |
| 411 | |
| 412 | for char in name.chars() { |
| 413 | match char { |
| 414 | 'A'..='Z' | 'a'..='z' | '0'..='9' | '.' | '-' | '_' => safe.push(char), |
| 415 | _ => safe.push('_'), |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // A name reduced to nothing recognisable — punctuation, or a name written entirely |
| 420 | // in a script the plain form cannot carry — downloads as `file` rather than as |
| 421 | // `___`. The real name is still in `filename*` for anything that reads it. |
| 422 | if !safe.chars().any(|char| char.is_ascii_alphanumeric()) { |
| 423 | safe = "file".to_owned(); |
| 424 | } |
| 425 | |
| 426 | format!( |
| 427 | "attachment; filename=\"{safe}\"; filename*=UTF-8''{}", |
| 428 | encode(name, false) |
| 429 | ) |
| 430 | } |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | pub(super) enum Switch<'a> { |
| 441 | Tree(&'a RepoPath), |
| 442 | Blame(&'a RepoPath), |
| 443 | Log, |
| 444 | |
| 445 | |
| 446 | PathLog(&'a RepoPath), |
| 447 | |
| 448 | |
| 449 | |
| 450 | Search(&'a str), |
| 451 | } |
| 452 | |
| 453 | |
| 454 | pub(super) struct RefLink { |
| 455 | name: String, |
| 456 | href: String, |
| 457 | current: bool, |
| 458 | } |
| 459 | |
| 460 | pub(super) fn ref_links( |
| 461 | handle: &str, |
| 462 | name: &str, |
| 463 | refs: &[RefName], |
| 464 | current: &str, |
| 465 | switch: &Switch, |
| 466 | ) -> Vec<RefLink> { |
| 467 | refs.iter() |
| 468 | .map(|git_ref| RefLink { |
| 469 | name: git_ref.to_string(), |
| 470 | href: match switch { |
| 471 | Switch::Tree(path) => tree_url(handle, name, git_ref, path), |
| 472 | Switch::Blame(path) => blame_url(handle, name, git_ref, path), |
| 473 | Switch::Log => log_url(handle, name, git_ref.as_str()), |
| 474 | Switch::PathLog(path) => path_log_url(handle, name, git_ref, path), |
| 475 | Switch::Search(query) => super::search::search_url(handle, name, git_ref, query), |
| 476 | }, |
| 477 | current: git_ref.as_str() == current, |
| 478 | }) |
| 479 | .collect() |
| 480 | } |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | fn is_object_id(rev: &str) -> bool { |
| 487 | rev.len() >= 7 && rev.len() <= 64 && rev.chars().all(|char| char.is_ascii_hexdigit()) |
| 488 | } |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | fn rev_label(rev: &str, known: bool) -> String { |
| 495 | if !known && is_object_id(rev) { |
| 496 | rev[..7].to_owned() |
| 497 | } else { |
| 498 | rev.to_owned() |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | #[component] |
| 514 | pub(super) async fn repo_toolbar( |
| 515 | handle: &str, |
| 516 | name: &str, |
| 517 | rev: &str, |
| 518 | path: &RepoPath, |
| 519 | refs: &RefList, |
| 520 | ) -> Result { |
| 521 | let known = RefName::new(rev).is_ok_and(|at| refs.contains(&at)); |
| 522 | let switch = Switch::Tree(path); |
| 523 | let branches = ref_links(handle, name, &refs.branches, rev, &switch); |
| 524 | let tags = ref_links(handle, name, &refs.tags, rev, &switch); |
| 525 | |
| 526 | view! { |
| 527 | <div class="mb-2 flex flex-wrap items-center gap-x-3 gap-y-2"> |
| 528 | rev_switcher(current: rev, known: known, branches: &branches, tags: &tags) |
| 529 | <span class="inline-flex items-center gap-1.5 text-xs text-muted-foreground"> |
| 530 | icon(data: iconify_icon!("feather:git-branch"), attrs: attributes! { |
| 531 | class="size-3.5" |
| 532 | }) |
| 533 | <a href=(branches_url(handle, name)) class="font-mono hover:text-foreground"> |
| 534 | (counted(refs.branches.len(), "branch", "branches")) |
| 535 | </a> |
| 536 | "·" |
| 537 | <a href=(tags_url(handle, name)) class="font-mono hover:text-foreground"> |
| 538 | (counted(refs.tags.len(), "tag", "tags")) |
| 539 | </a> |
| 540 | </span> |
| 541 | |
| 542 | <div class="w-full sm:ml-auto sm:w-auto"> |
| 543 | search_form( |
| 544 | handle: handle, |
| 545 | name: name, |
| 546 | rev: rev, |
| 547 | query: "", |
| 548 | full_width: false, |
| 549 | ) |
| 550 | </div> |
| 551 | </div> |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | |
| 556 | fn counted(count: usize, one: &str, many: &str) -> String { |
| 557 | format!("{count} {}", if count == 1 { one } else { many }) |
| 558 | } |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | #[component] |
| 570 | pub(super) async fn rev_switcher( |
| 571 | current: &str, |
| 572 | known: bool, |
| 573 | branches: &[RefLink], |
| 574 | tags: &[RefLink], |
| 575 | ) -> Result { |
| 576 | let label = rev_label(current, known); |
| 577 | let empty = branches.is_empty() && tags.is_empty(); |
| 578 | |
| 579 | view! { |
| 580 | if empty { |
| 581 | |
| 582 | |
| 583 | if !current.is_empty() { |
| 584 | <span class="inline-flex items-center gap-1.5 font-mono text-xs text-muted-foreground"> |
| 585 | icon(data: iconify_icon!("feather:git-branch"), attrs: attributes! { |
| 586 | class="size-3.5" |
| 587 | }) |
| 588 | (label) |
| 589 | </span> |
| 590 | } |
| 591 | } else { |
| 592 | <details class="group relative inline-block"> |
| 593 | <summary class="inline-flex cursor-pointer list-none items-center gap-1.5 rounded-lg border border-border px-2.5 py-1 font-mono text-xs text-muted-foreground hover:text-foreground [&::-webkit-details-marker]:hidden"> |
| 594 | icon( |
| 595 | data: if known { |
| 596 | iconify_icon!("feather:git-branch") |
| 597 | } else { |
| 598 | iconify_icon!("feather:git-commit") |
| 599 | }, |
| 600 | attrs: attributes! { class="size-3.5" }, |
| 601 | ) |
| 602 | (if label.is_empty() { "Revision" } else { label.as_str() }) |
| 603 | icon( |
| 604 | data: iconify_icon!("feather:chevron-down"), |
| 605 | attrs: attributes! { |
| 606 | class="size-3.5 transition-transform group-open:rotate-180" |
| 607 | }, |
| 608 | ) |
| 609 | </summary> |
| 610 | |
| 611 | <div class="absolute right-0 z-20 mt-1 max-h-80 w-64 overflow-y-auto rounded-lg border border-border bg-background p-1 shadow-lg"> |
| 612 | if !known && !current.is_empty() { |
| 613 | <p class="px-2 py-1.5 font-mono text-xs text-muted-foreground"> |
| 614 | "At commit " (label) |
| 615 | </p> |
| 616 | } |
| 617 | |
| 618 | ref_group(title: "Branches", links: branches) |
| 619 | ref_group(title: "Tags", links: tags) |
| 620 | </div> |
| 621 | </details> |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | #[component] |
| 632 | async fn ref_group(title: &str, links: &[RefLink]) -> Result { |
| 633 | view! { |
| 634 | if !links.is_empty() { |
| 635 | <p class="px-2 pt-1.5 pb-1 text-xs font-medium uppercase tracking-wider text-muted-foreground"> |
| 636 | (title) |
| 637 | </p> |
| 638 | <ul> |
| 639 | for link in links { |
| 640 | <li> |
| 641 | <a |
| 642 | href=(&link.href) |
| 643 | class=(format!( |
| 644 | "flex items-center gap-2 rounded-md px-2 py-1.5 font-mono text-sm hover:bg-foreground/5 {}", |
| 645 | if link.current { "font-medium" } else { "" }, |
| 646 | )) |
| 647 | > |
| 648 | <span class="truncate">(&link.name)</span> |
| 649 | if link.current { |
| 650 | <span class="ml-auto text-muted-foreground"> |
| 651 | icon( |
| 652 | data: iconify_icon!("feather:check"), |
| 653 | label: "Current", |
| 654 | attrs: attributes! { class="size-3.5" }, |
| 655 | ) |
| 656 | </span> |
| 657 | } |
| 658 | </a> |
| 659 | </li> |
| 660 | } |
| 661 | </ul> |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | #[component] |
| 671 | pub(super) async fn empty_repo(url: &str) -> Result { |
| 672 | let push = format!("git remote add origin {url}\ngit branch -M main\ngit push -u origin main"); |
| 673 | |
| 674 | view! { |
| 675 | <div class="rounded-lg border border-border px-4 py-5"> |
| 676 | <p class="text-sm text-muted-foreground"> |
| 677 | "This repository has no commits yet. Push one to see it here." |
| 678 | </p> |
| 679 | <p class="mt-4 text-xs font-medium uppercase tracking-wider text-muted-foreground"> |
| 680 | "Push an existing repository" |
| 681 | </p> |
| 682 | <pre class="mt-2 overflow-x-auto rounded-lg border border-border bg-surface px-4 py-3 font-mono text-sm">(push)</pre> |
| 683 | </div> |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | #[component] |
| 692 | pub(super) async fn crumbs(handle: &str, name: &str, rev: &RefName, path: &RepoPath) -> Result { |
| 693 | let parts: Vec<&str> = path.components().collect(); |
| 694 | let mut walked = RepoPath::root(); |
| 695 | let mut trail: Vec<(String, String)> = Vec::new(); |
| 696 | |
| 697 | for (index, part) in parts.iter().enumerate() { |
| 698 | walked = walked.join(part); |
| 699 | |
| 700 | let href = if index + 1 == parts.len() { |
| 701 | String::new() |
| 702 | } else { |
| 703 | tree_url(handle, name, rev, &walked) |
| 704 | }; |
| 705 | |
| 706 | trail.push(((*part).to_owned(), href)); |
| 707 | } |
| 708 | |
| 709 | view! { |
| 710 | <div class="flex flex-wrap items-center gap-1 font-mono text-sm"> |
| 711 | <a |
| 712 | href=(tree_url(handle, name, rev, &RepoPath::root())) |
| 713 | class="text-muted-foreground hover:text-foreground" |
| 714 | >(name)</a> |
| 715 | |
| 716 | for (part, href) in &trail { |
| 717 | <span class="text-muted-foreground">"/"</span> |
| 718 | match href.is_empty() { |
| 719 | true => <span class="font-medium">(part)</span>, |
| 720 | false => <a href=(href) class="text-muted-foreground hover:text-foreground">(part)</a>, |
| 721 | } |
| 722 | } |
| 723 | </div> |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | /// What a listing knows about its entries beyond the tree itself. |
| 728 | /// |
| 729 | /// One parameter rather than one map per fact: every one of them is sparse, keyed by |
| 730 | /// entry name, and read by the same row at the same moment. Copied rather than borrowed |
| 731 | /// because it is two references and nothing else. |
| 732 | #[derive(Clone, Copy)] |
| 733 | pub(super) struct Listing<'a> { |
| 734 | pub last_commits: &'a HashMap<String, EntryCommit>, |
| 735 | pub details: &'a HashMap<String, EntryDetail>, |
| 736 | } |
| 737 | |
| 738 | impl<'a> Listing<'a> { |
| 739 | pub fn of( |
| 740 | last_commits: &'a HashMap<String, EntryCommit>, |
| 741 | details: &'a HashMap<String, EntryDetail>, |
| 742 | ) -> Self { |
| 743 | Self { |
| 744 | last_commits, |
| 745 | details, |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | /// A directory listing. |
| 751 | /// |
| 752 | /// Entries arrive ordered by the use case — directories first, then case-insensitively |
| 753 | /// by name — so nothing here re-sorts them. |
| 754 | #[component] |
| 755 | pub(super) async fn directory( |
| 756 | handle: &str, |
| 757 | name: &str, |
| 758 | rev: &RefName, |
| 759 | path: &RepoPath, |
| 760 | entries: &[TreeEntry], |
| 761 | listing: Listing<'_>, |
| 762 | ) -> Result { |
| 763 | // The root's history is the whole log, which already has a page and a URL of its |
| 764 | // own; only a subdirectory needs the narrowed one. |
| 765 | // Named for what it holds, not for the page it points at: `history` is a |
| 766 | // `#[component]` in this module and so a unit struct in scope — a `let history` |
| 767 | // would parse as a pattern rather than a new binding. |
| 768 | let history_href = match path.is_root() { |
| 769 | true => log_url(handle, name, rev.as_str()), |
| 770 | false => path_log_url(handle, name, rev, path), |
| 771 | }; |
| 772 | |
| 773 | view! { |
| 774 | <div class="overflow-hidden rounded-lg border border-border"> |
| 775 | <div class="flex flex-wrap items-center justify-between gap-3 border-b border-border px-4 py-2.5"> |
| 776 | crumbs(handle: handle, name: name, rev: rev, path: path) |
| 777 | // The directory's own history, in the same muted mono as a file |
| 778 | // header's — the entry point for "what has been happening in here", |
| 779 | // which the log answers only for the repository as a whole. |
| 780 | <a |
| 781 | href=(&history_href) |
| 782 | class="font-mono text-xs text-muted-foreground hover:text-foreground" |
| 783 | >"History"</a> |
| 784 | </div> |
| 785 | |
| 786 | if entries.is_empty() { |
| 787 | <p class="px-4 py-6 text-center text-sm text-muted-foreground"> |
| 788 | "This directory is empty." |
| 789 | </p> |
| 790 | } else { |
| 791 | <ul class="divide-y divide-border text-sm"> |
| 792 | match path.parent() { |
| 793 | Some(parent) => <li class="px-4 py-2"> |
| 794 | <a |
| 795 | href=(tree_url(handle, name, rev, &parent)) |
| 796 | class="inline-flex items-center gap-2 font-mono text-muted-foreground hover:text-foreground" |
| 797 | > |
| 798 | icon(data: iconify_icon!("feather:corner-left-up"), attrs: attributes! { |
| 799 | class="size-4" |
| 800 | }) |
| 801 | ".." |
| 802 | </a> |
| 803 | </li>, |
| 804 | None => "", |
| 805 | } |
| 806 | |
| 807 | for entry in entries { |
| 808 | <li class="flex items-center gap-3 px-4 py-2"> |
| 809 | entry_row( |
| 810 | handle: handle, |
| 811 | name: name, |
| 812 | rev: rev, |
| 813 | path: path, |
| 814 | entry: entry, |
| 815 | listing: listing, |
| 816 | ) |
| 817 | </li> |
| 818 | } |
| 819 | </ul> |
| 820 | } |
| 821 | </div> |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | /// One entry in a listing. |
| 826 | /// |
| 827 | /// A symlink and a submodule are their own kinds, not files: a submodule is another |
| 828 | /// repository Steid cannot look inside, so it is labelled and left unlinked rather |
| 829 | /// than offered as a click that would 404. |
| 830 | #[component] |
| 831 | async fn entry_row( |
| 832 | handle: &str, |
| 833 | name: &str, |
| 834 | rev: &RefName, |
| 835 | path: &RepoPath, |
| 836 | entry: &TreeEntry, |
| 837 | listing: Listing<'_>, |
| 838 | ) -> Result { |
| 839 | let last = listing.last_commits.get(&entry.name); |
| 840 | let detail = listing.details.get(&entry.name); |
| 841 | let detail = detail.cloned().unwrap_or_default(); |
| 842 | |
| 843 | // Where the name goes when clicked, in the three cases a listing has: |
| 844 | // |
| 845 | // - a submodule links **out**, to the upstream `.gitmodules` names, when that is an |
| 846 | // `https://` one. Steid cannot look inside another repository, so the alternative |
| 847 | // is a click that 404s. |
| 848 | // - a symlink follows itself, to whatever it points at inside this repository. A |
| 849 | // link out of the tree, or one Steid could not read, falls back to the blob — |
| 850 | // which is the link's own text, since a symlink is stored as its target path. |
| 851 | // - everything else is the path it is. |
| 852 | let href = match (&detail.url, &detail.resolved) { |
| 853 | (Some(url), _) if entry.kind == EntryKind::Submodule => url.clone(), |
| 854 | (_, Some(target)) if entry.kind == EntryKind::Symlink => { |
| 855 | tree_url(handle, name, rev, target) |
| 856 | } |
| 857 | _ => tree_url(handle, name, rev, &path.join(&entry.name)), |
| 858 | }; |
| 859 | |
| 860 | // Only a submodule with no upstream to send anyone to. A symlink always has |
| 861 | // somewhere to go, even if that is only its own contents. |
| 862 | let linkable = entry.kind != EntryKind::Submodule || detail.url.is_some(); |
| 863 | |
| 864 | // An external link, so it carries the two attributes any link out of Steid does. |
| 865 | let external = entry.kind == EntryKind::Submodule && detail.url.is_some(); |
| 866 | |
| 867 | view! { |
| 868 | <span class="text-muted-foreground"> |
| 869 | match entry.kind { |
| 870 | EntryKind::Tree => icon( |
| 871 | data: iconify_icon!("feather:folder"), |
| 872 | label: "Directory", |
| 873 | attrs: attributes! { class="size-4" }, |
| 874 | ), |
| 875 | EntryKind::Blob => icon( |
| 876 | data: iconify_icon!("feather:file"), |
| 877 | label: "File", |
| 878 | attrs: attributes! { class="size-4" }, |
| 879 | ), |
| 880 | EntryKind::Symlink => icon( |
| 881 | data: iconify_icon!("feather:link-2"), |
| 882 | label: "Symlink", |
| 883 | attrs: attributes! { class="size-4" }, |
| 884 | ), |
| 885 | EntryKind::Submodule => icon( |
| 886 | data: iconify_icon!("feather:package"), |
| 887 | label: "Submodule", |
| 888 | attrs: attributes! { class="size-4" }, |
| 889 | ), |
| 890 | } |
| 891 | </span> |
| 892 | |
| 893 | match (linkable, external) { |
| 894 | // The one link in a listing that leaves Steid, so it says so: a click that |
| 895 | // silently lands on another host is a surprise, and the icon is the same |
| 896 | // muted size the rest of the row's metadata is. |
| 897 | (true, true) => <a |
| 898 | href=(href) |
| 899 | rel="noopener noreferrer" |
| 900 | class="inline-flex items-center gap-1 font-mono hover:underline" |
| 901 | > |
| 902 | (&entry.name) |
| 903 | icon( |
| 904 | data: iconify_icon!("feather:external-link"), |
| 905 | label: "Opens the submodule's own repository", |
| 906 | attrs: attributes! { class="size-3 text-muted-foreground" }, |
| 907 | ) |
| 908 | </a>, |
| 909 | (true, false) => <a |
| 910 | href=(href) |
| 911 | class=(if entry.kind.is_tree() { |
| 912 | "font-mono font-medium hover:underline" |
| 913 | } else { |
| 914 | "font-mono hover:underline" |
| 915 | }) |
| 916 | >(&entry.name)</a>, |
| 917 | (false, _) => <span class="font-mono">(&entry.name)</span>, |
| 918 | } |
| 919 | |
| 920 | // Where a link points, muted and after the name: part of what the row *is*, |
| 921 | // and never mistakable for the name itself. Absent when the target could not |
| 922 | // be read, which is honest — the row then says only that this is a symlink. |
| 923 | match &detail.target { |
| 924 | Some(target) => <span class="hidden truncate font-mono text-xs text-muted-foreground sm:inline"> |
| 925 | "→ " (target) |
| 926 | </span>, |
| 927 | None => "", |
| 928 | } |
| 929 | |
| 930 | match entry.kind { |
| 931 | EntryKind::Symlink => badge(variant: BadgeVariant::Outline, "symlink"), |
| 932 | EntryKind::Submodule => badge(variant: BadgeVariant::Outline, "submodule"), |
| 933 | _ => "", |
| 934 | } |
| 935 | |
| 936 | // The two columns 0006 deferred until a kept-alive `cat-file` made them |
| 937 | // affordable. Muted and small: what changed and when is context for the name, |
| 938 | // not a second thing to read. **Dropped below `md` rather than wrapped** — the |
| 939 | // rule the branches page settled: a row is a scanning surface, and a row that |
| 940 | // wraps stops being one. The latest-commit bar above says the same thing once |
| 941 | // for the directory as a whole, which is what a narrow viewport keeps. |
| 942 | match last { |
| 943 | Some(commit) => <a |
| 944 | href=(commit_url(handle, name, commit.id.as_str())) |
| 945 | title=(&commit.summary) |
| 946 | class="ml-3 hidden min-w-0 flex-1 truncate text-xs text-muted-foreground hover:text-foreground md:block" |
| 947 | >(&commit.summary)</a>, |
| 948 | // An entry whose last change is older than the walk reaches. The gap still |
| 949 | // has to hold the column open, or the sizes beside it stop lining up. |
| 950 | None => <span class="ml-3 hidden min-w-0 flex-1 md:block"></span>, |
| 951 | } |
| 952 | |
| 953 | <span class="ml-auto flex shrink-0 items-center gap-3 font-mono text-xs text-muted-foreground"> |
| 954 | match last { |
| 955 | Some(commit) => <span |
| 956 | class="hidden md:inline" |
| 957 | title=(timestamp(commit.committed_at)) |
| 958 | >(ago(commit.committed_at))</span>, |
| 959 | None => "", |
| 960 | } |
| 961 | // Fixed width and right-aligned, so the dates beside it line up rather |
| 962 | // than stepping in and out with whether a row has a size at all — a |
| 963 | // directory has none. The same rule the branches page settled on for the |
| 964 | // sha: trailing metadata is a column, not a trailer. |
| 965 | <span class="w-16 text-right"> |
| 966 | match entry.size { |
| 967 | Some(size) => (size_of(size)), |
| 968 | None if entry.kind == EntryKind::Submodule => (entry.id.short()), |
| 969 | None => "", |
| 970 | } |
| 971 | </span> |
| 972 | </span> |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | /// A single file. |
| 977 | /// |
| 978 | /// Three outcomes, all of them a page rather than an error: text, something that is not |
| 979 | /// text, and something too big to be worth rendering. The last says how big, because |
| 980 | /// that is the only useful thing left to say about it. |
| 981 | #[component] |
| 982 | pub(super) async fn blob( |
| 983 | handle: &str, |
| 984 | name: &str, |
| 985 | rev: &RefName, |
| 986 | path: &RepoPath, |
| 987 | file: &FileView, |
| 988 | /// Whether `?view=source` asked for the line table of a file that would otherwise |
| 989 | /// be rendered. Nothing for a file with only one view. |
| 990 | show_source: bool, |
| 991 | ) -> Result { |
| 992 | // Rendered *by default*, because a rendered document is what the file is for — the |
| 993 | // source of a README is the last thing a reader arriving at it wants. The toggle is |
| 994 | // in the header and the URL says which it is showing. |
| 995 | let markdown = is_markdown(path); |
| 996 | let rendered = markdown && !show_source; |
| 997 | // Only when a grammar was actually found: a file shown as plain text says nothing |
| 998 | // here rather than naming a language whose colour is absent. Nothing at all for a |
| 999 | // binary or an over-large file — `file.text` is `None` there and there is no language |
| 1000 | // to have detected. |
| 1001 | let language = file |
| 1002 | .text |
| 1003 | .as_deref() |
| 1004 | .and_then(|text| language_of(path.file_name().unwrap_or_default(), text)); |
| 1005 | |
| 1006 | view! { |
| 1007 | <div class="overflow-hidden rounded-lg border border-border"> |
| 1008 | <div class="flex flex-wrap items-center justify-between gap-3 border-b border-border px-4 py-2.5"> |
| 1009 | crumbs(handle: handle, name: name, rev: rev, path: path) |
| 1010 | <span class="flex items-center gap-3 font-mono text-xs text-muted-foreground"> |
| 1011 | // Beside the size and in the same muted mono, because it is the same |
| 1012 | // kind of fact about the file: what it is, not what to do with it. |
| 1013 | // In its own element rather than as bare text: two adjacent text |
| 1014 | // nodes are one anonymous flex item, so the row's `gap-3` would not |
| 1015 | // separate them and the header would read "Rust7.3 KB". |
| 1016 | match language { |
| 1017 | Some(language) => <span>(language)</span>, |
| 1018 | None => "", |
| 1019 | } |
| 1020 | <span>(size_of(file.size))</span> |
| 1021 | view_toggle( |
| 1022 | handle: handle, |
| 1023 | name: name, |
| 1024 | rev: rev, |
| 1025 | path: path, |
| 1026 | active: if rendered { FileTab::Code } else if markdown { FileTab::Source } else { FileTab::Code }, |
| 1027 | markdown: markdown, |
| 1028 | ) |
| 1029 | </span> |
| 1030 | </div> |
| 1031 | |
| 1032 | match &file.text { |
| 1033 | Some(text) if rendered => rendered_markdown( |
| 1034 | handle: handle, |
| 1035 | name: name, |
| 1036 | rev: rev, |
| 1037 | path: path, |
| 1038 | text: text.as_str(), |
| 1039 | ), |
| 1040 | Some(text) => source( |
| 1041 | text: text.as_str(), |
| 1042 | file_name: path.file_name().unwrap_or_default(), |
| 1043 | id: file.id.as_str(), |
| 1044 | ), |
| 1045 | None if file.too_large => <p class="px-4 py-6 text-center text-sm text-muted-foreground"> |
| 1046 | "This file is " (size_of(file.size)) ", which is too large to display. Clone the repository to read it." |
| 1047 | </p>, |
| 1048 | None => <p class="px-4 py-6 text-center text-sm text-muted-foreground"> |
| 1049 | "This file cannot be displayed as text." |
| 1050 | </p>, |
| 1051 | } |
| 1052 | </div> |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | /// Extensions Steid renders as markdown. |
| 1057 | /// |
| 1058 | /// The same set the README card accepts, minus the extensionless spelling: a file |
| 1059 | /// called `NOTES` is not markdown, whereas a `README` is by convention. |
| 1060 | const MARKDOWN_EXTENSIONS: [&str; 4] = ["md", "markdown", "mdown", "mkd"]; |
| 1061 | |
| 1062 | /// Whether a path names a markdown file, which is what gives it two views. |
| 1063 | pub(super) fn is_markdown(path: &RepoPath) -> bool { |
| 1064 | let Some(name) = path.file_name() else { |
| 1065 | return false; |
| 1066 | }; |
| 1067 | let Some((_, extension)) = name.rsplit_once('.') else { |
| 1068 | return false; |
| 1069 | }; |
| 1070 | |
| 1071 | MARKDOWN_EXTENSIONS.contains(&extension.to_ascii_lowercase().as_str()) |
| 1072 | } |
| 1073 | |
| 1074 | /// A markdown file, rendered. |
| 1075 | /// |
| 1076 | /// The README card's typography, because it is the same thing in a different frame — |
| 1077 | /// two readings of one document would be two designs for one thing. |
| 1078 | /// |
| 1079 | /// **Relative destinations resolve against the file's own directory**, which is where |
| 1080 | /// they were written from, and links and images resolve *differently*: a link to |
| 1081 | /// another file means the page that shows it, and an image means the file's bytes. The |
| 1082 | /// README card resolves against the root and leaves images alone, which is the same |
| 1083 | /// rule for a document that happens to be at the root. |
| 1084 | #[component] |
| 1085 | async fn rendered_markdown( |
| 1086 | handle: &str, |
| 1087 | name: &str, |
| 1088 | rev: &RefName, |
| 1089 | path: &RepoPath, |
| 1090 | text: &str, |
| 1091 | ) -> Result { |
| 1092 | // A file has a parent by construction — the root is a directory, not a file. |
| 1093 | let dir = path.parent().unwrap_or_default(); |
| 1094 | |
| 1095 | view! { |
| 1096 | // The only unescaped content on any Steid page, and safe for the reason the |
| 1097 | // README card's is: `markdown` writes every tag itself and never passes source |
| 1098 | // HTML through. See that module's header. |
| 1099 | <div class="px-5 py-4 text-sm [&>*:first-child]:mt-0 [&>*:last-child]:mb-0"> |
| 1100 | (markdown::render_with_targets( |
| 1101 | text, |
| 1102 | Some(markdown::Crossref { |
| 1103 | handle, |
| 1104 | name, |
| 1105 | origin: None, |
| 1106 | }), |
| 1107 | |kind, destination| { |
| 1108 | // The fragment is carried across: rendered headings have ids, so |
| 1109 | // `notes.md#usage` lands on the section it names. A query means |
| 1110 | // nothing to a tree URL and is dropped, and a bare `#section` has no |
| 1111 | // path to resolve and is left exactly as written. |
| 1112 | let (target, fragment) = split_fragment(destination); |
| 1113 | let target = target.split('?').next()?; |
| 1114 | let resolved = resolve_relative(&dir, target)?; |
| 1115 | |
| 1116 | Some(match kind { |
| 1117 | markdown::Relative::Link => { |
| 1118 | format!("{}{fragment}", tree_url(handle, name, rev, &resolved)) |
| 1119 | } |
| 1120 | // Bytes, not a page: an image pointed at a tree URL is a broken |
| 1121 | // image rather than a picture, and a fragment means nothing to |
| 1122 | // one. |
| 1123 | markdown::Relative::Image => raw_url(handle, name, rev, &resolved), |
| 1124 | }) |
| 1125 | }, |
| 1126 | )) |
| 1127 | </div> |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | /// A file's contents, with line numbers. |
| 1132 | /// |
| 1133 | /// A table rather than a `<pre>` with a gutter: the numbers stay put when the code |
| 1134 | /// scrolls sideways, and selecting the code does not drag the numbers along with it. |
| 1135 | /// |
| 1136 | /// Highlighting only ever changes what is *inside* the code cell — the rows, the |
| 1137 | /// numbers and the scroll container are the same whether a language was recognised or |
| 1138 | /// not, so nothing else on the page has to know. |
| 1139 | /// |
| 1140 | /// |
| 1141 | /// Each number cell carries an `id="L{n}"`, which is what a search result links to: a |
| 1142 | /// match on line 400 of a long file should land on line 400. |
| 1143 | #[component] |
| 1144 | async fn source(cx: &Cx, text: &str, file_name: &str, id: &str) -> Result { |
| 1145 | // Through the cache, keyed by the blob's object id: git has already hashed these |
| 1146 | // bytes, so the key is free and two revisions of an unchanged file share one entry. |
| 1147 | // |
| 1148 | // Cloned out of the `Arc` rather than borrowed from it, because the view outlives |
| 1149 | // this function body and a local it only borrows does not — the same reason blame |
| 1150 | // flattens its rows before its `view!`. It is a `Vec<String>` copy against a parse |
| 1151 | // of the whole file: three orders of magnitude apart, and the parse is what the |
| 1152 | // cache is here to skip. |
| 1153 | let Source { lines, too_large } = highlights(cx) |
| 1154 | .source_lines(id, file_name, text) |
| 1155 | .as_ref() |
| 1156 | .clone(); |
| 1157 | |
| 1158 | view! { |
| 1159 | if too_large { |
| 1160 | <p class="border-b border-border px-4 py-1.5 font-mono text-xs text-muted-foreground"> |
| 1161 | "Too large to highlight — shown as plain text." |
| 1162 | </p> |
| 1163 | } |
| 1164 | |
| 1165 | <div class="overflow-x-auto"> |
| 1166 | <table class="w-full border-collapse font-mono text-xs leading-relaxed"> |
| 1167 | <tbody> |
| 1168 | for (index, line) in lines.into_iter().enumerate() { |
| 1169 | <tr> |
| 1170 | <td |
| 1171 | id=(format!("L{}", index + 1)) |
| 1172 | class="w-px select-none border-r border-border px-3 text-right align-top text-muted-foreground" |
| 1173 | > |
| 1174 | ((index + 1).to_string()) |
| 1175 | </td> |
| 1176 | <td class="whitespace-pre px-4 align-top"> |
| 1177 | match line { |
| 1178 | // Markup this codebase wrote: `highlight` escapes |
| 1179 | // the source itself and emits nothing but spans. |
| 1180 | SourceLine::Classed(html) => (Unescaped::new_unchecked(html)), |
| 1181 | SourceLine::Plain(text) => (text), |
| 1182 | } |
| 1183 | </td> |
| 1184 | </tr> |
| 1185 | } |
| 1186 | </tbody> |
| 1187 | </table> |
| 1188 | </div> |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | /// The commit log — the most recent commits, newest first, and no paging in v1. |
| 1193 | /// |
| 1194 | /// Shared with the compare page, which lists the commits a branch adds in exactly this |
| 1195 | /// shape: two lists of commits that read differently would be two designs for one |
| 1196 | /// thing. |
| 1197 | #[component] |
| 1198 | pub(super) async fn commit_log(handle: &str, name: &str, commits: &[CommitSummary]) -> Result { |
| 1199 | view! { |
| 1200 | if commits.is_empty() { |
| 1201 | <p class="rounded-lg border border-border px-4 py-10 text-center text-sm text-muted-foreground"> |
| 1202 | "No commits yet." |
| 1203 | </p> |
| 1204 | } else { |
| 1205 | <ul class="divide-y divide-border rounded-lg border border-border"> |
| 1206 | for commit in commits { |
| 1207 | <li class="px-4 py-3"> |
| 1208 | <div class="flex items-baseline justify-between gap-4"> |
| 1209 | <p class="text-sm font-medium">(&commit.summary)</p> |
| 1210 | <a |
| 1211 | href=(commit_url(handle, name, commit.id.as_str())) |
| 1212 | class="shrink-0 font-mono text-xs text-muted-foreground hover:text-foreground" |
| 1213 | >(commit.id.short())</a> |
| 1214 | </div> |
| 1215 | <p class="mt-1 text-xs text-muted-foreground"> |
| 1216 | (&commit.author_name) |
| 1217 | " committed " |
| 1218 | <span title=(timestamp(commit.committed_at))>(ago(commit.committed_at))</span> |
| 1219 | </p> |
| 1220 | </li> |
| 1221 | } |
| 1222 | </ul> |
| 1223 | } |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | #[cfg(test)] |
| 1228 | mod tests { |
| 1229 | use super::*; |
| 1230 | |
| 1231 | #[test] |
| 1232 | fn markdown_is_recognised_by_extension_and_nothing_else() { |
| 1233 | for name in ["README.md", "docs/design.MARKDOWN", "notes.mkd", "a.mdown"] { |
| 1234 | assert!( |
| 1235 | is_markdown(&RepoPath::new(name).expect("valid")), |
| 1236 | "{name} should be markdown" |
| 1237 | ); |
| 1238 | } |
| 1239 | |
| 1240 | for name in ["README", "main.rs", "docs/md", "notes.md.bak"] { |
| 1241 | assert!( |
| 1242 | !is_markdown(&RepoPath::new(name).expect("valid")), |
| 1243 | "{name} should not be markdown" |
| 1244 | ); |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | #[test] |
| 1249 | fn switching_revision_on_a_paths_history_stays_on_that_path() { |
| 1250 | let refs = [RefName::from_trusted("v1.0")]; |
| 1251 | let path = RepoPath::new("src").expect("valid"); |
| 1252 | let links = ref_links("ada", "steid", &refs, "main", &Switch::PathLog(&path)); |
| 1253 | |
| 1254 | assert_eq!(links[0].href, "/ada/repos/steid/log/v1.0/-/src"); |
| 1255 | } |
| 1256 | |
| 1257 | #[test] |
| 1258 | fn a_raw_response_carries_the_whole_policy() { |
| 1259 | let response = raw_response("notes.txt", b"hello".to_vec()).expect("should build"); |
| 1260 | let header = |name: &str| { |
| 1261 | response |
| 1262 | .headers() |
| 1263 | .get(name) |
| 1264 | .and_then(|value| value.to_str().ok()) |
| 1265 | .unwrap_or_default() |
| 1266 | .to_owned() |
| 1267 | }; |
| 1268 | |
| 1269 | // Each of these is load-bearing on its own; see `raw_response`. |
| 1270 | assert_eq!(header("content-type"), "application/octet-stream"); |
| 1271 | assert_eq!(header("x-content-type-options"), "nosniff"); |
| 1272 | assert!(header("content-disposition").starts_with("attachment;")); |
| 1273 | assert_eq!( |
| 1274 | header("content-security-policy"), |
| 1275 | "default-src \'none\'; sandbox" |
| 1276 | ); |
| 1277 | } |
| 1278 | |
| 1279 | #[test] |
| 1280 | fn a_disposition_carries_both_spellings_of_the_name() { |
| 1281 | assert_eq!( |
| 1282 | disposition("notes.txt"), |
| 1283 | "attachment; filename=\"notes.txt\"; filename*=UTF-8\'\'notes.txt" |
| 1284 | ); |
| 1285 | } |
| 1286 | |
| 1287 | #[test] |
| 1288 | fn a_disposition_cannot_be_escaped_by_a_filename() { |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | let hostile = disposition("a\"; x=1\r\nSet-Cookie: nope=1"); |
| 1293 | |
| 1294 | assert!(!hostile.contains('\r')); |
| 1295 | assert!(!hostile.contains('\n')); |
| 1296 | assert_eq!(hostile.matches('"').count(), 2); |
| 1297 | } |
| 1298 | |
| 1299 | #[test] |
| 1300 | fn a_nameless_file_still_downloads_as_something() { |
| 1301 | assert!(disposition("...").starts_with("attachment; filename=\"file\"")); |
| 1302 | } |
| 1303 | |
| 1304 | #[test] |
| 1305 | fn a_non_ascii_name_survives_in_the_extended_form() { |
| 1306 | let value = disposition("日本語.txt"); |
| 1307 | |
| 1308 | |
| 1309 | |
| 1310 | assert!(value.contains("filename=\"___.txt\"")); |
| 1311 | assert!(value.contains("filename*=UTF-8\'\'%E6%97%A5%E6%9C%AC%E8%AA%9E.txt")); |
| 1312 | } |
| 1313 | |
| 1314 | #[test] |
| 1315 | fn the_switcher_marks_the_revision_it_is_on() { |
| 1316 | let refs = [RefName::from_trusted("main"), RefName::from_trusted("next")]; |
| 1317 | let path = RepoPath::new("src").expect("valid"); |
| 1318 | let links = ref_links("ada", "steid", &refs, "next", &Switch::Tree(&path)); |
| 1319 | |
| 1320 | assert_eq!(links[0].href, "/ada/repos/steid/tree/main/-/src"); |
| 1321 | assert!(!links[0].current); |
| 1322 | assert!(links[1].current); |
| 1323 | } |
| 1324 | |
| 1325 | #[test] |
| 1326 | fn switching_from_the_log_stays_on_the_log() { |
| 1327 | let refs = [RefName::from_trusted("v1.0")]; |
| 1328 | let links = ref_links("ada", "steid", &refs, "main", &Switch::Log); |
| 1329 | |
| 1330 | assert_eq!(links[0].href, "/ada/repos/steid/log/v1.0"); |
| 1331 | } |
| 1332 | |
| 1333 | #[test] |
| 1334 | fn an_object_id_is_labelled_as_a_commit_rather_than_a_branch() { |
| 1335 | let id = "0123456789abcdef0123456789abcdef01234567"; |
| 1336 | |
| 1337 | assert!(is_object_id(id)); |
| 1338 | assert_eq!(rev_label(id, false), "0123456"); |
| 1339 | |
| 1340 | assert_eq!(rev_label("deadbeef", true), "deadbeef"); |
| 1341 | assert_eq!(rev_label("main", false), "main"); |
| 1342 | } |
| 1343 | } |