M4.8: merge conflicts with a three-pane resolve view
Backend (crates/vcs/src/merge.rs, cmd/git_merge.rs via start_with):
git.merge {branch} -> {result: merged|upToDate|conflicts, conflicts},
git.mergeAbort, git.conflictVersions {path} -> {base?, ours, theirs,
binary, tooLarge} from the index stages :1:/:2:/:3:, git.markResolved
{paths}. Repo::lock becomes pub(crate) so merge and abort hold the
mutation lock.
UI (ui/src/components/vcs/merge/): merge dialog with branch picker,
conflicts list from the shared status store, a three-pane resolve view
(ours / editable result / theirs) with accept left/right per chunk,
accept all, previous/next conflict, Apply through the file's buffer then
mark resolved, keep-a-side for modify/delete, abort after confirmation.
Own three-way line merge on @codemirror/merge's diff. UI commands
vcs.merge, vcs.resolveConflicts, vcs.abortMerge (no default shortcuts);
the Changes window opens conflicted rows in the resolve view.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
74c674ed12
commit
d82e57b3ac
26 changed files with 3872 additions and 16 deletions
248
crates/vcs/tests/merge.rs
Normal file
248
crates/vcs/tests/merge.rs
Normal file
|
|
@ -0,0 +1,248 @@
|
|||
//! 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<Git> {
|
||||
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", "<?php\none\ntwo\nthree\n");
|
||||
commit(dir, "base");
|
||||
git(dir, &["branch", "feature"]);
|
||||
}
|
||||
|
||||
/// Opens the repository at `dir`.
|
||||
fn open(git: &Git, dir: &Path) -> 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", "<?php\none\nTWO (main)\nthree\n");
|
||||
commit(dir, "main side");
|
||||
git(dir, &["switch", "-q", "feature"]);
|
||||
write(dir, "a.php", "<?php\none\nTWO (feature)\nthree\n");
|
||||
commit(dir, "feature side");
|
||||
git(dir, &["switch", "-q", "main"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clean_merge_fast_forward_and_up_to_date() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
init(root);
|
||||
git(root, &["switch", "-q", "feature"]);
|
||||
write(root, "b.php", "<?php // b\n");
|
||||
commit(root, "feature b");
|
||||
git(root, &["switch", "-q", "main"]);
|
||||
let repo = open(&cli, root);
|
||||
let ff = repo.merge("feature").unwrap();
|
||||
assert_eq!(ff.result, MergeResult::Merged);
|
||||
assert!(ff.conflicts.is_empty());
|
||||
assert!(root.join("b.php").exists());
|
||||
assert_eq!(repo.merge("feature").unwrap().result, MergeResult::UpToDate);
|
||||
git(root, &["switch", "-q", "feature"]);
|
||||
write(root, "c.php", "<?php // c\n");
|
||||
commit(root, "feature c");
|
||||
git(root, &["switch", "-q", "main"]);
|
||||
write(root, "d.php", "<?php // d\n");
|
||||
commit(root, "main d");
|
||||
let merged = repo.merge("feature").unwrap();
|
||||
assert_eq!(merged.result, MergeResult::Merged);
|
||||
assert_eq!(git(root, &["rev-list", "--count", "--merges", "HEAD"]), "1");
|
||||
assert!(!repo.merging().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_conflict_its_versions_resolving_and_committing() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
init(root);
|
||||
conflicting(root);
|
||||
let repo = open(&cli, root);
|
||||
let outcome = repo.merge("feature").unwrap();
|
||||
assert_eq!(outcome.result, MergeResult::Conflicts);
|
||||
assert_eq!(outcome.conflicts, vec!["a.php"]);
|
||||
assert!(repo.merging().unwrap());
|
||||
let status = repo.status().unwrap();
|
||||
let file = status.files.iter().find(|f| f.path == "a.php").unwrap();
|
||||
assert_eq!(file.worktree, FileState::Conflicted);
|
||||
let versions = repo.conflict_versions("a.php").unwrap();
|
||||
assert_eq!(versions.base.as_deref(), Some("<?php\none\ntwo\nthree\n"));
|
||||
assert_eq!(
|
||||
versions.ours.as_deref(),
|
||||
Some("<?php\none\nTWO (main)\nthree\n")
|
||||
);
|
||||
assert_eq!(
|
||||
versions.theirs.as_deref(),
|
||||
Some("<?php\none\nTWO (feature)\nthree\n")
|
||||
);
|
||||
assert!(!versions.binary && !versions.too_large);
|
||||
let on_disk = std::fs::read_to_string(root.join("a.php")).unwrap();
|
||||
assert!(on_disk.contains("<<<<<<<"));
|
||||
assert!(matches!(
|
||||
repo.merge("feature"),
|
||||
Err(VcsError::Git(message)) if message.contains("in progress")
|
||||
));
|
||||
write(root, "a.php", "<?php\none\nTWO (both)\nthree\n");
|
||||
repo.mark_resolved(&["a.php".to_string()]).unwrap();
|
||||
assert!(repo.unmerged().unwrap().is_empty());
|
||||
assert!(matches!(
|
||||
repo.conflict_versions("a.php"),
|
||||
Err(VcsError::Git(message)) if message.contains("not in conflict")
|
||||
));
|
||||
repo.commit("Merge feature", false).unwrap();
|
||||
assert!(!repo.merging().unwrap());
|
||||
assert_eq!(git(root, &["rev-list", "--count", "--merges", "HEAD"]), "1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_add_and_modify_delete_conflicts() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
init(root);
|
||||
write(root, "new file.php", "<?php // main\n");
|
||||
git(root, &["rm", "-q", "a.php"]);
|
||||
commit(root, "main adds, deletes a");
|
||||
git(root, &["switch", "-q", "feature"]);
|
||||
write(root, "new file.php", "<?php // feature\n");
|
||||
write(root, "a.php", "<?php\none\nchanged\nthree\n");
|
||||
commit(root, "feature adds, changes a");
|
||||
git(root, &["switch", "-q", "main"]);
|
||||
let repo = open(&cli, root);
|
||||
let outcome = repo.merge("feature").unwrap();
|
||||
assert_eq!(outcome.result, MergeResult::Conflicts);
|
||||
assert_eq!(outcome.conflicts, vec!["a.php", "new file.php"]);
|
||||
let added = repo.conflict_versions("new file.php").unwrap();
|
||||
assert_eq!(added.base, None);
|
||||
assert_eq!(added.ours.as_deref(), Some("<?php // main\n"));
|
||||
assert_eq!(added.theirs.as_deref(), Some("<?php // feature\n"));
|
||||
let deleted = repo.conflict_versions("a.php").unwrap();
|
||||
assert!(deleted.base.is_some());
|
||||
assert_eq!(deleted.ours, None);
|
||||
assert_eq!(
|
||||
deleted.theirs.as_deref(),
|
||||
Some("<?php\none\nchanged\nthree\n")
|
||||
);
|
||||
std::fs::remove_file(root.join("a.php")).unwrap();
|
||||
repo.mark_resolved(&["a.php".to_string(), "new file.php".to_string()])
|
||||
.unwrap();
|
||||
assert!(repo.unmerged().unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn abort_restores_the_state_before_the_merge() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
init(root);
|
||||
conflicting(root);
|
||||
let repo = open(&cli, root);
|
||||
let head = git(root, &["rev-parse", "HEAD"]);
|
||||
assert_eq!(
|
||||
repo.merge("feature").unwrap().result,
|
||||
MergeResult::Conflicts
|
||||
);
|
||||
repo.merge_abort().unwrap();
|
||||
assert!(!repo.merging().unwrap());
|
||||
assert!(repo.unmerged().unwrap().is_empty());
|
||||
assert_eq!(git(root, &["rev-parse", "HEAD"]), head);
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(root.join("a.php")).unwrap(),
|
||||
"<?php\none\nTWO (main)\nthree\n"
|
||||
);
|
||||
assert!(matches!(repo.merge_abort(), Err(VcsError::Git(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refusals_pass_on_gits_message() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
init(root);
|
||||
conflicting(root);
|
||||
write(root, "a.php", "<?php\nlocal edit\n");
|
||||
let repo = open(&cli, root);
|
||||
assert!(matches!(repo.merge("feature"), Err(VcsError::Git(_))));
|
||||
assert!(!repo.merging().unwrap());
|
||||
assert!(matches!(
|
||||
repo.merge("no-such-branch"),
|
||||
Err(VcsError::Git(_))
|
||||
));
|
||||
assert!(matches!(
|
||||
repo.merge("--abort"),
|
||||
Err(VcsError::InvalidName(_))
|
||||
));
|
||||
assert!(matches!(
|
||||
repo.conflict_versions("../x"),
|
||||
Err(VcsError::InvalidPath(_))
|
||||
));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue