f42a185feat: a file read by who last changed each line, and a way between the two views19h | 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::{attributes, component, view}, |
| 20 | }; |
| 21 | |
| 22 | use crate::{ |
00d9c06refactor: one place decides that git ran out of time18h | 23 | application::{Blame, BlameContent, BlameFile, BlameGroup, blame::AGE_STEPS, blame_file}, |
f42a185feat: a file read by who last changed each line, and a way between the two views19h | 24 | domain::{ObjectId, RefName, RepoPath}, |
| 25 | }; |
| 26 | |
| 27 | use super::{ |
8804ad9feat: blame can change revision, like every other page under Code18h | 28 | browse::{ |
| 29 | Switch, ago, crumbs, encode, raw_url, ref_links, refs_for, rev_switcher, size_of, tree_url, |
| 30 | }, |
00d9c06refactor: one place decides that git ran out of time18h | 31 | context::{current_actor, memberships, orgs, queries, repos, server_error, timed_out}, |
f42a185feat: a file read by who last changed each line, and a way between the two views19h | 32 | layout::wide, |
| 33 | repo::{Tab, repo_for, repo_header}, |
| 34 | }; |
| 35 | |
| 36 | |
| 37 | #[path_param] |
| 38 | struct Rev(str); |
| 39 | |
| 40 | |
| 41 | #[path_param] |
| 42 | struct Path(str); |
| 43 | |
| 44 | #[page("/{handle}/repos/{name}/blame/{rev}/-/{*path}")] |
| 45 | async fn blame_page(cx: &Cx) -> Result { |
| 46 | |
| 47 | |
| 48 | let rev = RefName::new(path_param::<Rev>(cx)).map_err(|_| not_found())?; |
| 49 | let path = RepoPath::new(path_param::<Path>(cx)).map_err(|_| not_found())?; |
| 50 | |
| 51 | view! { blaming(rev: rev, path: path) } |
| 52 | } |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | enum Body<'a> { |
| 60 | |
| 61 | TimedOut, |
| 62 | Ready(&'a Blame), |
| 63 | |
| 64 | Empty, |
| 65 | Binary, |
| 66 | TooLarge(u64), |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | #[component] |
| 72 | async fn blaming(cx: &Cx, rev: RefName, path: RepoPath) -> Result { |
| 73 | let repo = repo_for(cx).await?; |
| 74 | |
| 75 | let outcome = blame_file( |
| 76 | &repo.handle, |
| 77 | &repo.name, |
| 78 | &rev, |
| 79 | &path, |
| 80 | ¤t_actor(cx).await?, |
| 81 | &orgs(cx), |
| 82 | &memberships(cx), |
| 83 | &repos(cx), |
| 84 | &queries(cx), |
| 85 | ) |
| 86 | .await; |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | let file: Option<BlameFile> = match outcome { |
| 92 | Ok(Some(file)) => Some(file), |
| 93 | Ok(None) => return Err(not_found().into()), |
00d9c06refactor: one place decides that git ran out of time18h | 94 | Err(error) if timed_out(&error) => None, |
f42a185feat: a file read by who last changed each line, and a way between the two views19h | 95 | Err(other) => return Err(server_error(std::io::Error::other(other.to_string()))), |
| 96 | }; |
| 97 | |
| 98 | let body = match &file { |
| 99 | None => Body::TimedOut, |
| 100 | Some(file) => match &file.content { |
| 101 | BlameContent::Binary => Body::Binary, |
| 102 | BlameContent::TooLarge => Body::TooLarge(file.size), |
| 103 | BlameContent::Ready(blame) if blame.is_empty() => Body::Empty, |
| 104 | BlameContent::Ready(blame) => Body::Ready(blame), |
| 105 | }, |
| 106 | }; |
| 107 | |
| 108 | let handle = repo.handle.as_str(); |
| 109 | let name = repo.name.as_str(); |
| 110 | let blob = tree_url(handle, name, &rev, &path); |
| 111 | |
8804ad9feat: blame can change revision, like every other page under Code18h | 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | let refs = refs_for(cx, &repo).await?; |
| 117 | let at = rev.as_str(); |
| 118 | let known = refs.contains(&rev); |
| 119 | let switch = Switch::Blame(&path); |
| 120 | let branches = ref_links(handle, name, &refs.branches, at, &switch); |
| 121 | let tags = ref_links(handle, name, &refs.tags, at, &switch); |
| 122 | |
f42a185feat: a file read by who last changed each line, and a way between the two views19h | 123 | |
| 124 | |
| 125 | let oversized = match &body { |
| 126 | Body::TooLarge(size) => format!( |
| 127 | "This file is {}, which is too large to blame. Clone the repository to read it.", |
| 128 | size_of(*size), |
| 129 | ), |
| 130 | _ => String::new(), |
| 131 | }; |
| 132 | |
| 133 | view! { |
| 134 | wide( |
8804ad9feat: blame can change revision, like every other page under Code18h | 135 | repo_header( |
| 136 | repo: &repo, |
| 137 | rev: at, |
| 138 | active: Tab::Code, |
| 139 | rev_switcher(current: at, known: known, branches: &branches, tags: &tags) |
| 140 | ) |
f42a185feat: a file read by who last changed each line, and a way between the two views19h | 141 | |
| 142 | <div class="overflow-hidden rounded-lg border border-border"> |
| 143 | <div class="flex flex-wrap items-center justify-between gap-3 border-b border-border px-4 py-2.5"> |
| 144 | crumbs(handle: handle, name: name, rev: &rev, path: &path) |
| 145 | <span class="flex items-center gap-3 font-mono text-xs text-muted-foreground"> |
| 146 | match &file { |
| 147 | |
| 148 | |
| 149 | Some(file) => (size_of(file.size)), |
| 150 | None => "", |
| 151 | } |
| 152 | view_toggle( |
| 153 | handle: handle, |
| 154 | name: name, |
| 155 | rev: &rev, |
| 156 | path: &path, |
| 157 | active: FileTab::Blame, |
| 158 | ) |
| 159 | </span> |
| 160 | </div> |
| 161 | |
| 162 | match body { |
| 163 | Body::Ready(blame) => blame_rows( |
| 164 | handle: handle, |
| 165 | name: name, |
| 166 | path: &path, |
| 167 | blob: blob.as_str(), |
| 168 | blame: blame, |
| 169 | ), |
| 170 | Body::Empty => note(message: "This file is empty."), |
| 171 | Body::Binary => note(message: "This file cannot be blamed as text."), |
| 172 | Body::TooLarge(_) => note(message: oversized.as_str()), |
00d9c06refactor: one place decides that git ran out of time18h | 173 | Body::TimedOut => took_too_long(blob: blob.as_str()), |
f42a185feat: a file read by who last changed each line, and a way between the two views19h | 174 | } |
| 175 | </div> |
| 176 | ) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | |
| 181 | #[component] |
| 182 | async fn note(message: &str) -> Result { |
| 183 | view! { |
| 184 | <p class="px-4 py-6 text-center text-sm text-muted-foreground">(message)</p> |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | #[component] |
00d9c06refactor: one place decides that git ran out of time18h | 193 | async fn took_too_long(blob: &str) -> Result { |
f42a185feat: a file read by who last changed each line, and a way between the two views19h | 194 | view! { |
| 195 | <div class="px-4 py-6 text-center"> |
| 196 | <p class="text-sm text-muted-foreground"> |
| 197 | "Blame took too long for this file." |
| 198 | </p> |
| 199 | <p class="mt-1 text-xs text-muted-foreground"> |
| 200 | "Its history is long enough that walking it ran out of time. " |
| 201 | <a href=(blob) class="text-primary hover:underline">"Read the file instead"</a> |
| 202 | "." |
| 203 | </p> |
| 204 | </div> |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | #[component] |
| 215 | async fn blame_rows( |
| 216 | handle: &str, |
| 217 | name: &str, |
| 218 | path: &RepoPath, |
| 219 | blob: &str, |
| 220 | blame: &Blame, |
| 221 | ) -> Result { |
| 222 | view! { |
| 223 | <div class="overflow-x-auto"> |
| 224 | <table class="w-full border-collapse font-mono text-xs leading-relaxed"> |
| 225 | <tbody> |
| 226 | for (index, group) in blame.groups.iter().enumerate() { |
| 227 | for (offset, line) in group.lines.iter().enumerate() { |
| 228 | <tr class=(if offset == 0 && index > 0 { |
| 229 | "border-t border-border" |
| 230 | } else { |
| 231 | "" |
| 232 | })> |
| 233 | |
| 234 | |
| 235 | |
| 236 | <td class=(format!( |
| 237 | "w-px border-r border-border pl-2 pr-3 align-top blame-age blame-age-{}", |
| 238 | group.age.min(AGE_STEPS - 1), |
| 239 | ))> |
| 240 | if offset == 0 { |
| 241 | commit_cell(handle: handle, name: name, path: path, group: group) |
| 242 | } |
| 243 | </td> |
| 244 | <td class="w-px select-none border-r border-border px-3 text-right align-top text-muted-foreground"> |
| 245 | <a |
| 246 | href=(format!("{blob}#L{}", group.start_line + offset)) |
| 247 | class="hover:text-foreground" |
| 248 | >((group.start_line + offset).to_string())</a> |
| 249 | </td> |
| 250 | <td class="whitespace-pre px-4 align-top"> |
| 251 | (if line.is_empty() { " " } else { line.as_str() }) |
| 252 | </td> |
| 253 | </tr> |
| 254 | } |
| 255 | } |
| 256 | </tbody> |
| 257 | </table> |
| 258 | </div> |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
e141fa6fix: the commit column takes half an 800px window, so it narrows there18h | 266 | |
| 267 | |
| 268 | |
| 269 | |
f42a185feat: a file read by who last changed each line, and a way between the two views19h | 270 | #[component] |
| 271 | async fn commit_cell(handle: &str, name: &str, path: &RepoPath, group: &BlameGroup) -> Result { |
| 272 | let commit = &group.commit; |
| 273 | |
| 274 | |
| 275 | let moved = !commit.filename.is_empty() && commit.filename != path.as_str(); |
| 276 | |
| 277 | let mut about = format!( |
| 278 | "{} · {} · {}", |
| 279 | commit.author_name, |
| 280 | ago(commit.authored_at), |
| 281 | commit.summary, |
| 282 | ); |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | if commit.boundary { |
| 288 | about.push_str(" · the oldest commit blame reached"); |
| 289 | } |
| 290 | |
| 291 | view! { |
e141fa6fix: the commit column takes half an 800px window, so it narrows there18h | 292 | <div class="flex w-56 items-baseline gap-2 overflow-hidden lg:w-80"> |
f42a185feat: a file read by who last changed each line, and a way between the two views19h | 293 | <a |
| 294 | href=(commit_url(handle, name, &commit.id)) |
| 295 | title=(about.as_str()) |
| 296 | class="shrink-0 text-muted-foreground hover:text-foreground" |
| 297 | >(commit.id.short())</a> |
| 298 | |
| 299 | if moved { |
| 300 | <span |
| 301 | class="shrink-0 self-center text-muted-foreground" |
| 302 | title=(format!("Moved from {}", commit.filename)) |
| 303 | > |
| 304 | icon( |
| 305 | data: iconify_icon!("feather:corner-down-right"), |
| 306 | label: "Moved from another file", |
| 307 | attrs: attributes! { class="size-3" }, |
| 308 | ) |
| 309 | </span> |
| 310 | } |
| 311 | |
| 312 | <span class="min-w-0 flex-1 truncate text-muted-foreground" title=(about.as_str())> |
| 313 | (&commit.summary) |
| 314 | </span> |
| 315 | <span class="shrink-0 text-muted-foreground">(short_ago(commit.authored_at))</span> |
| 316 | </div> |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 327 | pub(super) enum FileTab { |
| 328 | Code, |
| 329 | Blame, |
| 330 | } |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | #[component] |
| 338 | pub(super) async fn view_toggle( |
| 339 | handle: &str, |
| 340 | name: &str, |
| 341 | rev: &RefName, |
| 342 | path: &RepoPath, |
| 343 | active: FileTab, |
| 344 | ) -> Result { |
| 345 | let item = |current| { |
| 346 | if current { |
| 347 | "text-primary" |
| 348 | } else { |
| 349 | "text-muted-foreground hover:text-foreground" |
| 350 | } |
| 351 | }; |
| 352 | |
| 353 | view! { |
| 354 | <span class="flex items-center gap-1.5"> |
| 355 | <a |
| 356 | href=(tree_url(handle, name, rev, path)) |
| 357 | class=(item(active == FileTab::Code)) |
| 358 | >"Code"</a> |
| 359 | <span class="text-border">"·"</span> |
| 360 | <a |
| 361 | href=(blame_url(handle, name, rev, path)) |
| 362 | class=(item(active == FileTab::Blame)) |
| 363 | >"Blame"</a> |
| 364 | <span class="text-border">"·"</span> |
| 365 | |
| 366 | |
| 367 | <a |
| 368 | href=(raw_url(handle, name, rev, path)) |
| 369 | class="inline-flex items-center gap-1 text-muted-foreground hover:text-foreground" |
| 370 | > |
| 371 | icon(data: iconify_icon!("feather:download"), attrs: attributes! { |
| 372 | class="size-3.5" |
| 373 | }) |
| 374 | "Raw" |
| 375 | </a> |
| 376 | </span> |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | pub(super) fn blame_url(handle: &str, name: &str, rev: &RefName, path: &RepoPath) -> String { |
| 385 | format!( |
| 386 | "/{handle}/repos/{name}/blame/{}/-/{}", |
| 387 | encode(rev.as_str(), false), |
| 388 | encode(path.as_str(), true) |
| 389 | ) |
| 390 | } |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | fn commit_url(handle: &str, name: &str, id: &ObjectId) -> String { |
| 397 | format!("/{handle}/repos/{name}/commits/{}", id.as_str()) |
| 398 | } |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | fn short_ago(time: SystemTime) -> String { |
| 408 | let Ok(elapsed) = SystemTime::now().duration_since(time) else { |
| 409 | |
| 410 | |
| 411 | return "now".to_owned(); |
| 412 | }; |
| 413 | |
| 414 | let seconds = elapsed.as_secs(); |
| 415 | |
| 416 | match seconds { |
| 417 | 0..=59 => "now".to_owned(), |
| 418 | 60..=3599 => format!("{}m", seconds / 60), |
| 419 | 3600..=86_399 => format!("{}h", seconds / 3600), |
| 420 | 86_400..=2_591_999 => format!("{}d", seconds / 86_400), |
| 421 | 2_592_000..=31_535_999 => format!("{}mo", seconds / 2_592_000), |
| 422 | _ => format!("{}y", seconds / 31_536_000), |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | #[cfg(test)] |
| 427 | mod tests { |
| 428 | use std::time::Duration; |
| 429 | |
| 430 | use super::*; |
| 431 | |
| 432 | fn rev(value: &str) -> RefName { |
| 433 | RefName::new(value).expect("valid revision") |
| 434 | } |
| 435 | |
| 436 | #[test] |
| 437 | fn a_blame_url_mirrors_the_tree_url_it_is_reached_from() { |
| 438 | let path = RepoPath::new("src/domain/repo.rs").expect("valid"); |
| 439 | |
| 440 | assert_eq!( |
| 441 | blame_url("ada", "steid", &rev("main"), &path), |
| 442 | "/ada/repos/steid/blame/main/-/src/domain/repo.rs" |
| 443 | ); |
| 444 | } |
| 445 | |
| 446 | #[test] |
| 447 | fn a_revisions_slashes_stay_inside_one_segment() { |
| 448 | |
| 449 | |
| 450 | let path = RepoPath::new("README.md").expect("valid"); |
| 451 | |
| 452 | assert_eq!( |
| 453 | blame_url("ada", "steid", &rev("feature/login"), &path), |
| 454 | "/ada/repos/steid/blame/feature%2Flogin/-/README.md" |
| 455 | ); |
| 456 | } |
| 457 | |
| 458 | #[test] |
| 459 | fn a_commit_url_carries_the_whole_id() { |
| 460 | |
| 461 | let id = ObjectId::from_trusted("0123456789abcdef0123456789abcdef01234567"); |
| 462 | |
| 463 | assert_eq!( |
| 464 | commit_url("ada", "steid", &id), |
| 465 | "/ada/repos/steid/commits/0123456789abcdef0123456789abcdef01234567" |
| 466 | ); |
| 467 | } |
| 468 | |
| 469 | #[test] |
| 470 | fn a_relative_date_fits_beside_a_sha() { |
| 471 | let now = SystemTime::now(); |
| 472 | let since = |seconds| short_ago(now - Duration::from_secs(seconds)); |
| 473 | |
| 474 | assert_eq!(since(5), "now"); |
| 475 | assert_eq!(since(600), "10m"); |
| 476 | assert_eq!(since(7200), "2h"); |
| 477 | assert_eq!(since(86_400 * 3), "3d"); |
| 478 | assert_eq!(since(86_400 * 70), "2mo"); |
| 479 | assert_eq!(since(86_400 * 400), "1y"); |
| 480 | } |
| 481 | |
| 482 | #[test] |
| 483 | fn a_commit_from_the_future_reads_as_now() { |
| 484 | assert_eq!( |
| 485 | short_ago(SystemTime::now() + Duration::from_secs(3600)), |
| 486 | "now" |
| 487 | ); |
| 488 | } |
| 489 | } |