5727ed7feat: who last changed each line, as a thing the port can answer22h | 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | use std::{ |
| 14 | collections::HashMap, |
| 15 | time::{Duration, SystemTime, UNIX_EPOCH}, |
| 16 | }; |
| 17 | |
| 18 | use crate::domain::{ |
| 19 | Actor, ObjectId, OrgName, RefName, RepoName, RepoPath, |
| 20 | repository::{MembershipRepository, OrgRepository, RepoRepository}, |
| 21 | }; |
| 22 | |
| 23 | use super::{browse::MAX_BLOB_BYTES, error::Result, port::GitQuery, repo::view_repo}; |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | pub const AGE_STEPS: u8 = 5; |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 38 | pub struct BlameCommit { |
| 39 | pub id: ObjectId, |
| 40 | pub summary: String, |
| 41 | pub author_name: String, |
| 42 | pub authored_at: SystemTime, |
| 43 | |
| 44 | |
| 45 | pub boundary: bool, |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | pub filename: String, |
| 52 | } |
| 53 | |
| 54 | |
| 55 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 56 | pub struct BlameGroup { |
| 57 | pub commit: BlameCommit, |
| 58 | |
| 59 | pub start_line: usize, |
| 60 | pub lines: Vec<String>, |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | pub age: u8, |
| 66 | } |
| 67 | |
| 68 | |
| 69 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 70 | pub struct Blame { |
| 71 | pub groups: Vec<BlameGroup>, |
| 72 | } |
| 73 | |
| 74 | impl Blame { |
| 75 | |
| 76 | |
| 77 | pub fn is_empty(&self) -> bool { |
| 78 | self.groups.is_empty() |
| 79 | } |
| 80 | |
| 81 | pub fn line_count(&self) -> usize { |
| 82 | self.groups.iter().map(|group| group.lines.len()).sum() |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 91 | pub enum BlameContent { |
| 92 | Ready(Blame), |
| 93 | |
| 94 | Binary, |
| 95 | |
| 96 | TooLarge, |
| 97 | } |
| 98 | |
| 99 | |
| 100 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 101 | pub struct BlameFile { |
| 102 | pub size: u64, |
| 103 | pub content: BlameContent, |
| 104 | } |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | #[allow(clippy::too_many_arguments)] |
| 118 | pub async fn blame_file( |
| 119 | handle: &OrgName, |
| 120 | name: &RepoName, |
| 121 | rev: &RefName, |
| 122 | path: &RepoPath, |
| 123 | actor: &Actor, |
| 124 | orgs: &impl OrgRepository, |
| 125 | memberships: &impl MembershipRepository, |
| 126 | repos: &impl RepoRepository, |
| 127 | queries: &impl GitQuery, |
| 128 | ) -> Result<Option<BlameFile>> { |
| 129 | if view_repo(handle, name, actor, orgs, memberships, repos) |
| 130 | .await? |
| 131 | .is_none() |
| 132 | { |
| 133 | return Ok(None); |
| 134 | } |
| 135 | |
| 136 | let Some(blob) = queries |
| 137 | .read_blob(handle, name, rev, path, MAX_BLOB_BYTES) |
| 138 | .await? |
| 139 | else { |
| 140 | return Ok(None); |
| 141 | }; |
| 142 | |
| 143 | let content = match &blob.content { |
| 144 | |
| 145 | |
| 146 | None => BlameContent::TooLarge, |
| 147 | Some(bytes) if std::str::from_utf8(bytes).is_err() => BlameContent::Binary, |
| 148 | Some(_) => match queries.blame(handle, name, rev, path).await? { |
| 149 | Some(blame) => BlameContent::Ready(blame), |
| 150 | |
| 151 | |
| 152 | None => return Ok(None), |
| 153 | }, |
| 154 | }; |
| 155 | |
| 156 | Ok(Some(BlameFile { |
| 157 | size: blob.size, |
| 158 | content, |
| 159 | })) |
| 160 | } |
| 161 | |
| 162 | |
| 163 | struct Attributed { |
| 164 | commit: ObjectId, |
| 165 | line: usize, |
| 166 | content: String, |
| 167 | } |
| 168 | |
| 169 | |
| 170 | #[derive(Default, Clone)] |
| 171 | struct CommitHeader { |
| 172 | summary: String, |
| 173 | author_name: String, |
| 174 | authored_at: i64, |
| 175 | boundary: bool, |
| 176 | filename: String, |
| 177 | } |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | pub fn parse_blame(stdout: &[u8]) -> Blame { |
| 194 | let mut headers: HashMap<String, CommitHeader> = HashMap::new(); |
| 195 | let mut attributed: Vec<Attributed> = Vec::new(); |
| 196 | let mut pending: Option<(ObjectId, usize)> = None; |
| 197 | |
| 198 | for record in stdout.split(|byte| *byte == b'\n') { |
| 199 | |
| 200 | |
| 201 | if let Some((commit, line)) = pending.clone() |
| 202 | && record.first() == Some(&b'\t') |
| 203 | { |
| 204 | attributed.push(Attributed { |
| 205 | commit, |
| 206 | line, |
| 207 | content: String::from_utf8_lossy(&record[1..]) |
| 208 | .trim_end_matches('\r') |
| 209 | .to_owned(), |
| 210 | }); |
| 211 | pending = None; |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | let text = String::from_utf8_lossy(record); |
| 216 | let text = text.trim_end_matches('\r'); |
| 217 | |
| 218 | if text.is_empty() { |
| 219 | continue; |
| 220 | } |
| 221 | |
| 222 | let Some((commit, _)) = pending.clone() else { |
| 223 | |
| 224 | |
| 225 | |
| 226 | let mut fields = text.split(' '); |
| 227 | let (Some(sha), Some(_original), Some(line)) = |
| 228 | (fields.next(), fields.next(), fields.next()) |
| 229 | else { |
| 230 | continue; |
| 231 | }; |
| 232 | |
| 233 | let (Ok(sha), Ok(line)) = (ObjectId::new(sha), line.parse::<usize>()) else { |
| 234 | continue; |
| 235 | }; |
| 236 | |
| 237 | pending = Some((sha, line)); |
| 238 | continue; |
| 239 | }; |
| 240 | |
| 241 | let (key, value) = text.split_once(' ').unwrap_or((text, "")); |
| 242 | let header = headers.entry(commit.as_str().to_owned()).or_default(); |
| 243 | |
| 244 | match key { |
| 245 | "author" => header.author_name = value.to_owned(), |
| 246 | "author-time" => header.authored_at = value.parse().unwrap_or_default(), |
| 247 | "summary" => header.summary = value.to_owned(), |
| 248 | |
| 249 | "boundary" => header.boundary = true, |
| 250 | "filename" => header.filename = value.to_owned(), |
| 251 | _ => {} |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | group(attributed, &headers) |
| 256 | } |
| 257 | |
| 258 | |
| 259 | fn group(attributed: Vec<Attributed>, headers: &HashMap<String, CommitHeader>) -> Blame { |
| 260 | let mut groups: Vec<BlameGroup> = Vec::new(); |
| 261 | let mut times: Vec<i64> = Vec::new(); |
| 262 | |
| 263 | for line in attributed { |
| 264 | let continues = groups.last().is_some_and(|last| { |
| 265 | last.commit.id == line.commit && last.start_line + last.lines.len() == line.line |
| 266 | }); |
| 267 | |
| 268 | if continues { |
| 269 | groups |
| 270 | .last_mut() |
| 271 | .expect("a continued run has a last group") |
| 272 | .lines |
| 273 | .push(line.content); |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | let header = headers |
| 278 | .get(line.commit.as_str()) |
| 279 | .cloned() |
| 280 | .unwrap_or_default(); |
| 281 | |
| 282 | times.push(header.authored_at); |
| 283 | groups.push(BlameGroup { |
| 284 | commit: BlameCommit { |
| 285 | id: line.commit, |
| 286 | summary: header.summary, |
| 287 | author_name: header.author_name, |
| 288 | authored_at: unix_time(header.authored_at), |
| 289 | boundary: header.boundary, |
| 290 | filename: header.filename, |
| 291 | }, |
| 292 | start_line: line.line, |
| 293 | lines: vec![line.content], |
| 294 | age: 0, |
| 295 | }); |
| 296 | } |
| 297 | |
| 298 | tint(&mut groups, ×); |
| 299 | |
| 300 | Blame { groups } |
| 301 | } |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | fn tint(groups: &mut [BlameGroup], times: &[i64]) { |
| 312 | let (Some(oldest), Some(newest)) = (times.iter().min(), times.iter().max()) else { |
| 313 | return; |
| 314 | }; |
| 315 | |
| 316 | let span = newest - oldest; |
| 317 | let last = i64::from(AGE_STEPS - 1); |
| 318 | |
| 319 | for (group, time) in groups.iter_mut().zip(times) { |
| 320 | group.age = if span == 0 { |
| 321 | AGE_STEPS - 1 |
| 322 | } else { |
| 323 | |
| 324 | |
| 325 | (((time - oldest) * i64::from(AGE_STEPS) / span).min(last)) as u8 |
| 326 | }; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | |
| 331 | |
| 332 | fn unix_time(seconds: i64) -> SystemTime { |
| 333 | match u64::try_from(seconds) { |
| 334 | Ok(seconds) => UNIX_EPOCH + Duration::from_secs(seconds), |
| 335 | Err(_) => UNIX_EPOCH - Duration::from_secs(seconds.unsigned_abs()), |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | #[cfg(test)] |
| 340 | mod tests { |
| 341 | use super::*; |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | const PORCELAIN: &[u8] = b"\ |
| 347 | af126c7704b91896c347742dbf91c9abac20cb99 1 1 2 |
| 348 | author Ada Lovelace |
| 349 | author-mail <ada@example.com> |
| 350 | author-time 1786485070 |
| 351 | author-tz +0100 |
| 352 | committer Ada Lovelace |
| 353 | committer-mail <ada@example.com> |
| 354 | committer-time 1786485070 |
| 355 | committer-tz +0100 |
| 356 | summary feat: the first components |
| 357 | boundary |
| 358 | filename components.toml |
| 359 | \t# Topcoat UI install state. |
| 360 | af126c7704b91896c347742dbf91c9abac20cb99 2 2 |
| 361 | \tversion = 1 |
| 362 | 285f5fd29aabe8814c7b94bca3a89b3e176410e8 3 3 2 |
| 363 | author Grace Hopper |
| 364 | author-mail <grace@example.com> |
| 365 | author-time 1786614911 |
| 366 | author-tz +0100 |
| 367 | committer Grace Hopper |
| 368 | committer-mail <grace@example.com> |
| 369 | committer-time 1786614911 |
| 370 | committer-tz +0100 |
| 371 | summary feat: create and view repositories |
| 372 | previous af126c7704b91896c347742dbf91c9abac20cb99 old-name.toml |
| 373 | filename old-name.toml |
| 374 | \t[theme] |
| 375 | 285f5fd29aabe8814c7b94bca3a89b3e176410e8 4 4 |
| 376 | \tname = \"neutral\" |
| 377 | af126c7704b91896c347742dbf91c9abac20cb99 5 5 1 |
| 378 | filename components.toml |
| 379 | \tregistry = \"topcoat\" |
| 380 | "; |
| 381 | |
| 382 | fn blame() -> Blame { |
| 383 | parse_blame(PORCELAIN) |
| 384 | } |
| 385 | |
| 386 | #[test] |
| 387 | fn consecutive_lines_from_one_commit_become_one_run() { |
| 388 | let blame = blame(); |
| 389 | |
| 390 | assert_eq!(blame.groups.len(), 3, "{:#?}", blame.groups); |
| 391 | assert_eq!(blame.line_count(), 5); |
| 392 | assert_eq!(blame.groups[0].start_line, 1); |
| 393 | assert_eq!( |
| 394 | blame.groups[0].lines, |
| 395 | vec!["# Topcoat UI install state.", "version = 1"] |
| 396 | ); |
| 397 | assert_eq!(blame.groups[1].start_line, 3); |
| 398 | assert_eq!(blame.groups[2].start_line, 5); |
| 399 | } |
| 400 | |
| 401 | #[test] |
| 402 | fn a_commit_returning_later_is_a_second_run_rather_than_a_continuation() { |
| 403 | |
| 404 | |
| 405 | let blame = blame(); |
| 406 | |
| 407 | assert_eq!(blame.groups[0].commit.id, blame.groups[2].commit.id); |
| 408 | assert_eq!(blame.groups[2].lines, vec!["registry = \"topcoat\""]); |
| 409 | } |
| 410 | |
| 411 | #[test] |
| 412 | fn commit_details_are_remembered_for_later_runs() { |
| 413 | |
| 414 | |
| 415 | let blame = blame(); |
| 416 | let third = &blame.groups[2].commit; |
| 417 | |
| 418 | assert_eq!(third.author_name, "Ada Lovelace"); |
| 419 | assert_eq!(third.summary, "feat: the first components"); |
| 420 | assert_eq!(third.authored_at, unix_time(1_786_485_070)); |
| 421 | } |
| 422 | |
| 423 | #[test] |
| 424 | fn a_boundary_commit_is_marked_as_one() { |
| 425 | let blame = blame(); |
| 426 | |
| 427 | assert!(blame.groups[0].commit.boundary); |
| 428 | assert!(!blame.groups[1].commit.boundary); |
| 429 | } |
| 430 | |
| 431 | #[test] |
| 432 | fn a_run_moved_by_a_rename_carries_the_name_it_had() { |
| 433 | |
| 434 | |
| 435 | let blame = blame(); |
| 436 | |
| 437 | assert_eq!(blame.groups[1].commit.filename, "old-name.toml"); |
| 438 | assert_eq!(blame.groups[0].commit.filename, "components.toml"); |
| 439 | } |
| 440 | |
| 441 | #[test] |
| 442 | fn the_oldest_and_newest_runs_sit_at_the_ends_of_the_tint() { |
| 443 | let blame = blame(); |
| 444 | |
| 445 | assert_eq!(blame.groups[0].age, 0, "the oldest commit is the faintest"); |
| 446 | assert_eq!( |
| 447 | blame.groups[2].age, 0, |
| 448 | "and so is the same commit's later run" |
| 449 | ); |
| 450 | assert_eq!( |
| 451 | blame.groups[1].age, |
| 452 | AGE_STEPS - 1, |
| 453 | "the newest commit is the strongest" |
| 454 | ); |
| 455 | } |
| 456 | |
| 457 | #[test] |
| 458 | fn a_file_written_in_one_commit_is_uniformly_new() { |
| 459 | |
| 460 | |
| 461 | let single = b"\ |
| 462 | 1111111111111111111111111111111111111111 1 1 1 |
| 463 | author Ada Lovelace |
| 464 | author-time 1786485070 |
| 465 | summary only |
| 466 | filename notes.md |
| 467 | \thello |
| 468 | " as &[u8]; |
| 469 | |
| 470 | let blame = parse_blame(single); |
| 471 | |
| 472 | assert_eq!(blame.groups.len(), 1); |
| 473 | assert_eq!(blame.groups[0].age, AGE_STEPS - 1); |
| 474 | } |
| 475 | |
| 476 | #[test] |
| 477 | fn every_step_of_the_tint_is_reachable() { |
| 478 | |
| 479 | |
| 480 | let mut porcelain = Vec::new(); |
| 481 | |
| 482 | for index in 0..5u32 { |
| 483 | let sha = format!("{index}").repeat(40); |
| 484 | let line = index as usize + 1; |
| 485 | porcelain.extend_from_slice( |
| 486 | format!( |
| 487 | "{} {line} {line} 1\nauthor Ada\nauthor-time {}\nsummary s\nfilename f\n\tline\n", |
| 488 | &sha[..40], |
| 489 | 1_700_000_000 + index * 1000 |
| 490 | ) |
| 491 | .as_bytes(), |
| 492 | ); |
| 493 | } |
| 494 | |
| 495 | let ages: Vec<u8> = parse_blame(&porcelain) |
| 496 | .groups |
| 497 | .iter() |
| 498 | .map(|group| group.age) |
| 499 | .collect(); |
| 500 | |
| 501 | assert_eq!(ages, vec![0, 1, 2, 3, 4]); |
| 502 | } |
| 503 | |
| 504 | #[test] |
| 505 | fn an_empty_file_blames_to_nothing() { |
| 506 | assert!(parse_blame(b"").is_empty()); |
| 507 | } |
| 508 | |
| 509 | #[test] |
| 510 | fn a_line_that_is_not_utf8_still_blames() { |
| 511 | let mut porcelain = b"\ |
| 512 | 1111111111111111111111111111111111111111 1 1 1 |
| 513 | author Ada |
| 514 | author-time 1786485070 |
| 515 | summary only |
| 516 | filename notes.md |
| 517 | \t" |
| 518 | .to_vec(); |
| 519 | porcelain.extend_from_slice(&[0xff, 0xfe]); |
| 520 | porcelain.push(b'\n'); |
| 521 | |
| 522 | let blame = parse_blame(&porcelain); |
| 523 | |
| 524 | assert_eq!(blame.groups.len(), 1); |
| 525 | assert_eq!(blame.groups[0].lines[0], "\u{fffd}\u{fffd}"); |
| 526 | } |
| 527 | |
| 528 | #[test] |
| 529 | fn a_line_of_code_that_looks_like_a_header_is_still_content() { |
| 530 | |
| 531 | |
| 532 | let porcelain = b"\ |
| 533 | 1111111111111111111111111111111111111111 1 1 1 |
| 534 | author Ada |
| 535 | author-time 1786485070 |
| 536 | summary only |
| 537 | filename notes.md |
| 538 | \t2222222222222222222222222222222222222222 9 9 9 |
| 539 | " as &[u8]; |
| 540 | |
| 541 | let blame = parse_blame(porcelain); |
| 542 | |
| 543 | assert_eq!(blame.groups.len(), 1); |
| 544 | assert_eq!( |
| 545 | blame.groups[0].lines[0], |
| 546 | "2222222222222222222222222222222222222222 9 9 9" |
| 547 | ); |
| 548 | } |
| 549 | |
| 550 | |
| 551 | |
| 552 | use crate::{ |
| 553 | domain::{ |
| 554 | Membership, MembershipId, OrgId, Organization, RepoId, Repository, UserId, Visibility, |
| 555 | }, |
| 556 | infrastructure::{ |
| 557 | git::InMemoryGitQuery, |
| 558 | repository::{InMemoryMembershipRepo, InMemoryOrgRepo, InMemoryRepoRepo}, |
| 559 | }, |
| 560 | }; |
| 561 | |
| 562 | struct Fixture { |
| 563 | orgs: InMemoryOrgRepo, |
| 564 | memberships: InMemoryMembershipRepo, |
| 565 | repos: InMemoryRepoRepo, |
| 566 | handle: OrgName, |
| 567 | owner: Actor, |
| 568 | stranger: Actor, |
| 569 | } |
| 570 | |
| 571 | |
| 572 | async fn fixture(visibility: Visibility) -> Fixture { |
| 573 | let orgs = InMemoryOrgRepo::new(); |
| 574 | let memberships = InMemoryMembershipRepo::new(); |
| 575 | let repos = InMemoryRepoRepo::new(); |
| 576 | |
| 577 | let org = Organization::new(OrgId::generate(), "acme", None).expect("valid org"); |
| 578 | orgs.save(&org).await.expect("save org"); |
| 579 | |
| 580 | let owner = UserId::generate(); |
| 581 | memberships |
| 582 | .save(&Membership::new( |
| 583 | MembershipId::generate(), |
| 584 | org.id.clone(), |
| 585 | owner.clone(), |
| 586 | crate::domain::Role::Owner, |
| 587 | )) |
| 588 | .await |
| 589 | .expect("save membership"); |
| 590 | |
| 591 | repos |
| 592 | .save( |
| 593 | &Repository::new( |
| 594 | RepoId::generate(), |
| 595 | org.id.clone(), |
| 596 | "steid", |
| 597 | None, |
| 598 | visibility, |
| 599 | SystemTime::now(), |
| 600 | ) |
| 601 | .expect("valid repository"), |
| 602 | ) |
| 603 | .await |
| 604 | .expect("save repo"); |
| 605 | |
| 606 | Fixture { |
| 607 | orgs, |
| 608 | memberships, |
| 609 | repos, |
| 610 | handle: org.name, |
| 611 | owner: Actor::User(owner), |
| 612 | stranger: Actor::Anonymous, |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | impl Fixture { |
| 617 | async fn blame( |
| 618 | &self, |
| 619 | actor: &Actor, |
| 620 | path: &str, |
| 621 | queries: &InMemoryGitQuery, |
| 622 | ) -> Result<Option<BlameFile>> { |
| 623 | blame_file( |
| 624 | &self.handle, |
| 625 | &RepoName::new("steid").expect("valid repository name"), |
| 626 | &RefName::new("main").expect("valid revision"), |
| 627 | &RepoPath::new(path).expect("valid path"), |
| 628 | actor, |
| 629 | &self.orgs, |
| 630 | &self.memberships, |
| 631 | &self.repos, |
| 632 | queries, |
| 633 | ) |
| 634 | .await |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | fn one_line_blame() -> Blame { |
| 639 | parse_blame( |
| 640 | b"1111111111111111111111111111111111111111 1 1 1\nauthor Ada\nauthor-time 1786485070\nsummary only\nfilename notes.md\n\thello\n", |
| 641 | ) |
| 642 | } |
| 643 | |
| 644 | #[tokio::test] |
| 645 | async fn a_text_file_blames() { |
| 646 | let f = fixture(Visibility::Public).await; |
| 647 | let queries = InMemoryGitQuery::new() |
| 648 | .with_blob("main", "notes.md", b"hello\n") |
| 649 | .with_blame("main", "notes.md", one_line_blame()); |
| 650 | |
| 651 | let file = f |
| 652 | .blame(&f.owner, "notes.md", &queries) |
| 653 | .await |
| 654 | .expect("should read") |
| 655 | .expect("found"); |
| 656 | |
| 657 | assert_eq!(file.size, 6); |
| 658 | assert_eq!(file.content, BlameContent::Ready(one_line_blame())); |
| 659 | } |
| 660 | |
| 661 | #[tokio::test] |
| 662 | async fn a_binary_file_has_no_lines_to_attribute() { |
| 663 | |
| 664 | |
| 665 | let f = fixture(Visibility::Public).await; |
| 666 | let queries = InMemoryGitQuery::new().with_blob("main", "logo.png", vec![0x00, 0xff, 0xfe]); |
| 667 | |
| 668 | let file = f |
| 669 | .blame(&f.owner, "logo.png", &queries) |
| 670 | .await |
| 671 | .expect("should read") |
| 672 | .expect("found"); |
| 673 | |
| 674 | assert_eq!(file.content, BlameContent::Binary); |
| 675 | } |
| 676 | |
| 677 | #[tokio::test] |
| 678 | async fn a_file_past_the_page_cap_says_so_rather_than_blaming() { |
| 679 | let f = fixture(Visibility::Public).await; |
| 680 | let size = MAX_BLOB_BYTES as usize + 1; |
| 681 | let queries = InMemoryGitQuery::new().with_blob("main", "big.txt", vec![b'x'; size]); |
| 682 | |
| 683 | let file = f |
| 684 | .blame(&f.owner, "big.txt", &queries) |
| 685 | .await |
| 686 | .expect("should read") |
| 687 | .expect("found"); |
| 688 | |
| 689 | assert_eq!(file.content, BlameContent::TooLarge); |
| 690 | assert_eq!(file.size, size as u64); |
| 691 | } |
| 692 | |
| 693 | #[tokio::test] |
| 694 | async fn a_path_that_is_not_a_file_is_not_found() { |
| 695 | let f = fixture(Visibility::Public).await; |
| 696 | let queries = InMemoryGitQuery::new().with_tree("main", "src", Vec::new()); |
| 697 | |
| 698 | for path in ["src", "nope.md"] { |
| 699 | assert!( |
| 700 | f.blame(&f.owner, path, &queries) |
| 701 | .await |
| 702 | .expect("should read") |
| 703 | .is_none(), |
| 704 | "{path} should not blame" |
| 705 | ); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | #[tokio::test] |
| 710 | async fn a_private_repositorys_blame_is_invisible_to_a_stranger() { |
| 711 | |
| 712 | |
| 713 | let f = fixture(Visibility::Private).await; |
| 714 | let queries = InMemoryGitQuery::new() |
| 715 | .with_blob("main", "notes.md", b"hello\n") |
| 716 | .with_blame("main", "notes.md", one_line_blame()); |
| 717 | |
| 718 | assert!( |
| 719 | f.blame(&f.stranger, "notes.md", &queries) |
| 720 | .await |
| 721 | .expect("should read") |
| 722 | .is_none() |
| 723 | ); |
| 724 | assert!( |
| 725 | f.blame(&f.owner, "notes.md", &queries) |
| 726 | .await |
| 727 | .expect("should read") |
| 728 | .is_some() |
| 729 | ); |
| 730 | } |
| 731 | } |