jj/cli/tests/test_interdiff_command.rs
Yuya Nishihara b63ab33b10 diff: add option to render color-words diffs without materializing conflicts
The original idea was to flatten left/right conflict trees and pair up adjacent
negative/positive terms. For example, diff(A, B-C+D) could be rendered as
diff(A, B) and diff(C, D). The problem of this formalization is that one of the
diff pairs is often empty (because e.g. A=B), so the context is fully omitted.
The resulting diff(C, D) doesn't provide any notion why the hunk is conflicted,
and how it is different from A.

Instead, this patch implements diffs in which each left/right pair is compared.
In the example above, the left terms are padded, and the diffs are rendered as
diff(A, B), diff(-A, -C), diff(A, D). This appears to be working reasonably well
so long as either side is resolved or both sides have the same numbers of terms.

Closes #4062
2025-04-08 09:12:39 +00:00

187 lines
5.5 KiB
Rust
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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_interdiff_basic() {
let test_env = TestEnvironment::default();
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
work_dir.write_file("file1", "foo\n");
work_dir.run_jj(["new"]).success();
work_dir.write_file("file2", "foo\n");
work_dir
.run_jj(["bookmark", "create", "-r@", "left"])
.success();
work_dir.run_jj(["new", "root()"]).success();
work_dir.write_file("file3", "foo\n");
work_dir.run_jj(["new"]).success();
work_dir.write_file("file2", "foo\nbar\n");
work_dir
.run_jj(["bookmark", "create", "-r@", "right"])
.success();
// implicit --to
let output = work_dir.run_jj(["interdiff", "--from", "left"]);
insta::assert_snapshot!(output, @r"
Modified regular file file2:
1 1: foo
2: bar
[EOF]
");
// explicit --to
work_dir.run_jj(["new", "@-"]).success();
let output = work_dir.run_jj(["interdiff", "--from", "left", "--to", "right"]);
insta::assert_snapshot!(output, @r"
Modified regular file file2:
1 1: foo
2: bar
[EOF]
");
work_dir.run_jj(["undo"]).success();
// formats specifiers
let output = work_dir.run_jj(["interdiff", "--from", "left", "--to", "right", "-s"]);
insta::assert_snapshot!(output, @r"
M file2
[EOF]
");
let output = work_dir.run_jj(["interdiff", "--from", "left", "--to", "right", "--git"]);
insta::assert_snapshot!(output, @r"
diff --git a/file2 b/file2
index 257cc5642c..3bd1f0e297 100644
--- a/file2
+++ b/file2
@@ -1,1 +1,2 @@
foo
+bar
[EOF]
");
}
#[test]
fn test_interdiff_paths() {
let test_env = TestEnvironment::default();
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
work_dir.write_file("file1", "foo\n");
work_dir.write_file("file2", "foo\n");
work_dir.run_jj(["new"]).success();
work_dir.write_file("file1", "bar\n");
work_dir.write_file("file2", "bar\n");
work_dir
.run_jj(["bookmark", "create", "-r@", "left"])
.success();
work_dir.run_jj(["new", "root()"]).success();
work_dir.write_file("file1", "foo\n");
work_dir.write_file("file2", "foo\n");
work_dir.run_jj(["new"]).success();
work_dir.write_file("file1", "baz\n");
work_dir.write_file("file2", "baz\n");
work_dir
.run_jj(["bookmark", "create", "-r@", "right"])
.success();
let output = work_dir.run_jj(["interdiff", "--from", "left", "--to", "right", "file1"]);
insta::assert_snapshot!(output, @r"
Modified regular file file1:
1 1: barbaz
[EOF]
");
let output = work_dir.run_jj([
"interdiff",
"--from",
"left",
"--to",
"right",
"file1",
"file2",
]);
insta::assert_snapshot!(output, @r"
Modified regular file file1:
1 1: barbaz
Modified regular file file2:
1 1: barbaz
[EOF]
");
}
#[test]
fn test_interdiff_conflicting() {
let test_env = TestEnvironment::default();
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
work_dir.write_file("file", "foo\n");
work_dir.run_jj(["new"]).success();
work_dir.write_file("file", "bar\n");
work_dir
.run_jj(["bookmark", "create", "-r@", "left"])
.success();
work_dir.run_jj(["new", "root()"]).success();
work_dir.write_file("file", "abc\n");
work_dir.run_jj(["new"]).success();
work_dir.write_file("file", "def\n");
work_dir
.run_jj(["bookmark", "create", "-r@", "right"])
.success();
let output = work_dir.run_jj(["interdiff", "--from", "left", "--to", "right", "--git"]);
insta::assert_snapshot!(output, @r"
diff --git a/file b/file
index 0000000000..24c5735c3e 100644
--- a/file
+++ b/file
@@ -1,7 +1,1 @@
-<<<<<<< Conflict 1 of 1
-%%%%%%% Changes from base to side #1
--foo
-+abc
-+++++++ Contents of side #2
-bar
->>>>>>> Conflict 1 of 1 ends
+def
[EOF]
");
let output = work_dir.run_jj([
"interdiff",
"--config=diff.color-words.conflict=pair",
"--color=always",
"--from=left",
"--to=right",
]);
insta::assert_snapshot!(output, @r"
Resolved conflict in file:
<<<<<<< Resolved conflict
+++++++ left side #1 to right side #1
 1  1: abcdef
------- left base #1 to right side #1
 1  1: foodef
+++++++ left side #2 to right side #1
 1  1: bardef
>>>>>>> Conflict ends
[EOF]
");
}