dce0bf3feat: browse a repository's files and history8d | 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
dce0bf3feat: browse a repository's files and history8d | 21 | |
| 22 | use std::time::{SystemTime, UNIX_EPOCH}; |
| 23 | |
| 24 | use topcoat::{ |
| 25 | Result, |
| 26 | context::Cx, |
| 27 | icon::{icon, iconify::iconify_icon}, |
| 28 | router::{ |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 29 | Body, Response, StatusCode, |
dce0bf3feat: browse a repository's files and history8d | 30 | error::{RouterErrorExt, not_found}, |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 31 | header::{CONTENT_DISPOSITION, CONTENT_TYPE}, |
| 32 | page, path_param, route, |
dce0bf3feat: browse a repository's files and history8d | 33 | }, |
979134bfeat: code reads as code, in classes rather than colours19h | 34 | view::{Unescaped, attributes, component, view}, |
dce0bf3feat: browse a repository's files and history8d | 35 | }; |
| 36 | |
| 37 | use crate::{ |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 38 | application::{ |
| 39 | Browsed, FileView, RepoView, |
| 40 | browse::{RawFile, RefList, list_refs, read_raw_file}, |
| 41 | browse_repo, repo_log, |
| 42 | }, |
dce0bf3feat: browse a repository's files and history8d | 43 | components::badge::{BadgeVariant, badge}, |
| 44 | domain::{CommitSummary, EntryKind, RefName, RepoPath, TreeEntry}, |
979134bfeat: code reads as code, in classes rather than colours19h | 45 | infrastructure::highlight::{Source, SourceLine, source_lines}, |
dce0bf3feat: browse a repository's files and history8d | 46 | }; |
| 47 | |
| 48 | use super::{ |
8804ad9feat: blame can change revision, like every other page under Code18h | 49 | blame::{FileTab, blame_url, view_toggle}, |
2268309feat: a commit is a page, and two revisions can be compared19h | 50 | commit::commit_url, |
dce0bf3feat: browse a repository's files and history8d | 51 | context::{current_actor, memberships, orgs, queries, repos, server_error}, |
5d3dfa5feat: a global top bar, and pages choose their own width1d | 52 | layout::wide, |
adac3a7feat: branches and tags get pages, so the counts stop being dead ends19h | 53 | refs::{branches_url, tags_url}, |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 54 | repo::{Tab, clone_url_for, repo_for, repo_header}, |
1b7587dfeat: finding a string in a repository, and landing on the line19h | 55 | search::search_form, |
dce0bf3feat: browse a repository's files and history8d | 56 | }; |
| 57 | |
| 58 | |
| 59 | #[path_param] |
| 60 | struct Rev(str); |
| 61 | |
| 62 | |
| 63 | #[path_param] |
| 64 | struct Path(str); |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | fn rev_param(cx: &Cx) -> Result<RefName> { |
| 71 | Ok(RefName::new(path_param::<Rev>(cx)).map_err(|_| not_found())?) |
| 72 | } |
| 73 | |
| 74 | |
| 75 | fn path_arg(cx: &Cx) -> Result<RepoPath> { |
| 76 | Ok(RepoPath::new(path_param::<Path>(cx)).map_err(|_| not_found())?) |
| 77 | } |
| 78 | |
| 79 | #[page("/{handle}/repos/{name}/tree/{rev}")] |
| 80 | async fn tree_root_page(cx: &Cx) -> Result { |
| 81 | let rev = rev_param(cx)?; |
| 82 | |
| 83 | view! { browsing(rev: Some(rev), path: RepoPath::root()) } |
| 84 | } |
| 85 | |
| 86 | #[page("/{handle}/repos/{name}/tree/{rev}/-/{*path}")] |
| 87 | async fn tree_path_page(cx: &Cx) -> Result { |
| 88 | let rev = rev_param(cx)?; |
| 89 | let path = path_arg(cx)?; |
| 90 | |
| 91 | view! { browsing(rev: Some(rev), path: path) } |
| 92 | } |
| 93 | |
| 94 | #[page("/{handle}/repos/{name}/log")] |
| 95 | async fn log_page(_cx: &Cx) -> Result { |
| 96 | view! { history(rev: None) } |
| 97 | } |
| 98 | |
| 99 | #[page("/{handle}/repos/{name}/log/{rev}")] |
| 100 | async fn log_rev_page(cx: &Cx) -> Result { |
| 101 | let rev = rev_param(cx)?; |
| 102 | |
| 103 | view! { history(rev: Some(rev)) } |
| 104 | } |
| 105 | |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | #[route(GET "/{handle}/repos/{name}/raw/{rev}/-/{*path}")] |
| 115 | async fn raw_page(cx: &Cx) -> Result<Response<Body>> { |
| 116 | let rev = rev_param(cx)?; |
| 117 | let path = path_arg(cx)?; |
| 118 | let repo = repo_for(cx).await?; |
| 119 | |
| 120 | let raw = read_raw_file( |
| 121 | &repo.handle, |
| 122 | &repo.name, |
| 123 | Some(&rev), |
| 124 | &path, |
| 125 | ¤t_actor(cx).await?, |
| 126 | &orgs(cx), |
| 127 | &memberships(cx), |
| 128 | &repos(cx), |
| 129 | &queries(cx), |
| 130 | ) |
| 131 | .await |
| 132 | .map_err(server_error)? |
| 133 | .ok_or_not_found()?; |
| 134 | |
| 135 | match raw { |
| 136 | RawFile::Ready { name, content } => raw_response(&name, content), |
| 137 | |
| 138 | |
| 139 | RawFile::TooLarge { size } => Response::builder() |
| 140 | .status(StatusCode::PAYLOAD_TOO_LARGE) |
| 141 | .header(CONTENT_TYPE, "text/plain; charset=utf-8") |
| 142 | .header(NOSNIFF.0, NOSNIFF.1) |
| 143 | .body(Body::from(format!( |
| 144 | "This file is {}, which is larger than this instance serves raw. Clone the repository to read it.\n", |
| 145 | size_of(size) |
| 146 | ))) |
| 147 | .map_err(server_error), |
| 148 | } |
| 149 | } |
| 150 | |
dce0bf3feat: browse a repository's files and history8d | 151 | |
| 152 | |
| 153 | |
| 154 | pub(super) async fn browsed_at( |
| 155 | cx: &Cx, |
| 156 | repo: &RepoView, |
| 157 | rev: Option<&RefName>, |
| 158 | path: &RepoPath, |
| 159 | ) -> Result<Browsed> { |
| 160 | Ok(browse_repo( |
| 161 | &repo.handle, |
| 162 | &repo.name, |
| 163 | rev, |
| 164 | path, |
| 165 | ¤t_actor(cx).await?, |
| 166 | &orgs(cx), |
| 167 | &memberships(cx), |
| 168 | &repos(cx), |
| 169 | &queries(cx), |
| 170 | ) |
| 171 | .await |
| 172 | .map_err(server_error)? |
| 173 | .ok_or_not_found()?) |
| 174 | } |
| 175 | |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
8804ad9feat: blame can change revision, like every other page under Code18h | 183 | pub(super) async fn refs_for(cx: &Cx, repo: &RepoView) -> Result<RefList> { |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 184 | Ok(list_refs( |
| 185 | &repo.handle, |
| 186 | &repo.name, |
| 187 | ¤t_actor(cx).await?, |
| 188 | &orgs(cx), |
| 189 | &memberships(cx), |
| 190 | &repos(cx), |
| 191 | &queries(cx), |
| 192 | ) |
| 193 | .await |
| 194 | .map_err(server_error)? |
| 195 | .ok_or_not_found()?) |
| 196 | } |
| 197 | |
dce0bf3feat: browse a repository's files and history8d | 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | #[component] |
| 204 | async fn browsing(cx: &Cx, rev: Option<RefName>, path: RepoPath) -> Result { |
| 205 | let repo = repo_for(cx).await?; |
| 206 | let browsed = browsed_at(cx, &repo, rev.as_ref(), &path).await?; |
| 207 | let clone = clone_url_for(cx, &repo); |
| 208 | |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 209 | |
| 210 | let refs = match browsed { |
| 211 | Browsed::Empty => RefList::default(), |
| 212 | _ => refs_for(cx, &repo).await?, |
| 213 | }; |
| 214 | |
| 215 | let at = browsed_rev(&browsed); |
| 216 | let switch = Switch::Tree(&path); |
| 217 | let handle = repo.handle.as_str(); |
| 218 | let name = repo.name.as_str(); |
| 219 | let known = RefName::new(at).is_ok_and(|at| refs.contains(&at)); |
| 220 | let branches = ref_links(handle, name, &refs.branches, at, &switch); |
| 221 | let tags = ref_links(handle, name, &refs.tags, at, &switch); |
| 222 | |
dce0bf3feat: browse a repository's files and history8d | 223 | view! { |
5d3dfa5feat: a global top bar, and pages choose their own width1d | 224 | wide( |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 225 | repo_header( |
5d3dfa5feat: a global top bar, and pages choose their own width1d | 226 | repo: &repo, |
| 227 | rev: at, |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 228 | active: Tab::Code, |
5d3dfa5feat: a global top bar, and pages choose their own width1d | 229 | rev_switcher(current: at, known: known, branches: &branches, tags: &tags) |
| 230 | ) |
| 231 | |
| 232 | match &browsed { |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 233 | Browsed::Empty => empty_repo(url: clone.as_str()), |
5d3dfa5feat: a global top bar, and pages choose their own width1d | 234 | Browsed::Directory { rev, path, entries } => directory( |
| 235 | handle: repo.handle.as_str(), |
| 236 | name: repo.name.as_str(), |
| 237 | rev: rev, |
| 238 | path: path, |
| 239 | entries: entries, |
| 240 | ), |
| 241 | Browsed::File { rev, path, file } => blob( |
| 242 | handle: repo.handle.as_str(), |
| 243 | name: repo.name.as_str(), |
| 244 | rev: rev, |
| 245 | path: path, |
| 246 | file: file, |
| 247 | ), |
| 248 | } |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 249 | ) |
dce0bf3feat: browse a repository's files and history8d | 250 | } |
| 251 | } |
| 252 | |
| 253 | |
| 254 | #[component] |
| 255 | async fn history(cx: &Cx, rev: Option<RefName>) -> Result { |
| 256 | let repo = repo_for(cx).await?; |
| 257 | |
| 258 | let log = repo_log( |
| 259 | &repo.handle, |
| 260 | &repo.name, |
| 261 | rev.as_ref(), |
| 262 | ¤t_actor(cx).await?, |
| 263 | &orgs(cx), |
| 264 | &memberships(cx), |
| 265 | &repos(cx), |
| 266 | &queries(cx), |
| 267 | ) |
| 268 | .await |
| 269 | .map_err(server_error)? |
| 270 | .ok_or_not_found()?; |
| 271 | |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 272 | let refs = refs_for(cx, &repo).await?; |
| 273 | let at = rev.as_ref().map(RefName::as_str).unwrap_or_default(); |
| 274 | let handle = repo.handle.as_str(); |
| 275 | let name = repo.name.as_str(); |
| 276 | let known = RefName::new(at).is_ok_and(|at| refs.contains(&at)); |
| 277 | let branches = ref_links(handle, name, &refs.branches, at, &Switch::Log); |
| 278 | let tags = ref_links(handle, name, &refs.tags, at, &Switch::Log); |
| 279 | |
dce0bf3feat: browse a repository's files and history8d | 280 | view! { |
5d3dfa5feat: a global top bar, and pages choose their own width1d | 281 | wide( |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 282 | repo_header( |
5d3dfa5feat: a global top bar, and pages choose their own width1d | 283 | repo: &repo, |
| 284 | rev: at, |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 285 | active: Tab::Commits, |
5d3dfa5feat: a global top bar, and pages choose their own width1d | 286 | |
| 287 | |
| 288 | |
| 289 | rev_switcher(current: at, known: known, branches: &branches, tags: &tags) |
| 290 | ) |
2268309feat: a commit is a page, and two revisions can be compared19h | 291 | commit_log(handle: handle, name: name, commits: &log) |
dce0bf3feat: browse a repository's files and history8d | 292 | ) |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 297 | pub(super) fn browsed_rev(browsed: &Browsed) -> &str { |
dce0bf3feat: browse a repository's files and history8d | 298 | match browsed { |
| 299 | Browsed::Empty => "", |
| 300 | Browsed::Directory { rev, .. } | Browsed::File { rev, .. } => rev.as_str(), |
| 301 | } |
| 302 | } |
| 303 | |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | const NOSNIFF: (&str, &str) = ("x-content-type-options", "nosniff"); |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | fn raw_response(name: &str, content: Vec<u8>) -> Result<Response<Body>> { |
| 342 | Response::builder() |
| 343 | .header(CONTENT_TYPE, "application/octet-stream") |
| 344 | .header(NOSNIFF.0, NOSNIFF.1) |
| 345 | .header(CONTENT_DISPOSITION, disposition(name)) |
| 346 | .header("content-security-policy", "default-src 'none'; sandbox") |
| 347 | .body(Body::from(content)) |
| 348 | .map_err(server_error) |
| 349 | } |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | fn disposition(name: &str) -> String { |
| 359 | let mut safe = String::with_capacity(name.len()); |
| 360 | |
| 361 | for char in name.chars() { |
| 362 | match char { |
| 363 | 'A'..='Z' | 'a'..='z' | '0'..='9' | '.' | '-' | '_' => safe.push(char), |
| 364 | _ => safe.push('_'), |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | if !safe.chars().any(|char| char.is_ascii_alphanumeric()) { |
| 372 | safe = "file".to_owned(); |
| 373 | } |
| 374 | |
| 375 | format!( |
| 376 | "attachment; filename=\"{safe}\"; filename*=UTF-8''{}", |
| 377 | encode(name, false) |
| 378 | ) |
| 379 | } |
| 380 | |
dce0bf3feat: browse a repository's files and history8d | 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | pub(super) fn tree_url(handle: &str, name: &str, rev: &RefName, path: &RepoPath) -> String { |
| 389 | let encoded = encode(rev.as_str(), false); |
| 390 | |
| 391 | if path.is_root() { |
| 392 | format!("/{handle}/repos/{name}/tree/{encoded}") |
| 393 | } else { |
| 394 | format!( |
| 395 | "/{handle}/repos/{name}/tree/{encoded}/-/{}", |
| 396 | encode(path.as_str(), true) |
| 397 | ) |
| 398 | } |
| 399 | } |
| 400 | |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | pub(super) fn raw_url(handle: &str, name: &str, rev: &RefName, path: &RepoPath) -> String { |
| 406 | format!( |
| 407 | "/{handle}/repos/{name}/raw/{}/-/{}", |
| 408 | encode(rev.as_str(), false), |
| 409 | encode(path.as_str(), true) |
| 410 | ) |
| 411 | } |
| 412 | |
dce0bf3feat: browse a repository's files and history8d | 413 | |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 414 | pub(super) fn log_url(handle: &str, name: &str, rev: &str) -> String { |
dce0bf3feat: browse a repository's files and history8d | 415 | if rev.is_empty() { |
| 416 | format!("/{handle}/repos/{name}/log") |
| 417 | } else { |
| 418 | format!("/{handle}/repos/{name}/log/{}", encode(rev, false)) |
| 419 | } |
| 420 | } |
| 421 | |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 422 | |
| 423 | |
| 424 | |
| 425 | |
8804ad9feat: blame can change revision, like every other page under Code18h | 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | pub(super) enum Switch<'a> { |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 431 | Tree(&'a RepoPath), |
8804ad9feat: blame can change revision, like every other page under Code18h | 432 | Blame(&'a RepoPath), |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 433 | Log, |
| 434 | } |
| 435 | |
| 436 | |
8804ad9feat: blame can change revision, like every other page under Code18h | 437 | pub(super) struct RefLink { |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 438 | name: String, |
| 439 | href: String, |
| 440 | current: bool, |
| 441 | } |
| 442 | |
8804ad9feat: blame can change revision, like every other page under Code18h | 443 | pub(super) fn ref_links( |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 444 | handle: &str, |
| 445 | name: &str, |
| 446 | refs: &[RefName], |
| 447 | current: &str, |
| 448 | switch: &Switch, |
| 449 | ) -> Vec<RefLink> { |
| 450 | refs.iter() |
| 451 | .map(|git_ref| RefLink { |
| 452 | name: git_ref.to_string(), |
| 453 | href: match switch { |
| 454 | Switch::Tree(path) => tree_url(handle, name, git_ref, path), |
8804ad9feat: blame can change revision, like every other page under Code18h | 455 | Switch::Blame(path) => blame_url(handle, name, git_ref, path), |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 456 | Switch::Log => log_url(handle, name, git_ref.as_str()), |
| 457 | }, |
| 458 | current: git_ref.as_str() == current, |
| 459 | }) |
| 460 | .collect() |
| 461 | } |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | fn is_object_id(rev: &str) -> bool { |
| 468 | rev.len() >= 7 && rev.len() <= 64 && rev.chars().all(|char| char.is_ascii_hexdigit()) |
| 469 | } |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | fn rev_label(rev: &str, known: bool) -> String { |
| 476 | if !known && is_object_id(rev) { |
| 477 | rev[..7].to_owned() |
| 478 | } else { |
| 479 | rev.to_owned() |
| 480 | } |
| 481 | } |
| 482 | |
dce0bf3feat: browse a repository's files and history8d | 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
adac3a7feat: branches and tags get pages, so the counts stop being dead ends19h | 488 | pub(super) fn encode(value: &str, keep_slash: bool) -> String { |
dce0bf3feat: browse a repository's files and history8d | 489 | let mut encoded = String::with_capacity(value.len()); |
| 490 | |
| 491 | for byte in value.bytes() { |
| 492 | match byte { |
| 493 | b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => { |
| 494 | encoded.push(byte as char); |
| 495 | } |
| 496 | b'/' if keep_slash => encoded.push('/'), |
| 497 | other => encoded.push_str(&format!("%{other:02X}")), |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | encoded |
| 502 | } |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
f42a185feat: a file read by who last changed each line, and a way between the two views19h | 507 | pub(super) fn size_of(bytes: u64) -> String { |
dce0bf3feat: browse a repository's files and history8d | 508 | const UNITS: [&str; 4] = ["KB", "MB", "GB", "TB"]; |
| 509 | |
| 510 | if bytes < 1024 { |
| 511 | return format!("{bytes} B"); |
| 512 | } |
| 513 | |
| 514 | let mut value = bytes as f64 / 1024.0; |
| 515 | let mut unit = UNITS[0]; |
| 516 | |
| 517 | for next in &UNITS[1..] { |
| 518 | if value < 1024.0 { |
| 519 | break; |
| 520 | } |
| 521 | |
| 522 | value /= 1024.0; |
| 523 | unit = next; |
| 524 | } |
| 525 | |
| 526 | format!("{value:.1} {unit}") |
| 527 | } |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
ef23868feat: rebuild the profile page on flat navigation7d | 534 | pub(super) fn ago(time: SystemTime) -> String { |
dce0bf3feat: browse a repository's files and history8d | 535 | let Ok(elapsed) = SystemTime::now().duration_since(time) else { |
| 536 | return "just now".to_owned(); |
| 537 | }; |
| 538 | |
| 539 | let seconds = elapsed.as_secs(); |
| 540 | |
| 541 | let (count, unit) = match seconds { |
| 542 | 0..=59 => return "just now".to_owned(), |
| 543 | 60..=3599 => (seconds / 60, "minute"), |
| 544 | 3600..=86_399 => (seconds / 3600, "hour"), |
| 545 | 86_400..=2_591_999 => (seconds / 86_400, "day"), |
| 546 | 2_592_000..=31_535_999 => (seconds / 2_592_000, "month"), |
| 547 | _ => (seconds / 31_536_000, "year"), |
| 548 | }; |
| 549 | |
| 550 | if count == 1 { |
| 551 | format!("1 {unit} ago") |
| 552 | } else { |
| 553 | format!("{count} {unit}s ago") |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | |
adac3a7feat: branches and tags get pages, so the counts stop being dead ends19h | 558 | pub(super) fn timestamp(time: SystemTime) -> String { |
dce0bf3feat: browse a repository's files and history8d | 559 | let seconds = time |
| 560 | .duration_since(UNIX_EPOCH) |
| 561 | .map(|since| since.as_secs() as i64) |
| 562 | .unwrap_or(0); |
| 563 | |
| 564 | let (year, month, day) = civil_from_days(seconds.div_euclid(86_400)); |
| 565 | let rest = seconds.rem_euclid(86_400); |
| 566 | |
| 567 | format!( |
| 568 | "{year:04}-{month:02}-{day:02} {:02}:{:02} UTC", |
| 569 | rest / 3600, |
| 570 | (rest % 3600) / 60 |
| 571 | ) |
| 572 | } |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | fn civil_from_days(days: i64) -> (i64, u32, u32) { |
| 579 | |
| 580 | let shifted = days + 719_468; |
| 581 | let era = shifted.div_euclid(146_097); |
| 582 | let day_of_era = shifted.rem_euclid(146_097); |
| 583 | |
| 584 | let year_of_era = |
| 585 | (day_of_era - day_of_era / 1460 + day_of_era / 36_524 - day_of_era / 146_096) / 365; |
| 586 | let year = year_of_era + era * 400; |
| 587 | let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100); |
| 588 | |
| 589 | let shifted_month = (5 * day_of_year + 2) / 153; |
| 590 | let day = (day_of_year - (153 * shifted_month + 2) / 5 + 1) as u32; |
| 591 | let month = if shifted_month < 10 { |
| 592 | shifted_month + 3 |
| 593 | } else { |
| 594 | shifted_month - 9 |
| 595 | } as u32; |
| 596 | |
| 597 | (if month <= 2 { year + 1 } else { year }, month, day) |
| 598 | } |
| 599 | |
| 600 | |
| 601 | |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 602 | |
dce0bf3feat: browse a repository's files and history8d | 603 | |
adac3a7feat: branches and tags get pages, so the counts stop being dead ends19h | 604 | |
| 605 | |
1b7587dfeat: finding a string in a repository, and landing on the line19h | 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
dce0bf3feat: browse a repository's files and history8d | 611 | #[component] |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 612 | pub(super) async fn repo_toolbar( |
| 613 | handle: &str, |
| 614 | name: &str, |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 615 | rev: &str, |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 616 | path: &RepoPath, |
| 617 | refs: &RefList, |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 618 | ) -> Result { |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 619 | let known = RefName::new(rev).is_ok_and(|at| refs.contains(&at)); |
| 620 | let switch = Switch::Tree(path); |
| 621 | let branches = ref_links(handle, name, &refs.branches, rev, &switch); |
| 622 | let tags = ref_links(handle, name, &refs.tags, rev, &switch); |
dce0bf3feat: browse a repository's files and history8d | 623 | |
| 624 | view! { |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 625 | <div class="mb-2 flex flex-wrap items-center gap-x-3 gap-y-2"> |
| 626 | rev_switcher(current: rev, known: known, branches: &branches, tags: &tags) |
| 627 | <span class="inline-flex items-center gap-1.5 text-xs text-muted-foreground"> |
| 628 | icon(data: iconify_icon!("feather:git-branch"), attrs: attributes! { |
| 629 | class="size-3.5" |
| 630 | }) |
adac3a7feat: branches and tags get pages, so the counts stop being dead ends19h | 631 | <a href=(branches_url(handle, name)) class="font-mono hover:text-foreground"> |
| 632 | (counted(refs.branches.len(), "branch", "branches")) |
| 633 | </a> |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 634 | "·" |
adac3a7feat: branches and tags get pages, so the counts stop being dead ends19h | 635 | <a href=(tags_url(handle, name)) class="font-mono hover:text-foreground"> |
| 636 | (counted(refs.tags.len(), "tag", "tags")) |
| 637 | </a> |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 638 | </span> |
1b7587dfeat: finding a string in a repository, and landing on the line19h | 639 | |
| 640 | <div class="w-full sm:ml-auto sm:w-auto"> |
| 641 | search_form( |
| 642 | handle: handle, |
| 643 | name: name, |
| 644 | rev: rev, |
| 645 | query: "", |
| 646 | full_width: false, |
| 647 | ) |
| 648 | </div> |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 649 | </div> |
dce0bf3feat: browse a repository's files and history8d | 650 | } |
| 651 | } |
| 652 | |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 653 | |
| 654 | fn counted(count: usize, one: &str, many: &str) -> String { |
| 655 | format!("{count} {}", if count == 1 { one } else { many }) |
| 656 | } |
| 657 | |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | #[component] |
8804ad9feat: blame can change revision, like every other page under Code18h | 668 | pub(super) async fn rev_switcher( |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 669 | current: &str, |
| 670 | known: bool, |
| 671 | branches: &[RefLink], |
| 672 | tags: &[RefLink], |
| 673 | ) -> Result { |
| 674 | let label = rev_label(current, known); |
| 675 | let empty = branches.is_empty() && tags.is_empty(); |
| 676 | |
| 677 | view! { |
| 678 | if empty { |
| 679 | |
| 680 | |
| 681 | if !current.is_empty() { |
| 682 | <span class="inline-flex items-center gap-1.5 font-mono text-xs text-muted-foreground"> |
| 683 | icon(data: iconify_icon!("feather:git-branch"), attrs: attributes! { |
| 684 | class="size-3.5" |
| 685 | }) |
| 686 | (label) |
| 687 | </span> |
| 688 | } |
| 689 | } else { |
| 690 | <details class="group relative inline-block"> |
| 691 | <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"> |
| 692 | icon( |
| 693 | data: if known { |
| 694 | iconify_icon!("feather:git-branch") |
| 695 | } else { |
| 696 | iconify_icon!("feather:git-commit") |
| 697 | }, |
| 698 | attrs: attributes! { class="size-3.5" }, |
| 699 | ) |
| 700 | (if label.is_empty() { "Revision" } else { label.as_str() }) |
| 701 | icon( |
| 702 | data: iconify_icon!("feather:chevron-down"), |
| 703 | attrs: attributes! { |
| 704 | class="size-3.5 transition-transform group-open:rotate-180" |
| 705 | }, |
| 706 | ) |
| 707 | </summary> |
| 708 | |
| 709 | <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"> |
| 710 | if !known && !current.is_empty() { |
| 711 | <p class="px-2 py-1.5 font-mono text-xs text-muted-foreground"> |
| 712 | "At commit " (label) |
| 713 | </p> |
| 714 | } |
| 715 | |
| 716 | ref_group(title: "Branches", links: branches) |
| 717 | ref_group(title: "Tags", links: tags) |
| 718 | </div> |
| 719 | </details> |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | #[component] |
| 730 | async fn ref_group(title: &str, links: &[RefLink]) -> Result { |
| 731 | view! { |
| 732 | if !links.is_empty() { |
| 733 | <p class="px-2 pt-1.5 pb-1 text-xs font-medium uppercase tracking-wider text-muted-foreground"> |
| 734 | (title) |
| 735 | </p> |
| 736 | <ul> |
| 737 | for link in links { |
| 738 | <li> |
| 739 | <a |
| 740 | href=(&link.href) |
| 741 | class=(format!( |
| 742 | "flex items-center gap-2 rounded-md px-2 py-1.5 font-mono text-sm hover:bg-foreground/5 {}", |
| 743 | if link.current { "font-medium" } else { "" }, |
| 744 | )) |
| 745 | > |
| 746 | <span class="truncate">(&link.name)</span> |
| 747 | if link.current { |
| 748 | <span class="ml-auto text-muted-foreground"> |
| 749 | icon( |
| 750 | data: iconify_icon!("feather:check"), |
| 751 | label: "Current", |
| 752 | attrs: attributes! { class="size-3.5" }, |
| 753 | ) |
| 754 | </span> |
| 755 | } |
| 756 | </a> |
| 757 | </li> |
| 758 | } |
| 759 | </ul> |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
dce0bf3feat: browse a repository's files and history8d | 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | #[component] |
| 769 | pub(super) async fn empty_repo(url: &str) -> Result { |
| 770 | let push = format!("git remote add origin {url}\ngit branch -M main\ngit push -u origin main"); |
| 771 | |
| 772 | view! { |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 773 | <div class="rounded-lg border border-border px-4 py-5"> |
dce0bf3feat: browse a repository's files and history8d | 774 | <p class="text-sm text-muted-foreground"> |
| 775 | "This repository has no commits yet. Push one to see it here." |
| 776 | </p> |
| 777 | <p class="mt-4 text-xs font-medium uppercase tracking-wider text-muted-foreground"> |
| 778 | "Push an existing repository" |
| 779 | </p> |
0aca94efeat: a repository is one place, and its landing page says what it is20h | 780 | <pre class="mt-2 overflow-x-auto rounded-lg border border-border bg-surface px-4 py-3 font-mono text-sm">(push)</pre> |
dce0bf3feat: browse a repository's files and history8d | 781 | </div> |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | #[component] |
f42a185feat: a file read by who last changed each line, and a way between the two views19h | 790 | pub(super) async fn crumbs(handle: &str, name: &str, rev: &RefName, path: &RepoPath) -> Result { |
dce0bf3feat: browse a repository's files and history8d | 791 | let parts: Vec<&str> = path.components().collect(); |
| 792 | let mut walked = RepoPath::root(); |
| 793 | let mut trail: Vec<(String, String)> = Vec::new(); |
| 794 | |
| 795 | for (index, part) in parts.iter().enumerate() { |
| 796 | walked = walked.join(part); |
| 797 | |
| 798 | let href = if index + 1 == parts.len() { |
| 799 | String::new() |
| 800 | } else { |
| 801 | tree_url(handle, name, rev, &walked) |
| 802 | }; |
| 803 | |
| 804 | trail.push(((*part).to_owned(), href)); |
| 805 | } |
| 806 | |
| 807 | view! { |
| 808 | <div class="flex flex-wrap items-center gap-1 font-mono text-sm"> |
| 809 | <a |
| 810 | href=(tree_url(handle, name, rev, &RepoPath::root())) |
| 811 | class="text-muted-foreground hover:text-foreground" |
| 812 | >(name)</a> |
| 813 | |
| 814 | for (part, href) in &trail { |
| 815 | <span class="text-muted-foreground">"/"</span> |
| 816 | match href.is_empty() { |
| 817 | true => <span class="font-medium">(part)</span>, |
| 818 | false => <a href=(href) class="text-muted-foreground hover:text-foreground">(part)</a>, |
| 819 | } |
| 820 | } |
| 821 | </div> |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | /// A directory listing. |
| 826 | /// |
| 827 | /// Entries arrive ordered by the use case — directories first, then case-insensitively |
| 828 | /// by name — so nothing here re-sorts them. |
| 829 | #[component] |
| 830 | pub(super) async fn directory( |
| 831 | handle: &str, |
| 832 | name: &str, |
| 833 | rev: &RefName, |
| 834 | path: &RepoPath, |
| 835 | entries: &[TreeEntry], |
| 836 | ) -> Result { |
| 837 | view! { |
| 838 | <div class="overflow-hidden rounded-lg border border-border"> |
| 839 | <div class="border-b border-border px-4 py-2.5"> |
| 840 | crumbs(handle: handle, name: name, rev: rev, path: path) |
| 841 | </div> |
| 842 | |
| 843 | if entries.is_empty() { |
| 844 | <p class="px-4 py-6 text-center text-sm text-muted-foreground"> |
| 845 | "This directory is empty." |
| 846 | </p> |
| 847 | } else { |
| 848 | <ul class="divide-y divide-border text-sm"> |
| 849 | match path.parent() { |
| 850 | Some(parent) => <li class="px-4 py-2"> |
| 851 | <a |
| 852 | href=(tree_url(handle, name, rev, &parent)) |
| 853 | class="inline-flex items-center gap-2 font-mono text-muted-foreground hover:text-foreground" |
| 854 | > |
| 855 | icon(data: iconify_icon!("feather:corner-left-up"), attrs: attributes! { |
| 856 | class="size-4" |
| 857 | }) |
| 858 | ".." |
| 859 | </a> |
| 860 | </li>, |
| 861 | None => "", |
| 862 | } |
| 863 | |
| 864 | for entry in entries { |
| 865 | <li class="flex items-center gap-3 px-4 py-2"> |
| 866 | entry_row( |
| 867 | handle: handle, |
| 868 | name: name, |
| 869 | rev: rev, |
| 870 | path: path, |
| 871 | entry: entry, |
| 872 | ) |
| 873 | </li> |
| 874 | } |
| 875 | </ul> |
| 876 | } |
| 877 | </div> |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | /// One entry in a listing. |
| 882 | /// |
| 883 | /// A symlink and a submodule are their own kinds, not files: a submodule is another |
| 884 | /// repository Steid cannot look inside, so it is labelled and left unlinked rather |
| 885 | /// than offered as a click that would 404. |
| 886 | #[component] |
| 887 | async fn entry_row( |
| 888 | handle: &str, |
| 889 | name: &str, |
| 890 | rev: &RefName, |
| 891 | path: &RepoPath, |
| 892 | entry: &TreeEntry, |
| 893 | ) -> Result { |
| 894 | let href = tree_url(handle, name, rev, &path.join(&entry.name)); |
| 895 | let linkable = entry.kind != EntryKind::Submodule; |
| 896 | |
| 897 | view! { |
| 898 | <span class="text-muted-foreground"> |
| 899 | match entry.kind { |
| 900 | EntryKind::Tree => icon( |
| 901 | data: iconify_icon!("feather:folder"), |
| 902 | label: "Directory", |
| 903 | attrs: attributes! { class="size-4" }, |
| 904 | ), |
| 905 | EntryKind::Blob => icon( |
| 906 | data: iconify_icon!("feather:file"), |
| 907 | label: "File", |
| 908 | attrs: attributes! { class="size-4" }, |
| 909 | ), |
| 910 | EntryKind::Symlink => icon( |
| 911 | data: iconify_icon!("feather:link-2"), |
| 912 | label: "Symlink", |
| 913 | attrs: attributes! { class="size-4" }, |
| 914 | ), |
| 915 | EntryKind::Submodule => icon( |
| 916 | data: iconify_icon!("feather:package"), |
| 917 | label: "Submodule", |
| 918 | attrs: attributes! { class="size-4" }, |
| 919 | ), |
| 920 | } |
| 921 | </span> |
| 922 | |
| 923 | match linkable { |
| 924 | true => <a |
| 925 | href=(href) |
| 926 | class=(if entry.kind.is_tree() { |
| 927 | "font-mono font-medium hover:underline" |
| 928 | } else { |
| 929 | "font-mono hover:underline" |
| 930 | }) |
| 931 | >(&entry.name)</a>, |
| 932 | false => <span class="font-mono">(&entry.name)</span>, |
| 933 | } |
| 934 | |
| 935 | match entry.kind { |
| 936 | EntryKind::Symlink => badge(variant: BadgeVariant::Outline, "symlink"), |
| 937 | EntryKind::Submodule => badge(variant: BadgeVariant::Outline, "submodule"), |
| 938 | _ => "", |
| 939 | } |
| 940 | |
| 941 | <span class="ml-auto font-mono text-xs text-muted-foreground"> |
| 942 | match entry.size { |
| 943 | Some(size) => (size_of(size)), |
| 944 | None if entry.kind == EntryKind::Submodule => (entry.id.short()), |
| 945 | None => "", |
| 946 | } |
| 947 | </span> |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | /// A single file. |
| 952 | /// |
| 953 | /// Three outcomes, all of them a page rather than an error: text, something that is not |
| 954 | /// text, and something too big to be worth rendering. The last says how big, because |
| 955 | /// that is the only useful thing left to say about it. |
| 956 | #[component] |
| 957 | pub(super) async fn blob( |
| 958 | handle: &str, |
| 959 | name: &str, |
| 960 | rev: &RefName, |
| 961 | path: &RepoPath, |
| 962 | file: &FileView, |
| 963 | ) -> Result { |
| 964 | view! { |
| 965 | <div class="overflow-hidden rounded-lg border border-border"> |
| 966 | <div class="flex flex-wrap items-center justify-between gap-3 border-b border-border px-4 py-2.5"> |
| 967 | crumbs(handle: handle, name: name, rev: rev, path: path) |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 968 | <span class="flex items-center gap-3 font-mono text-xs text-muted-foreground"> |
| 969 | (size_of(file.size)) |
f42a185feat: a file read by who last changed each line, and a way between the two views19h | 970 | view_toggle( |
| 971 | handle: handle, |
| 972 | name: name, |
| 973 | rev: rev, |
| 974 | path: path, |
| 975 | active: FileTab::Code, |
| 976 | ) |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 977 | </span> |
dce0bf3feat: browse a repository's files and history8d | 978 | </div> |
| 979 | |
| 980 | match &file.text { |
979134bfeat: code reads as code, in classes rather than colours19h | 981 | Some(text) => source( |
| 982 | text: text.as_str(), |
| 983 | file_name: path.file_name().unwrap_or_default(), |
| 984 | ), |
dce0bf3feat: browse a repository's files and history8d | 985 | None if file.too_large => <p class="px-4 py-6 text-center text-sm text-muted-foreground"> |
| 986 | "This file is " (size_of(file.size)) ", which is too large to display. Clone the repository to read it." |
| 987 | </p>, |
| 988 | None => <p class="px-4 py-6 text-center text-sm text-muted-foreground"> |
| 989 | "This file cannot be displayed as text." |
| 990 | </p>, |
| 991 | } |
| 992 | </div> |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | /// A file's contents, with line numbers. |
| 997 | /// |
| 998 | /// A table rather than a `<pre>` with a gutter: the numbers stay put when the code |
| 999 | /// scrolls sideways, and selecting the code does not drag the numbers along with it. |
979134bfeat: code reads as code, in classes rather than colours19h | 1000 | /// |
| 1001 | /// Highlighting only ever changes what is *inside* the code cell — the rows, the |
| 1002 | /// numbers and the scroll container are the same whether a language was recognised or |
| 1003 | /// not, so nothing else on the page has to know. |
1b7587dfeat: finding a string in a repository, and landing on the line19h | 1004 | /// |
| 1005 | /// |
| 1006 | /// Each number cell carries an `id="L{n}"`, which is what a search result links to: a |
| 1007 | /// match on line 400 of a long file should land on line 400. |
dce0bf3feat: browse a repository's files and history8d | 1008 | #[component] |
979134bfeat: code reads as code, in classes rather than colours19h | 1009 | async fn source(text: &str, file_name: &str) -> Result { |
| 1010 | let Source { lines, too_large } = source_lines(file_name, text); |
| 1011 | |
dce0bf3feat: browse a repository's files and history8d | 1012 | view! { |
979134bfeat: code reads as code, in classes rather than colours19h | 1013 | if too_large { |
| 1014 | <p class="border-b border-border px-4 py-1.5 font-mono text-xs text-muted-foreground"> |
| 1015 | "Too large to highlight — shown as plain text." |
| 1016 | </p> |
| 1017 | } |
| 1018 | |
dce0bf3feat: browse a repository's files and history8d | 1019 | <div class="overflow-x-auto"> |
| 1020 | <table class="w-full border-collapse font-mono text-xs leading-relaxed"> |
| 1021 | <tbody> |
979134bfeat: code reads as code, in classes rather than colours19h | 1022 | for (index, line) in lines.into_iter().enumerate() { |
dce0bf3feat: browse a repository's files and history8d | 1023 | <tr> |
1b7587dfeat: finding a string in a repository, and landing on the line19h | 1024 | <td |
| 1025 | id=(format!("L{}", index + 1)) |
| 1026 | class="w-px select-none border-r border-border px-3 text-right align-top text-muted-foreground" |
| 1027 | > |
dce0bf3feat: browse a repository's files and history8d | 1028 | ((index + 1).to_string()) |
| 1029 | </td> |
| 1030 | <td class="whitespace-pre px-4 align-top"> |
979134bfeat: code reads as code, in classes rather than colours19h | 1031 | match line { |
| 1032 | // Markup this codebase wrote: `highlight` escapes |
| 1033 | // the source itself and emits nothing but spans. |
| 1034 | SourceLine::Classed(html) => (Unescaped::new_unchecked(html)), |
| 1035 | SourceLine::Plain(text) => (text), |
| 1036 | } |
dce0bf3feat: browse a repository's files and history8d | 1037 | </td> |
| 1038 | </tr> |
| 1039 | } |
| 1040 | </tbody> |
| 1041 | </table> |
| 1042 | </div> |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | /// The commit log — the most recent commits, newest first, and no paging in v1. |
2268309feat: a commit is a page, and two revisions can be compared19h | 1047 | /// |
| 1048 | /// Shared with the compare page, which lists the commits a branch adds in exactly this |
| 1049 | /// shape: two lists of commits that read differently would be two designs for one |
| 1050 | /// thing. |
dce0bf3feat: browse a repository's files and history8d | 1051 | #[component] |
2268309feat: a commit is a page, and two revisions can be compared19h | 1052 | pub(super) async fn commit_log(handle: &str, name: &str, commits: &[CommitSummary]) -> Result { |
dce0bf3feat: browse a repository's files and history8d | 1053 | view! { |
| 1054 | if commits.is_empty() { |
| 1055 | <p class="rounded-lg border border-border px-4 py-10 text-center text-sm text-muted-foreground"> |
| 1056 | "No commits yet." |
| 1057 | </p> |
| 1058 | } else { |
| 1059 | <ul class="divide-y divide-border rounded-lg border border-border"> |
| 1060 | for commit in commits { |
| 1061 | <li class="px-4 py-3"> |
| 1062 | <div class="flex items-baseline justify-between gap-4"> |
| 1063 | <p class="text-sm font-medium">(&commit.summary)</p> |
2268309feat: a commit is a page, and two revisions can be compared19h | 1064 | <a |
| 1065 | href=(commit_url(handle, name, commit.id.as_str())) |
| 1066 | class="shrink-0 font-mono text-xs text-muted-foreground hover:text-foreground" |
| 1067 | >(commit.id.short())</a> |
dce0bf3feat: browse a repository's files and history8d | 1068 | </div> |
| 1069 | <p class="mt-1 text-xs text-muted-foreground"> |
| 1070 | (&commit.author_name) |
| 1071 | " committed " |
| 1072 | <span title=(timestamp(commit.committed_at))>(ago(commit.committed_at))</span> |
| 1073 | </p> |
| 1074 | </li> |
| 1075 | } |
| 1076 | </ul> |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | #[cfg(test)] |
| 1082 | mod tests { |
| 1083 | use std::time::Duration; |
| 1084 | |
| 1085 | use super::*; |
| 1086 | |
| 1087 | fn rev(value: &str) -> RefName { |
| 1088 | RefName::new(value).expect("valid revision") |
| 1089 | } |
| 1090 | |
| 1091 | #[test] |
| 1092 | fn a_root_tree_url_has_no_separator() { |
| 1093 | assert_eq!( |
| 1094 | tree_url("ada", "steid", &rev("main"), &RepoPath::root()), |
| 1095 | "/ada/repos/steid/tree/main" |
| 1096 | ); |
| 1097 | } |
| 1098 | |
| 1099 | #[test] |
| 1100 | fn a_path_follows_the_separator_with_its_slashes_intact() { |
| 1101 | let path = RepoPath::new("src/domain/repo.rs").expect("valid"); |
| 1102 | |
| 1103 | assert_eq!( |
| 1104 | tree_url("ada", "steid", &rev("main"), &path), |
| 1105 | "/ada/repos/steid/tree/main/-/src/domain/repo.rs" |
| 1106 | ); |
| 1107 | } |
| 1108 | |
| 1109 | #[test] |
| 1110 | fn a_revisions_slashes_are_encoded_so_it_stays_one_segment() { |
| 1111 | // Otherwise `feature/login` would look like a revision plus a path, which is |
| 1112 | // the ambiguity the separator exists to remove. |
| 1113 | assert_eq!( |
| 1114 | tree_url("ada", "steid", &rev("feature/login"), &RepoPath::root()), |
| 1115 | "/ada/repos/steid/tree/feature%2Flogin" |
| 1116 | ); |
| 1117 | } |
| 1118 | |
| 1119 | #[test] |
| 1120 | fn names_needing_escaping_are_encoded() { |
| 1121 | let path = RepoPath::new("docs/a b#c.md").expect("valid"); |
| 1122 | |
| 1123 | assert_eq!( |
| 1124 | tree_url("ada", "steid", &rev("main"), &path), |
| 1125 | "/ada/repos/steid/tree/main/-/docs/a%20b%23c.md" |
| 1126 | ); |
| 1127 | } |
| 1128 | |
| 1129 | #[test] |
| 1130 | fn the_log_url_is_the_default_branch_when_no_revision_is_named() { |
| 1131 | assert_eq!(log_url("ada", "steid", ""), "/ada/repos/steid/log"); |
| 1132 | assert_eq!( |
| 1133 | log_url("ada", "steid", "feature/login"), |
| 1134 | "/ada/repos/steid/log/feature%2Flogin" |
| 1135 | ); |
| 1136 | } |
| 1137 | |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 1138 | #[test] |
| 1139 | fn a_raw_url_always_carries_a_path() { |
| 1140 | let path = RepoPath::new("src/main.rs").expect("valid"); |
| 1141 | |
| 1142 | assert_eq!( |
| 1143 | raw_url("ada", "steid", &rev("main"), &path), |
| 1144 | "/ada/repos/steid/raw/main/-/src/main.rs" |
| 1145 | ); |
| 1146 | assert_eq!( |
| 1147 | raw_url("ada", "steid", &rev("feature/login"), &path), |
| 1148 | "/ada/repos/steid/raw/feature%2Flogin/-/src/main.rs" |
| 1149 | ); |
| 1150 | } |
| 1151 | |
| 1152 | #[test] |
| 1153 | fn a_raw_response_carries_the_whole_policy() { |
| 1154 | let response = raw_response("notes.txt", b"hello".to_vec()).expect("should build"); |
| 1155 | let header = |name: &str| { |
| 1156 | response |
| 1157 | .headers() |
| 1158 | .get(name) |
| 1159 | .and_then(|value| value.to_str().ok()) |
| 1160 | .unwrap_or_default() |
| 1161 | .to_owned() |
| 1162 | }; |
| 1163 | |
| 1164 | // Each of these is load-bearing on its own; see `raw_response`. |
| 1165 | assert_eq!(header("content-type"), "application/octet-stream"); |
| 1166 | assert_eq!(header("x-content-type-options"), "nosniff"); |
| 1167 | assert!(header("content-disposition").starts_with("attachment;")); |
| 1168 | assert_eq!( |
| 1169 | header("content-security-policy"), |
| 1170 | "default-src \'none\'; sandbox" |
| 1171 | ); |
| 1172 | } |
| 1173 | |
| 1174 | #[test] |
| 1175 | fn a_disposition_carries_both_spellings_of_the_name() { |
| 1176 | assert_eq!( |
| 1177 | disposition("notes.txt"), |
| 1178 | "attachment; filename=\"notes.txt\"; filename*=UTF-8\'\'notes.txt" |
| 1179 | ); |
| 1180 | } |
| 1181 | |
| 1182 | #[test] |
| 1183 | fn a_disposition_cannot_be_escaped_by_a_filename() { |
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | let hostile = disposition("a\"; x=1\r\nSet-Cookie: nope=1"); |
| 1188 | |
| 1189 | assert!(!hostile.contains('\r')); |
| 1190 | assert!(!hostile.contains('\n')); |
| 1191 | assert_eq!(hostile.matches('"').count(), 2); |
| 1192 | } |
| 1193 | |
| 1194 | #[test] |
| 1195 | fn a_nameless_file_still_downloads_as_something() { |
| 1196 | assert!(disposition("...").starts_with("attachment; filename=\"file\"")); |
| 1197 | } |
| 1198 | |
| 1199 | #[test] |
| 1200 | fn a_non_ascii_name_survives_in_the_extended_form() { |
| 1201 | let value = disposition("日本語.txt"); |
| 1202 | |
| 1203 | |
| 1204 | |
| 1205 | assert!(value.contains("filename=\"___.txt\"")); |
| 1206 | assert!(value.contains("filename*=UTF-8\'\'%E6%97%A5%E6%9C%AC%E8%AA%9E.txt")); |
| 1207 | } |
| 1208 | |
| 1209 | #[test] |
| 1210 | fn the_switcher_marks_the_revision_it_is_on() { |
| 1211 | let refs = [RefName::from_trusted("main"), RefName::from_trusted("next")]; |
| 1212 | let path = RepoPath::new("src").expect("valid"); |
| 1213 | let links = ref_links("ada", "steid", &refs, "next", &Switch::Tree(&path)); |
| 1214 | |
| 1215 | assert_eq!(links[0].href, "/ada/repos/steid/tree/main/-/src"); |
| 1216 | assert!(!links[0].current); |
| 1217 | assert!(links[1].current); |
| 1218 | } |
| 1219 | |
| 1220 | #[test] |
| 1221 | fn switching_from_the_log_stays_on_the_log() { |
| 1222 | let refs = [RefName::from_trusted("v1.0")]; |
| 1223 | let links = ref_links("ada", "steid", &refs, "main", &Switch::Log); |
| 1224 | |
| 1225 | assert_eq!(links[0].href, "/ada/repos/steid/log/v1.0"); |
| 1226 | } |
| 1227 | |
| 1228 | #[test] |
| 1229 | fn an_object_id_is_labelled_as_a_commit_rather_than_a_branch() { |
| 1230 | let id = "0123456789abcdef0123456789abcdef01234567"; |
| 1231 | |
| 1232 | assert!(is_object_id(id)); |
| 1233 | assert_eq!(rev_label(id, false), "0123456"); |
| 1234 | |
| 1235 | assert_eq!(rev_label("deadbeef", true), "deadbeef"); |
| 1236 | assert_eq!(rev_label("main", false), "main"); |
| 1237 | } |
| 1238 | |
dce0bf3feat: browse a repository's files and history8d | 1239 | #[test] |
| 1240 | fn sizes_read_as_sizes() { |
| 1241 | assert_eq!(size_of(0), "0 B"); |
| 1242 | assert_eq!(size_of(999), "999 B"); |
| 1243 | assert_eq!(size_of(1024), "1.0 KB"); |
| 1244 | assert_eq!(size_of(1_048_576), "1.0 MB"); |
| 1245 | assert_eq!(size_of(1_572_864), "1.5 MB"); |
| 1246 | } |
| 1247 | |
| 1248 | #[test] |
| 1249 | fn elapsed_time_reads_as_words() { |
| 1250 | let now = SystemTime::now(); |
| 1251 | let since = |seconds| ago(now - Duration::from_secs(seconds)); |
| 1252 | |
| 1253 | assert_eq!(since(5), "just now"); |
| 1254 | assert_eq!(since(60), "1 minute ago"); |
| 1255 | assert_eq!(since(7200), "2 hours ago"); |
| 1256 | assert_eq!(since(86_400 * 3), "3 days ago"); |
| 1257 | assert_eq!(since(86_400 * 400), "1 year ago"); |
| 1258 | } |
| 1259 | |
| 1260 | #[test] |
| 1261 | fn a_commit_from_the_future_reads_as_now_rather_than_as_a_negative() { |
| 1262 | |
| 1263 | assert_eq!( |
| 1264 | ago(SystemTime::now() + Duration::from_secs(3600)), |
| 1265 | "just now" |
| 1266 | ); |
| 1267 | } |
| 1268 | |
| 1269 | #[test] |
| 1270 | fn timestamps_are_utc_calendar_dates() { |
| 1271 | assert_eq!( |
| 1272 | timestamp(UNIX_EPOCH + Duration::from_secs(0)), |
| 1273 | "1970-01-01 00:00 UTC" |
| 1274 | ); |
| 1275 | |
| 1276 | assert_eq!( |
| 1277 | timestamp(UNIX_EPOCH + Duration::from_secs(1_788_006_840)), |
| 1278 | "2026-08-29 12:34 UTC" |
| 1279 | ); |
| 1280 | |
| 1281 | assert_eq!( |
| 1282 | timestamp(UNIX_EPOCH + Duration::from_secs(1_709_164_800)), |
| 1283 | "2024-02-29 00:00 UTC" |
| 1284 | ); |
| 1285 | } |
| 1286 | } |