| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | use std::collections::HashMap; |
| 8 | |
| 9 | use crate::domain::{ |
| 10 | Actor, BranchRow, CommitSummary, EntryCommit, ObjectId, OrgName, RefKind, RefName, RepoName, |
| 11 | RepoPath, TagRow, |
| 12 | repository::{MembershipRepository, OrgRepository, RepoRepository}, |
| 13 | }; |
| 14 | |
| 15 | use super::{ |
| 16 | error::Result, |
| 17 | paging::{Paged, clamp, offset, page_count}, |
| 18 | port::{Blob, GitQuery}, |
| 19 | repo::visible_repo, |
| 20 | }; |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | pub const MAX_BLOB_BYTES: u64 = 1024 * 1024; |
| 28 | |
| 29 | |
| 30 | pub const LOG_LIMIT: usize = 50; |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | pub const REF_LIMIT: usize = 100; |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | pub const MAX_RAW_BYTES: u64 = 10 * 1024 * 1024; |
| 47 | |
| 48 | |
| 49 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 50 | pub struct FileView { |
| 51 | pub id: ObjectId, |
| 52 | pub size: u64, |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | pub text: Option<String>, |
| 58 | pub too_large: bool, |
| 59 | } |
| 60 | |
| 61 | impl FileView { |
| 62 | |
| 63 | pub fn is_binary(&self) -> bool { |
| 64 | self.text.is_none() && !self.too_large |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | |
| 69 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 70 | pub enum Browsed { |
| 71 | |
| 72 | |
| 73 | |
| 74 | Empty, |
| 75 | Directory { |
| 76 | rev: RefName, |
| 77 | path: RepoPath, |
| 78 | |
| 79 | entries: Vec<crate::domain::TreeEntry>, |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | history: HashMap<String, EntryCommit>, |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | details: HashMap<String, EntryDetail>, |
| 92 | }, |
| 93 | File { |
| 94 | rev: RefName, |
| 95 | path: RepoPath, |
| 96 | file: FileView, |
| 97 | }, |
| 98 | } |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 105 | pub struct EntryDetail { |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | pub url: Option<String>, |
| 112 | |
| 113 | pub target: Option<String>, |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | pub resolved: Option<RepoPath>, |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | const MAX_GITMODULES_BYTES: u64 = 64 * 1024; |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | const MAX_SYMLINK_TARGETS: usize = 100; |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | #[allow(clippy::too_many_arguments)] |
| 146 | pub async fn browse_repo( |
| 147 | handle: &OrgName, |
| 148 | name: &RepoName, |
| 149 | rev: Option<&RefName>, |
| 150 | path: &RepoPath, |
| 151 | actor: &Actor, |
| 152 | orgs: &impl OrgRepository, |
| 153 | memberships: &impl MembershipRepository, |
| 154 | repos: &impl RepoRepository, |
| 155 | queries: &impl GitQuery, |
| 156 | ) -> Result<Option<Browsed>> { |
| 157 | if visible_repo(handle, name, actor, orgs, memberships, repos) |
| 158 | .await? |
| 159 | .is_none() |
| 160 | { |
| 161 | return Ok(None); |
| 162 | } |
| 163 | |
| 164 | let Some(rev) = resolve_revision(handle, name, rev, queries).await? else { |
| 165 | return Ok(Some(Browsed::Empty)); |
| 166 | }; |
| 167 | |
| 168 | |
| 169 | if let Some(mut entries) = queries.list_tree(handle, name, &rev, path).await? { |
| 170 | entries.sort_by_key(crate::domain::TreeEntry::ordering_key); |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | let names: Vec<String> = entries.iter().map(|entry| entry.name.clone()).collect(); |
| 177 | let history = queries |
| 178 | .last_commits(handle, name, &rev, path, &names) |
| 179 | .await?; |
| 180 | |
| 181 | let details = entry_details(handle, name, &rev, path, &entries, queries).await?; |
| 182 | |
| 183 | return Ok(Some(Browsed::Directory { |
| 184 | rev, |
| 185 | path: path.clone(), |
| 186 | entries, |
| 187 | history, |
| 188 | details, |
| 189 | })); |
| 190 | } |
| 191 | |
| 192 | let Some(blob) = queries |
| 193 | .read_blob(handle, name, &rev, path, MAX_BLOB_BYTES) |
| 194 | .await? |
| 195 | else { |
| 196 | return Ok(None); |
| 197 | }; |
| 198 | |
| 199 | Ok(Some(Browsed::File { |
| 200 | rev, |
| 201 | path: path.clone(), |
| 202 | file: view_of(blob), |
| 203 | })) |
| 204 | } |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | async fn entry_details( |
| 212 | handle: &OrgName, |
| 213 | name: &RepoName, |
| 214 | rev: &RefName, |
| 215 | path: &RepoPath, |
| 216 | entries: &[crate::domain::TreeEntry], |
| 217 | queries: &impl GitQuery, |
| 218 | ) -> Result<HashMap<String, EntryDetail>> { |
| 219 | use crate::domain::EntryKind; |
| 220 | |
| 221 | let mut details: HashMap<String, EntryDetail> = HashMap::new(); |
| 222 | |
| 223 | if entries |
| 224 | .iter() |
| 225 | .any(|entry| entry.kind == EntryKind::Submodule) |
| 226 | { |
| 227 | |
| 228 | |
| 229 | let modules = RepoPath::from_trusted(".gitmodules"); |
| 230 | let text = queries |
| 231 | .read_blob(handle, name, rev, &modules, MAX_GITMODULES_BYTES) |
| 232 | .await? |
| 233 | .and_then(|blob| blob.content) |
| 234 | .and_then(|bytes| String::from_utf8(bytes).ok()) |
| 235 | .unwrap_or_default(); |
| 236 | |
| 237 | let urls = parse_gitmodules(&text); |
| 238 | |
| 239 | for entry in entries |
| 240 | .iter() |
| 241 | .filter(|entry| entry.kind == EntryKind::Submodule) |
| 242 | { |
| 243 | |
| 244 | |
| 245 | let full = path.join(&entry.name); |
| 246 | |
| 247 | if let Some((_, url)) = urls.iter().find(|(at, _)| at == full.as_str()) { |
| 248 | details.entry(entry.name.clone()).or_default().url = Some(url.clone()); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | let links: Vec<&crate::domain::TreeEntry> = entries |
| 254 | .iter() |
| 255 | .filter(|entry| entry.kind == EntryKind::Symlink) |
| 256 | .collect(); |
| 257 | |
| 258 | if !links.is_empty() && links.len() <= MAX_SYMLINK_TARGETS { |
| 259 | let ids: Vec<ObjectId> = links.iter().map(|entry| entry.id.clone()).collect(); |
| 260 | let targets = queries.symlink_targets(handle, name, &ids).await?; |
| 261 | |
| 262 | for entry in links { |
| 263 | let Some(target) = targets.get(&entry.id) else { |
| 264 | continue; |
| 265 | }; |
| 266 | |
| 267 | let detail = details.entry(entry.name.clone()).or_default(); |
| 268 | |
| 269 | |
| 270 | |
| 271 | detail.resolved = resolve_relative(path, target); |
| 272 | detail.target = Some(target.clone()); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | Ok(details) |
| 277 | } |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | pub fn parse_gitmodules(text: &str) -> Vec<(String, String)> { |
| 290 | let mut found: Vec<(String, String)> = Vec::new(); |
| 291 | let mut path: Option<String> = None; |
| 292 | let mut url: Option<String> = None; |
| 293 | |
| 294 | |
| 295 | |
| 296 | fn bank( |
| 297 | found: &mut Vec<(String, String)>, |
| 298 | path: &mut Option<String>, |
| 299 | url: &mut Option<String>, |
| 300 | ) { |
| 301 | if let (Some(path), Some(url)) = (path.take(), url.take()) |
| 302 | && url.starts_with("https://") |
| 303 | && !path.is_empty() |
| 304 | { |
| 305 | found.push((path, url)); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | for line in text.lines() { |
| 310 | let line = line.trim(); |
| 311 | |
| 312 | if line.is_empty() || line.starts_with('#') || line.starts_with(';') { |
| 313 | continue; |
| 314 | } |
| 315 | |
| 316 | if line.starts_with('[') { |
| 317 | bank(&mut found, &mut path, &mut url); |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | let Some((key, value)) = line.split_once('=') else { |
| 322 | continue; |
| 323 | }; |
| 324 | |
| 325 | |
| 326 | |
| 327 | let value = value.trim().trim_matches('"').to_owned(); |
| 328 | |
| 329 | match key.trim().to_ascii_lowercase().as_str() { |
| 330 | "path" => path = Some(value.trim_matches('/').to_owned()), |
| 331 | "url" => url = Some(value), |
| 332 | _ => {} |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | bank(&mut found, &mut path, &mut url); |
| 337 | |
| 338 | found |
| 339 | } |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | pub fn resolve_relative(dir: &RepoPath, target: &str) -> Option<RepoPath> { |
| 352 | let target = target.trim(); |
| 353 | |
| 354 | if target.is_empty() || target.starts_with('/') { |
| 355 | return None; |
| 356 | } |
| 357 | |
| 358 | let mut parts: Vec<&str> = dir.components().collect(); |
| 359 | |
| 360 | for component in target.split('/') { |
| 361 | match component { |
| 362 | "" | "." => {} |
| 363 | ".." => { |
| 364 | |
| 365 | parts.pop()?; |
| 366 | } |
| 367 | name => parts.push(name), |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | if parts.is_empty() { |
| 372 | return None; |
| 373 | } |
| 374 | |
| 375 | RepoPath::new(parts.join("/")).ok() |
| 376 | } |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | #[allow(clippy::too_many_arguments)] |
| 390 | pub async fn repo_log( |
| 391 | handle: &OrgName, |
| 392 | name: &RepoName, |
| 393 | rev: Option<&RefName>, |
| 394 | page: usize, |
| 395 | actor: &Actor, |
| 396 | orgs: &impl OrgRepository, |
| 397 | memberships: &impl MembershipRepository, |
| 398 | repos: &impl RepoRepository, |
| 399 | queries: &impl GitQuery, |
| 400 | ) -> Result<Option<Paged<CommitSummary>>> { |
| 401 | if visible_repo(handle, name, actor, orgs, memberships, repos) |
| 402 | .await? |
| 403 | .is_none() |
| 404 | { |
| 405 | return Ok(None); |
| 406 | } |
| 407 | |
| 408 | let Some(rev) = resolve_revision(handle, name, rev, queries).await? else { |
| 409 | return Ok(Some(Paged::of(Vec::new(), 0, 1, LOG_LIMIT))); |
| 410 | }; |
| 411 | |
| 412 | let total = queries.count_commits(handle, name, &rev).await? as usize; |
| 413 | |
| 414 | |
| 415 | let page = clamp(page, page_count(total, LOG_LIMIT)); |
| 416 | |
| 417 | let commits = queries |
| 418 | .log(handle, name, &rev, offset(page, LOG_LIMIT), LOG_LIMIT) |
| 419 | .await?; |
| 420 | |
| 421 | Ok(Some(Paged::of(commits, total, page, LOG_LIMIT))) |
| 422 | } |
| 423 | |
| 424 | |
| 425 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 426 | pub struct PathLog { |
| 427 | pub rev: RefName, |
| 428 | pub path: RepoPath, |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | pub is_file: bool, |
| 436 | pub commits: Paged<CommitSummary>, |
| 437 | |
| 438 | |
| 439 | |
| 440 | pub capped: bool, |
| 441 | } |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | #[allow(clippy::too_many_arguments)] |
| 456 | pub async fn repo_path_log( |
| 457 | handle: &OrgName, |
| 458 | name: &RepoName, |
| 459 | rev: Option<&RefName>, |
| 460 | path: &RepoPath, |
| 461 | page: usize, |
| 462 | actor: &Actor, |
| 463 | orgs: &impl OrgRepository, |
| 464 | memberships: &impl MembershipRepository, |
| 465 | repos: &impl RepoRepository, |
| 466 | queries: &impl GitQuery, |
| 467 | ) -> Result<Option<PathLog>> { |
| 468 | if visible_repo(handle, name, actor, orgs, memberships, repos) |
| 469 | .await? |
| 470 | .is_none() |
| 471 | { |
| 472 | return Ok(None); |
| 473 | } |
| 474 | |
| 475 | |
| 476 | |
| 477 | let Some(rev) = resolve_revision(handle, name, rev, queries).await? else { |
| 478 | return Ok(None); |
| 479 | }; |
| 480 | |
| 481 | |
| 482 | |
| 483 | let is_file = if queries.list_tree(handle, name, &rev, path).await?.is_some() { |
| 484 | false |
| 485 | } else { |
| 486 | |
| 487 | |
| 488 | let Some(_) = queries.read_blob(handle, name, &rev, path, 0).await? else { |
| 489 | return Ok(None); |
| 490 | }; |
| 491 | |
| 492 | true |
| 493 | }; |
| 494 | |
| 495 | let counted = queries |
| 496 | .count_commits_at_path(handle, name, &rev, path, is_file) |
| 497 | .await?; |
| 498 | let total = counted.total as usize; |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | let page = clamp(page, page_count(total, LOG_LIMIT)); |
| 504 | |
| 505 | let commits = queries |
| 506 | .log_at_path( |
| 507 | handle, |
| 508 | name, |
| 509 | &rev, |
| 510 | path, |
| 511 | is_file, |
| 512 | offset(page, LOG_LIMIT), |
| 513 | LOG_LIMIT, |
| 514 | ) |
| 515 | .await?; |
| 516 | |
| 517 | Ok(Some(PathLog { |
| 518 | rev, |
| 519 | path: path.clone(), |
| 520 | is_file, |
| 521 | commits: Paged::of(commits, total, page, LOG_LIMIT), |
| 522 | capped: counted.capped, |
| 523 | })) |
| 524 | } |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 532 | pub struct RefList { |
| 533 | pub branches: Vec<RefName>, |
| 534 | pub tags: Vec<RefName>, |
| 535 | } |
| 536 | |
| 537 | impl RefList { |
| 538 | pub fn is_empty(&self) -> bool { |
| 539 | self.branches.is_empty() && self.tags.is_empty() |
| 540 | } |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | pub fn contains(&self, rev: &RefName) -> bool { |
| 547 | self.branches |
| 548 | .iter() |
| 549 | .chain(&self.tags) |
| 550 | .any(|name| name == rev) |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | pub async fn list_refs( |
| 568 | handle: &OrgName, |
| 569 | name: &RepoName, |
| 570 | actor: &Actor, |
| 571 | orgs: &impl OrgRepository, |
| 572 | memberships: &impl MembershipRepository, |
| 573 | repos: &impl RepoRepository, |
| 574 | queries: &impl GitQuery, |
| 575 | ) -> Result<Option<RefList>> { |
| 576 | if visible_repo(handle, name, actor, orgs, memberships, repos) |
| 577 | .await? |
| 578 | .is_none() |
| 579 | { |
| 580 | return Ok(None); |
| 581 | } |
| 582 | |
| 583 | Ok(Some(organise_refs(queries.list_refs(handle, name).await?))) |
| 584 | } |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | pub fn organise_refs(refs: Vec<crate::domain::GitRef>) -> RefList { |
| 592 | let mut list = RefList::default(); |
| 593 | |
| 594 | for git_ref in refs { |
| 595 | match git_ref.kind { |
| 596 | RefKind::Branch => list.branches.push(git_ref.name), |
| 597 | RefKind::Tag => list.tags.push(git_ref.name), |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | for names in [&mut list.branches, &mut list.tags] { |
| 602 | names.sort_by(|left, right| { |
| 603 | left.as_str() |
| 604 | .to_lowercase() |
| 605 | .cmp(&right.as_str().to_lowercase()) |
| 606 | .then_with(|| left.as_str().cmp(right.as_str())) |
| 607 | }); |
| 608 | } |
| 609 | |
| 610 | list |
| 611 | } |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 622 | pub struct RefPage<Row> { |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | pub rows: Paged<Row>, |
| 629 | |
| 630 | pub repo_is_empty: bool, |
| 631 | } |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | fn one_page<Row>(rows: Vec<Row>, page: Option<usize>) -> Paged<Row> { |
| 639 | match page { |
| 640 | Some(page) => Paged::slice(rows, page, REF_LIMIT), |
| 641 | None => { |
| 642 | let size = rows.len().max(1); |
| 643 | Paged::slice(rows, 1, size) |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | #[allow(clippy::too_many_arguments)] |
| 662 | pub async fn list_branches( |
| 663 | handle: &OrgName, |
| 664 | name: &RepoName, |
| 665 | page: Option<usize>, |
| 666 | actor: &Actor, |
| 667 | orgs: &impl OrgRepository, |
| 668 | memberships: &impl MembershipRepository, |
| 669 | repos: &impl RepoRepository, |
| 670 | queries: &impl GitQuery, |
| 671 | ) -> Result<Option<RefPage<BranchRow>>> { |
| 672 | if visible_repo(handle, name, actor, orgs, memberships, repos) |
| 673 | .await? |
| 674 | .is_none() |
| 675 | { |
| 676 | return Ok(None); |
| 677 | } |
| 678 | |
| 679 | let rows = pin_default(queries.branches(handle, name).await?); |
| 680 | let empty = rows.is_empty() && !has_commits(handle, name, queries).await?; |
| 681 | |
| 682 | Ok(Some(RefPage { |
| 683 | rows: one_page(rows, page), |
| 684 | repo_is_empty: empty, |
| 685 | })) |
| 686 | } |
| 687 | |
| 688 | |
| 689 | #[allow(clippy::too_many_arguments)] |
| 690 | pub async fn list_tags( |
| 691 | handle: &OrgName, |
| 692 | name: &RepoName, |
| 693 | page: Option<usize>, |
| 694 | actor: &Actor, |
| 695 | orgs: &impl OrgRepository, |
| 696 | memberships: &impl MembershipRepository, |
| 697 | repos: &impl RepoRepository, |
| 698 | queries: &impl GitQuery, |
| 699 | ) -> Result<Option<RefPage<TagRow>>> { |
| 700 | if visible_repo(handle, name, actor, orgs, memberships, repos) |
| 701 | .await? |
| 702 | .is_none() |
| 703 | { |
| 704 | return Ok(None); |
| 705 | } |
| 706 | |
| 707 | let rows = queries.tags(handle, name).await?; |
| 708 | let empty = rows.is_empty() && !has_commits(handle, name, queries).await?; |
| 709 | |
| 710 | Ok(Some(RefPage { |
| 711 | rows: one_page(rows, page), |
| 712 | repo_is_empty: empty, |
| 713 | })) |
| 714 | } |
| 715 | |
| 716 | |
| 717 | async fn has_commits(handle: &OrgName, name: &RepoName, queries: &impl GitQuery) -> Result<bool> { |
| 718 | Ok(queries.default_branch(handle, name).await?.is_some()) |
| 719 | } |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | fn pin_default(rows: Vec<BranchRow>) -> Vec<BranchRow> { |
| 731 | let (default, rest): (Vec<_>, Vec<_>) = rows.into_iter().partition(|row| row.is_default); |
| 732 | |
| 733 | default.into_iter().chain(rest).collect() |
| 734 | } |
| 735 | |
| 736 | |
| 737 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 738 | pub enum RawFile { |
| 739 | Ready { |
| 740 | |
| 741 | name: String, |
| 742 | content: Vec<u8>, |
| 743 | }, |
| 744 | |
| 745 | |
| 746 | TooLarge { size: u64 }, |
| 747 | } |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | #[allow(clippy::too_many_arguments)] |
| 759 | pub async fn read_raw_file( |
| 760 | handle: &OrgName, |
| 761 | name: &RepoName, |
| 762 | rev: Option<&RefName>, |
| 763 | path: &RepoPath, |
| 764 | actor: &Actor, |
| 765 | orgs: &impl OrgRepository, |
| 766 | memberships: &impl MembershipRepository, |
| 767 | repos: &impl RepoRepository, |
| 768 | queries: &impl GitQuery, |
| 769 | ) -> Result<Option<RawFile>> { |
| 770 | if visible_repo(handle, name, actor, orgs, memberships, repos) |
| 771 | .await? |
| 772 | .is_none() |
| 773 | { |
| 774 | return Ok(None); |
| 775 | } |
| 776 | |
| 777 | let Some(rev) = resolve_revision(handle, name, rev, queries).await? else { |
| 778 | return Ok(None); |
| 779 | }; |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | let Some(blob) = queries |
| 786 | .read_blob(handle, name, &rev, path, MAX_RAW_BYTES) |
| 787 | .await? |
| 788 | else { |
| 789 | return Ok(None); |
| 790 | }; |
| 791 | |
| 792 | let Some(content) = blob.content else { |
| 793 | return Ok(Some(RawFile::TooLarge { size: blob.size })); |
| 794 | }; |
| 795 | |
| 796 | Ok(Some(RawFile::Ready { |
| 797 | |
| 798 | |
| 799 | name: path.file_name().unwrap_or_default().to_owned(), |
| 800 | content, |
| 801 | })) |
| 802 | } |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | pub(crate) async fn resolve_revision( |
| 809 | handle: &OrgName, |
| 810 | name: &RepoName, |
| 811 | rev: Option<&RefName>, |
| 812 | queries: &impl GitQuery, |
| 813 | ) -> Result<Option<RefName>> { |
| 814 | match rev { |
| 815 | Some(rev) => Ok(Some(rev.clone())), |
| 816 | None => Ok(queries.default_branch(handle, name).await?), |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | fn view_of(blob: Blob) -> FileView { |
| 825 | let too_large = blob.content.is_none(); |
| 826 | let text = blob.content.and_then(|bytes| String::from_utf8(bytes).ok()); |
| 827 | |
| 828 | FileView { |
| 829 | id: blob.id, |
| 830 | size: blob.size, |
| 831 | text, |
| 832 | too_large, |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | #[cfg(test)] |
| 837 | mod tests { |
| 838 | use std::time::SystemTime; |
| 839 | |
| 840 | use super::*; |
| 841 | use crate::{ |
| 842 | domain::{ |
| 843 | EntryKind, Membership, MembershipId, OrgId, Organization, RepoId, Repository, |
| 844 | TreeEntry, UserId, Visibility, |
| 845 | }, |
| 846 | infrastructure::{ |
| 847 | git::InMemoryGitQuery, |
| 848 | repository::{InMemoryMembershipRepo, InMemoryOrgRepo, InMemoryRepoRepo}, |
| 849 | }, |
| 850 | }; |
| 851 | |
| 852 | struct Fixture { |
| 853 | orgs: InMemoryOrgRepo, |
| 854 | memberships: InMemoryMembershipRepo, |
| 855 | repos: InMemoryRepoRepo, |
| 856 | handle: OrgName, |
| 857 | owner: Actor, |
| 858 | stranger: Actor, |
| 859 | } |
| 860 | |
| 861 | |
| 862 | async fn fixture(visibility: Visibility) -> Fixture { |
| 863 | let orgs = InMemoryOrgRepo::new(); |
| 864 | let memberships = InMemoryMembershipRepo::new(); |
| 865 | let repos = InMemoryRepoRepo::new(); |
| 866 | |
| 867 | let org = Organization::new(OrgId::generate(), "acme", None).expect("valid org"); |
| 868 | orgs.save(&org).await.expect("save org"); |
| 869 | |
| 870 | let owner = UserId::generate(); |
| 871 | memberships |
| 872 | .save(&Membership::new( |
| 873 | MembershipId::generate(), |
| 874 | org.id.clone(), |
| 875 | owner.clone(), |
| 876 | crate::domain::Role::Owner, |
| 877 | )) |
| 878 | .await |
| 879 | .expect("save membership"); |
| 880 | |
| 881 | repos |
| 882 | .save( |
| 883 | &Repository::new( |
| 884 | RepoId::generate(), |
| 885 | org.id.clone(), |
| 886 | "steid", |
| 887 | None, |
| 888 | visibility, |
| 889 | SystemTime::now(), |
| 890 | ) |
| 891 | .expect("valid repository"), |
| 892 | ) |
| 893 | .await |
| 894 | .expect("save repo"); |
| 895 | |
| 896 | Fixture { |
| 897 | orgs, |
| 898 | memberships, |
| 899 | repos, |
| 900 | handle: org.name, |
| 901 | owner: Actor::User(owner), |
| 902 | stranger: Actor::Anonymous, |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | fn repo_name() -> RepoName { |
| 907 | RepoName::new("steid").expect("valid repository name") |
| 908 | } |
| 909 | |
| 910 | fn rev(value: &str) -> RefName { |
| 911 | RefName::new(value).expect("valid revision") |
| 912 | } |
| 913 | |
| 914 | fn path(value: &str) -> RepoPath { |
| 915 | RepoPath::new(value).expect("valid path") |
| 916 | } |
| 917 | |
| 918 | impl Fixture { |
| 919 | async fn refs(&self, actor: &Actor, queries: &InMemoryGitQuery) -> Result<Option<RefList>> { |
| 920 | list_refs( |
| 921 | &self.handle, |
| 922 | &repo_name(), |
| 923 | actor, |
| 924 | &self.orgs, |
| 925 | &self.memberships, |
| 926 | &self.repos, |
| 927 | queries, |
| 928 | ) |
| 929 | .await |
| 930 | } |
| 931 | |
| 932 | async fn raw( |
| 933 | &self, |
| 934 | actor: &Actor, |
| 935 | path: &RepoPath, |
| 936 | queries: &InMemoryGitQuery, |
| 937 | ) -> Result<Option<RawFile>> { |
| 938 | read_raw_file( |
| 939 | &self.handle, |
| 940 | &repo_name(), |
| 941 | Some(&rev("main")), |
| 942 | path, |
| 943 | actor, |
| 944 | &self.orgs, |
| 945 | &self.memberships, |
| 946 | &self.repos, |
| 947 | queries, |
| 948 | ) |
| 949 | .await |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | fn history(count: usize) -> Vec<CommitSummary> { |
| 957 | (0..count) |
| 958 | .map(|n| CommitSummary { |
| 959 | id: ObjectId::from_trusted(format!("{n:040x}")), |
| 960 | summary: format!("commit {n}"), |
| 961 | author_name: "Ada Lovelace".to_owned(), |
| 962 | committed_at: at(n as u64), |
| 963 | }) |
| 964 | .collect() |
| 965 | } |
| 966 | |
| 967 | async fn log_page( |
| 968 | f: &Fixture, |
| 969 | queries: &InMemoryGitQuery, |
| 970 | page: usize, |
| 971 | ) -> Paged<CommitSummary> { |
| 972 | repo_log( |
| 973 | &f.handle, |
| 974 | &repo_name(), |
| 975 | Some(&rev("main")), |
| 976 | page, |
| 977 | &f.owner, |
| 978 | &f.orgs, |
| 979 | &f.memberships, |
| 980 | &f.repos, |
| 981 | queries, |
| 982 | ) |
| 983 | .await |
| 984 | .expect("should read") |
| 985 | .expect("visible") |
| 986 | } |
| 987 | |
| 988 | #[tokio::test] |
| 989 | async fn the_second_page_of_a_log_starts_where_the_first_ended() { |
| 990 | let f = fixture(Visibility::Public).await; |
| 991 | let queries = InMemoryGitQuery::new().with_log(history(LOG_LIMIT + 10)); |
| 992 | |
| 993 | let first = log_page(&f, &queries, 1).await; |
| 994 | let second = log_page(&f, &queries, 2).await; |
| 995 | |
| 996 | assert_eq!(first.items.len(), LOG_LIMIT); |
| 997 | assert_eq!(second.items.len(), 10); |
| 998 | assert_eq!(second.offset(), LOG_LIMIT); |
| 999 | assert_eq!(second.items[0].summary, format!("commit {LOG_LIMIT}")); |
| 1000 | assert_eq!(second.pages, 2); |
| 1001 | assert_eq!(second.total, LOG_LIMIT + 10); |
| 1002 | } |
| 1003 | |
| 1004 | #[tokio::test] |
| 1005 | async fn a_log_page_past_the_end_clamps_to_the_last_one() { |
| 1006 | |
| 1007 | |
| 1008 | let f = fixture(Visibility::Public).await; |
| 1009 | let queries = InMemoryGitQuery::new().with_log(history(LOG_LIMIT + 10)); |
| 1010 | |
| 1011 | let page = log_page(&f, &queries, 400).await; |
| 1012 | |
| 1013 | assert_eq!(page.page, 2); |
| 1014 | assert_eq!(page.items.len(), 10); |
| 1015 | } |
| 1016 | |
| 1017 | #[tokio::test] |
| 1018 | async fn a_log_that_fits_on_one_page_is_not_paged() { |
| 1019 | let f = fixture(Visibility::Public).await; |
| 1020 | let queries = InMemoryGitQuery::new().with_log(history(3)); |
| 1021 | |
| 1022 | let page = log_page(&f, &queries, 1).await; |
| 1023 | |
| 1024 | assert!(!page.is_paged()); |
| 1025 | assert_eq!(page.total, 3); |
| 1026 | } |
| 1027 | |
| 1028 | #[tokio::test] |
| 1029 | async fn an_empty_repository_logs_one_empty_page() { |
| 1030 | let f = fixture(Visibility::Public).await; |
| 1031 | let queries = InMemoryGitQuery::empty(); |
| 1032 | |
| 1033 | let page = repo_log( |
| 1034 | &f.handle, |
| 1035 | &repo_name(), |
| 1036 | None, |
| 1037 | 1, |
| 1038 | &f.owner, |
| 1039 | &f.orgs, |
| 1040 | &f.memberships, |
| 1041 | &f.repos, |
| 1042 | &queries, |
| 1043 | ) |
| 1044 | .await |
| 1045 | .expect("should read") |
| 1046 | .expect("visible"); |
| 1047 | |
| 1048 | assert!(page.items.is_empty()); |
| 1049 | assert!(!page.is_paged()); |
| 1050 | } |
| 1051 | |
| 1052 | #[tokio::test] |
| 1053 | async fn a_log_is_invisible_to_a_stranger_when_the_repository_is() { |
| 1054 | let f = fixture(Visibility::Private).await; |
| 1055 | let queries = InMemoryGitQuery::new().with_log(history(3)); |
| 1056 | |
| 1057 | let seen = repo_log( |
| 1058 | &f.handle, |
| 1059 | &repo_name(), |
| 1060 | None, |
| 1061 | 1, |
| 1062 | &f.stranger, |
| 1063 | &f.orgs, |
| 1064 | &f.memberships, |
| 1065 | &f.repos, |
| 1066 | &queries, |
| 1067 | ) |
| 1068 | .await |
| 1069 | .expect("should read"); |
| 1070 | |
| 1071 | assert!(seen.is_none()); |
| 1072 | } |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | async fn browsed(f: &Fixture, queries: &InMemoryGitQuery, at: &str) -> Option<Browsed> { |
| 1078 | browse_repo( |
| 1079 | &f.handle, |
| 1080 | &repo_name(), |
| 1081 | Some(&rev("main")), |
| 1082 | &path(at), |
| 1083 | &f.owner, |
| 1084 | &f.orgs, |
| 1085 | &f.memberships, |
| 1086 | &f.repos, |
| 1087 | queries, |
| 1088 | ) |
| 1089 | .await |
| 1090 | .expect("should read") |
| 1091 | } |
| 1092 | |
| 1093 | #[test] |
| 1094 | fn a_gitmodules_gives_each_submodules_path_and_url() { |
| 1095 | let found = parse_gitmodules( |
| 1096 | "[submodule \"topcoat\"]\n\ |
| 1097 | \tpath = vendor/topcoat\n\ |
| 1098 | \turl = https://github.com/tokio-rs/topcoat.git\n\ |
| 1099 | [submodule \"docs\"]\n\ |
| 1100 | \tpath = docs/theme\n\ |
| 1101 | \turl = https://example.com/theme\n", |
| 1102 | ); |
| 1103 | |
| 1104 | assert_eq!( |
| 1105 | found, |
| 1106 | [ |
| 1107 | ( |
| 1108 | "vendor/topcoat".to_owned(), |
| 1109 | "https://github.com/tokio-rs/topcoat.git".to_owned() |
| 1110 | ), |
| 1111 | ( |
| 1112 | "docs/theme".to_owned(), |
| 1113 | "https://example.com/theme".to_owned() |
| 1114 | ), |
| 1115 | ] |
| 1116 | ); |
| 1117 | } |
| 1118 | |
| 1119 | #[test] |
| 1120 | fn a_url_a_browser_cannot_open_is_not_offered_as_a_link() { |
| 1121 | |
| 1122 | for url in [ |
| 1123 | "git@github.com:ada/steid.git", |
| 1124 | "ssh://git@example.com/ada/steid", |
| 1125 | "../sibling", |
| 1126 | "http://example.com/insecure", |
| 1127 | ] { |
| 1128 | let text = format!("[submodule \"x\"]\npath = vendor/x\nurl = {url}\n"); |
| 1129 | |
| 1130 | assert!( |
| 1131 | parse_gitmodules(&text).is_empty(), |
| 1132 | "{url} should not become a link" |
| 1133 | ); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | #[test] |
| 1138 | fn a_gitmodules_survives_comments_quotes_and_a_missing_half() { |
| 1139 | let found = parse_gitmodules( |
| 1140 | "# a comment\n\ |
| 1141 | ; another\n\ |
| 1142 | [submodule \"quoted\"]\n\ |
| 1143 | path = \"vendor/quoted\"\n\ |
| 1144 | url = \"https://example.com/quoted\"\n\ |
| 1145 | [submodule \"no-url\"]\n\ |
| 1146 | path = vendor/orphan\n\ |
| 1147 | [submodule \"no-path\"]\n\ |
| 1148 | url = https://example.com/orphan\n", |
| 1149 | ); |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | assert_eq!( |
| 1154 | found, |
| 1155 | [( |
| 1156 | "vendor/quoted".to_owned(), |
| 1157 | "https://example.com/quoted".to_owned() |
| 1158 | )] |
| 1159 | ); |
| 1160 | } |
| 1161 | |
| 1162 | #[test] |
| 1163 | fn a_relative_target_resolves_against_the_directory_it_is_written_in() { |
| 1164 | let at = |dir: &str, target: &str| { |
| 1165 | resolve_relative(&RepoPath::from_trusted(dir), target) |
| 1166 | .map(|path| path.as_str().to_owned()) |
| 1167 | }; |
| 1168 | |
| 1169 | assert_eq!(at("src", "main.rs").as_deref(), Some("src/main.rs")); |
| 1170 | assert_eq!(at("src", "./main.rs").as_deref(), Some("src/main.rs")); |
| 1171 | assert_eq!( |
| 1172 | at("src/web", "../domain/id.rs").as_deref(), |
| 1173 | Some("src/domain/id.rs") |
| 1174 | ); |
| 1175 | assert_eq!(at("", "README.md").as_deref(), Some("README.md")); |
| 1176 | assert_eq!( |
| 1177 | at("docs/a/b", "../../../LICENSE").as_deref(), |
| 1178 | Some("LICENSE") |
| 1179 | ); |
| 1180 | } |
| 1181 | |
| 1182 | #[test] |
| 1183 | fn a_target_that_leaves_the_repository_resolves_to_nothing() { |
| 1184 | let at = |dir: &str, target: &str| resolve_relative(&RepoPath::from_trusted(dir), target); |
| 1185 | |
| 1186 | |
| 1187 | assert!(at("src", "../../etc/passwd").is_none()); |
| 1188 | assert!(at("", "/etc/passwd").is_none()); |
| 1189 | assert!(at("src", "..").is_none()); |
| 1190 | assert!(at("src", "").is_none()); |
| 1191 | |
| 1192 | |
| 1193 | assert!(at("src", "weird:name").is_none()); |
| 1194 | } |
| 1195 | |
| 1196 | #[tokio::test] |
| 1197 | async fn a_submodule_is_linked_to_the_upstream_gitmodules_names() { |
| 1198 | let f = fixture(Visibility::Public).await; |
| 1199 | let queries = InMemoryGitQuery::new() |
| 1200 | .with_tree( |
| 1201 | "main", |
| 1202 | "vendor", |
| 1203 | vec![TreeEntry { |
| 1204 | name: "topcoat".to_owned(), |
| 1205 | kind: EntryKind::Submodule, |
| 1206 | id: ObjectId::from_trusted("c".repeat(40)), |
| 1207 | size: None, |
| 1208 | }], |
| 1209 | ) |
| 1210 | .with_blob( |
| 1211 | "main", |
| 1212 | ".gitmodules", |
| 1213 | "[submodule \"topcoat\"]\npath = vendor/topcoat\nurl = https://example.com/topcoat\n" |
| 1214 | .as_bytes() |
| 1215 | .to_vec(), |
| 1216 | ); |
| 1217 | |
| 1218 | let Some(Browsed::Directory { details, .. }) = browsed(&f, &queries, "vendor").await else { |
| 1219 | panic!("a directory"); |
| 1220 | }; |
| 1221 | |
| 1222 | |
| 1223 | |
| 1224 | assert_eq!( |
| 1225 | details["topcoat"].url.as_deref(), |
| 1226 | Some("https://example.com/topcoat") |
| 1227 | ); |
| 1228 | } |
| 1229 | |
| 1230 | #[tokio::test] |
| 1231 | async fn a_symlink_carries_its_target_and_where_that_lands() { |
| 1232 | let f = fixture(Visibility::Public).await; |
| 1233 | let inside = ObjectId::from_trusted("d".repeat(40)); |
| 1234 | let outside = ObjectId::from_trusted("e".repeat(40)); |
| 1235 | let queries = InMemoryGitQuery::new() |
| 1236 | .with_tree( |
| 1237 | "main", |
| 1238 | "src", |
| 1239 | vec![ |
| 1240 | TreeEntry { |
| 1241 | name: "here".to_owned(), |
| 1242 | kind: EntryKind::Symlink, |
| 1243 | id: inside.clone(), |
| 1244 | size: Some(9), |
| 1245 | }, |
| 1246 | TreeEntry { |
| 1247 | name: "elsewhere".to_owned(), |
| 1248 | kind: EntryKind::Symlink, |
| 1249 | id: outside.clone(), |
| 1250 | size: Some(11), |
| 1251 | }, |
| 1252 | ], |
| 1253 | ) |
| 1254 | .with_symlink_target(&inside, "../README.md") |
| 1255 | .with_symlink_target(&outside, "/etc/passwd"); |
| 1256 | |
| 1257 | let Some(Browsed::Directory { details, .. }) = browsed(&f, &queries, "src").await else { |
| 1258 | panic!("a directory"); |
| 1259 | }; |
| 1260 | |
| 1261 | assert_eq!(details["here"].target.as_deref(), Some("../README.md")); |
| 1262 | assert_eq!( |
| 1263 | details["here"].resolved.as_ref().map(RepoPath::as_str), |
| 1264 | Some("README.md") |
| 1265 | ); |
| 1266 | |
| 1267 | assert_eq!(details["elsewhere"].target.as_deref(), Some("/etc/passwd")); |
| 1268 | assert!(details["elsewhere"].resolved.is_none()); |
| 1269 | } |
| 1270 | |
| 1271 | #[tokio::test] |
| 1272 | async fn an_ordinary_directory_asks_for_no_details_at_all() { |
| 1273 | let f = fixture(Visibility::Public).await; |
| 1274 | let queries = InMemoryGitQuery::new().with_tree( |
| 1275 | "main", |
| 1276 | "src", |
| 1277 | vec![TreeEntry { |
| 1278 | name: "main.rs".to_owned(), |
| 1279 | kind: EntryKind::Blob, |
| 1280 | id: ObjectId::from_trusted("a".repeat(40)), |
| 1281 | size: Some(12), |
| 1282 | }], |
| 1283 | ); |
| 1284 | |
| 1285 | let Some(Browsed::Directory { details, .. }) = browsed(&f, &queries, "src").await else { |
| 1286 | panic!("a directory"); |
| 1287 | }; |
| 1288 | |
| 1289 | assert!(details.is_empty()); |
| 1290 | } |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | async fn path_log( |
| 1295 | f: &Fixture, |
| 1296 | queries: &InMemoryGitQuery, |
| 1297 | at: &str, |
| 1298 | page: usize, |
| 1299 | ) -> Option<PathLog> { |
| 1300 | repo_path_log( |
| 1301 | &f.handle, |
| 1302 | &repo_name(), |
| 1303 | Some(&rev("main")), |
| 1304 | &path(at), |
| 1305 | page, |
| 1306 | &f.owner, |
| 1307 | &f.orgs, |
| 1308 | &f.memberships, |
| 1309 | &f.repos, |
| 1310 | queries, |
| 1311 | ) |
| 1312 | .await |
| 1313 | .expect("should read") |
| 1314 | } |
| 1315 | |
| 1316 | |
| 1317 | fn with_history() -> InMemoryGitQuery { |
| 1318 | InMemoryGitQuery::new() |
| 1319 | .with_tree( |
| 1320 | "main", |
| 1321 | "src", |
| 1322 | vec![TreeEntry { |
| 1323 | name: "main.rs".to_owned(), |
| 1324 | kind: EntryKind::Blob, |
| 1325 | id: ObjectId::from_trusted("a".repeat(40)), |
| 1326 | size: Some(12), |
| 1327 | }], |
| 1328 | ) |
| 1329 | .with_blob("main", "src/main.rs", b"fn main() {}".to_vec()) |
| 1330 | .with_path_log("main", "src", history(LOG_LIMIT + 4)) |
| 1331 | .with_path_log("main", "src/main.rs", history(2)) |
| 1332 | } |
| 1333 | |
| 1334 | #[tokio::test] |
| 1335 | async fn a_files_history_is_followed_and_a_directorys_is_not() { |
| 1336 | let f = fixture(Visibility::Public).await; |
| 1337 | let queries = with_history(); |
| 1338 | |
| 1339 | |
| 1340 | |
| 1341 | assert!( |
| 1342 | path_log(&f, &queries, "src/main.rs", 1) |
| 1343 | .await |
| 1344 | .expect("found") |
| 1345 | .is_file |
| 1346 | ); |
| 1347 | assert!( |
| 1348 | !path_log(&f, &queries, "src", 1) |
| 1349 | .await |
| 1350 | .expect("found") |
| 1351 | .is_file |
| 1352 | ); |
| 1353 | } |
| 1354 | |
| 1355 | #[tokio::test] |
| 1356 | async fn a_paths_history_pages_like_every_other_list() { |
| 1357 | let f = fixture(Visibility::Public).await; |
| 1358 | let queries = with_history(); |
| 1359 | |
| 1360 | let second = path_log(&f, &queries, "src", 2).await.expect("found"); |
| 1361 | |
| 1362 | assert_eq!(second.commits.items.len(), 4); |
| 1363 | assert_eq!(second.commits.total, LOG_LIMIT + 4); |
| 1364 | assert_eq!(second.commits.offset(), LOG_LIMIT); |
| 1365 | |
| 1366 | assert_eq!( |
| 1367 | path_log(&f, &queries, "src", 400) |
| 1368 | .await |
| 1369 | .expect("found") |
| 1370 | .commits |
| 1371 | .page, |
| 1372 | 2 |
| 1373 | ); |
| 1374 | } |
| 1375 | |
| 1376 | #[tokio::test] |
| 1377 | async fn a_long_followed_history_stops_counting_at_the_cap() { |
| 1378 | |
| 1379 | |
| 1380 | |
| 1381 | use crate::infrastructure::git_query::FOLLOWED_LOG_SCAN; |
| 1382 | |
| 1383 | let f = fixture(Visibility::Public).await; |
| 1384 | let queries = with_history().with_path_log( |
| 1385 | "main", |
| 1386 | "src/main.rs", |
| 1387 | history(FOLLOWED_LOG_SCAN + LOG_LIMIT), |
| 1388 | ); |
| 1389 | |
| 1390 | let log = path_log(&f, &queries, "src/main.rs", 1) |
| 1391 | .await |
| 1392 | .expect("found"); |
| 1393 | |
| 1394 | assert!(log.capped); |
| 1395 | assert_eq!(log.commits.total, FOLLOWED_LOG_SCAN); |
| 1396 | assert_eq!(log.commits.pages, FOLLOWED_LOG_SCAN / LOG_LIMIT); |
| 1397 | |
| 1398 | |
| 1399 | |
| 1400 | let last = path_log(&f, &queries, "src/main.rs", 999) |
| 1401 | .await |
| 1402 | .expect("found"); |
| 1403 | assert_eq!(last.commits.page, FOLLOWED_LOG_SCAN / LOG_LIMIT); |
| 1404 | assert_eq!(last.commits.items.len(), LOG_LIMIT); |
| 1405 | } |
| 1406 | |
| 1407 | #[tokio::test] |
| 1408 | async fn a_directorys_count_is_exact_because_it_is_not_followed() { |
| 1409 | let f = fixture(Visibility::Public).await; |
| 1410 | |
| 1411 | assert!( |
| 1412 | !path_log(&f, &with_history(), "src", 1) |
| 1413 | .await |
| 1414 | .expect("found") |
| 1415 | .capped |
| 1416 | ); |
| 1417 | } |
| 1418 | |
| 1419 | #[tokio::test] |
| 1420 | async fn a_path_that_is_not_there_has_no_history_page() { |
| 1421 | |
| 1422 | |
| 1423 | let f = fixture(Visibility::Public).await; |
| 1424 | |
| 1425 | assert!(path_log(&f, &with_history(), "nope.rs", 1).await.is_none()); |
| 1426 | } |
| 1427 | |
| 1428 | #[tokio::test] |
| 1429 | async fn an_empty_repository_has_no_path_to_have_a_history() { |
| 1430 | let f = fixture(Visibility::Public).await; |
| 1431 | |
| 1432 | assert!( |
| 1433 | path_log(&f, &InMemoryGitQuery::empty(), "src", 1) |
| 1434 | .await |
| 1435 | .is_none() |
| 1436 | ); |
| 1437 | } |
| 1438 | |
| 1439 | #[tokio::test] |
| 1440 | async fn a_paths_history_is_invisible_to_a_stranger_when_the_repository_is() { |
| 1441 | let f = fixture(Visibility::Private).await; |
| 1442 | |
| 1443 | let seen = repo_path_log( |
| 1444 | &f.handle, |
| 1445 | &repo_name(), |
| 1446 | Some(&rev("main")), |
| 1447 | &path("src"), |
| 1448 | 1, |
| 1449 | &f.stranger, |
| 1450 | &f.orgs, |
| 1451 | &f.memberships, |
| 1452 | &f.repos, |
| 1453 | &with_history(), |
| 1454 | ) |
| 1455 | .await |
| 1456 | .expect("should read"); |
| 1457 | |
| 1458 | assert!(seen.is_none()); |
| 1459 | } |
| 1460 | |
| 1461 | |
| 1462 | |
| 1463 | fn at(offset: u64) -> SystemTime { |
| 1464 | std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_700_000_000 + offset) |
| 1465 | } |
| 1466 | |
| 1467 | #[tokio::test] |
| 1468 | async fn the_default_branch_is_pinned_above_the_rest() { |
| 1469 | let f = fixture(Visibility::Public).await; |
| 1470 | |
| 1471 | |
| 1472 | let queries = InMemoryGitQuery::new() |
| 1473 | .with_branch_row("feature/login", false, at(200)) |
| 1474 | .with_branch_row("spike", false, at(100)) |
| 1475 | .with_branch_row("main", true, at(0)); |
| 1476 | |
| 1477 | let page = list_branches( |
| 1478 | &f.handle, |
| 1479 | &repo_name(), |
| 1480 | Some(1), |
| 1481 | &f.owner, |
| 1482 | &f.orgs, |
| 1483 | &f.memberships, |
| 1484 | &f.repos, |
| 1485 | &queries, |
| 1486 | ) |
| 1487 | .await |
| 1488 | .expect("should read") |
| 1489 | .expect("visible"); |
| 1490 | |
| 1491 | assert_eq!( |
| 1492 | page.rows |
| 1493 | .items |
| 1494 | .iter() |
| 1495 | .map(|row| row.name.as_str()) |
| 1496 | .collect::<Vec<_>>(), |
| 1497 | vec!["main", "feature/login", "spike"] |
| 1498 | ); |
| 1499 | |
| 1500 | assert!(!page.repo_is_empty); |
| 1501 | } |
| 1502 | |
| 1503 | #[tokio::test] |
| 1504 | async fn a_repository_with_no_branches_says_it_is_empty() { |
| 1505 | |
| 1506 | |
| 1507 | let f = fixture(Visibility::Public).await; |
| 1508 | |
| 1509 | let page = list_branches( |
| 1510 | &f.handle, |
| 1511 | &repo_name(), |
| 1512 | Some(1), |
| 1513 | &f.owner, |
| 1514 | &f.orgs, |
| 1515 | &f.memberships, |
| 1516 | &f.repos, |
| 1517 | &InMemoryGitQuery::empty(), |
| 1518 | ) |
| 1519 | .await |
| 1520 | .expect("should read") |
| 1521 | .expect("visible"); |
| 1522 | |
| 1523 | assert!(page.rows.items.is_empty()); |
| 1524 | assert!(page.repo_is_empty); |
| 1525 | } |
| 1526 | |
| 1527 | #[tokio::test] |
| 1528 | async fn a_repository_with_commits_but_no_tags_is_not_empty() { |
| 1529 | |
| 1530 | let f = fixture(Visibility::Public).await; |
| 1531 | |
| 1532 | let page = list_tags( |
| 1533 | &f.handle, |
| 1534 | &repo_name(), |
| 1535 | Some(1), |
| 1536 | &f.owner, |
| 1537 | &f.orgs, |
| 1538 | &f.memberships, |
| 1539 | &f.repos, |
| 1540 | &InMemoryGitQuery::new(), |
| 1541 | ) |
| 1542 | .await |
| 1543 | .expect("should read") |
| 1544 | .expect("visible"); |
| 1545 | |
| 1546 | assert!(page.rows.items.is_empty()); |
| 1547 | assert!(!page.repo_is_empty); |
| 1548 | } |
| 1549 | |
| 1550 | #[tokio::test] |
| 1551 | async fn tags_keep_the_order_git_sorted_them_into() { |
| 1552 | let f = fixture(Visibility::Public).await; |
| 1553 | let queries = InMemoryGitQuery::new() |
| 1554 | .with_tag_row("v2.0", true, at(200)) |
| 1555 | .with_tag_row("v0.9", false, at(100)); |
| 1556 | |
| 1557 | let page = list_tags( |
| 1558 | &f.handle, |
| 1559 | &repo_name(), |
| 1560 | Some(1), |
| 1561 | &f.owner, |
| 1562 | &f.orgs, |
| 1563 | &f.memberships, |
| 1564 | &f.repos, |
| 1565 | &queries, |
| 1566 | ) |
| 1567 | .await |
| 1568 | .expect("should read") |
| 1569 | .expect("visible"); |
| 1570 | |
| 1571 | assert_eq!( |
| 1572 | page.rows |
| 1573 | .items |
| 1574 | .iter() |
| 1575 | .map(|row| row.name.as_str()) |
| 1576 | .collect::<Vec<_>>(), |
| 1577 | vec!["v2.0", "v0.9"] |
| 1578 | ); |
| 1579 | assert_eq!(page.rows.items[0].message.as_deref(), Some("release v2.0")); |
| 1580 | assert_eq!(page.rows.items[1].message, None); |
| 1581 | } |
| 1582 | |
| 1583 | #[tokio::test] |
| 1584 | async fn the_second_page_of_branches_starts_where_the_first_ended() { |
| 1585 | let f = fixture(Visibility::Public).await; |
| 1586 | let mut queries = InMemoryGitQuery::new(); |
| 1587 | |
| 1588 | for n in 0..REF_LIMIT + 5 { |
| 1589 | queries = queries.with_branch_row(&format!("branch-{n:03}"), n == 0, at(n as u64)); |
| 1590 | } |
| 1591 | |
| 1592 | let first = list_branches( |
| 1593 | &f.handle, |
| 1594 | &repo_name(), |
| 1595 | Some(1), |
| 1596 | &f.owner, |
| 1597 | &f.orgs, |
| 1598 | &f.memberships, |
| 1599 | &f.repos, |
| 1600 | &queries, |
| 1601 | ) |
| 1602 | .await |
| 1603 | .expect("should read") |
| 1604 | .expect("visible"); |
| 1605 | let second = list_branches( |
| 1606 | &f.handle, |
| 1607 | &repo_name(), |
| 1608 | Some(2), |
| 1609 | &f.owner, |
| 1610 | &f.orgs, |
| 1611 | &f.memberships, |
| 1612 | &f.repos, |
| 1613 | &queries, |
| 1614 | ) |
| 1615 | .await |
| 1616 | .expect("should read") |
| 1617 | .expect("visible"); |
| 1618 | |
| 1619 | assert_eq!(first.rows.items.len(), REF_LIMIT); |
| 1620 | assert_eq!(second.rows.items.len(), 5); |
| 1621 | assert_eq!(second.rows.offset(), REF_LIMIT); |
| 1622 | assert_eq!(second.rows.total, REF_LIMIT + 5); |
| 1623 | assert_eq!(first.rows.items[REF_LIMIT - 1].name.as_str(), "branch-099"); |
| 1624 | assert_eq!( |
| 1625 | second.rows.items[0].name.as_str(), |
| 1626 | "branch-100", |
| 1627 | "the second page starts where the first ended" |
| 1628 | ); |
| 1629 | } |
| 1630 | |
| 1631 | #[tokio::test] |
| 1632 | async fn no_page_number_means_every_branch() { |
| 1633 | |
| 1634 | |
| 1635 | let f = fixture(Visibility::Public).await; |
| 1636 | let mut queries = InMemoryGitQuery::new(); |
| 1637 | |
| 1638 | for n in 0..REF_LIMIT + 5 { |
| 1639 | queries = queries.with_branch_row(&format!("branch-{n:03}"), n == 0, at(n as u64)); |
| 1640 | } |
| 1641 | |
| 1642 | let all = list_branches( |
| 1643 | &f.handle, |
| 1644 | &repo_name(), |
| 1645 | None, |
| 1646 | &f.owner, |
| 1647 | &f.orgs, |
| 1648 | &f.memberships, |
| 1649 | &f.repos, |
| 1650 | &queries, |
| 1651 | ) |
| 1652 | .await |
| 1653 | .expect("should read") |
| 1654 | .expect("visible"); |
| 1655 | |
| 1656 | assert_eq!(all.rows.items.len(), REF_LIMIT + 5); |
| 1657 | assert!(!all.rows.is_paged()); |
| 1658 | } |
| 1659 | |
| 1660 | #[tokio::test] |
| 1661 | async fn a_private_repositorys_branches_and_tags_are_invisible_to_a_stranger() { |
| 1662 | |
| 1663 | let f = fixture(Visibility::Private).await; |
| 1664 | let queries = InMemoryGitQuery::new() |
| 1665 | .with_branch_row("secret-work", true, at(0)) |
| 1666 | .with_tag_row("unreleased", true, at(0)); |
| 1667 | |
| 1668 | assert!( |
| 1669 | list_branches( |
| 1670 | &f.handle, |
| 1671 | &repo_name(), |
| 1672 | Some(1), |
| 1673 | &f.stranger, |
| 1674 | &f.orgs, |
| 1675 | &f.memberships, |
| 1676 | &f.repos, |
| 1677 | &queries, |
| 1678 | ) |
| 1679 | .await |
| 1680 | .expect("should read") |
| 1681 | .is_none() |
| 1682 | ); |
| 1683 | assert!( |
| 1684 | list_tags( |
| 1685 | &f.handle, |
| 1686 | &repo_name(), |
| 1687 | Some(1), |
| 1688 | &f.stranger, |
| 1689 | &f.orgs, |
| 1690 | &f.memberships, |
| 1691 | &f.repos, |
| 1692 | &queries, |
| 1693 | ) |
| 1694 | .await |
| 1695 | .expect("should read") |
| 1696 | .is_none() |
| 1697 | ); |
| 1698 | } |
| 1699 | |
| 1700 | |
| 1701 | |
| 1702 | #[tokio::test] |
| 1703 | async fn branches_and_tags_come_back_separated_and_ordered() { |
| 1704 | let f = fixture(Visibility::Public).await; |
| 1705 | let queries = InMemoryGitQuery::new() |
| 1706 | .with_branch("main") |
| 1707 | .with_branch("Feature") |
| 1708 | .with_tag("v2.0") |
| 1709 | .with_tag("v1.0"); |
| 1710 | |
| 1711 | let refs = f |
| 1712 | .refs(&f.owner, &queries) |
| 1713 | .await |
| 1714 | .expect("should read") |
| 1715 | .expect("visible"); |
| 1716 | |
| 1717 | |
| 1718 | |
| 1719 | assert_eq!( |
| 1720 | refs.branches |
| 1721 | .iter() |
| 1722 | .map(RefName::as_str) |
| 1723 | .collect::<Vec<_>>(), |
| 1724 | vec!["Feature", "main"] |
| 1725 | ); |
| 1726 | assert_eq!( |
| 1727 | refs.tags.iter().map(RefName::as_str).collect::<Vec<_>>(), |
| 1728 | vec!["v1.0", "v2.0"] |
| 1729 | ); |
| 1730 | } |
| 1731 | |
| 1732 | #[tokio::test] |
| 1733 | async fn an_empty_repository_has_no_refs_to_switch_between() { |
| 1734 | let f = fixture(Visibility::Public).await; |
| 1735 | |
| 1736 | let refs = f |
| 1737 | .refs(&f.owner, &InMemoryGitQuery::empty()) |
| 1738 | .await |
| 1739 | .expect("should read") |
| 1740 | .expect("visible"); |
| 1741 | |
| 1742 | assert!(refs.is_empty()); |
| 1743 | } |
| 1744 | |
| 1745 | #[tokio::test] |
| 1746 | async fn a_private_repositorys_refs_are_invisible_to_a_stranger() { |
| 1747 | |
| 1748 | let f = fixture(Visibility::Private).await; |
| 1749 | let queries = InMemoryGitQuery::new().with_branch("secret-work"); |
| 1750 | |
| 1751 | assert!( |
| 1752 | f.refs(&f.stranger, &queries) |
| 1753 | .await |
| 1754 | .expect("should read") |
| 1755 | .is_none() |
| 1756 | ); |
| 1757 | assert!( |
| 1758 | f.refs(&f.owner, &queries) |
| 1759 | .await |
| 1760 | .expect("should read") |
| 1761 | .is_some() |
| 1762 | ); |
| 1763 | } |
| 1764 | |
| 1765 | #[test] |
| 1766 | fn a_ref_list_knows_the_revision_it_is_showing() { |
| 1767 | let refs = RefList { |
| 1768 | branches: vec![RefName::from_trusted("main")], |
| 1769 | tags: vec![RefName::from_trusted("v1.0")], |
| 1770 | }; |
| 1771 | |
| 1772 | assert!(refs.contains(&rev("main"))); |
| 1773 | assert!(refs.contains(&rev("v1.0"))); |
| 1774 | |
| 1775 | |
| 1776 | assert!(!refs.contains(&rev("0123456789abcdef0123456789abcdef01234567"))); |
| 1777 | } |
| 1778 | |
| 1779 | |
| 1780 | |
| 1781 | #[tokio::test] |
| 1782 | async fn a_text_file_comes_back_with_its_own_name() { |
| 1783 | let f = fixture(Visibility::Public).await; |
| 1784 | let queries = InMemoryGitQuery::new().with_blob("main", "src/main.rs", b"fn main() {}\n"); |
| 1785 | |
| 1786 | let raw = f |
| 1787 | .raw(&f.owner, &path("src/main.rs"), &queries) |
| 1788 | .await |
| 1789 | .expect("should read") |
| 1790 | .expect("found"); |
| 1791 | |
| 1792 | assert_eq!( |
| 1793 | raw, |
| 1794 | RawFile::Ready { |
| 1795 | name: "main.rs".to_owned(), |
| 1796 | content: b"fn main() {}\n".to_vec(), |
| 1797 | } |
| 1798 | ); |
| 1799 | } |
| 1800 | |
| 1801 | #[tokio::test] |
| 1802 | async fn a_binary_file_comes_back_byte_for_byte() { |
| 1803 | |
| 1804 | let f = fixture(Visibility::Public).await; |
| 1805 | let bytes: Vec<u8> = (0..=255u8).chain(0..=255u8).collect(); |
| 1806 | let queries = InMemoryGitQuery::new().with_blob("main", "logo.png", bytes.clone()); |
| 1807 | |
| 1808 | let raw = f |
| 1809 | .raw(&f.owner, &path("logo.png"), &queries) |
| 1810 | .await |
| 1811 | .expect("should read") |
| 1812 | .expect("found"); |
| 1813 | |
| 1814 | match raw { |
| 1815 | RawFile::Ready { content, .. } => assert_eq!(content, bytes), |
| 1816 | other => panic!("expected the bytes, got {other:?}"), |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | #[tokio::test] |
| 1821 | async fn a_file_too_large_to_hold_in_memory_is_refused_by_size() { |
| 1822 | let f = fixture(Visibility::Public).await; |
| 1823 | let size = MAX_RAW_BYTES as usize + 1; |
| 1824 | let queries = InMemoryGitQuery::new().with_blob("main", "huge.bin", vec![0u8; size]); |
| 1825 | |
| 1826 | let raw = f |
| 1827 | .raw(&f.owner, &path("huge.bin"), &queries) |
| 1828 | .await |
| 1829 | .expect("should read") |
| 1830 | .expect("found"); |
| 1831 | |
| 1832 | assert_eq!(raw, RawFile::TooLarge { size: size as u64 }); |
| 1833 | } |
| 1834 | |
| 1835 | #[tokio::test] |
| 1836 | async fn a_file_larger_than_a_page_will_render_is_still_served_raw() { |
| 1837 | |
| 1838 | |
| 1839 | let f = fixture(Visibility::Public).await; |
| 1840 | let size = MAX_BLOB_BYTES as usize + 1; |
| 1841 | let queries = InMemoryGitQuery::new().with_blob("main", "big.txt", vec![b'x'; size]); |
| 1842 | |
| 1843 | let raw = f |
| 1844 | .raw(&f.owner, &path("big.txt"), &queries) |
| 1845 | .await |
| 1846 | .expect("should read") |
| 1847 | .expect("found"); |
| 1848 | |
| 1849 | match raw { |
| 1850 | RawFile::Ready { content, .. } => assert_eq!(content.len(), size), |
| 1851 | other => panic!("expected the bytes, got {other:?}"), |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | #[tokio::test] |
| 1856 | async fn a_directory_has_no_raw_bytes() { |
| 1857 | |
| 1858 | |
| 1859 | let f = fixture(Visibility::Public).await; |
| 1860 | let queries = InMemoryGitQuery::new() |
| 1861 | .with_tree("main", "src", Vec::new()) |
| 1862 | .with_blob("main", "src/main.rs", b"fn main() {}\n"); |
| 1863 | |
| 1864 | assert!( |
| 1865 | f.raw(&f.owner, &path("src"), &queries) |
| 1866 | .await |
| 1867 | .expect("should read") |
| 1868 | .is_none() |
| 1869 | ); |
| 1870 | } |
| 1871 | |
| 1872 | #[tokio::test] |
| 1873 | async fn a_path_that_is_not_there_is_not_found() { |
| 1874 | let f = fixture(Visibility::Public).await; |
| 1875 | let queries = InMemoryGitQuery::new(); |
| 1876 | |
| 1877 | assert!( |
| 1878 | f.raw(&f.owner, &path("nope.txt"), &queries) |
| 1879 | .await |
| 1880 | .expect("should read") |
| 1881 | .is_none() |
| 1882 | ); |
| 1883 | } |
| 1884 | |
| 1885 | #[tokio::test] |
| 1886 | async fn an_empty_repository_serves_nothing_raw() { |
| 1887 | let f = fixture(Visibility::Public).await; |
| 1888 | |
| 1889 | assert!( |
| 1890 | f.raw(&f.owner, &path("README.md"), &InMemoryGitQuery::empty()) |
| 1891 | .await |
| 1892 | .expect("should read") |
| 1893 | .is_none() |
| 1894 | ); |
| 1895 | } |
| 1896 | |
| 1897 | #[tokio::test] |
| 1898 | async fn a_private_repositorys_files_are_invisible_to_a_stranger() { |
| 1899 | |
| 1900 | |
| 1901 | let f = fixture(Visibility::Private).await; |
| 1902 | let queries = InMemoryGitQuery::new().with_blob("main", "secret.txt", b"shh\n"); |
| 1903 | |
| 1904 | assert!( |
| 1905 | f.raw(&f.stranger, &path("secret.txt"), &queries) |
| 1906 | .await |
| 1907 | .expect("should read") |
| 1908 | .is_none() |
| 1909 | ); |
| 1910 | assert!( |
| 1911 | f.raw(&f.owner, &path("secret.txt"), &queries) |
| 1912 | .await |
| 1913 | .expect("should read") |
| 1914 | .is_some() |
| 1915 | ); |
| 1916 | } |
| 1917 | } |