//! Merges against real temp repositories: a clean merge, a fast-forward, //! already up to date, a content conflict with its three versions, an //! add/add conflict without a base, a modify/delete conflict, marking files //! resolved and committing, aborting, a refused merge (local changes) and a //! second merge while one is in progress. Every test skips with a message //! when git is not installed. #![allow( clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing )] use std::path::Path; use std::process::Command; use lutra_vcs::{FileState, Git, MergeResult, Repo, VcsError}; /// The installed git, or `None` (with a message) when there is none. fn installed() -> Option { match Git::detect("") { Ok(git) => Some(git), Err(e) => { eprintln!("skipped: no usable git installed ({e})"); None } } } /// Runs git in `dir` for test setup and asserts it succeeded. fn git(dir: &Path, args: &[&str]) -> String { let out = Command::new("git") .args(args) .current_dir(dir) .env_remove("GIT_DIR") .env_remove("GIT_WORK_TREE") .env_remove("GIT_INDEX_FILE") .output() .unwrap(); assert!( out.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&out.stderr) ); String::from_utf8_lossy(&out.stdout).trim().to_string() } /// Writes a file. fn write(dir: &Path, path: &str, text: &str) { std::fs::write(dir.join(path), text).unwrap(); } /// Stages everything and commits. fn commit(dir: &Path, message: &str) { git(dir, &["add", "-A"]); git(dir, &["commit", "-q", "-m", message]); } /// A repository on `main` with `a.php` committed, and a branch `feature` /// at that commit. fn init(dir: &Path) { git(dir, &["init", "-q", "-b", "main"]); for (key, value) in [ ("user.name", "Lutra Test"), ("user.email", "test@lutra.invalid"), ("commit.gpgsign", "false"), ("core.autocrlf", "false"), ("core.hooksPath", ".git/hooks"), ("merge.conflictStyle", "merge"), ] { git(dir, &["config", key, value]); } write(dir, "a.php", " Repo { git.open(dir).unwrap().expect("a repository") } /// `a.php` changed on both branches in the same line. fn conflicting(dir: &Path) { write(dir, "a.php", "