| 1 | |
| 2 | |
| 3 | use std::{ |
| 4 | ffi::OsStr, |
| 5 | path::PathBuf, |
| 6 | process::{Output, Stdio}, |
| 7 | sync::{Arc, Mutex}, |
| 8 | }; |
| 9 | |
| 10 | use crate::{ |
| 11 | application::port::{GitImport, GitImportError}, |
| 12 | domain::{ImportSource, OrgName, RepoName, is_public}, |
| 13 | infrastructure::metrics::metrics, |
| 14 | }; |
| 15 | |
| 16 | use super::{count_spawn, git_command}; |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | const IMPORT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(600); |
| 28 | |
| 29 | |
| 30 | |
| 31 | const HTTPS_PORT: u16 = 443; |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | #[derive(Debug, Clone)] |
| 41 | pub struct DiskGitImport { |
| 42 | data_dir: PathBuf, |
| 43 | } |
| 44 | |
| 45 | impl DiskGitImport { |
| 46 | pub fn new(data_dir: impl Into<PathBuf>) -> Self { |
| 47 | Self { |
| 48 | data_dir: data_dir.into(), |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | |
| 53 | fn repo_path(&self, handle: &OrgName, name: &RepoName) -> PathBuf { |
| 54 | self.data_dir |
| 55 | .join(handle.as_str()) |
| 56 | .join(format!("{name}.git")) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl GitImport for DiskGitImport { |
| 61 | async fn mirror( |
| 62 | &self, |
| 63 | handle: &OrgName, |
| 64 | name: &RepoName, |
| 65 | source: &ImportSource, |
| 66 | ) -> Result<(), GitImportError> { |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | metrics().record_import_started(); |
| 75 | |
| 76 | let outcome = self.mirror_inner(handle, name, source).await; |
| 77 | |
| 78 | match &outcome { |
| 79 | Ok(()) => metrics().record_import_finished(), |
| 80 | Err(_) => metrics().record_import_failed(), |
| 81 | } |
| 82 | |
| 83 | outcome |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | impl DiskGitImport { |
| 88 | |
| 89 | |
| 90 | async fn mirror_inner( |
| 91 | &self, |
| 92 | handle: &OrgName, |
| 93 | name: &RepoName, |
| 94 | source: &ImportSource, |
| 95 | ) -> Result<(), GitImportError> { |
| 96 | resolve_public_host(source.host()).await?; |
| 97 | |
| 98 | let path = self.repo_path(handle, name); |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | if path.exists() { |
| 104 | return Err(GitImportError::new( |
| 105 | "something is already on disk at this repository's path", |
| 106 | )); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | run_import( |
| 114 | [ |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | OsStr::new("-c"), |
| 123 | OsStr::new("http.followRedirects=false"), |
| 124 | OsStr::new("clone"), |
| 125 | OsStr::new("--mirror"), |
| 126 | OsStr::new("--quiet"), |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | OsStr::new("--"), |
| 132 | OsStr::new(source.as_str()), |
| 133 | path.as_os_str(), |
| 134 | ], |
| 135 | None, |
| 136 | ) |
| 137 | .await?; |
| 138 | |
| 139 | finish_mirror(&path).await |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | async fn finish_mirror(path: &std::path::Path) -> Result<(), GitImportError> { |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | run_import( |
| 154 | [ |
| 155 | OsStr::new("remote"), |
| 156 | OsStr::new("remove"), |
| 157 | OsStr::new("origin"), |
| 158 | ], |
| 159 | Some(path), |
| 160 | ) |
| 161 | .await?; |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | run_import( |
| 169 | [ |
| 170 | OsStr::new("config"), |
| 171 | OsStr::new("core.logAllRefUpdates"), |
| 172 | OsStr::new("true"), |
| 173 | ], |
| 174 | Some(path), |
| 175 | ) |
| 176 | .await?; |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | promote_remote_refs(path).await |
| 183 | } |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | async fn promote_remote_refs(path: &std::path::Path) -> Result<(), GitImportError> { |
| 195 | let listed = run_import( |
| 196 | [ |
| 197 | OsStr::new("for-each-ref"), |
| 198 | OsStr::new("--format=%(objectname) %(refname)"), |
| 199 | OsStr::new("refs/remotes/"), |
| 200 | ], |
| 201 | Some(path), |
| 202 | ) |
| 203 | .await?; |
| 204 | |
| 205 | let listed = String::from_utf8_lossy(&listed.stdout); |
| 206 | |
| 207 | for line in listed.lines() { |
| 208 | let Some((id, reference)) = line.trim().split_once(' ') else { |
| 209 | continue; |
| 210 | }; |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | if let Some(branch) = reference |
| 216 | .strip_prefix("refs/remotes/origin/") |
| 217 | .filter(|branch| *branch != "HEAD") |
| 218 | { |
| 219 | run_import( |
| 220 | [ |
| 221 | OsStr::new("update-ref"), |
| 222 | OsStr::new(&format!("refs/heads/{branch}")), |
| 223 | OsStr::new(id), |
| 224 | ], |
| 225 | Some(path), |
| 226 | ) |
| 227 | .await?; |
| 228 | } |
| 229 | |
| 230 | run_import( |
| 231 | [ |
| 232 | OsStr::new("update-ref"), |
| 233 | OsStr::new("-d"), |
| 234 | OsStr::new(reference), |
| 235 | ], |
| 236 | Some(path), |
| 237 | ) |
| 238 | .await?; |
| 239 | } |
| 240 | |
| 241 | Ok(()) |
| 242 | } |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | async fn resolve_public_host(host: &str) -> Result<(), GitImportError> { |
| 261 | let refused = || { |
| 262 | GitImportError::new(format!( |
| 263 | "{host} resolves to an address that is not on the public internet" |
| 264 | )) |
| 265 | }; |
| 266 | |
| 267 | let addresses = tokio::net::lookup_host((host, HTTPS_PORT)) |
| 268 | .await |
| 269 | .map_err(|error| GitImportError::new(format!("could not resolve {host}: {error}")))?; |
| 270 | |
| 271 | let mut any = false; |
| 272 | |
| 273 | for address in addresses { |
| 274 | any = true; |
| 275 | |
| 276 | if !is_public(address.ip()) { |
| 277 | return Err(refused()); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if !any { |
| 282 | return Err(GitImportError::new(format!("{host} resolves to nothing"))); |
| 283 | } |
| 284 | |
| 285 | Ok(()) |
| 286 | } |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | async fn run_import<I, S>(args: I, repo: Option<&std::path::Path>) -> Result<Output, GitImportError> |
| 295 | where |
| 296 | I: IntoIterator<Item = S>, |
| 297 | S: AsRef<OsStr>, |
| 298 | { |
| 299 | let mut command = git_command(); |
| 300 | |
| 301 | if let Some(repo) = repo { |
| 302 | command.arg("-C").arg(repo); |
| 303 | } |
| 304 | |
| 305 | command |
| 306 | .args(args) |
| 307 | .stdin(Stdio::null()) |
| 308 | |
| 309 | |
| 310 | |
| 311 | .env("GIT_TERMINAL_PROMPT", "0") |
| 312 | |
| 313 | |
| 314 | .kill_on_drop(true); |
| 315 | count_spawn(&command); |
| 316 | |
| 317 | let output = match tokio::time::timeout(IMPORT_TIMEOUT, command.output()).await { |
| 318 | Ok(result) => { |
| 319 | result.map_err(|error| GitImportError::new(format!("could not run git: {error}")))? |
| 320 | } |
| 321 | Err(_elapsed) => { |
| 322 | return Err(GitImportError::new(format!( |
| 323 | "the import did not finish within {IMPORT_TIMEOUT:?}" |
| 324 | ))); |
| 325 | } |
| 326 | }; |
| 327 | |
| 328 | if !output.status.success() { |
| 329 | |
| 330 | |
| 331 | let stderr = String::from_utf8_lossy(&output.stderr); |
| 332 | let reason = stderr.lines().rev().find(|line| !line.trim().is_empty()); |
| 333 | |
| 334 | return Err(GitImportError::new(match reason { |
| 335 | Some(reason) => reason.trim().to_owned(), |
| 336 | None => format!("git exited with {}", output.status), |
| 337 | })); |
| 338 | } |
| 339 | |
| 340 | Ok(output) |
| 341 | } |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | #[derive(Debug, Default, Clone)] |
| 349 | pub struct InMemoryGitImport { |
| 350 | requests: Arc<Mutex<Vec<(String, String, String)>>>, |
| 351 | failure: Option<String>, |
| 352 | } |
| 353 | |
| 354 | impl InMemoryGitImport { |
| 355 | pub fn new() -> Self { |
| 356 | Self::default() |
| 357 | } |
| 358 | |
| 359 | |
| 360 | pub fn failing(reason: impl Into<String>) -> Self { |
| 361 | Self { |
| 362 | requests: Arc::default(), |
| 363 | failure: Some(reason.into()), |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | |
| 368 | pub fn requests(&self) -> Vec<(String, String, String)> { |
| 369 | self.requests.lock().expect("lock poisoned").clone() |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | impl GitImport for InMemoryGitImport { |
| 374 | async fn mirror( |
| 375 | &self, |
| 376 | handle: &OrgName, |
| 377 | name: &RepoName, |
| 378 | source: &ImportSource, |
| 379 | ) -> Result<(), GitImportError> { |
| 380 | self.requests.lock().expect("lock poisoned").push(( |
| 381 | handle.to_string(), |
| 382 | name.to_string(), |
| 383 | source.as_str().to_owned(), |
| 384 | )); |
| 385 | |
| 386 | match &self.failure { |
| 387 | Some(reason) => Err(GitImportError::new(reason.clone())), |
| 388 | None => Ok(()), |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | #[cfg(test)] |
| 394 | mod tests { |
| 395 | use tempfile::TempDir; |
| 396 | |
| 397 | use super::*; |
| 398 | use crate::application::port::GitStorage; |
| 399 | use crate::infrastructure::git::fixture::{git_says, handle, repo_name, storage}; |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | fn source_repo() -> (TempDir, PathBuf) { |
| 406 | let dir = TempDir::new().expect("temp dir"); |
| 407 | let path = dir.path().join("source"); |
| 408 | |
| 409 | std::fs::create_dir(&path).expect("create"); |
| 410 | |
| 411 | let run = |args: &[&str]| { |
| 412 | let output = std::process::Command::new("git") |
| 413 | .arg("-C") |
| 414 | .arg(&path) |
| 415 | .args(args) |
| 416 | .env("GIT_CONFIG_GLOBAL", "/dev/null") |
| 417 | .env("GIT_CONFIG_SYSTEM", "/dev/null") |
| 418 | .env("GIT_AUTHOR_NAME", "Ada") |
| 419 | .env("GIT_AUTHOR_EMAIL", "ada@example.com") |
| 420 | .env("GIT_COMMITTER_NAME", "Ada") |
| 421 | .env("GIT_COMMITTER_EMAIL", "ada@example.com") |
| 422 | .output() |
| 423 | .expect("git should be on PATH"); |
| 424 | assert!( |
| 425 | output.status.success(), |
| 426 | "git {args:?} failed: {}", |
| 427 | String::from_utf8_lossy(&output.stderr) |
| 428 | ); |
| 429 | }; |
| 430 | |
| 431 | run(&["init", "--quiet", "--initial-branch=main"]); |
| 432 | std::fs::write(path.join("README.md"), "# source\n").expect("write"); |
| 433 | run(&["add", "README.md"]); |
| 434 | run(&["commit", "--quiet", "-m", "first"]); |
| 435 | run(&["tag", "-a", "v0.1.0", "-m", "first"]); |
| 436 | run(&["checkout", "--quiet", "-b", "side"]); |
| 437 | std::fs::write(path.join("more.md"), "more\n").expect("write"); |
| 438 | run(&["add", "more.md"]); |
| 439 | run(&["commit", "--quiet", "-m", "second"]); |
| 440 | run(&["checkout", "--quiet", "main"]); |
| 441 | |
| 442 | (dir, path) |
| 443 | } |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | #[tokio::test] |
| 452 | async fn a_mirror_lands_as_an_ordinary_bare_repository() { |
| 453 | let (_source_dir, source) = source_repo(); |
| 454 | let (dest_dir, _storage) = storage(); |
| 455 | let destination = dest_dir.path().join("mirrored.git"); |
| 456 | |
| 457 | run_import( |
| 458 | [ |
| 459 | OsStr::new("clone"), |
| 460 | OsStr::new("--mirror"), |
| 461 | OsStr::new("--quiet"), |
| 462 | OsStr::new("--"), |
| 463 | source.as_os_str(), |
| 464 | destination.as_os_str(), |
| 465 | ], |
| 466 | None, |
| 467 | ) |
| 468 | .await |
| 469 | .expect("the clone should succeed"); |
| 470 | |
| 471 | finish_mirror(&destination) |
| 472 | .await |
| 473 | .expect("the mirror should finish"); |
| 474 | |
| 475 | |
| 476 | let branches = git_says( |
| 477 | &destination, |
| 478 | &["for-each-ref", "--format=%(refname)", "refs/heads/"], |
| 479 | ); |
| 480 | assert_eq!(branches, "refs/heads/main\nrefs/heads/side"); |
| 481 | |
| 482 | |
| 483 | assert_eq!( |
| 484 | git_says( |
| 485 | &destination, |
| 486 | &["for-each-ref", "--format=%(refname)", "refs/tags/"] |
| 487 | ), |
| 488 | "refs/tags/v0.1.0" |
| 489 | ); |
| 490 | |
| 491 | |
| 492 | assert_eq!( |
| 493 | git_says( |
| 494 | &destination, |
| 495 | &["for-each-ref", "--format=%(refname)", "refs/remotes/"] |
| 496 | ), |
| 497 | "" |
| 498 | ); |
| 499 | assert_eq!(git_says(&destination, &["remote"]), ""); |
| 500 | |
| 501 | |
| 502 | assert_eq!( |
| 503 | git_says(&destination, &["symbolic-ref", "HEAD"]), |
| 504 | "refs/heads/main" |
| 505 | ); |
| 506 | assert_eq!( |
| 507 | git_says(&destination, &["rev-parse", "--is-bare-repository"]), |
| 508 | "true" |
| 509 | ); |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | assert_eq!( |
| 515 | git_says(&destination, &["config", "--get", "core.logAllRefUpdates"]), |
| 516 | "true" |
| 517 | ); |
| 518 | } |
| 519 | |
| 520 | |
| 521 | |
| 522 | #[tokio::test] |
| 523 | async fn branches_left_under_refs_remotes_are_moved_to_refs_heads() { |
| 524 | let (_source_dir, source) = source_repo(); |
| 525 | let (dest_dir, _storage) = storage(); |
| 526 | let destination = dest_dir.path().join("remotes.git"); |
| 527 | |
| 528 | |
| 529 | |
| 530 | run_import( |
| 531 | [ |
| 532 | OsStr::new("clone"), |
| 533 | OsStr::new("--bare"), |
| 534 | OsStr::new("--quiet"), |
| 535 | OsStr::new("--"), |
| 536 | source.as_os_str(), |
| 537 | destination.as_os_str(), |
| 538 | ], |
| 539 | None, |
| 540 | ) |
| 541 | .await |
| 542 | .expect("clone"); |
| 543 | |
| 544 | |
| 545 | run_import( |
| 546 | [ |
| 547 | OsStr::new("update-ref"), |
| 548 | OsStr::new("refs/remotes/origin/side"), |
| 549 | OsStr::new("refs/heads/main"), |
| 550 | ], |
| 551 | Some(&destination), |
| 552 | ) |
| 553 | .await |
| 554 | .expect("stage a remote-tracking ref"); |
| 555 | run_import( |
| 556 | [ |
| 557 | OsStr::new("symbolic-ref"), |
| 558 | OsStr::new("refs/remotes/origin/HEAD"), |
| 559 | OsStr::new("refs/remotes/origin/side"), |
| 560 | ], |
| 561 | Some(&destination), |
| 562 | ) |
| 563 | .await |
| 564 | .expect("stage origin/HEAD"); |
| 565 | |
| 566 | finish_mirror(&destination).await.expect("finish"); |
| 567 | |
| 568 | let branches = git_says( |
| 569 | &destination, |
| 570 | &["for-each-ref", "--format=%(refname)", "refs/heads/"], |
| 571 | ); |
| 572 | assert!( |
| 573 | branches.contains("refs/heads/side"), |
| 574 | "expected the remote-tracking branch to become a branch, got {branches:?}" |
| 575 | ); |
| 576 | assert_eq!( |
| 577 | git_says( |
| 578 | &destination, |
| 579 | &["for-each-ref", "--format=%(refname)", "refs/remotes/"] |
| 580 | ), |
| 581 | "", |
| 582 | "origin/HEAD is a pointer, not a branch, and must not survive either" |
| 583 | ); |
| 584 | } |
| 585 | |
| 586 | |
| 587 | |
| 588 | #[tokio::test] |
| 589 | async fn a_host_that_resolves_to_loopback_is_refused() { |
| 590 | let error = resolve_public_host("localhost") |
| 591 | .await |
| 592 | .expect_err("localhost must never be dialled"); |
| 593 | |
| 594 | assert!( |
| 595 | error.message().contains("not on the public internet"), |
| 596 | "{error}" |
| 597 | ); |
| 598 | } |
| 599 | |
| 600 | #[tokio::test] |
| 601 | async fn a_host_that_does_not_resolve_is_a_failed_import_not_a_panic() { |
| 602 | let error = resolve_public_host("no-such-host.invalid") |
| 603 | .await |
| 604 | .expect_err("`.invalid` never resolves"); |
| 605 | |
| 606 | assert!(error.message().contains("no-such-host.invalid"), "{error}"); |
| 607 | } |
| 608 | |
| 609 | |
| 610 | #[tokio::test] |
| 611 | async fn a_failed_git_command_carries_gits_own_last_word() { |
| 612 | let (dir, _storage) = storage(); |
| 613 | |
| 614 | let error = run_import( |
| 615 | [OsStr::new("rev-parse"), OsStr::new("HEAD")], |
| 616 | Some(dir.path()), |
| 617 | ) |
| 618 | .await |
| 619 | .expect_err("an empty directory is not a repository"); |
| 620 | |
| 621 | assert!( |
| 622 | error.message().contains("not a git repository"), |
| 623 | "expected git's own words, got {error}" |
| 624 | ); |
| 625 | } |
| 626 | |
| 627 | #[tokio::test] |
| 628 | async fn an_import_refuses_a_path_that_is_already_occupied() { |
| 629 | let (dir, storage) = storage(); |
| 630 | let importer = DiskGitImport::new(dir.path()); |
| 631 | |
| 632 | storage |
| 633 | .init_bare(&handle(), &repo_name("taken")) |
| 634 | .await |
| 635 | .expect("seed"); |
| 636 | |
| 637 | let error = importer |
| 638 | .mirror( |
| 639 | &handle(), |
| 640 | &repo_name("taken"), |
| 641 | &ImportSource::new("https://example.com/taken.git").expect("valid"), |
| 642 | ) |
| 643 | .await |
| 644 | .expect_err("must not adopt what is already there"); |
| 645 | |
| 646 | assert!(error.message().contains("already on disk"), "{error}"); |
| 647 | } |
| 648 | |
| 649 | #[tokio::test] |
| 650 | async fn the_in_memory_importer_records_what_it_was_asked_for() { |
| 651 | let importer = InMemoryGitImport::new(); |
| 652 | let source = ImportSource::new("https://example.com/thing.git").expect("valid"); |
| 653 | |
| 654 | importer |
| 655 | .mirror(&handle(), &repo_name("thing"), &source) |
| 656 | .await |
| 657 | .expect("succeeds"); |
| 658 | |
| 659 | assert_eq!( |
| 660 | importer.requests(), |
| 661 | vec![( |
| 662 | "jamesgill".to_owned(), |
| 663 | "thing".to_owned(), |
| 664 | "https://example.com/thing.git".to_owned() |
| 665 | )] |
| 666 | ); |
| 667 | } |
| 668 | } |