ok/jj
1
0
Fork 0
forked from mirrors/jj

conflicts: always use tree-level format for merged trees

It's been about six months since we started using tree-level conflicts
by default. I can't imagine we would switch back. So let's continue
the migration by always using tree-level conflicts when merging trees,
even if all inputs were legacy trees.
This commit is contained in:
Martin von Zweigbergk 2024-05-26 10:23:04 -07:00 committed by Martin von Zweigbergk
parent 26085f236e
commit 8e6e04b929
3 changed files with 26 additions and 118 deletions

View file

@ -61,7 +61,6 @@ mod test_squash_command;
mod test_status_command;
mod test_tag_command;
mod test_templater;
mod test_tree_level_conflicts;
mod test_undo;
mod test_unsquash_command;
mod test_untrack_command;

View file

@ -1,84 +0,0 @@
// Copyright 2022 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::common::TestEnvironment;
#[test]
fn test_enable_tree_level_conflicts() {
let test_env = TestEnvironment::default();
test_env.add_config(r#"format.tree-level-conflicts = false"#);
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let repo_path = test_env.env_root().join("repo");
// Create a few commits before we enable tree-level conflicts
let file_path = repo_path.join("file");
test_env.jj_cmd_ok(&repo_path, &["new", "root()", "-m=left"]);
std::fs::write(&file_path, "left").unwrap();
test_env.jj_cmd_ok(&repo_path, &["new", "root()", "-m=right"]);
std::fs::write(&file_path, "right").unwrap();
test_env.jj_cmd_ok(
&repo_path,
&[
"new",
r#"description("left")"#,
r#"description("right")"#,
"-m=merge",
],
);
test_env.jj_cmd_ok(&repo_path, &["new"]);
let stdout = test_env.jj_cmd_success(&repo_path, &["log"]);
insta::assert_snapshot!(stdout, @r###"
@ mzvwutvl test.user@example.com 2001-02-03 08:05:11 f2101bed conflict
(empty) (no description set)
zsuskuln test.user@example.com 2001-02-03 08:05:10 5100e4e1 conflict
(empty) merge
kkmpptxz test.user@example.com 2001-02-03 08:05:10 0b65c8fb
right
rlvkpnrz test.user@example.com 2001-02-03 08:05:09 32003b88
left
zzzzzzzz root() 00000000
"###);
// Enable tree-level conflicts
test_env.add_config(r#"format.tree-level-conflicts = true"#);
// We get a new working-copy commit. The working copy unfortunately appears
// non-empty
let stdout = test_env.jj_cmd_success(&repo_path, &["log"]);
insta::assert_snapshot!(stdout, @r###"
@ mzvwutvl test.user@example.com 2001-02-03 08:05:13 ada1f5f1 conflict
(no description set)
zsuskuln test.user@example.com 2001-02-03 08:05:10 5100e4e1 conflict
(empty) merge
kkmpptxz test.user@example.com 2001-02-03 08:05:10 0b65c8fb
right
rlvkpnrz test.user@example.com 2001-02-03 08:05:09 32003b88
left
zzzzzzzz root() 00000000
"###);
// ...but at least it has no diff
let stdout = test_env.jj_cmd_success(&repo_path, &["diff"]);
insta::assert_snapshot!(stdout, @"");
// If we create new commit off of an unconflicted commit, it correctly appears
// empty
test_env.jj_cmd_ok(&repo_path, &["new", "k"]);
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-r=@"]);
insta::assert_snapshot!(stdout, @r###"
@ yostqsxw test.user@example.com 2001-02-03 08:05:15 112f0ac2
(empty) (no description set)
~
"###);
let stdout = test_env.jj_cmd_success(&repo_path, &["diff"]);
insta::assert_snapshot!(stdout, @"");
}

View file

@ -26,6 +26,7 @@ use futures::stream::StreamExt;
use futures::{Future, Stream, TryStreamExt};
use itertools::Itertools;
use crate::backend;
use crate::backend::{BackendResult, ConflictId, MergedTreeId, TreeId, TreeValue};
use crate::matchers::{EverythingMatcher, Matcher};
use crate::merge::{Merge, MergeBuilder, MergedTreeValue};
@ -33,7 +34,6 @@ use crate::repo_path::{RepoPath, RepoPathBuf, RepoPathComponent};
use crate::store::Store;
use crate::tree::{try_resolve_file_conflict, Tree};
use crate::tree_builder::TreeBuilder;
use crate::{backend, tree};
/// Presents a view of a merged set of trees.
#[derive(PartialEq, Eq, Clone, Debug)]
@ -372,41 +372,34 @@ impl MergedTree {
/// Merges this tree with `other`, using `base` as base.
pub fn merge(&self, base: &MergedTree, other: &MergedTree) -> BackendResult<MergedTree> {
if let (MergedTree::Legacy(this), MergedTree::Legacy(base), MergedTree::Legacy(other)) =
(self, base, other)
{
let merged_tree = tree::merge_trees(this, base, other)?;
Ok(MergedTree::legacy(merged_tree))
} else {
// Convert legacy trees to merged trees and unwrap to `Merge<Tree>`
let to_merge = |tree: &MergedTree| -> BackendResult<Merge<Tree>> {
match tree {
MergedTree::Legacy(tree) => {
let MergedTree::Merge(tree) = MergedTree::from_legacy_tree(tree.clone())?
else {
unreachable!();
};
Ok(tree)
}
MergedTree::Merge(conflict) => Ok(conflict.clone()),
// Convert legacy trees to merged trees and unwrap to `Merge<Tree>`
let to_merge = |tree: &MergedTree| -> BackendResult<Merge<Tree>> {
match tree {
MergedTree::Legacy(tree) => {
let MergedTree::Merge(tree) = MergedTree::from_legacy_tree(tree.clone())?
else {
unreachable!();
};
Ok(tree)
}
};
let nested = Merge::from_vec(vec![to_merge(self)?, to_merge(base)?, to_merge(other)?]);
let tree = merge_trees(&nested.flatten().simplify())?;
// If the result can be resolved, then `merge_trees()` above would have returned
// a resolved merge. However, that function will always preserve the arity of
// conflicts it cannot resolve. So we simplify the conflict again
// here to possibly reduce a complex conflict to a simpler one.
let tree = tree.simplify();
// If debug assertions are enabled, check that the merge was idempotent. In
// particular, that this last simplification doesn't enable further automatic
// resolutions
if cfg!(debug_assertions) {
let re_merged = merge_trees(&tree).unwrap();
debug_assert_eq!(re_merged, tree);
MergedTree::Merge(conflict) => Ok(conflict.clone()),
}
Ok(MergedTree::Merge(tree))
};
let nested = Merge::from_vec(vec![to_merge(self)?, to_merge(base)?, to_merge(other)?]);
let tree = merge_trees(&nested.flatten().simplify())?;
// If the result can be resolved, then `merge_trees()` above would have returned
// a resolved merge. However, that function will always preserve the arity of
// conflicts it cannot resolve. So we simplify the conflict again
// here to possibly reduce a complex conflict to a simpler one.
let tree = tree.simplify();
// If debug assertions are enabled, check that the merge was idempotent. In
// particular, that this last simplification doesn't enable further automatic
// resolutions
if cfg!(debug_assertions) {
let re_merged = merge_trees(&tree).unwrap();
debug_assert_eq!(re_merged, tree);
}
Ok(MergedTree::Merge(tree))
}
}