Debian Codegraph @

lib/tests/test_refs.rs

raw
1 // Copyright 2021 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 jj_lib::merge::Merge;
16 use jj_lib::op_store::RefTarget;
17 use jj_lib::refs::merge_ref_targets;
18 use jj_lib::repo::Repo as _;
19 use pollster::FutureExt as _;
20 use testutils::TestResult;
21 use testutils::TestWorkspace;
22 use testutils::write_random_commit;
23 use testutils::write_random_commit_with_parents;
24
25 #[test]
26 fn test_merge_ref_targets() -> TestResult {
27 let test_workspace = TestWorkspace::init();
28 let repo = &test_workspace.repo;
29
30 // 6 7
31 // |/
32 // 5
33 // | 3 4
34 // | |/
35 // | 2
36 // |/
37 // 1
38 let mut tx = repo.start_transaction();
39 let commit1 = write_random_commit(tx.repo_mut());
40 let commit2 = write_random_commit_with_parents(tx.repo_mut(), &[&commit1]);
41 let commit3 = write_random_commit_with_parents(tx.repo_mut(), &[&commit2]);
42 let commit4 = write_random_commit_with_parents(tx.repo_mut(), &[&commit2]);
43 let commit5 = write_random_commit_with_parents(tx.repo_mut(), &[&commit1]);
44 let commit6 = write_random_commit_with_parents(tx.repo_mut(), &[&commit5]);
45 let commit7 = write_random_commit_with_parents(tx.repo_mut(), &[&commit5]);
46 let repo = tx.commit("test").block_on()?;
47
48 let target1 = RefTarget::normal(commit1.id().clone());
49 let target2 = RefTarget::normal(commit2.id().clone());
50 let target3 = RefTarget::normal(commit3.id().clone());
51 let target4 = RefTarget::normal(commit4.id().clone());
52 let target5 = RefTarget::normal(commit5.id().clone());
53 let target6 = RefTarget::normal(commit6.id().clone());
54 let _target7 = RefTarget::normal(commit7.id().clone());
55
56 let index = repo.index();
57 let merge = |left: &RefTarget, base: &RefTarget, right: &RefTarget| {
58 merge_ref_targets(index, left, base, right)
59 .block_on()
60 .unwrap()
61 };
62
63 // Left moved forward
64 assert_eq!(merge(&target3, &target1, &target1), target3);
65
66 // Right moved forward
67 assert_eq!(merge(&target1, &target1, &target3), target3);
68
69 // Left moved backward
70 assert_eq!(merge(&target1, &target3, &target3), target1);
71
72 // Right moved backward
73 assert_eq!(merge(&target3, &target3, &target1), target1);
74
75 // Left moved sideways
76 assert_eq!(merge(&target4, &target3, &target3), target4);
77
78 // Right moved sideways
79 assert_eq!(merge(&target3, &target3, &target4), target4);
80
81 // Both moved sideways ("A - B + A" - type conflict)
82 assert_eq!(merge(&target4, &target3, &target4), target4);
83
84 // Both added same target ("A - B + A" - type conflict)
85 assert_eq!(merge(&target3, RefTarget::absent_ref(), &target3), target3);
86
87 // Both removed ("A - B + A" - type conflict)
88 assert_eq!(
89 merge(RefTarget::absent_ref(), &target3, RefTarget::absent_ref()),
90 RefTarget::absent()
91 );
92
93 // Left added target, right added descendant target
94 assert_eq!(merge(&target2, RefTarget::absent_ref(), &target3), target3);
95
96 // Right added target, left added descendant target
97 assert_eq!(merge(&target3, RefTarget::absent_ref(), &target2), target3);
98
99 // Both moved forward to same target
100 assert_eq!(merge(&target3, &target1, &target3), target3);
101
102 // Both moved forward, left moved further
103 assert_eq!(merge(&target3, &target1, &target2), target3);
104
105 // Both moved forward, right moved further
106 assert_eq!(merge(&target2, &target1, &target3), target3);
107
108 // Left and right moved forward to divergent targets
109 assert_eq!(
110 merge(&target3, &target1, &target4),
111 RefTarget::from_legacy_form(
112 [commit1.id().clone()],
113 [commit3.id().clone(), commit4.id().clone()]
114 )
115 );
116
117 // Left moved back, right moved forward
118 assert_eq!(
119 merge(&target1, &target2, &target3),
120 RefTarget::from_legacy_form(
121 [commit2.id().clone()],
122 [commit1.id().clone(), commit3.id().clone()]
123 )
124 );
125
126 // Right moved back, left moved forward
127 assert_eq!(
128 merge(&target3, &target2, &target1),
129 RefTarget::from_legacy_form(
130 [commit2.id().clone()],
131 [commit3.id().clone(), commit1.id().clone()]
132 )
133 );
134
135 // Left removed
136 assert_eq!(
137 merge(RefTarget::absent_ref(), &target3, &target3),
138 RefTarget::absent()
139 );
140
141 // Right removed
142 assert_eq!(
143 merge(&target3, &target3, RefTarget::absent_ref()),
144 RefTarget::absent()
145 );
146
147 // Left removed, right moved forward
148 assert_eq!(
149 merge(RefTarget::absent_ref(), &target1, &target3),
150 RefTarget::from_merge(Merge::from_vec(vec![
151 None,
152 Some(commit1.id().clone()),
153 Some(commit3.id().clone()),
154 ]))
155 );
156
157 // Right removed, left moved forward
158 assert_eq!(
159 merge(&target3, &target1, RefTarget::absent_ref()),
160 RefTarget::from_merge(Merge::from_vec(vec![
161 Some(commit3.id().clone()),
162 Some(commit1.id().clone()),
163 None,
164 ]))
165 );
166
167 // Left became conflicted, right moved forward
168 assert_eq!(
169 merge(
170 &RefTarget::from_legacy_form(
171 [commit2.id().clone()],
172 [commit3.id().clone(), commit4.id().clone()]
173 ),
174 &target1,
175 &target3
176 ),
177 // TODO: "removes" should have commit 2, just like it does in the next test case
178 RefTarget::from_legacy_form(
179 [commit1.id().clone()],
180 [commit3.id().clone(), commit4.id().clone()]
181 )
182 );
183
184 // Right became conflicted, left moved forward
185 assert_eq!(
186 merge(
187 &target3,
188 &target1,
189 &RefTarget::from_legacy_form(
190 [commit2.id().clone()],
191 [commit3.id().clone(), commit4.id().clone()]
192 ),
193 ),
194 RefTarget::from_legacy_form(
195 [commit2.id().clone()],
196 [commit4.id().clone(), commit3.id().clone()]
197 )
198 );
199
200 // Existing conflict on left, right moves an "add" sideways
201 //
202 // Under the hood, the conflict is simplified as below:
203 // ```
204 // 3 4 5 3 4 5 5 4
205 // 2 / => 2 3 => 2
206 // 3
207 // ```
208 assert_eq!(
209 merge(
210 &RefTarget::from_legacy_form(
211 [commit2.id().clone()],
212 [commit3.id().clone(), commit4.id().clone()]
213 ),
214 &target3,
215 &target5
216 ),
217 RefTarget::from_legacy_form(
218 [commit2.id().clone()],
219 [commit5.id().clone(), commit4.id().clone()]
220 )
221 );
222
223 // Existing conflict on right, left moves an "add" sideways
224 //
225 // Under the hood, the conflict is simplified as below:
226 // ```
227 // 5 3 4 5 3 4 5 4
228 // \ 2 => 3 2 => 2
229 // 3
230 // ```
231 assert_eq!(
232 merge(
233 &target5,
234 &target3,
235 &RefTarget::from_legacy_form(
236 [commit2.id().clone()],
237 [commit3.id().clone(), commit4.id().clone()]
238 ),
239 ),
240 RefTarget::from_legacy_form(
241 [commit2.id().clone()],
242 [commit5.id().clone(), commit4.id().clone()]
243 )
244 );
245
246 // Existing conflict on left, right moves an "add" backwards, past point of
247 // divergence
248 //
249 // Under the hood, the conflict is simplified as below:
250 // ```
251 // 3 4 1 3 4 1 1 4
252 // 2 / => 2 3 => 2
253 // 3
254 // ```
255 assert_eq!(
256 merge(
257 &RefTarget::from_legacy_form(
258 [commit2.id().clone()],
259 [commit3.id().clone(), commit4.id().clone()]
260 ),
261 &target3,
262 &target1
263 ),
264 RefTarget::from_legacy_form(
265 [commit2.id().clone()],
266 [commit1.id().clone(), commit4.id().clone()]
267 )
268 );
269
270 // Existing conflict on right, left moves an "add" backwards, past point of
271 // divergence
272 //
273 // Under the hood, the conflict is simplified as below:
274 // ```
275 // 1 3 4 1 3 4 1 4
276 // \ 2 => 3 2 => 2
277 // 3
278 // ```
279 assert_eq!(
280 merge(
281 &target1,
282 &target3,
283 &RefTarget::from_legacy_form(
284 [commit2.id().clone()],
285 [commit3.id().clone(), commit4.id().clone()]
286 ),
287 ),
288 RefTarget::from_legacy_form(
289 [commit2.id().clone()],
290 [commit1.id().clone(), commit4.id().clone()]
291 )
292 );
293
294 // Existing conflict on left, right undoes one side of conflict
295 assert_eq!(
296 merge(
297 &RefTarget::from_legacy_form(
298 [commit2.id().clone()],
299 [commit3.id().clone(), commit4.id().clone()]
300 ),
301 &target3,
302 &target2
303 ),
304 target4
305 );
306
307 // Existing conflict on right, left undoes one side of conflict
308 assert_eq!(
309 merge(
310 &target2,
311 &target3,
312 &RefTarget::from_legacy_form(
313 [commit2.id().clone()],
314 [commit3.id().clone(), commit4.id().clone()]
315 ),
316 ),
317 target4
318 );
319
320 // Existing conflict on left, right moves one side of conflict to the other
321 // side ("A - B + A" - type conflict)
322 assert_eq!(
323 merge(
324 &RefTarget::from_legacy_form(
325 [commit5.id().clone()], // not an ancestor of commit3, 4
326 [commit3.id().clone(), commit4.id().clone()],
327 ),
328 &target4,
329 &target3,
330 ),
331 target3
332 );
333
334 // Existing conflict on right, left moves one side of conflict to the other
335 // side ("A - B + A" - type conflict)
336 assert_eq!(
337 merge(
338 &target4,
339 &target3,
340 &RefTarget::from_legacy_form(
341 [commit5.id().clone()], // not an ancestor of commit3, 4
342 [commit3.id().clone(), commit4.id().clone()],
343 ),
344 ),
345 target4
346 );
347
348 // Existing conflict on left, right makes unrelated update
349 assert_eq!(
350 merge(
351 &RefTarget::from_legacy_form(
352 [commit2.id().clone()],
353 [commit3.id().clone(), commit4.id().clone()]
354 ),
355 &target5,
356 &target6
357 ),
358 RefTarget::from_legacy_form(
359 [commit2.id().clone(), commit5.id().clone()],
360 [
361 commit3.id().clone(),
362 commit4.id().clone(),
363 commit6.id().clone()
364 ]
365 )
366 );
367
368 // Existing conflict on right, left makes unrelated update
369 assert_eq!(
370 merge(
371 &target6,
372 &target5,
373 &RefTarget::from_legacy_form(
374 [commit2.id().clone()],
375 [commit3.id().clone(), commit4.id().clone()]
376 ),
377 ),
378 RefTarget::from_legacy_form(
379 [commit5.id().clone(), commit2.id().clone()],
380 [
381 commit6.id().clone(),
382 commit3.id().clone(),
383 commit4.id().clone()
384 ]
385 )
386 );
387 Ok(())
388 }