| 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 | access::{Stores, visible_repo}, |
| 17 | error::Result, |
| 18 | paging::{Paged, clamp, offset, page_count}, |
| 19 | port::{Blob, GitQuery}, |
| 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 | pub async fn browse_repo( |
| 146 | handle: &OrgName, |
| 147 | name: &RepoName, |
| 148 | rev: Option<&RefName>, |
| 149 | path: &RepoPath, |
| 150 | actor: &Actor, |
| 151 | stores: &Stores<'_, impl OrgRepository, impl MembershipRepository, impl RepoRepository>, |
| 152 | queries: &impl GitQuery, |
| 153 | ) -> Result<Option<Browsed>> { |
| 154 | if visible_repo(actor, handle, name, stores).await?.is_none() { |
| 155 | return Ok(None); |
| 156 | } |
| 157 | |
| 158 | let Some(rev) = resolve_revision(handle, name, rev, queries).await? else { |
| 159 | return Ok(Some(Browsed::Empty)); |
| 160 | }; |
| 161 | |
| 162 | |
| 163 | if let Some(mut entries) = queries.list_tree(handle, name, &rev, path).await? { |
| 164 | entries.sort_by_key(crate::domain::TreeEntry::ordering_key); |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | let names: Vec<String> = entries.iter().map(|entry| entry.name.clone()).collect(); |
| 171 | let history = queries |
| 172 | .last_commits(handle, name, &rev, path, &names) |
| 173 | .await?; |
| 174 | |
| 175 | let details = entry_details(handle, name, &rev, path, &entries, queries).await?; |
| 176 | |
| 177 | return Ok(Some(Browsed::Directory { |
| 178 | rev, |
| 179 | path: path.clone(), |
| 180 | entries, |
| 181 | history, |
| 182 | details, |
| 183 | })); |
| 184 | } |
| 185 | |
| 186 | let Some(blob) = queries |
| 187 | .read_blob(handle, name, &rev, path, MAX_BLOB_BYTES) |
| 188 | .await? |
| 189 | else { |
| 190 | return Ok(None); |
| 191 | }; |
| 192 | |
| 193 | Ok(Some(Browsed::File { |
| 194 | rev, |
| 195 | path: path.clone(), |
| 196 | file: view_of(blob), |
| 197 | })) |
| 198 | } |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | async fn entry_details( |
| 206 | handle: &OrgName, |
| 207 | name: &RepoName, |
| 208 | rev: &RefName, |
| 209 | path: &RepoPath, |
| 210 | entries: &[crate::domain::TreeEntry], |
| 211 | queries: &impl GitQuery, |
| 212 | ) -> Result<HashMap<String, EntryDetail>> { |
| 213 | use crate::domain::EntryKind; |
| 214 | |
| 215 | let mut details: HashMap<String, EntryDetail> = HashMap::new(); |
| 216 | |
| 217 | if entries |
| 218 | .iter() |
| 219 | .any(|entry| entry.kind == EntryKind::Submodule) |
| 220 | { |
| 221 | |
| 222 | |
| 223 | let modules = RepoPath::from_trusted(".gitmodules"); |
| 224 | let text = queries |
| 225 | .read_blob(handle, name, rev, &modules, MAX_GITMODULES_BYTES) |
| 226 | .await? |
| 227 | .and_then(|blob| blob.content) |
| 228 | .and_then(|bytes| String::from_utf8(bytes).ok()) |
| 229 | .unwrap_or_default(); |
| 230 | |
| 231 | let urls = parse_gitmodules(&text); |
| 232 | |
| 233 | for entry in entries |
| 234 | .iter() |
| 235 | .filter(|entry| entry.kind == EntryKind::Submodule) |
| 236 | { |
| 237 | |
| 238 | |
| 239 | let full = path.join(&entry.name); |
| 240 | |
| 241 | if let Some((_, url)) = urls.iter().find(|(at, _)| at == full.as_str()) { |
| 242 | details.entry(entry.name.clone()).or_default().url = Some(url.clone()); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | let links: Vec<&crate::domain::TreeEntry> = entries |
| 248 | .iter() |
| 249 | .filter(|entry| entry.kind == EntryKind::Symlink) |
| 250 | .collect(); |
| 251 | |
| 252 | if !links.is_empty() && links.len() <= MAX_SYMLINK_TARGETS { |
| 253 | let ids: Vec<ObjectId> = links.iter().map(|entry| entry.id.clone()).collect(); |
| 254 | let targets = queries.symlink_targets(handle, name, &ids).await?; |
| 255 | |
| 256 | for entry in links { |
| 257 | let Some(target) = targets.get(&entry.id) else { |
| 258 | continue; |
| 259 | }; |
| 260 | |
| 261 | let detail = details.entry(entry.name.clone()).or_default(); |
| 262 | |
| 263 | |
| 264 | |
| 265 | detail.resolved = resolve_relative(path, target); |
| 266 | detail.target = Some(target.clone()); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | Ok(details) |
| 271 | } |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | pub fn parse_gitmodules(text: &str) -> Vec<(String, String)> { |
| 284 | let mut found: Vec<(String, String)> = Vec::new(); |
| 285 | let mut path: Option<String> = None; |
| 286 | let mut url: Option<String> = None; |
| 287 | |
| 288 | |
| 289 | |
| 290 | fn bank( |
| 291 | found: &mut Vec<(String, String)>, |
| 292 | path: &mut Option<String>, |
| 293 | url: &mut Option<String>, |
| 294 | ) { |
| 295 | if let (Some(path), Some(url)) = (path.take(), url.take()) |
| 296 | && url.starts_with("https://") |
| 297 | && !path.is_empty() |
| 298 | { |
| 299 | found.push((path, url)); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | for line in text.lines() { |
| 304 | let line = line.trim(); |
| 305 | |
| 306 | if line.is_empty() || line.starts_with('#') || line.starts_with(';') { |
| 307 | continue; |
| 308 | } |
| 309 | |
| 310 | if line.starts_with('[') { |
| 311 | bank(&mut found, &mut path, &mut url); |
| 312 | continue; |
| 313 | } |
| 314 | |
| 315 | let Some((key, value)) = line.split_once('=') else { |
| 316 | continue; |
| 317 | }; |
| 318 | |
| 319 | |
| 320 | |
| 321 | let value = value.trim().trim_matches('"').to_owned(); |
| 322 | |
| 323 | match key.trim().to_ascii_lowercase().as_str() { |
| 324 | "path" => path = Some(value.trim_matches('/').to_owned()), |
| 325 | "url" => url = Some(value), |
| 326 | _ => {} |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | bank(&mut found, &mut path, &mut url); |
| 331 | |
| 332 | found |
| 333 | } |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | pub fn resolve_relative(dir: &RepoPath, target: &str) -> Option<RepoPath> { |
| 346 | let target = target.trim(); |
| 347 | |
| 348 | if target.is_empty() || target.starts_with('/') { |
| 349 | return None; |
| 350 | } |
| 351 | |
| 352 | let mut parts: Vec<&str> = dir.components().collect(); |
| 353 | |
| 354 | for component in target.split('/') { |
| 355 | match component { |
| 356 | "" | "." => {} |
| 357 | ".." => { |
| 358 | |
| 359 | parts.pop()?; |
| 360 | } |
| 361 | name => parts.push(name), |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | if parts.is_empty() { |
| 366 | return None; |
| 367 | } |
| 368 | |
| 369 | RepoPath::new(parts.join("/")).ok() |
| 370 | } |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | pub async fn repo_log( |
| 384 | handle: &OrgName, |
| 385 | name: &RepoName, |
| 386 | rev: Option<&RefName>, |
| 387 | page: usize, |
| 388 | actor: &Actor, |
| 389 | stores: &Stores<'_, impl OrgRepository, impl MembershipRepository, impl RepoRepository>, |
| 390 | queries: &impl GitQuery, |
| 391 | ) -> Result<Option<Paged<CommitSummary>>> { |
| 392 | if visible_repo(actor, handle, name, stores).await?.is_none() { |
| 393 | return Ok(None); |
| 394 | } |
| 395 | |
| 396 | let Some(rev) = resolve_revision(handle, name, rev, queries).await? else { |
| 397 | return Ok(Some(Paged::of(Vec::new(), 0, 1, LOG_LIMIT))); |
| 398 | }; |
| 399 | |
| 400 | let total = queries.count_commits(handle, name, &rev).await? as usize; |
| 401 | |
| 402 | |
| 403 | let page = clamp(page, page_count(total, LOG_LIMIT)); |
| 404 | |
| 405 | let commits = queries |
| 406 | .log(handle, name, &rev, offset(page, LOG_LIMIT), LOG_LIMIT) |
| 407 | .await?; |
| 408 | |
| 409 | Ok(Some(Paged::of(commits, total, page, LOG_LIMIT))) |
| 410 | } |
| 411 | |
| 412 | |
| 413 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 414 | pub struct PathLog { |
| 415 | pub rev: RefName, |
| 416 | pub path: RepoPath, |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | pub is_file: bool, |
| 424 | pub commits: Paged<CommitSummary>, |
| 425 | |
| 426 | |
| 427 | |
| 428 | pub capped: bool, |
| 429 | } |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | #[allow(clippy::too_many_arguments)] |
| 444 | pub async fn repo_path_log( |
| 445 | handle: &OrgName, |
| 446 | name: &RepoName, |
| 447 | rev: Option<&RefName>, |
| 448 | path: &RepoPath, |
| 449 | page: usize, |
| 450 | actor: &Actor, |
| 451 | stores: &Stores<'_, impl OrgRepository, impl MembershipRepository, impl RepoRepository>, |
| 452 | queries: &impl GitQuery, |
| 453 | ) -> Result<Option<PathLog>> { |
| 454 | if visible_repo(actor, handle, name, stores).await?.is_none() { |
| 455 | return Ok(None); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | |
| 460 | let Some(rev) = resolve_revision(handle, name, rev, queries).await? else { |
| 461 | return Ok(None); |
| 462 | }; |
| 463 | |
| 464 | |
| 465 | |
| 466 | let is_file = if queries.list_tree(handle, name, &rev, path).await?.is_some() { |
| 467 | false |
| 468 | } else { |
| 469 | |
| 470 | |
| 471 | let Some(_) = queries.read_blob(handle, name, &rev, path, 0).await? else { |
| 472 | return Ok(None); |
| 473 | }; |
| 474 | |
| 475 | true |
| 476 | }; |
| 477 | |
| 478 | let counted = queries |
| 479 | .count_commits_at_path(handle, name, &rev, path, is_file) |
| 480 | .await?; |
| 481 | let total = counted.total as usize; |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | let page = clamp(page, page_count(total, LOG_LIMIT)); |
| 487 | |
| 488 | let commits = queries |
| 489 | .log_at_path( |
| 490 | handle, |
| 491 | name, |
| 492 | &rev, |
| 493 | path, |
| 494 | is_file, |
| 495 | offset(page, LOG_LIMIT), |
| 496 | LOG_LIMIT, |
| 497 | ) |
| 498 | .await?; |
| 499 | |
| 500 | Ok(Some(PathLog { |
| 501 | rev, |
| 502 | path: path.clone(), |
| 503 | is_file, |
| 504 | commits: Paged::of(commits, total, page, LOG_LIMIT), |
| 505 | capped: counted.capped, |
| 506 | })) |
| 507 | } |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 515 | pub struct RefList { |
| 516 | pub branches: Vec<RefName>, |
| 517 | pub tags: Vec<RefName>, |
| 518 | } |
| 519 | |
| 520 | impl RefList { |
| 521 | pub fn is_empty(&self) -> bool { |
| 522 | self.branches.is_empty() && self.tags.is_empty() |
| 523 | } |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | pub fn contains(&self, rev: &RefName) -> bool { |
| 530 | self.branches |
| 531 | .iter() |
| 532 | .chain(&self.tags) |
| 533 | .any(|name| name == rev) |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | pub async fn list_refs( |
| 551 | handle: &OrgName, |
| 552 | name: &RepoName, |
| 553 | actor: &Actor, |
| 554 | stores: &Stores<'_, impl OrgRepository, impl MembershipRepository, impl RepoRepository>, |
| 555 | queries: &impl GitQuery, |
| 556 | ) -> Result<Option<RefList>> { |
| 557 | if visible_repo(actor, handle, name, stores).await?.is_none() { |
| 558 | return Ok(None); |
| 559 | } |
| 560 | |
| 561 | Ok(Some(organise_refs(queries.list_refs(handle, name).await?))) |
| 562 | } |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | pub fn organise_refs(refs: Vec<crate::domain::GitRef>) -> RefList { |
| 570 | let mut list = RefList::default(); |
| 571 | |
| 572 | for git_ref in refs { |
| 573 | match git_ref.kind { |
| 574 | RefKind::Branch => list.branches.push(git_ref.name), |
| 575 | RefKind::Tag => list.tags.push(git_ref.name), |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | for names in [&mut list.branches, &mut list.tags] { |
| 580 | names.sort_by(|left, right| { |
| 581 | left.as_str() |
| 582 | .to_lowercase() |
| 583 | .cmp(&right.as_str().to_lowercase()) |
| 584 | .then_with(|| left.as_str().cmp(right.as_str())) |
| 585 | }); |
| 586 | } |
| 587 | |
| 588 | list |
| 589 | } |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 600 | pub struct RefPage<Row> { |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | pub rows: Paged<Row>, |
| 607 | |
| 608 | pub repo_is_empty: bool, |
| 609 | } |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | fn one_page<Row>(rows: Vec<Row>, page: Option<usize>) -> Paged<Row> { |
| 617 | match page { |
| 618 | Some(page) => Paged::slice(rows, page, REF_LIMIT), |
| 619 | None => { |
| 620 | let size = rows.len().max(1); |
| 621 | Paged::slice(rows, 1, size) |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | pub async fn list_branches( |
| 640 | handle: &OrgName, |
| 641 | name: &RepoName, |
| 642 | page: Option<usize>, |
| 643 | actor: &Actor, |
| 644 | stores: &Stores<'_, impl OrgRepository, impl MembershipRepository, impl RepoRepository>, |
| 645 | queries: &impl GitQuery, |
| 646 | ) -> Result<Option<RefPage<BranchRow>>> { |
| 647 | if visible_repo(actor, handle, name, stores).await?.is_none() { |
| 648 | return Ok(None); |
| 649 | } |
| 650 | |
| 651 | let rows = pin_default(queries.branches(handle, name).await?); |
| 652 | let empty = rows.is_empty() && !has_commits(handle, name, queries).await?; |
| 653 | |
| 654 | Ok(Some(RefPage { |
| 655 | rows: one_page(rows, page), |
| 656 | repo_is_empty: empty, |
| 657 | })) |
| 658 | } |
| 659 | |
| 660 | |
| 661 | pub async fn list_tags( |
| 662 | handle: &OrgName, |
| 663 | name: &RepoName, |
| 664 | page: Option<usize>, |
| 665 | actor: &Actor, |
| 666 | stores: &Stores<'_, impl OrgRepository, impl MembershipRepository, impl RepoRepository>, |
| 667 | queries: &impl GitQuery, |
| 668 | ) -> Result<Option<RefPage<TagRow>>> { |
| 669 | if visible_repo(actor, handle, name, stores).await?.is_none() { |
| 670 | return Ok(None); |
| 671 | } |
| 672 | |
| 673 | let rows = queries.tags(handle, name).await?; |
| 674 | let empty = rows.is_empty() && !has_commits(handle, name, queries).await?; |
| 675 | |
| 676 | Ok(Some(RefPage { |
| 677 | rows: one_page(rows, page), |
| 678 | repo_is_empty: empty, |
| 679 | })) |
| 680 | } |
| 681 | |
| 682 | |
| 683 | async fn has_commits(handle: &OrgName, name: &RepoName, queries: &impl GitQuery) -> Result<bool> { |
| 684 | Ok(queries.default_branch(handle, name).await?.is_some()) |
| 685 | } |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | fn pin_default(rows: Vec<BranchRow>) -> Vec<BranchRow> { |
| 697 | let (default, rest): (Vec<_>, Vec<_>) = rows.into_iter().partition(|row| row.is_default); |
| 698 | |
| 699 | default.into_iter().chain(rest).collect() |
| 700 | } |
| 701 | |
| 702 | |
| 703 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 704 | pub enum RawFile { |
| 705 | Ready { |
| 706 | |
| 707 | name: String, |
| 708 | content: Vec<u8>, |
| 709 | }, |
| 710 | |
| 711 | |
| 712 | TooLarge { size: u64 }, |
| 713 | } |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | pub async fn read_raw_file( |
| 725 | handle: &OrgName, |
| 726 | name: &RepoName, |
| 727 | rev: Option<&RefName>, |
| 728 | path: &RepoPath, |
| 729 | actor: &Actor, |
| 730 | stores: &Stores<'_, impl OrgRepository, impl MembershipRepository, impl RepoRepository>, |
| 731 | queries: &impl GitQuery, |
| 732 | ) -> Result<Option<RawFile>> { |
| 733 | if visible_repo(actor, handle, name, stores).await?.is_none() { |
| 734 | return Ok(None); |
| 735 | } |
| 736 | |
| 737 | let Some(rev) = resolve_revision(handle, name, rev, queries).await? else { |
| 738 | return Ok(None); |
| 739 | }; |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | let Some(blob) = queries |
| 746 | .read_blob(handle, name, &rev, path, MAX_RAW_BYTES) |
| 747 | .await? |
| 748 | else { |
| 749 | return Ok(None); |
| 750 | }; |
| 751 | |
| 752 | let Some(content) = blob.content else { |
| 753 | return Ok(Some(RawFile::TooLarge { size: blob.size })); |
| 754 | }; |
| 755 | |
| 756 | Ok(Some(RawFile::Ready { |
| 757 | |
| 758 | |
| 759 | name: path.file_name().unwrap_or_default().to_owned(), |
| 760 | content, |
| 761 | })) |
| 762 | } |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | pub(crate) async fn resolve_revision( |
| 769 | handle: &OrgName, |
| 770 | name: &RepoName, |
| 771 | rev: Option<&RefName>, |
| 772 | queries: &impl GitQuery, |
| 773 | ) -> Result<Option<RefName>> { |
| 774 | match rev { |
| 775 | Some(rev) => Ok(Some(rev.clone())), |
| 776 | None => Ok(queries.default_branch(handle, name).await?), |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | fn view_of(blob: Blob) -> FileView { |
| 785 | let too_large = blob.content.is_none(); |
| 786 | let text = blob.content.and_then(|bytes| String::from_utf8(bytes).ok()); |
| 787 | |
| 788 | FileView { |
| 789 | id: blob.id, |
| 790 | size: blob.size, |
| 791 | text, |
| 792 | too_large, |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | #[cfg(test)] |
| 797 | #[path = "browse_tests.rs"] |
| 798 | mod tests; |