| 1 | |
| 2 | |
| 3 | use std::{ |
| 4 | path::PathBuf, |
| 5 | process::Stdio, |
| 6 | sync::{Arc, Mutex}, |
| 7 | time::SystemTime, |
| 8 | }; |
| 9 | |
| 10 | use crate::{ |
| 11 | application::port::{CommitTreeRequest, GitMerge, GitMergeError}, |
| 12 | domain::{ObjectId, OrgName, RefName, RepoName}, |
| 13 | }; |
| 14 | |
| 15 | use super::{count_spawn, git_command}; |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | const MERGE_TREE_SINCE: (u32, u32) = (2, 38); |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | pub(super) const CURLOPT_RESOLVE_SINCE: (u32, u32) = (2, 37); |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 50 | pub struct GitFeatures { |
| 51 | |
| 52 | |
| 53 | pub merge_tree: bool, |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | pub pinned_resolve: bool, |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | pub version: Option<(u32, u32)>, |
| 71 | } |
| 72 | |
| 73 | impl GitFeatures { |
| 74 | |
| 75 | #[must_use] |
| 76 | pub fn version_string(&self) -> Option<String> { |
| 77 | self.version |
| 78 | .map(|(major, minor)| format!("{major}.{minor}")) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | impl GitFeatures { |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | pub async fn detect() -> Self { |
| 90 | let mut command = git_command(); |
| 91 | command.arg("--version").stdin(Stdio::null()); |
| 92 | count_spawn(&command); |
| 93 | |
| 94 | let Ok(output) = command.output().await else { |
| 95 | return Self::default(); |
| 96 | }; |
| 97 | |
| 98 | Self::of_version(parse_git_version(&String::from_utf8_lossy(&output.stdout))) |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | pub(super) fn of_version(version: Option<(u32, u32)>) -> Self { |
| 104 | Self { |
| 105 | merge_tree: version.is_some_and(|version| version >= MERGE_TREE_SINCE), |
| 106 | pinned_resolve: version.is_some_and(|version| version >= CURLOPT_RESOLVE_SINCE), |
| 107 | version, |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | fn parse_git_version(output: &str) -> Option<(u32, u32)> { |
| 117 | let version = output.split_whitespace().nth(2)?; |
| 118 | let mut parts = version.split('.'); |
| 119 | |
| 120 | Some((parts.next()?.parse().ok()?, parts.next()?.parse().ok()?)) |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | #[derive(Debug, Clone)] |
| 130 | pub struct DiskGitMerge { |
| 131 | data_dir: PathBuf, |
| 132 | } |
| 133 | |
| 134 | impl DiskGitMerge { |
| 135 | pub fn new(data_dir: impl Into<PathBuf>) -> Self { |
| 136 | Self { |
| 137 | data_dir: data_dir.into(), |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
| 142 | fn repo_path(&self, handle: &OrgName, name: &RepoName) -> PathBuf { |
| 143 | self.data_dir |
| 144 | .join(handle.as_str()) |
| 145 | .join(format!("{name}.git")) |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | async fn guarded_update( |
| 154 | &self, |
| 155 | handle: &OrgName, |
| 156 | name: &RepoName, |
| 157 | branch: &RefName, |
| 158 | new: Option<&ObjectId>, |
| 159 | old: &ObjectId, |
| 160 | ) -> Result<(), GitMergeError> { |
| 161 | let path = self.repo_path(handle, name); |
| 162 | let full = format!("refs/heads/{branch}"); |
| 163 | |
| 164 | let mut command = git_command(); |
| 165 | command |
| 166 | .arg("--git-dir") |
| 167 | .arg(&path) |
| 168 | .arg("update-ref") |
| 169 | .stdin(Stdio::null()); |
| 170 | |
| 171 | count_spawn(&command); |
| 172 | |
| 173 | match new { |
| 174 | Some(new) => { |
| 175 | command.arg(&full).arg(new.as_str()).arg(old.as_str()); |
| 176 | } |
| 177 | None => { |
| 178 | command.arg("-d").arg(&full).arg(old.as_str()); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | let output = command |
| 183 | .output() |
| 184 | .await |
| 185 | .map_err(|error| GitMergeError::backend(format!("could not run git: {error}")))?; |
| 186 | |
| 187 | if output.status.success() { |
| 188 | return Ok(()); |
| 189 | } |
| 190 | |
| 191 | let stderr = String::from_utf8_lossy(&output.stderr); |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | if stderr.contains("but expected") { |
| 199 | return Err(GitMergeError::Stale); |
| 200 | } |
| 201 | |
| 202 | Err(GitMergeError::backend(format!( |
| 203 | "git update-ref exited with {}: {}", |
| 204 | output.status, |
| 205 | stderr.trim() |
| 206 | ))) |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | impl GitMerge for DiskGitMerge { |
| 211 | async fn commit_tree( |
| 212 | &self, |
| 213 | handle: &OrgName, |
| 214 | name: &RepoName, |
| 215 | request: &CommitTreeRequest, |
| 216 | ) -> Result<ObjectId, GitMergeError> { |
| 217 | let path = self.repo_path(handle, name); |
| 218 | |
| 219 | let mut command = git_command(); |
| 220 | command |
| 221 | .arg("--git-dir") |
| 222 | .arg(&path) |
| 223 | .arg("commit-tree") |
| 224 | .arg(request.tree.as_str()); |
| 225 | |
| 226 | for parent in &request.parents { |
| 227 | command.arg("-p").arg(parent.as_str()); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | command.arg("-m").arg(&request.message); |
| 234 | |
| 235 | let stamp = git_timestamp(request.now); |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | command |
| 246 | .env("GIT_AUTHOR_NAME", &request.author_name) |
| 247 | .env("GIT_AUTHOR_EMAIL", &request.author_email) |
| 248 | .env("GIT_AUTHOR_DATE", &stamp) |
| 249 | .env("GIT_COMMITTER_NAME", &request.author_name) |
| 250 | .env("GIT_COMMITTER_EMAIL", &request.author_email) |
| 251 | .env("GIT_COMMITTER_DATE", &stamp) |
| 252 | .stdin(Stdio::null()); |
| 253 | count_spawn(&command); |
| 254 | |
| 255 | let output = command |
| 256 | .output() |
| 257 | .await |
| 258 | .map_err(|error| GitMergeError::backend(format!("could not run git: {error}")))?; |
| 259 | |
| 260 | if !output.status.success() { |
| 261 | return Err(GitMergeError::backend(format!( |
| 262 | "git commit-tree exited with {}: {}", |
| 263 | output.status, |
| 264 | String::from_utf8_lossy(&output.stderr).trim() |
| 265 | ))); |
| 266 | } |
| 267 | |
| 268 | let id = String::from_utf8_lossy(&output.stdout); |
| 269 | |
| 270 | ObjectId::new(id.trim()) |
| 271 | .map_err(|error| GitMergeError::backend(format!("git named a bad commit: {error}"))) |
| 272 | } |
| 273 | |
| 274 | async fn update_ref( |
| 275 | &self, |
| 276 | handle: &OrgName, |
| 277 | name: &RepoName, |
| 278 | branch: &RefName, |
| 279 | new: &ObjectId, |
| 280 | old: &ObjectId, |
| 281 | ) -> Result<(), GitMergeError> { |
| 282 | self.guarded_update(handle, name, branch, Some(new), old) |
| 283 | .await |
| 284 | } |
| 285 | |
| 286 | async fn delete_ref( |
| 287 | &self, |
| 288 | handle: &OrgName, |
| 289 | name: &RepoName, |
| 290 | branch: &RefName, |
| 291 | old: &ObjectId, |
| 292 | ) -> Result<(), GitMergeError> { |
| 293 | self.guarded_update(handle, name, branch, None, old).await |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | fn git_timestamp(now: SystemTime) -> String { |
| 307 | let seconds = now |
| 308 | .duration_since(SystemTime::UNIX_EPOCH) |
| 309 | .map(|elapsed| elapsed.as_secs()) |
| 310 | .unwrap_or(0); |
| 311 | |
| 312 | format!("@{seconds} +0000") |
| 313 | } |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | #[derive(Debug, Default, Clone)] |
| 322 | pub struct InMemoryGitMerge { |
| 323 | commits: Arc<Mutex<Vec<CommitTreeRequest>>>, |
| 324 | updates: Arc<Mutex<Vec<RecordedRefUpdate>>>, |
| 325 | |
| 326 | written: Option<ObjectId>, |
| 327 | |
| 328 | stale: bool, |
| 329 | } |
| 330 | |
| 331 | |
| 332 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 333 | pub struct RecordedRefUpdate { |
| 334 | pub branch: RefName, |
| 335 | pub new: Option<ObjectId>, |
| 336 | pub old: ObjectId, |
| 337 | } |
| 338 | |
| 339 | impl InMemoryGitMerge { |
| 340 | pub fn new() -> Self { |
| 341 | Self::default() |
| 342 | } |
| 343 | |
| 344 | |
| 345 | pub fn writing(id: &str) -> Self { |
| 346 | Self { |
| 347 | written: Some(ObjectId::from_trusted(format!("{id:0>40}"))), |
| 348 | ..Self::default() |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | |
| 353 | pub fn stale() -> Self { |
| 354 | Self { |
| 355 | stale: true, |
| 356 | ..Self::default() |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | |
| 361 | pub fn commits(&self) -> Vec<CommitTreeRequest> { |
| 362 | self.commits.lock().expect("lock poisoned").clone() |
| 363 | } |
| 364 | |
| 365 | |
| 366 | pub fn updates(&self) -> Vec<RecordedRefUpdate> { |
| 367 | self.updates.lock().expect("lock poisoned").clone() |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | impl GitMerge for InMemoryGitMerge { |
| 372 | async fn commit_tree( |
| 373 | &self, |
| 374 | _handle: &OrgName, |
| 375 | _name: &RepoName, |
| 376 | request: &CommitTreeRequest, |
| 377 | ) -> Result<ObjectId, GitMergeError> { |
| 378 | self.commits |
| 379 | .lock() |
| 380 | .expect("lock poisoned") |
| 381 | .push(request.clone()); |
| 382 | |
| 383 | Ok(self |
| 384 | .written |
| 385 | .clone() |
| 386 | .unwrap_or_else(|| ObjectId::from_trusted("c".repeat(40)))) |
| 387 | } |
| 388 | |
| 389 | async fn update_ref( |
| 390 | &self, |
| 391 | _handle: &OrgName, |
| 392 | _name: &RepoName, |
| 393 | branch: &RefName, |
| 394 | new: &ObjectId, |
| 395 | old: &ObjectId, |
| 396 | ) -> Result<(), GitMergeError> { |
| 397 | if self.stale { |
| 398 | return Err(GitMergeError::Stale); |
| 399 | } |
| 400 | |
| 401 | self.updates |
| 402 | .lock() |
| 403 | .expect("lock poisoned") |
| 404 | .push(RecordedRefUpdate { |
| 405 | branch: branch.clone(), |
| 406 | new: Some(new.clone()), |
| 407 | old: old.clone(), |
| 408 | }); |
| 409 | |
| 410 | Ok(()) |
| 411 | } |
| 412 | |
| 413 | async fn delete_ref( |
| 414 | &self, |
| 415 | _handle: &OrgName, |
| 416 | _name: &RepoName, |
| 417 | branch: &RefName, |
| 418 | old: &ObjectId, |
| 419 | ) -> Result<(), GitMergeError> { |
| 420 | if self.stale { |
| 421 | return Err(GitMergeError::Stale); |
| 422 | } |
| 423 | |
| 424 | self.updates |
| 425 | .lock() |
| 426 | .expect("lock poisoned") |
| 427 | .push(RecordedRefUpdate { |
| 428 | branch: branch.clone(), |
| 429 | new: None, |
| 430 | old: old.clone(), |
| 431 | }); |
| 432 | |
| 433 | Ok(()) |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | #[cfg(test)] |
| 438 | mod tests { |
| 439 | use std::path::Path; |
| 440 | |
| 441 | use tempfile::TempDir; |
| 442 | |
| 443 | use super::*; |
| 444 | use crate::infrastructure::git::fixture::{git_says, handle, repo_name}; |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | fn mergeable() -> (TempDir, DiskGitMerge, PathBuf) { |
| 454 | let dir = TempDir::new().expect("temp dir"); |
| 455 | let repo = dir.path().join("jamesgill").join("steid.git"); |
| 456 | let work = dir.path().join("work"); |
| 457 | |
| 458 | let git = |path: &Path, args: &[&str]| -> String { |
| 459 | let output = std::process::Command::new("git") |
| 460 | .arg("-C") |
| 461 | .arg(path) |
| 462 | .args(args) |
| 463 | .env("GIT_CONFIG_GLOBAL", "/dev/null") |
| 464 | .env("GIT_CONFIG_SYSTEM", "/dev/null") |
| 465 | .env("GIT_AUTHOR_NAME", "Ada Lovelace") |
| 466 | .env("GIT_AUTHOR_EMAIL", "ada@example.com") |
| 467 | .env("GIT_COMMITTER_NAME", "Ada Lovelace") |
| 468 | .env("GIT_COMMITTER_EMAIL", "ada@example.com") |
| 469 | .output() |
| 470 | .expect("git should be on PATH"); |
| 471 | |
| 472 | assert!( |
| 473 | output.status.success(), |
| 474 | "git {args:?} failed: {}", |
| 475 | String::from_utf8_lossy(&output.stderr) |
| 476 | ); |
| 477 | |
| 478 | String::from_utf8_lossy(&output.stdout).trim().to_owned() |
| 479 | }; |
| 480 | |
| 481 | std::fs::create_dir_all(&work).expect("create work tree"); |
| 482 | git( |
| 483 | dir.path(), |
| 484 | &[ |
| 485 | "init", |
| 486 | "--bare", |
| 487 | "--quiet", |
| 488 | "--template=", |
| 489 | "--initial-branch=main", |
| 490 | "--", |
| 491 | repo.to_str().expect("utf-8 fixture path"), |
| 492 | ], |
| 493 | ); |
| 494 | |
| 495 | git(&work, &["init", "--quiet", "-b", "main"]); |
| 496 | std::fs::write(work.join("README.md"), b"hello\n").expect("write"); |
| 497 | git(&work, &["add", "-A"]); |
| 498 | git(&work, &["commit", "--quiet", "-m", "first"]); |
| 499 | |
| 500 | git(&work, &["checkout", "--quiet", "-b", "tidy"]); |
| 501 | std::fs::write(work.join("other.txt"), b"tidy\n").expect("write"); |
| 502 | git(&work, &["add", "-A"]); |
| 503 | git(&work, &["commit", "--quiet", "-m", "tidy"]); |
| 504 | |
| 505 | git( |
| 506 | &work, |
| 507 | &[ |
| 508 | "push", |
| 509 | "--quiet", |
| 510 | repo.to_str().expect("utf-8"), |
| 511 | "main", |
| 512 | "tidy", |
| 513 | ], |
| 514 | ); |
| 515 | |
| 516 | let merges = DiskGitMerge::new(dir.path()); |
| 517 | |
| 518 | (dir, merges, repo) |
| 519 | } |
| 520 | |
| 521 | |
| 522 | fn head(repo: &Path, revision: &str) -> ObjectId { |
| 523 | ObjectId::new(git_says(repo, &["rev-parse", revision])).expect("a real object id") |
| 524 | } |
| 525 | |
| 526 | #[tokio::test] |
| 527 | async fn a_merge_commit_carries_both_parents_and_the_person_who_pressed_merge() { |
| 528 | let (_dir, merges, repo) = mergeable(); |
| 529 | let main = head(&repo, "main"); |
| 530 | let tidy = head(&repo, "tidy"); |
| 531 | let tree = ObjectId::new(git_says(&repo, &["rev-parse", "tidy^{tree}"])).expect("a tree"); |
| 532 | |
| 533 | let commit = merges |
| 534 | .commit_tree( |
| 535 | &handle(), |
| 536 | &repo_name("steid"), |
| 537 | &CommitTreeRequest { |
| 538 | tree, |
| 539 | parents: vec![main.clone(), tidy.clone()], |
| 540 | message: "Merge pull request #7 from tidy\n\nTidy things up.".to_owned(), |
| 541 | author_name: "Ada Lovelace".to_owned(), |
| 542 | author_email: "ada@example.com".to_owned(), |
| 543 | now: SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_700_000_000), |
| 544 | }, |
| 545 | ) |
| 546 | .await |
| 547 | .expect("should write a commit"); |
| 548 | |
| 549 | |
| 550 | assert_eq!( |
| 551 | git_says( |
| 552 | &repo, |
| 553 | &["rev-list", "--parents", "-n", "1", commit.as_str()] |
| 554 | ), |
| 555 | format!("{commit} {main} {tidy}") |
| 556 | ); |
| 557 | assert_eq!( |
| 558 | git_says( |
| 559 | &repo, |
| 560 | &["log", "-1", "--format=%an <%ae> %at", commit.as_str()] |
| 561 | ), |
| 562 | "Ada Lovelace <ada@example.com> 1700000000", |
| 563 | "the caller's `now` decides the commit's date, not git's clock" |
| 564 | ); |
| 565 | |
| 566 | assert_eq!(git_says(&repo, &["rev-parse", "main"]), main.as_str()); |
| 567 | } |
| 568 | |
| 569 | #[tokio::test] |
| 570 | async fn a_squash_commit_has_one_parent() { |
| 571 | let (_dir, merges, repo) = mergeable(); |
| 572 | let main = head(&repo, "main"); |
| 573 | let tree = ObjectId::new(git_says(&repo, &["rev-parse", "tidy^{tree}"])).expect("a tree"); |
| 574 | |
| 575 | let commit = merges |
| 576 | .commit_tree( |
| 577 | &handle(), |
| 578 | &repo_name("steid"), |
| 579 | &CommitTreeRequest { |
| 580 | tree, |
| 581 | parents: vec![main.clone()], |
| 582 | message: "Tidy things up (#7)".to_owned(), |
| 583 | author_name: "Ada Lovelace".to_owned(), |
| 584 | author_email: "ada@example.com".to_owned(), |
| 585 | now: SystemTime::UNIX_EPOCH, |
| 586 | }, |
| 587 | ) |
| 588 | .await |
| 589 | .expect("should write a commit"); |
| 590 | |
| 591 | assert_eq!( |
| 592 | git_says( |
| 593 | &repo, |
| 594 | &["rev-list", "--parents", "-n", "1", commit.as_str()] |
| 595 | ), |
| 596 | format!("{commit} {main}") |
| 597 | ); |
| 598 | } |
| 599 | |
| 600 | #[tokio::test] |
| 601 | async fn moving_a_branch_from_where_it_is_moves_it() { |
| 602 | let (_dir, merges, repo) = mergeable(); |
| 603 | let main = head(&repo, "main"); |
| 604 | let tidy = head(&repo, "tidy"); |
| 605 | |
| 606 | merges |
| 607 | .update_ref( |
| 608 | &handle(), |
| 609 | &repo_name("steid"), |
| 610 | &RefName::from_trusted("main"), |
| 611 | &tidy, |
| 612 | &main, |
| 613 | ) |
| 614 | .await |
| 615 | .expect("should move the branch"); |
| 616 | |
| 617 | assert_eq!(git_says(&repo, &["rev-parse", "main"]), tidy.as_str()); |
| 618 | } |
| 619 | |
| 620 | #[tokio::test] |
| 621 | async fn moving_a_branch_from_where_it_is_not_changes_nothing() { |
| 622 | |
| 623 | |
| 624 | let (_dir, merges, repo) = mergeable(); |
| 625 | let main = head(&repo, "main"); |
| 626 | let tidy = head(&repo, "tidy"); |
| 627 | |
| 628 | let error = merges |
| 629 | .update_ref( |
| 630 | &handle(), |
| 631 | &repo_name("steid"), |
| 632 | &RefName::from_trusted("main"), |
| 633 | &main, |
| 634 | &tidy, |
| 635 | ) |
| 636 | .await |
| 637 | .expect_err("expected the guard to refuse"); |
| 638 | |
| 639 | assert!(error.is_stale(), "expected staleness, got {error:?}"); |
| 640 | assert_eq!(git_says(&repo, &["rev-parse", "main"]), main.as_str()); |
| 641 | } |
| 642 | |
| 643 | #[tokio::test] |
| 644 | async fn deleting_a_branch_is_guarded_the_same_way() { |
| 645 | let (_dir, merges, repo) = mergeable(); |
| 646 | let main = head(&repo, "main"); |
| 647 | let tidy = head(&repo, "tidy"); |
| 648 | |
| 649 | let refused = merges |
| 650 | .delete_ref( |
| 651 | &handle(), |
| 652 | &repo_name("steid"), |
| 653 | &RefName::from_trusted("tidy"), |
| 654 | &main, |
| 655 | ) |
| 656 | .await |
| 657 | .expect_err("expected the guard to refuse"); |
| 658 | assert!(refused.is_stale(), "expected staleness, got {refused:?}"); |
| 659 | |
| 660 | merges |
| 661 | .delete_ref( |
| 662 | &handle(), |
| 663 | &repo_name("steid"), |
| 664 | &RefName::from_trusted("tidy"), |
| 665 | &tidy, |
| 666 | ) |
| 667 | .await |
| 668 | .expect("should delete the branch"); |
| 669 | |
| 670 | assert_eq!( |
| 671 | git_says( |
| 672 | &repo, |
| 673 | &["for-each-ref", "--format=%(refname)", "refs/heads/"] |
| 674 | ), |
| 675 | "refs/heads/main" |
| 676 | ); |
| 677 | } |
| 678 | |
| 679 | |
| 680 | |
| 681 | #[test] |
| 682 | fn a_version_reads_as_major_and_minor() { |
| 683 | assert_eq!(parse_git_version("git version 2.47.1\n"), Some((2, 47))); |
| 684 | |
| 685 | assert_eq!( |
| 686 | parse_git_version("git version 2.39.5 (Apple Git-154)\n"), |
| 687 | Some((2, 39)) |
| 688 | ); |
| 689 | assert_eq!(parse_git_version(""), None); |
| 690 | assert_eq!(parse_git_version("git version banana"), None); |
| 691 | } |
| 692 | |
| 693 | #[tokio::test] |
| 694 | async fn this_machine_reports_what_its_git_can_do() { |
| 695 | |
| 696 | |
| 697 | let features = GitFeatures::detect().await; |
| 698 | |
| 699 | assert_eq!( |
| 700 | features, |
| 701 | GitFeatures::of_version(parse_git_version(&git_says(Path::new("."), &["--version"]))) |
| 702 | ); |
| 703 | } |
| 704 | |
| 705 | #[test] |
| 706 | fn each_capability_starts_at_its_own_version() { |
| 707 | let at = |major, minor| GitFeatures::of_version(Some((major, minor))); |
| 708 | |
| 709 | |
| 710 | assert!(!at(2, 34).pinned_resolve); |
| 711 | assert!(!at(2, 36).pinned_resolve); |
| 712 | assert!(at(2, 37).pinned_resolve); |
| 713 | assert!(!at(2, 37).merge_tree); |
| 714 | assert!(at(2, 38).merge_tree); |
| 715 | assert!(at(3, 0).pinned_resolve && at(3, 0).merge_tree); |
| 716 | |
| 717 | assert_eq!(GitFeatures::of_version(None), GitFeatures::default()); |
| 718 | } |
| 719 | |
| 720 | #[tokio::test] |
| 721 | async fn detection_reports_the_version_it_decided_on() { |
| 722 | let features = GitFeatures::detect().await; |
| 723 | let parsed = parse_git_version(&git_says(Path::new("."), &["--version"])); |
| 724 | |
| 725 | assert_eq!(features.version, parsed); |
| 726 | assert_eq!( |
| 727 | features.version_string(), |
| 728 | parsed.map(|(major, minor)| format!("{major}.{minor}")), |
| 729 | ); |
| 730 | } |
| 731 | } |