941 lines · 30.0 KB · Rust
RawDownload
// 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 bstr::ByteSlice as _;use indoc::indoc; use crate::common::TestEnvironment; #[test]fn test_diffedit() {    let mut test_env = TestEnvironment::default();    let edit_script = test_env.set_up_fake_diff_editor();    test_env.run_jj_in(".", ["git", "init", "repo"]).success();    let work_dir = test_env.work_dir("repo");     work_dir.write_file("file1", "a\n");    work_dir.run_jj(["new"]).success();    work_dir.write_file("file2", "a\n");    work_dir.write_file("file3", "a\n");    work_dir.run_jj(["new"]).success();    work_dir.remove_file("file1");    work_dir.write_file("file2", "b\n");    work_dir.run_jj(["debug", "snapshot"]).success();    let setup_opid = work_dir.current_operation_id();     // Test the setup; nothing happens if we make no changes    std::fs::write(        &edit_script,        [            "files-before file1 file2",            "files-after JJ-INSTRUCTIONS file2",            "dump JJ-INSTRUCTIONS instrs",        ]        .join("\0"),    )    .unwrap();    let output = work_dir.run_jj(["diffedit"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Nothing changed.    [EOF]    ");    insta::assert_snapshot!(        std::fs::read_to_string(test_env.env_root().join("instrs")).unwrap(), @r"    You are editing changes in: kkmpptxz e4245972 (no description set)     The diff initially shows the commit's changes.     Adjust the right side until it shows the contents you want. If you    don't make any changes, then the operation will be aborted.    ");    let output = work_dir.run_jj(["diff", "-s"]);    insta::assert_snapshot!(output, @r"    D file1    M file2    [EOF]    ");     // Try again with ui.diff-instructions=false    std::fs::write(&edit_script, "files-before file1 file2\0files-after file2").unwrap();    let output = work_dir.run_jj(["diffedit", "--config=ui.diff-instructions=false"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Nothing changed.    [EOF]    ");    let output = work_dir.run_jj(["diff", "-s"]);    insta::assert_snapshot!(output, @r"    D file1    M file2    [EOF]    ");     // Try again with --tool=<name>    std::fs::write(        &edit_script,        "files-before file1 file2\0files-after JJ-INSTRUCTIONS file2",    )    .unwrap();    let output = work_dir.run_jj([        "diffedit",        "--config=ui.diff-editor='false'",        "--tool=fake-diff-editor",    ]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Nothing changed.    [EOF]    ");    let output = work_dir.run_jj(["diff", "-s"]);    insta::assert_snapshot!(output, @r"    D file1    M file2    [EOF]    ");     // Nothing happens if the diff-editor exits with an error    std::fs::write(&edit_script, "rm file2\0fail").unwrap();    let output = work_dir.run_jj(["diffedit"]);    insta::assert_snapshot!(output.normalize_stderr_exit_status(), @r"    ------- stderr -------    Error: Failed to edit diff    Caused by: Tool exited with exit status: 1 (run with --debug to see the exact invocation)    [EOF]    [exit status: 1]    ");    let output = work_dir.run_jj(["diff", "-s"]);    insta::assert_snapshot!(output, @r"    D file1    M file2    [EOF]    ");     // Can edit changes to individual files    std::fs::write(&edit_script, "reset file2").unwrap();    let output = work_dir.run_jj(["diffedit"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Working copy  (@) now at: kkmpptxz 40ad4f80 (no description set)    Parent commit (@-)      : rlvkpnrz 7e268da3 (no description set)    Added 0 files, modified 1 files, removed 0 files    [EOF]    ");    let output = work_dir.run_jj(["diff", "-s"]);    insta::assert_snapshot!(output, @r"    D file1    [EOF]    ");     // Changes to a commit are propagated to descendants    work_dir.run_jj(["op", "restore", &setup_opid]).success();    std::fs::write(&edit_script, "write file3\nmodified\n").unwrap();    let output = work_dir.run_jj(["diffedit", "-r", "@-"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Rebased 1 descendant commits    Working copy  (@) now at: kkmpptxz 9f0ebae1 (no description set)    Parent commit (@-)      : rlvkpnrz 72bcd8e9 (no description set)    Added 0 files, modified 1 files, removed 0 files    [EOF]    ");    let contents = work_dir.read_file("file3");    insta::assert_snapshot!(contents, @"modified");     // Test diffedit --from @--    work_dir.run_jj(["op", "restore", &setup_opid]).success();    std::fs::write(        &edit_script,        "files-before file1\0files-after JJ-INSTRUCTIONS file2 file3\0reset file2",    )    .unwrap();    let output = work_dir.run_jj(["diffedit", "--from", "@--"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Working copy  (@) now at: kkmpptxz 215fca5f (no description set)    Parent commit (@-)      : rlvkpnrz 7e268da3 (no description set)    Added 0 files, modified 0 files, removed 1 files    [EOF]    ");    let output = work_dir.run_jj(["diff", "-s"]);    insta::assert_snapshot!(output, @r"    D file1    D file2    [EOF]    ");     // Test with path restriction    work_dir.run_jj(["op", "restore", &setup_opid]).success();    work_dir.write_file("file3", "a\n");    work_dir.run_jj(["new"]).success();    work_dir.write_file("file1", "modified\n");    work_dir.write_file("file2", "modified\n");    work_dir.write_file("file3", "modified\n");     // Edit only file2 with path argument    std::fs::write(        &edit_script,        "files-before file2\0files-after JJ-INSTRUCTIONS file2\0reset file2",    )    .unwrap();    let output = work_dir.run_jj(["diffedit", "file2"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Working copy  (@) now at: tlkvzzqu 06bdff15 (no description set)    Parent commit (@-)      : kkmpptxz e4245972 (no description set)    Added 0 files, modified 1 files, removed 0 files    [EOF]    ");    let output = work_dir.run_jj(["diff", "-s"]);    insta::assert_snapshot!(output, @r"    C {file3 => file1}    M file3    [EOF]    ");     // Test reverse-order diffedit --to @-    work_dir.run_jj(["op", "restore", &setup_opid]).success();    std::fs::write(        &edit_script,        [            "files-before file2",            "files-after JJ-INSTRUCTIONS file1 file2",            "reset file2",            "dump JJ-INSTRUCTIONS instrs",        ]        .join("\0"),    )    .unwrap();    let output = work_dir.run_jj(["diffedit", "--to", "@-"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Rebased 1 descendant commits    Working copy  (@) now at: kkmpptxz 9a4e9bcc (no description set)    Parent commit (@-)      : rlvkpnrz fb5c77f4 (no description set)    [EOF]    ");    insta::assert_snapshot!(        std::fs::read_to_string(test_env.env_root().join("instrs")).unwrap(), @r"    You are editing changes in: rlvkpnrz 7e268da3 (no description set)     The diff initially shows the commit's changes relative to:    kkmpptxz e4245972 (no description set)     Adjust the right side until it shows the contents you want. If you    don't make any changes, then the operation will be aborted.    ");    let output = work_dir.run_jj(["diff", "-s"]);    insta::assert_snapshot!(output, @r"    D file1    [EOF]    ");} #[test]fn test_diffedit_new_file() {    let mut test_env = TestEnvironment::default();    let edit_script = test_env.set_up_fake_diff_editor();    test_env.run_jj_in(".", ["git", "init", "repo"]).success();    let work_dir = test_env.work_dir("repo");     work_dir.write_file("file1", "a\n");    work_dir.run_jj(["new"]).success();    work_dir.remove_file("file1");    work_dir.write_file("file2", "b\n");    work_dir.run_jj(["debug", "snapshot"]).success();    let setup_opid = work_dir.current_operation_id();     // Test the setup; nothing happens if we make no changes    std::fs::write(        &edit_script,        "files-before file1\0files-after JJ-INSTRUCTIONS file2",    )    .unwrap();    let output = work_dir.run_jj(["diffedit"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Nothing changed.    [EOF]    ");    let output = work_dir.run_jj(["diff", "-s"]);    insta::assert_snapshot!(output, @r"    D file1    A file2    [EOF]    ");     // Creating `file1` on the right side is noticed by `jj diffedit`    std::fs::write(&edit_script, "write file1\nmodified\n").unwrap();    let output = work_dir.run_jj(["diffedit"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Working copy  (@) now at: rlvkpnrz c26dcad1 (no description set)    Parent commit (@-)      : qpvuntsm eb7b8a1f (no description set)    Added 1 files, modified 0 files, removed 0 files    [EOF]    ");    let output = work_dir.run_jj(["diff", "-s"]);    insta::assert_snapshot!(output, @r"    M file1    A file2    [EOF]    ");     // Creating a file that wasn't on either side is ignored by diffedit.    // TODO(ilyagr) We should decide whether we like this behavior.    //    // On one hand, it is unexpected and potentially a minor BUG. On the other    // hand, this prevents `jj` from loading any backup files the merge tool    // generates.    work_dir.run_jj(["op", "restore", &setup_opid]).success();    std::fs::write(&edit_script, "write new_file\nnew file\n").unwrap();    let output = work_dir.run_jj(["diffedit"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Nothing changed.    [EOF]    ");    let output = work_dir.run_jj(["diff", "-s"]);    insta::assert_snapshot!(output, @r"    D file1    A file2    [EOF]    ");} #[test]fn test_diffedit_existing_instructions() {    let mut test_env = TestEnvironment::default();    let edit_script = test_env.set_up_fake_diff_editor();    test_env.run_jj_in(".", ["git", "init", "repo"]).success();    let work_dir = test_env.work_dir("repo");     // A diff containing an existing JJ-INSTRUCTIONS file themselves.    work_dir.write_file("JJ-INSTRUCTIONS", "instruct");     std::fs::write(&edit_script, "write JJ-INSTRUCTIONS\nmodified\n").unwrap();    let output = work_dir.run_jj(["diffedit"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Working copy  (@) now at: qpvuntsm e914aaad (no description set)    Parent commit (@-)      : zzzzzzzz 00000000 (empty) (no description set)    Added 0 files, modified 1 files, removed 0 files    [EOF]    ");    // Test that we didn't delete or overwrite the "JJ-INSTRUCTIONS" file.    let content = work_dir.read_file("JJ-INSTRUCTIONS");    insta::assert_snapshot!(content, @"modified");} #[test]fn test_diffedit_external_tool_conflict_marker_style() {    let mut test_env = TestEnvironment::default();    let edit_script = test_env.set_up_fake_diff_editor();    test_env.run_jj_in(".", ["git", "init", "repo"]).success();    let work_dir = test_env.work_dir("repo");    let file_path = "file";     // Create a conflict    work_dir.write_file(        file_path,        indoc! {"            line 1            line 2            line 3            line 4            line 5        "},    );    work_dir.run_jj(["commit", "-m", "base"]).success();    work_dir.write_file(        file_path,        indoc! {"            line 1            line 2.1            line 2.2            line 3            line 4.1            line 5        "},    );    work_dir.run_jj(["describe", "-m", "side-a"]).success();    work_dir        .run_jj(["new", "subject(base)", "-m", "side-b"])        .success();    work_dir.write_file(        file_path,        indoc! {"            line 1            line 2.3            line 3            line 4.2            line 4.3            line 5        "},    );     // Resolve one of the conflicts in the working copy    work_dir        .run_jj(["new", "subject(side-a)", "subject(side-b)"])        .success();    work_dir.write_file(        file_path,        indoc! {"            line 1            line 2.1            line 2.2            line 2.3            line 3            <<<<<<<            %%%%%%%            -line 4            +line 4.1            +++++++            line 4.2            line 4.3            >>>>>>>            line 5        "},    );     // Set up diff editor to use "snapshot" conflict markers    test_env.add_config(r#"merge-tools.fake-diff-editor.conflict-marker-style = "snapshot""#);     // We want to see whether the diff editor is using the correct conflict markers,    // and reset it to make sure that it parses the conflict markers as well    std::fs::write(        &edit_script,        [            "files-before file",            "files-after JJ-INSTRUCTIONS file",            "dump file after-file",            "reset file",            "dump file before-file",        ]        .join("\0"),    )    .unwrap();    let output = work_dir.run_jj(["diffedit"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Working copy  (@) now at: mzvwutvl 2fc7c1ba (conflict) (empty) (no description set)    Parent commit (@-)      : rlvkpnrz 74e448a1 side-a    Parent commit (@-)      : zsuskuln 6982bce7 side-b    Added 0 files, modified 1 files, removed 0 files    Warning: There are unresolved conflicts at these paths:    file    2-sided conflict    Existing conflicts were resolved or abandoned from 1 commits.    [EOF]    ");    // Conflicts should render using "snapshot" format in diff editor    insta::assert_snapshot!(        std::fs::read_to_string(test_env.env_root().join("before-file")).unwrap(), @r#"    line 1    <<<<<<< conflict 1 of 2    +++++++ rlvkpnrz 74e448a1 "side-a"    line 2.1    line 2.2    ------- qpvuntsm 9bd2e004 "base"    line 2    +++++++ zsuskuln 6982bce7 "side-b"    line 2.3    >>>>>>> conflict 1 of 2 ends    line 3    <<<<<<< conflict 2 of 2    +++++++ rlvkpnrz 74e448a1 "side-a"    line 4.1    ------- qpvuntsm 9bd2e004 "base"    line 4    +++++++ zsuskuln 6982bce7 "side-b"    line 4.2    line 4.3    >>>>>>> conflict 2 of 2 ends    line 5    "#);    insta::assert_snapshot!(        std::fs::read_to_string(test_env.env_root().join("after-file")).unwrap(), @r#"    line 1    line 2.1    line 2.2    line 2.3    line 3    <<<<<<< conflict 1 of 1    +++++++ rlvkpnrz 74e448a1 "side-a"    line 4.1    ------- qpvuntsm 9bd2e004 "base"    line 4    +++++++ zsuskuln 6982bce7 "side-b"    line 4.2    line 4.3    >>>>>>> conflict 1 of 1 ends    line 5    "#);    // Conflicts should be materialized using "diff" format in working copy    insta::assert_snapshot!(work_dir.read_file(file_path), @r#"    line 1    <<<<<<< conflict 1 of 2    +++++++ rlvkpnrz 74e448a1 "side-a"    line 2.1    line 2.2    %%%%%%% diff from: qpvuntsm 9bd2e004 "base"    \\\\\\\        to: zsuskuln 6982bce7 "side-b"    -line 2    +line 2.3    >>>>>>> conflict 1 of 2 ends    line 3    <<<<<<< conflict 2 of 2    %%%%%%% diff from: qpvuntsm 9bd2e004 "base"    \\\\\\\        to: rlvkpnrz 74e448a1 "side-a"    -line 4

Lines 1–500 of 941.