//! `CommitInfo.files` as documented: the files a commit changed against every readable parent, so a //! merge lists only what it changed against both sides, a root commit lists every file it holds as //! added, and `include_files = false` leaves the list empty. Fixtures are real repositories built //! with the `git` CLI. use std::fs; use std::path::Path; use basemind::git::{ChangeKind, CommitInfo, Repo}; fn run(repo: &Path, args: &[&str]) { let status = basemind::testing::git_command() .args(args) .current_dir(repo) .status() .expect("git in PATH"); assert!(status.success(), "git {args:?} failed"); } fn commit_all(repo: &Path, message: &str) { run(repo, &["add", "-A"]); run(repo, &["commit", "-qm", message]); } fn files(commit: &CommitInfo) -> Vec<(String, ChangeKind)> { commit .files .iter() .map(|(path, kind)| (path.to_string(), *kind)) .collect() } /// Root `R` holds `base.txt` and `shared.txt`; `main` adds `main.txt` and rewrites `shared.txt`, /// `side` adds `side.txt` and rewrites `shared.txt` differently; the merge `M` resolves the conflict /// in `shared.txt` to a third content, so it differs from both parents there and only there. fn merge_fixture() -> (tempfile::TempDir, Repo) { let dir = tempfile::tempdir().expect("tempdir"); let root = dir.path(); run(root, &["init", "-q", "-b", "main"]); run(root, &["config", "commit.gpgsign", "false"]); fs::write(root.join("base.txt"), "base\n").unwrap(); fs::write(root.join("shared.txt"), "root\n").unwrap(); commit_all(root, "R"); run(root, &["checkout", "-q", "-b", "side"]); fs::write(root.join("side.txt"), "side\n").unwrap(); fs::write(root.join("shared.txt"), "side\n").unwrap(); commit_all(root, "S"); run(root, &["checkout", "-q", "main"]); fs::write(root.join("main.txt"), "main\n").unwrap(); fs::write(root.join("shared.txt"), "main\n").unwrap(); commit_all(root, "T"); let conflicted = basemind::testing::git_command() .args(["merge", "--no-ff", "--no-commit", "side"]) .current_dir(root) .status() .expect("git in PATH"); assert!(!conflicted.success(), "the fixture's merge must conflict on shared.txt"); fs::write(root.join("shared.txt"), "resolved\n").unwrap(); commit_all(root, "M"); let repo = Repo::discover(root).expect("repo discover"); (dir, repo) } fn commit<'a>(log: &'a [CommitInfo], summary: &str) -> &'a CommitInfo { log.iter() .find(|commit| commit.summary == summary) .unwrap_or_else(|| panic!("no commit {summary:?} in the log")) } #[test] fn a_merge_lists_only_the_files_it_changed_against_every_parent() { let (_dir, repo) = merge_fixture(); let log = repo.log_paths(10, true).expect("log"); // Against `main` the merge also adds `side.txt` and against `side` it also adds `main.txt`; // those files were changed by the branches' own commits, which the log lists. assert_eq!( files(commit(&log, "M")), [("shared.txt".to_string(), ChangeKind::Modified)] ); assert_eq!( files(commit(&log, "S")), [ ("shared.txt".to_string(), ChangeKind::Modified), ("side.txt".to_string(), ChangeKind::Added), ] ); } #[test] fn a_root_commit_lists_every_file_it_holds_as_added() { let (_dir, repo) = merge_fixture(); let log = repo.log_paths(10, true).expect("log"); assert_eq!( files(commit(&log, "R")), [ ("base.txt".to_string(), ChangeKind::Added), ("shared.txt".to_string(), ChangeKind::Added), ] ); } #[test] fn include_files_false_leaves_every_file_list_empty() { let (_dir, repo) = merge_fixture(); let log = repo.log_paths(10, false).expect("log"); assert_eq!(log.len(), 4); assert!(log.iter().all(|commit| commit.files.is_empty())); }