// 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 std::path::Path; use itertools::Itertools as _; use crate::common::TestEnvironment; pub mod common; #[test] fn test_concurrent_operation_divergence() { let test_env = TestEnvironment::default(); test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); let repo_path = test_env.env_root().join("repo"); test_env.jj_cmd_success(&repo_path, &["describe", "-m", "message 1"]); test_env.jj_cmd_success( &repo_path, &["describe", "-m", "message 2", "--at-op", "@-"], ); // We should be informed about the concurrent modification let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "description"]); insta::assert_snapshot!(stdout, @r###" Concurrent modification detected, resolving automatically. ◉ message 2 │ @ message 1 ├─╯ ◉ "###); } #[test] fn test_concurrent_operations_auto_rebase() { let test_env = TestEnvironment::default(); test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); let repo_path = test_env.env_root().join("repo"); std::fs::write(repo_path.join("file"), "contents").unwrap(); test_env.jj_cmd_success(&repo_path, &["describe", "-m", "initial"]); let stdout = test_env.jj_cmd_success(&repo_path, &["op", "log"]); insta::assert_snapshot!(stdout, @r###" @ cfc96ff553b9 test-username@host.example.com 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00 │ describe commit 123ed18e4c4c0d77428df41112bc02ffc83fb935 │ args: jj describe -m initial ◉ 65a6c90b9544 test-username@host.example.com 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00 │ snapshot working copy │ args: jj describe -m initial ◉ 19b8089fc78b test-username@host.example.com 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 │ add workspace 'default' ◉ f1c462c494be test-username@host.example.com 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 initialize repo "###); let op_id_hex = stdout[3..15].to_string(); test_env.jj_cmd_success(&repo_path, &["describe", "-m", "rewritten"]); test_env.jj_cmd_success( &repo_path, &["new", "--at-op", &op_id_hex, "-m", "new child"], ); // We should be informed about the concurrent modification insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###" Concurrent modification detected, resolving automatically. Rebased 1 descendant commits onto commits rewritten by other operation ◉ 3f06323826b4a293a9ee6d24cc0e07ad2961b5d5 new child @ d91437157468ec86bbbc9e6a14a60d3e8d1790ac rewritten ◉ 0000000000000000000000000000000000000000 "###); } #[test] fn test_concurrent_operations_wc_modified() { let test_env = TestEnvironment::default(); test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); let repo_path = test_env.env_root().join("repo"); std::fs::write(repo_path.join("file"), "contents\n").unwrap(); test_env.jj_cmd_success(&repo_path, &["describe", "-m", "initial"]); let stdout = test_env.jj_cmd_success(&repo_path, &["op", "log"]); let op_id_hex = stdout[3..15].to_string(); test_env.jj_cmd_success( &repo_path, &["new", "--at-op", &op_id_hex, "-m", "new child1"], ); test_env.jj_cmd_success( &repo_path, &["new", "--at-op", &op_id_hex, "-m", "new child2"], ); std::fs::write(repo_path.join("file"), "modified\n").unwrap(); // We should be informed about the concurrent modification insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###" Concurrent modification detected, resolving automatically. @ 4eb0610031b7cd148ff9f729a673a3f815033170 new child1 │ ◉ 4b20e61d23ee7d7c4d5e61e11e97c26e716f9c30 new child2 ├─╯ ◉ 52c893bf3cd201e215b23e084e8a871244ca14d5 initial ◉ 0000000000000000000000000000000000000000 "###); let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "--git"]); insta::assert_snapshot!(stdout, @r###" diff --git a/file b/file index 12f00e90b6...2e0996000b 100644 --- a/file +++ b/file @@ -1,1 +1,1 @@ -contents +modified "###); // The working copy should be committed after merging the operations let stdout = test_env.jj_cmd_success(&repo_path, &["op", "log", "-Tdescription"]); insta::assert_snapshot!(stdout, @r###" @ snapshot working copy ◉ resolve concurrent operations ├─╮ ◉ │ new empty commit │ ◉ new empty commit ├─╯ ◉ describe commit cf911c223d3e24e001fc8264d6dbf0610804fc40 ◉ snapshot working copy ◉ add workspace 'default' ◉ initialize repo "###); } #[test] fn test_concurrent_snapshot_wc_reloadable() { let test_env = TestEnvironment::default(); test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); let repo_path = test_env.env_root().join("repo"); let op_heads_dir = repo_path .join(".jj") .join("repo") .join("op_heads") .join("heads"); std::fs::write(repo_path.join("base"), "").unwrap(); test_env.jj_cmd_success(&repo_path, &["commit", "-m", "initial"]); // Create new commit and checkout it. std::fs::write(repo_path.join("child1"), "").unwrap(); test_env.jj_cmd_success(&repo_path, &["commit", "-m", "new child1"]); let template = r#"id ++ "\n" ++ description ++ "\n" ++ tags"#; let op_log_stdout = test_env.jj_cmd_success(&repo_path, &["op", "log", "-T", template]); insta::assert_snapshot!(op_log_stdout, @r###" @ 9be517934aaabc351597e88ed4119aa9454ae3588ab7f28646a810272c82f3dafb1deb20b3c978dbb58ba9abc8f08fe870fe3c7ce5f682411991e83eee40a77f │ commit 323b414dd255b51375d7f4392b7b2641ffe4289f │ args: jj commit -m 'new child1' ◉ d967c09eb12b38dad2065a0bc9e251824247f9f84ba406a7356f5405e4c93c21562178a3f00cafedfa1df1435ba496265f39da9d1ccebaccb78bdcb4bd7031e1 │ snapshot working copy │ args: jj commit -m 'new child1' ◉ b6d168ba4fb4534257b6e58d53eb407582567342358eab07cf5a01a7e4d797313b692f27664c2fb7935b2380d398d0298233c9732f821b8c687e35607ea08a55 │ commit 3d918700494a9895696e955b85fa05eb0d314cc6 │ args: jj commit -m initial ◉ 5e9e3f82fc14750ff985c5a39f1935ed8876b973b8800b56bc03d1c9754795e724956d862d1fcb2c533d06ca36abc9fa9f7cb7d3b2b64e993e9a87f80d5af670 │ snapshot working copy │ args: jj commit -m initial ◉ 19b8089fc78b7c49171f3c8934248be6f89f52311005e961cab5780f9f138b142456d77b27d223d7ee84d21d8c30c4a80100eaf6735b548b1acd0da688f94c80 │ add workspace 'default' ◉ f1c462c494be39f6690928603c5393f908866bc8d81d8cd1ae0bb2ea02cb4f78cafa47165fa5b7cda258e2178f846881de199066991960a80954ba6066ba0821 initialize repo "###); let op_log_lines = op_log_stdout.lines().collect_vec(); let current_op_id = op_log_lines[0].split_once(" ").unwrap().1; let previous_op_id = op_log_lines[6].split_once(" ").unwrap().1; // Another process started from the "initial" operation, but snapshots after // the "child1" checkout has been completed. std::fs::rename( op_heads_dir.join(current_op_id), op_heads_dir.join(previous_op_id), ) .unwrap(); std::fs::write(repo_path.join("child2"), "").unwrap(); let stdout = test_env.jj_cmd_success(&repo_path, &["describe", "-m", "new child2"]); insta::assert_snapshot!(stdout, @r###" Working copy now at: kkmpptxz 4011424e new child2 Parent commit : rlvkpnrz e08863ee new child1 "###); // Since the repo can be reloaded before snapshotting, "child2" should be // a child of "child1", not of "initial". let template = r#"commit_id ++ " " ++ description"#; let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", template, "-s"]); insta::assert_snapshot!(stdout, @r###" @ 4011424ea0a210a914f869ea3c47d76931598d1d new child2 │ A child2 ◉ e08863ee7a0df688755d3d3126498afdf4f580ad new child1 │ A child1 ◉ 79989e62f8331e69a803058b57bacc264405cb65 initial │ A base ◉ 0000000000000000000000000000000000000000 "###); } fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String { let template = r#"commit_id ++ " " ++ description"#; test_env.jj_cmd_success(cwd, &["log", "-T", template]) }