Skip to content

Commit

Permalink
merge_tools: simplify and deduplicate file conflicts before attemptin…
Browse files Browse the repository at this point in the history
…g to resolve
  • Loading branch information
bnjmnt4n committed May 22, 2024
1 parent ad55238 commit 3f0e03f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
1 change: 1 addition & 0 deletions cli/src/merge_tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ impl MergeEditor {
String::from_utf8_lossy(summary_bytes.as_slice()).to_string(),
)
})?;
let file_merge = file_merge.deduplicate().simplify();
// We only support conflicts with 2 sides (3-way conflicts)
if file_merge.num_sides() > 2 {
return Err(ConflictResolveError::ConflictTooComplicated {
Expand Down
66 changes: 58 additions & 8 deletions cli/tests/test_resolve_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ fn test_too_many_parents() {

#[test]
fn test_simplify_conflict_sides() {
let test_env = TestEnvironment::default();
let mut test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let repo_path = test_env.env_root().join("repo");

Expand Down Expand Up @@ -552,13 +552,63 @@ fn test_simplify_conflict_sides() {
>>>>>>> Conflict 1 of 1 ends
"###);

// TODO: The conflict should be simplified before being resolved.
let error = test_env.jj_cmd_failure(&repo_path, &["resolve"]);
insta::assert_snapshot!(error, @r###"
Hint: Using default editor ':builtin'; run `jj config set --user ui.merge-editor :builtin` to disable this message.
Resolving conflicts in: file1
Error: Failed to resolve conflicts
Caused by: The conflict at "file1" has 3 sides. At most 2 sides are supported.
check_resolve_produces_input_file(&mut test_env, &repo_path, "file1", "base", "base\n");
check_resolve_produces_input_file(&mut test_env, &repo_path, "file1", "left", "a\n");
check_resolve_produces_input_file(&mut test_env, &repo_path, "file1", "right", "b\n");
// Manually resolve conflict in file 1.
std::fs::write(repo_path.join("file1"), "resolved").unwrap();
check_resolve_produces_input_file(&mut test_env, &repo_path, "file2", "base", "base\n");
check_resolve_produces_input_file(&mut test_env, &repo_path, "file2", "left", "a\n");
check_resolve_produces_input_file(&mut test_env, &repo_path, "file2", "right", "b\n");

// Check that simplified conflicts are still parsed as conflicts after editing
// when `merge-tool-edits-conflict-markers=true`.
let editor_script = test_env.set_up_fake_editor();
std::fs::write(
&editor_script,
indoc! {"
write
<<<<<<< Conflict 1 of 1
%%%%%%% Changes from base to side #1
-base_edited
+a_edited
+++++++ Contents of side #2
b_edited
>>>>>>> Conflict 1 of 1 ends
"},
)
.unwrap();
let (stdout, stderr) = test_env.jj_cmd_ok(
&repo_path,
&[
"resolve",
"--config-toml",
"merge-tools.fake-editor.merge-tool-edits-conflict-markers=true",
],
);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r###"
Resolving conflicts in: file2
Working copy now at: znkkpsqq c19e4299 conflict | (conflict) conflict
Parent commit : zsuskuln bd4692d0 a | a
Parent commit : royxmykx c4337af0 b | b
Parent commit : vruxwmqv 470d63cc c | c
Added 0 files, modified 1 files, removed 0 files
There are unresolved conflicts at these paths:
file2 2-sided conflict
"###);
insta::assert_snapshot!(std::fs::read_to_string(repo_path.join("file2")).unwrap(), @r###"
<<<<<<< Conflict 1 of 1
%%%%%%% Changes from base to side #1
-base_edited
+a_edited
+++++++ Contents of side #2
b_edited
>>>>>>> Conflict 1 of 1 ends
"###);
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["resolve", "--list"]),
@r###"
file2 2-sided conflict
"###);
}

Expand Down

0 comments on commit 3f0e03f

Please sign in to comment.