| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | use std::time::SystemTime; |
| 13 | |
| 14 | use topcoat::{ |
| 15 | Result, |
| 16 | context::Cx, |
| 17 | icon::{icon, iconify::iconify_icon}, |
| 18 | router::{error::not_found, page, path_param}, |
| 19 | view::{Unescaped, attributes, component, view}, |
| 20 | }; |
| 21 | |
| 22 | use crate::{ |
| 23 | application::{Blame, BlameContent, BlameFile, BlameGroup, blame::AGE_STEPS, blame_file}, |
| 24 | domain::{ObjectId, RefName, RepoPath}, |
| 25 | infrastructure::highlight::{Source, SourceLine, content_digest}, |
| 26 | }; |
| 27 | |
| 28 | use super::{ |
| 29 | browse::{ |
| 30 | Switch, ago, crumbs, encode, is_markdown, path_log_url, raw_url, ref_links, refs_for, |
| 31 | rev_switcher, size_of, tree_url, |
| 32 | }, |
| 33 | context::{ |
| 34 | current_actor, highlights, memberships, orgs, queries, repos, server_error, timed_out, |
| 35 | }, |
| 36 | layout::wide, |
| 37 | repo::{Tab, repo_for, repo_header}, |
| 38 | }; |
| 39 | |
| 40 | |
| 41 | #[path_param] |
| 42 | struct Rev(str); |
| 43 | |
| 44 | |
| 45 | #[path_param] |
| 46 | struct Path(str); |
| 47 | |
| 48 | #[page("/{handle}/repos/{name}/blame/{rev}/-/{*path}")] |
| 49 | async fn blame_page(cx: &Cx) -> Result { |
| 50 | |
| 51 | |
| 52 | let rev = RefName::new(path_param::<Rev>(cx)).map_err(|_| not_found())?; |
| 53 | let path = RepoPath::new(path_param::<Path>(cx)).map_err(|_| not_found())?; |
| 54 | |
| 55 | view! { blaming(rev: rev, path: path) } |
| 56 | } |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | enum Body<'a> { |
| 64 | |
| 65 | TimedOut, |
| 66 | Ready(&'a Blame), |
| 67 | |
| 68 | Empty, |
| 69 | Binary, |
| 70 | TooLarge(u64), |
| 71 | } |
| 72 | |
| 73 | |
| 74 | |
| 75 | #[component] |
| 76 | async fn blaming(cx: &Cx, rev: RefName, path: RepoPath) -> Result { |
| 77 | let repo = repo_for(cx).await?; |
| 78 | |
| 79 | let outcome = blame_file( |
| 80 | &repo.handle, |
| 81 | &repo.name, |
| 82 | &rev, |
| 83 | &path, |
| 84 | ¤t_actor(cx).await?, |
| 85 | &orgs(cx), |
| 86 | &memberships(cx), |
| 87 | &repos(cx), |
| 88 | &queries(cx), |
| 89 | ) |
| 90 | .await; |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | let file: Option<BlameFile> = match outcome { |
| 96 | Ok(Some(file)) => Some(file), |
| 97 | Ok(None) => return Err(not_found().into()), |
| 98 | Err(error) if timed_out(&error) => None, |
| 99 | Err(other) => return Err(server_error(std::io::Error::other(other.to_string()))), |
| 100 | }; |
| 101 | |
| 102 | let body = match &file { |
| 103 | None => Body::TimedOut, |
| 104 | Some(file) => match &file.content { |
| 105 | BlameContent::Binary => Body::Binary, |
| 106 | BlameContent::TooLarge => Body::TooLarge(file.size), |
| 107 | BlameContent::Ready(blame) if blame.is_empty() => Body::Empty, |
| 108 | BlameContent::Ready(blame) => Body::Ready(blame), |
| 109 | }, |
| 110 | }; |
| 111 | |
| 112 | let handle = repo.handle.as_str(); |
| 113 | let name = repo.name.as_str(); |
| 114 | let blob = tree_url(handle, name, &rev, &path); |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | let refs = refs_for(cx, &repo).await?; |
| 121 | let at = rev.as_str(); |
| 122 | let known = refs.contains(&rev); |
| 123 | let switch = Switch::Blame(&path); |
| 124 | let branches = ref_links(handle, name, &refs.branches, at, &switch); |
| 125 | let tags = ref_links(handle, name, &refs.tags, at, &switch); |
| 126 | |
| 127 | |
| 128 | |
| 129 | let oversized = match &body { |
| 130 | Body::TooLarge(size) => format!( |
| 131 | "This file is {}, which is too large to blame. Clone the repository to read it.", |
| 132 | size_of(*size), |
| 133 | ), |
| 134 | _ => String::new(), |
| 135 | }; |
| 136 | |
| 137 | view! { |
| 138 | wide( |
| 139 | repo_header( |
| 140 | repo: &repo, |
| 141 | rev: at, |
| 142 | active: Tab::Code, |
| 143 | rev_switcher(current: at, known: known, branches: &branches, tags: &tags) |
| 144 | ) |
| 145 | |
| 146 | <div class="overflow-hidden rounded-lg border border-border"> |
| 147 | <div class="flex flex-wrap items-center justify-between gap-3 border-b border-border px-4 py-2.5"> |
| 148 | crumbs(handle: handle, name: name, rev: &rev, path: &path) |
| 149 | <span class="flex items-center gap-3 font-mono text-xs text-muted-foreground"> |
| 150 | match &file { |
| 151 | |
| 152 | |
| 153 | Some(file) => (size_of(file.size)), |
| 154 | None => "", |
| 155 | } |
| 156 | view_toggle( |
| 157 | handle: handle, |
| 158 | name: name, |
| 159 | rev: &rev, |
| 160 | path: &path, |
| 161 | active: FileTab::Blame, |
| 162 | markdown: is_markdown(&path), |
| 163 | ) |
| 164 | </span> |
| 165 | </div> |
| 166 | |
| 167 | match body { |
| 168 | Body::Ready(blame) => blame_rows( |
| 169 | handle: handle, |
| 170 | name: name, |
| 171 | path: &path, |
| 172 | blob: blob.as_str(), |
| 173 | blame: blame, |
| 174 | ), |
| 175 | Body::Empty => note(message: "This file is empty."), |
| 176 | Body::Binary => note(message: "This file cannot be blamed as text."), |
| 177 | Body::TooLarge(_) => note(message: oversized.as_str()), |
| 178 | Body::TimedOut => took_too_long(blob: blob.as_str()), |
| 179 | } |
| 180 | </div> |
| 181 | ) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | |
| 186 | #[component] |
| 187 | async fn note(message: &str) -> Result { |
| 188 | view! { |
| 189 | <p class="px-4 py-6 text-center text-sm text-muted-foreground">(message)</p> |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | #[component] |
| 198 | async fn took_too_long(blob: &str) -> Result { |
| 199 | view! { |
| 200 | <div class="px-4 py-6 text-center"> |
| 201 | <p class="text-sm text-muted-foreground"> |
| 202 | "Blame took too long for this file." |
| 203 | </p> |
| 204 | <p class="mt-1 text-xs text-muted-foreground"> |
| 205 | "Its history is long enough that walking it ran out of time. " |
| 206 | <a href=(blob) class="text-primary hover:underline">"Read the file instead"</a> |
| 207 | "." |
| 208 | </p> |
| 209 | </div> |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | |
| 214 | struct Row<'a> { |
| 215 | group: &'a BlameGroup, |
| 216 | |
| 217 | starts_run: bool, |
| 218 | |
| 219 | ruled: bool, |
| 220 | |
| 221 | number: usize, |
| 222 | code: SourceLine, |
| 223 | } |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | fn line_index(group: &BlameGroup, offset: usize) -> usize { |
| 230 | (group.start_line + offset).saturating_sub(1) |
| 231 | } |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | #[component] |
| 248 | async fn blame_rows( |
| 249 | cx: &Cx, |
| 250 | handle: &str, |
| 251 | name: &str, |
| 252 | path: &RepoPath, |
| 253 | blob: &str, |
| 254 | blame: &Blame, |
| 255 | ) -> Result { |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | let mut ordered = vec![""; blame.line_count()]; |
| 262 | for group in &blame.groups { |
| 263 | for (offset, line) in group.lines.iter().enumerate() { |
| 264 | if let Some(slot) = ordered.get_mut(line_index(group, offset)) { |
| 265 | *slot = line.as_str(); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | let text = ordered.join("\n"); |
| 276 | let Source { lines, too_large } = highlights(cx) |
| 277 | .source_lines( |
| 278 | &content_digest(&text), |
| 279 | path.file_name().unwrap_or_default(), |
| 280 | &text, |
| 281 | ) |
| 282 | .as_ref() |
| 283 | .clone(); |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | let mut rows: Vec<Row> = Vec::new(); |
| 289 | let mut code = lines.into_iter(); |
| 290 | for (index, group) in blame.groups.iter().enumerate() { |
| 291 | for offset in 0..group.lines.len() { |
| 292 | rows.push(Row { |
| 293 | group, |
| 294 | |
| 295 | |
| 296 | |
| 297 | starts_run: offset == 0, |
| 298 | ruled: offset == 0 && index > 0, |
| 299 | number: line_index(group, offset) + 1, |
| 300 | |
| 301 | code: code |
| 302 | .next() |
| 303 | .unwrap_or_else(|| SourceLine::Plain(" ".to_owned())), |
| 304 | }); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | view! { |
| 309 | if too_large { |
| 310 | <p class="border-b border-border px-4 py-1.5 font-mono text-xs text-muted-foreground"> |
| 311 | "Too large to highlight — shown as plain text." |
| 312 | </p> |
| 313 | } |
| 314 | |
| 315 | <div class="overflow-x-auto"> |
| 316 | <table class="w-full border-collapse font-mono text-xs leading-relaxed"> |
| 317 | <tbody> |
| 318 | for row in rows { |
| 319 | <tr class=(if row.ruled { "border-t border-border" } else { "" })> |
| 320 | |
| 321 | |
| 322 | |
| 323 | <td class=(format!( |
| 324 | "w-px border-r border-border pl-2 pr-3 align-top blame-age blame-age-{}", |
| 325 | row.group.age.min(AGE_STEPS - 1), |
| 326 | ))> |
| 327 | if row.starts_run { |
| 328 | commit_cell( |
| 329 | handle: handle, |
| 330 | name: name, |
| 331 | path: path, |
| 332 | group: row.group, |
| 333 | ) |
| 334 | } |
| 335 | </td> |
| 336 | <td class="w-px select-none border-r border-border px-3 text-right align-top text-muted-foreground"> |
| 337 | <a |
| 338 | href=(format!("{blob}#L{}", row.number)) |
| 339 | class="hover:text-foreground" |
| 340 | >((row.number).to_string())</a> |
| 341 | </td> |
| 342 | <td class="whitespace-pre px-4 align-top"> |
| 343 | match row.code { |
| 344 | |
| 345 | |
| 346 | SourceLine::Classed(html) => (Unescaped::new_unchecked(html)), |
| 347 | SourceLine::Plain(text) => (text), |
| 348 | } |
| 349 | </td> |
| 350 | </tr> |
| 351 | } |
| 352 | </tbody> |
| 353 | </table> |
| 354 | </div> |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | #[component] |
| 367 | async fn commit_cell(handle: &str, name: &str, path: &RepoPath, group: &BlameGroup) -> Result { |
| 368 | let commit = &group.commit; |
| 369 | |
| 370 | |
| 371 | let moved = !commit.filename.is_empty() && commit.filename != path.as_str(); |
| 372 | |
| 373 | let mut about = format!( |
| 374 | "{} · {} · {}", |
| 375 | commit.author_name, |
| 376 | ago(commit.authored_at), |
| 377 | commit.summary, |
| 378 | ); |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | if commit.boundary { |
| 384 | about.push_str(" · the oldest commit blame reached"); |
| 385 | } |
| 386 | |
| 387 | view! { |
| 388 | <div class="flex w-56 items-baseline gap-2 overflow-hidden lg:w-80"> |
| 389 | <a |
| 390 | href=(commit_url(handle, name, &commit.id)) |
| 391 | title=(about.as_str()) |
| 392 | class="shrink-0 text-muted-foreground hover:text-foreground" |
| 393 | >(commit.id.short())</a> |
| 394 | |
| 395 | if moved { |
| 396 | <span |
| 397 | class="shrink-0 self-center text-muted-foreground" |
| 398 | title=(format!("Moved from {}", commit.filename)) |
| 399 | > |
| 400 | icon( |
| 401 | data: iconify_icon!("feather:corner-down-right"), |
| 402 | label: "Moved from another file", |
| 403 | attrs: attributes! { class="size-3" }, |
| 404 | ) |
| 405 | </span> |
| 406 | } |
| 407 | |
| 408 | <span class="min-w-0 flex-1 truncate text-muted-foreground" title=(about.as_str())> |
| 409 | (&commit.summary) |
| 410 | </span> |
| 411 | <span class="shrink-0 text-muted-foreground">(short_ago(commit.authored_at))</span> |
| 412 | </div> |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 423 | pub(super) enum FileTab { |
| 424 | |
| 425 | |
| 426 | Code, |
| 427 | |
| 428 | |
| 429 | Source, |
| 430 | Blame, |
| 431 | |
| 432 | |
| 433 | |
| 434 | History, |
| 435 | } |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | #[component] |
| 447 | pub(super) async fn view_toggle( |
| 448 | handle: &str, |
| 449 | name: &str, |
| 450 | rev: &RefName, |
| 451 | path: &RepoPath, |
| 452 | active: FileTab, |
| 453 | |
| 454 | markdown: bool, |
| 455 | ) -> Result { |
| 456 | let item = |current| { |
| 457 | if current { |
| 458 | "text-primary" |
| 459 | } else { |
| 460 | "text-muted-foreground hover:text-foreground" |
| 461 | } |
| 462 | }; |
| 463 | |
| 464 | view! { |
| 465 | <span class="flex items-center gap-1.5"> |
| 466 | if markdown { |
| 467 | <a |
| 468 | href=(tree_url(handle, name, rev, path)) |
| 469 | class=(item(active == FileTab::Code)) |
| 470 | >"Rendered"</a> |
| 471 | <span class="text-border">"·"</span> |
| 472 | <a |
| 473 | href=(source_url(handle, name, rev, path)) |
| 474 | class=(item(active == FileTab::Source)) |
| 475 | >"Source"</a> |
| 476 | } else { |
| 477 | <a |
| 478 | href=(tree_url(handle, name, rev, path)) |
| 479 | class=(item(active == FileTab::Code || active == FileTab::Source)) |
| 480 | >"Code"</a> |
| 481 | } |
| 482 | <span class="text-border">"·"</span> |
| 483 | <a |
| 484 | href=(blame_url(handle, name, rev, path)) |
| 485 | class=(item(active == FileTab::Blame)) |
| 486 | >"Blame"</a> |
| 487 | <span class="text-border">"·"</span> |
| 488 | <a |
| 489 | href=(path_log_url(handle, name, rev, path)) |
| 490 | class=(item(active == FileTab::History)) |
| 491 | >"History"</a> |
| 492 | <span class="text-border">"·"</span> |
| 493 | |
| 494 | |
| 495 | <a |
| 496 | href=(raw_url(handle, name, rev, path)) |
| 497 | class="inline-flex items-center gap-1 text-muted-foreground hover:text-foreground" |
| 498 | > |
| 499 | icon(data: iconify_icon!("feather:download"), attrs: attributes! { |
| 500 | class="size-3.5" |
| 501 | }) |
| 502 | "Raw" |
| 503 | </a> |
| 504 | </span> |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | pub(super) fn source_url(handle: &str, name: &str, rev: &RefName, path: &RepoPath) -> String { |
| 515 | format!("{}?view=source", tree_url(handle, name, rev, path)) |
| 516 | } |
| 517 | |
| 518 | |
| 519 | |
| 520 | pub(super) fn blame_url(handle: &str, name: &str, rev: &RefName, path: &RepoPath) -> String { |
| 521 | format!( |
| 522 | "/{handle}/repos/{name}/blame/{}/-/{}", |
| 523 | encode(rev.as_str(), false), |
| 524 | encode(path.as_str(), true) |
| 525 | ) |
| 526 | } |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | fn commit_url(handle: &str, name: &str, id: &ObjectId) -> String { |
| 533 | format!("/{handle}/repos/{name}/commits/{}", id.as_str()) |
| 534 | } |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | fn short_ago(time: SystemTime) -> String { |
| 544 | let Ok(elapsed) = SystemTime::now().duration_since(time) else { |
| 545 | |
| 546 | |
| 547 | return "now".to_owned(); |
| 548 | }; |
| 549 | |
| 550 | let seconds = elapsed.as_secs(); |
| 551 | |
| 552 | match seconds { |
| 553 | 0..=59 => "now".to_owned(), |
| 554 | 60..=3599 => format!("{}m", seconds / 60), |
| 555 | 3600..=86_399 => format!("{}h", seconds / 3600), |
| 556 | 86_400..=2_591_999 => format!("{}d", seconds / 86_400), |
| 557 | 2_592_000..=31_535_999 => format!("{}mo", seconds / 2_592_000), |
| 558 | _ => format!("{}y", seconds / 31_536_000), |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | #[cfg(test)] |
| 563 | mod tests { |
| 564 | use std::time::Duration; |
| 565 | |
| 566 | use super::*; |
| 567 | |
| 568 | fn rev(value: &str) -> RefName { |
| 569 | RefName::new(value).expect("valid revision") |
| 570 | } |
| 571 | |
| 572 | #[test] |
| 573 | fn a_blame_url_mirrors_the_tree_url_it_is_reached_from() { |
| 574 | let path = RepoPath::new("src/domain/repo.rs").expect("valid"); |
| 575 | |
| 576 | assert_eq!( |
| 577 | blame_url("ada", "steid", &rev("main"), &path), |
| 578 | "/ada/repos/steid/blame/main/-/src/domain/repo.rs" |
| 579 | ); |
| 580 | } |
| 581 | |
| 582 | #[test] |
| 583 | fn a_revisions_slashes_stay_inside_one_segment() { |
| 584 | |
| 585 | |
| 586 | let path = RepoPath::new("README.md").expect("valid"); |
| 587 | |
| 588 | assert_eq!( |
| 589 | blame_url("ada", "steid", &rev("feature/login"), &path), |
| 590 | "/ada/repos/steid/blame/feature%2Flogin/-/README.md" |
| 591 | ); |
| 592 | } |
| 593 | |
| 594 | #[test] |
| 595 | fn a_source_url_is_the_file_asking_for_its_other_view() { |
| 596 | let path = RepoPath::new("docs/design.md").expect("valid"); |
| 597 | |
| 598 | assert_eq!( |
| 599 | source_url("ada", "steid", &rev("main"), &path), |
| 600 | "/ada/repos/steid/tree/main/-/docs/design.md?view=source" |
| 601 | ); |
| 602 | } |
| 603 | |
| 604 | #[test] |
| 605 | fn a_commit_url_carries_the_whole_id() { |
| 606 | |
| 607 | let id = ObjectId::from_trusted("0123456789abcdef0123456789abcdef01234567"); |
| 608 | |
| 609 | assert_eq!( |
| 610 | commit_url("ada", "steid", &id), |
| 611 | "/ada/repos/steid/commits/0123456789abcdef0123456789abcdef01234567" |
| 612 | ); |
| 613 | } |
| 614 | |
| 615 | #[test] |
| 616 | fn a_relative_date_fits_beside_a_sha() { |
| 617 | let now = SystemTime::now(); |
| 618 | let since = |seconds| short_ago(now - Duration::from_secs(seconds)); |
| 619 | |
| 620 | assert_eq!(since(5), "now"); |
| 621 | assert_eq!(since(600), "10m"); |
| 622 | assert_eq!(since(7200), "2h"); |
| 623 | assert_eq!(since(86_400 * 3), "3d"); |
| 624 | assert_eq!(since(86_400 * 70), "2mo"); |
| 625 | assert_eq!(since(86_400 * 400), "1y"); |
| 626 | } |
| 627 | |
| 628 | #[test] |
| 629 | fn a_commit_from_the_future_reads_as_now() { |
| 630 | assert_eq!( |
| 631 | short_ago(SystemTime::now() + Duration::from_secs(3600)), |
| 632 | "now" |
| 633 | ); |
| 634 | } |
| 635 | } |