Debian Codegraph @

cli/tests/test_identical_commits.rs

raw
1 // Copyright 2025 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use crate::common::CommandOutput;
16 use crate::common::TestEnvironment;
17 use crate::common::TestWorkDir;
18
19 fn create_test_environment() -> TestEnvironment {
20 let mut test_env = TestEnvironment::default();
21 test_env.add_env_var("JJ_RANDOMNESS_SEED", "0");
22 test_env.add_env_var("JJ_TIMESTAMP", "2001-01-01T00:00:00+00:00");
23 test_env.add_config("experimental.record-predecessors-in-commit = false");
24 test_env
25 }
26
27 #[test]
28 fn test_identical_commits() {
29 let test_env = create_test_environment();
30 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
31 let work_dir = test_env.work_dir("repo");
32
33 work_dir.run_jj(["new", "root()", "-m=test"]).success();
34 // TODO: Should not fail
35 insta::assert_snapshot!(work_dir.run_jj(["new", "root()", "-m=test"]), @"
36 ------- stderr -------
37 Internal error: Unexpected error from backend
38 Caused by: Newly-created commit e94ed463cbb0776612e308eba2ecaae74a7f8a73 already exists
39 [EOF]
40 [exit status: 255]
41 ");
42 // There should be a single "test" commit
43 insta::assert_snapshot!(get_log_output(&work_dir), @"
44 @ e94ed463cbb0 test
45 ◆ 000000000000
46 [EOF]
47 ");
48 }
49
50 /// Create "test1" commit, then rewrite it in the same way "concurrently" (by
51 /// starting at the same operation)
52 #[test]
53 fn test_identical_commits_concurrently() {
54 let test_env = create_test_environment();
55 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
56 let work_dir = test_env.work_dir("repo");
57
58 work_dir.run_jj(["new", "root()", "-m=test1"]).success();
59 work_dir.run_jj(["describe", "-m=test2"]).success();
60 work_dir
61 .run_jj(["describe", "-m=test2", "--at-op=@-"])
62 .success();
63 // There should be a single "test2" commit
64 insta::assert_snapshot!(get_log_output(&work_dir), @"
65 @ c5abd2256ac0 test2
66 ◆ 000000000000
67 [EOF]
68 ------- stderr -------
69 Concurrent modification detected, resolving automatically.
70 [EOF]
71 ");
72 }
73
74 /// Create commit "test1", then rewrite it to "test2", then rewrite it back to
75 /// "test1"
76 #[test]
77 fn test_identical_commits_by_cycling_rewrite() {
78 let test_env = create_test_environment();
79 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
80 let work_dir = test_env.work_dir("repo");
81
82 work_dir.run_jj(["new", "root()", "-m=test1"]).success();
83 work_dir.run_jj(["describe", "-m=test2"]).success();
84 // TODO: Should not fail
85 insta::assert_snapshot!(work_dir.run_jj(["describe", "-m=test1"]), @"
86 ------- stderr -------
87 Internal error: Unexpected error from backend
88 Caused by: Newly-created commit 053222c21fa06b9492e22346f8f70e732231ad4f already exists
89 [EOF]
90 [exit status: 255]
91 ");
92 insta::assert_snapshot!(work_dir.run_jj(["evolog"]), @"
93 @ oxmtprsl [email protected] 2001-01-01 11:00:00 c5abd225
94 │ (empty) test2
95 │ -- operation 0b504315f7ba describe commit 053222c21fa06b9492e22346f8f70e732231ad4f
96 ○ oxmtprsl/1 [email protected] 2001-01-01 11:00:00 053222c2 (hidden)
97 (empty) test1
98 -- operation 166b2bc3dc53 new empty commit
99 [EOF]
100 ");
101 // TODO: Test `jj op diff --from @--`
102 }
103
104 /// Create commits "test1" and "test2" and rewrite "test1". Then rewrite "test2"
105 /// to become identical to the rewritten "test1".
106 #[test]
107 fn test_identical_commits_by_convergent_rewrite() {
108 let test_env = create_test_environment();
109 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
110 let work_dir = test_env.work_dir("repo");
111
112 work_dir.run_jj(["new", "root()", "-m=test1"]).success();
113 work_dir.run_jj(["new", "root()", "-m=test2"]).success();
114 work_dir
115 .run_jj(["describe", "-m=test3", "subject(test1)"])
116 .success();
117 // TODO: Should not fail
118 insta::assert_snapshot!(work_dir.run_jj(["describe", "-m=test3", "subject(test2)"]), @"
119 ------- stderr -------
120 Internal error: Unexpected error from backend
121 Caused by: Newly-created commit 460733f1f6f9283d5a810b231dd3f846fd3a6f04 already exists
122 [EOF]
123 [exit status: 255]
124 ");
125 // TODO: The "test3" commit should have either "test1" or "test2" as predecessor
126 // (or both?)
127 insta::assert_snapshot!(work_dir.run_jj(["evolog"]), @"
128 @ oxmtprsl/1 [email protected] 2001-01-01 11:00:00 c5abd225 (divergent)
129 (empty) test2
130 -- operation 4249d34636d2 new empty commit
131 [EOF]
132 ");
133 }
134
135 /// Create commits "test1" and "test2" and then rewrite both of them to be
136 /// identical in a single operation
137 #[test]
138 fn test_identical_commits_by_convergent_rewrite_one_operation() {
139 let test_env = create_test_environment();
140 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
141 let work_dir = test_env.work_dir("repo");
142
143 work_dir.run_jj(["new", "root()", "-m=test1"]).success();
144 work_dir.run_jj(["new", "root()", "-m=test2"]).success();
145 // TODO: Should not fail
146 insta::assert_snapshot!(work_dir.run_jj(["describe", "-m=test3", "root()+"]), @"
147 ------- stderr -------
148 Internal error: Unexpected error from backend
149 Caused by: Newly-created commit 460733f1f6f9283d5a810b231dd3f846fd3a6f04 already exists
150 [EOF]
151 [exit status: 255]
152 ");
153 // TODO: There should be a single "test3" commit
154 insta::assert_snapshot!(get_log_output(&work_dir), @"
155 @ c5abd2256ac0 test2
156 │ ○ 053222c21fa0 test1
157 ├─╯
158 ◆ 000000000000
159 [EOF]
160 ");
161 // TODO: The "test3" commit should have either "test1" or "test2" as predecessor
162 // (or both?)
163 insta::assert_snapshot!(work_dir.run_jj(["evolog"]), @"
164 @ oxmtprsl/0 [email protected] 2001-01-01 11:00:00 c5abd225 (divergent)
165 (empty) test2
166 -- operation 4249d34636d2 new empty commit
167 [EOF]
168 ");
169 }
170
171 /// Create two stacked commits. Then reorder them so they become rewrites of
172 /// each other.
173 #[test]
174 fn test_identical_commits_swap_by_reordering() {
175 let test_env = create_test_environment();
176 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
177 let work_dir = test_env.work_dir("repo");
178
179 work_dir.run_jj(["new", "root()", "-m=test"]).success();
180 work_dir.run_jj(["new", "-m=test"]).success();
181 // Test the setup
182 insta::assert_snapshot!(get_log_output(&work_dir), @"
183 @ 5bae90c9b34d test
184 ○ e94ed463cbb0 test
185 ◆ 000000000000
186 [EOF]
187 ");
188 // TODO: Should not fail
189 insta::assert_snapshot!(work_dir.run_jj(["rebase", "-r=@", "-B=@-"]), @"
190 ------- stderr -------
191 Internal error: Unexpected error from backend
192 Caused by: Newly-created commit e94ed463cbb0776612e308eba2ecaae74a7f8a73 already exists
193 [EOF]
194 [exit status: 255]
195 ");
196 // There same two commits should still be visible
197 insta::assert_snapshot!(get_log_output(&work_dir), @"
198 @ 5bae90c9b34d test
199 ○ e94ed463cbb0 test
200 ◆ 000000000000
201 [EOF]
202 ");
203 // TODO: Each commit should be a predecessor of the other
204 insta::assert_snapshot!(work_dir.run_jj(["evolog", "-r=@"]), @"
205 @ oxmtprsl/0 [email protected] 2001-01-01 11:00:00 5bae90c9 (divergent)
206 (empty) test
207 -- operation af5cee55f7e1 new empty commit
208 [EOF]
209 ");
210 insta::assert_snapshot!(work_dir.run_jj(["evolog", "-r=@-"]), @"
211 ○ oxmtprsl/1 [email protected] 2001-01-01 11:00:00 e94ed463 (divergent)
212 (empty) test
213 -- operation 9d92250a8782 new empty commit
214 [EOF]
215 ");
216 // TODO: Test that `jj op show` displays something reasonable
217 }
218
219 #[must_use]
220 fn get_log_output(work_dir: &TestWorkDir) -> CommandOutput {
221 let template = r#"commit_id.short() ++ " " ++ description"#;
222 work_dir.run_jj(["log", "-T", template])
223 }