Debian Codegraph @

lib/tests/test_rewrite.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 std::slice;
16
17 use assert_matches::assert_matches;
18 use itertools::Itertools as _;
19 use jj_lib::backend::ChangeId;
20 use jj_lib::commit::Commit;
21 use jj_lib::matchers::EverythingMatcher;
22 use jj_lib::matchers::FilesMatcher;
23 use jj_lib::merge::Merge;
24 use jj_lib::merged_tree::MergedTree;
25 use jj_lib::op_store::RefTarget;
26 use jj_lib::op_store::RemoteRef;
27 use jj_lib::op_store::RemoteRefState;
28 use jj_lib::ref_name::RefName;
29 use jj_lib::ref_name::RemoteName;
30 use jj_lib::ref_name::RemoteRefSymbol;
31 use jj_lib::ref_name::WorkspaceName;
32 use jj_lib::ref_name::WorkspaceNameBuf;
33 use jj_lib::repo::Repo as _;
34 use jj_lib::revset::RevsetExpression;
35 use jj_lib::rewrite::CommitRewriter;
36 use jj_lib::rewrite::CommitWithSelection;
37 use jj_lib::rewrite::EmptyBehavior;
38 use jj_lib::rewrite::MoveCommitsTarget;
39 use jj_lib::rewrite::RebaseOptions;
40 use jj_lib::rewrite::RebasedCommit;
41 use jj_lib::rewrite::RewriteRefsOptions;
42 use jj_lib::rewrite::find_duplicate_divergent_commits;
43 use jj_lib::rewrite::find_recursive_merge_commits;
44 use jj_lib::rewrite::merge_commit_trees;
45 use jj_lib::rewrite::merge_commit_trees_no_resolve;
46 use jj_lib::rewrite::rebase_commit_with_options;
47 use jj_lib::rewrite::restore_tree;
48 use maplit::hashmap;
49 use maplit::hashset;
50 use pollster::FutureExt as _;
51 use test_case::test_case;
52 use testutils::CommitBuilderExt as _;
53 use testutils::TestRepo;
54 use testutils::TestResult;
55 use testutils::assert_abandoned_with_parent;
56 use testutils::assert_rebased_onto;
57 use testutils::assert_tree_eq;
58 use testutils::create_random_commit;
59 use testutils::create_tree;
60 use testutils::create_tree_with;
61 use testutils::rebase_descendants_with_options_return_map;
62 use testutils::repo_path;
63 use testutils::write_random_commit;
64 use testutils::write_random_commit_with_parents;
65
66 fn remote_symbol<'a, N, M>(name: &'a N, remote: &'a M) -> RemoteRefSymbol<'a>
67 where
68 N: AsRef<RefName> + ?Sized,
69 M: AsRef<RemoteName> + ?Sized,
70 {
71 RemoteRefSymbol {
72 name: name.as_ref(),
73 remote: remote.as_ref(),
74 }
75 }
76
77 /// Based on https://lore.kernel.org/git/[email protected]/
78 /// (found in t/t6401-merge-criss-cross.sh in the git.git repo).
79 #[test]
80 fn test_merge_criss_cross() -> TestResult {
81 let test_repo = TestRepo::init();
82 let repo = &test_repo.repo;
83
84 let path = repo_path("file");
85 let tree_a = create_tree(repo, &[(path, "1\n2\n3\n4\n5\n6\n7\n8\n9\n")]);
86 let tree_b = create_tree(repo, &[(path, "1\n2\n3\n4\n5\n6\n7\n8B\n9\n")]);
87 let tree_c = create_tree(repo, &[(path, "1\n2\n3C\n4\n5\n6\n7\n8\n9\n")]);
88 let tree_d = create_tree(repo, &[(path, "1\n2\n3C\n4\n5\n6\n7\n8D\n9\n")]);
89 let tree_e = create_tree(repo, &[(path, "1\n2\n3E\n4\n5\n6\n7\n8B\n9\n")]);
90 let tree_expected = create_tree(repo, &[(path, "1\n2\n3E\n4\n5\n6\n7\n8D\n9\n")]);
91
92 let mut tx = repo.start_transaction();
93 let mut make_commit = |description, parents, tree| {
94 tx.repo_mut()
95 .new_commit(parents, tree)
96 .set_description(description)
97 .write_unwrap()
98 };
99 let commit_a = make_commit(
100 "A",
101 vec![repo.store().root_commit_id().clone()],
102 tree_a.clone(),
103 );
104 let commit_b = make_commit("B", vec![commit_a.id().clone()], tree_b.clone());
105 let commit_c = make_commit("C", vec![commit_a.id().clone()], tree_c.clone());
106 let commit_d = make_commit(
107 "D",
108 vec![commit_b.id().clone(), commit_c.id().clone()],
109 tree_d.clone(),
110 );
111 let commit_e = make_commit(
112 "E",
113 vec![commit_b.id().clone(), commit_c.id().clone()],
114 tree_e.clone(),
115 );
116 let merged =
117 merge_commit_trees(tx.repo_mut(), &[commit_d.clone(), commit_e.clone()]).block_on()?;
118 assert_tree_eq!(merged, tree_expected);
119
120 let tree_unresolved_expected = MergedTree::merge_no_resolve(Merge::from_vec(vec![
121 (tree_d, commit_d.conflict_label()),
122 (tree_b, commit_b.conflict_label()),
123 (tree_a, commit_a.conflict_label()),
124 (tree_c, commit_c.conflict_label()),
125 (tree_e, commit_e.conflict_label()),
126 ]));
127 let unresolved_merged =
128 merge_commit_trees_no_resolve(tx.repo_mut(), &[commit_d, commit_e]).block_on()?;
129 assert_tree_eq!(unresolved_merged, tree_unresolved_expected);
130 assert_tree_eq!(unresolved_merged.resolve().block_on()?, tree_expected);
131
132 Ok(())
133 }
134
135 #[test]
136 fn test_find_recursive_merge_commits() -> TestResult {
137 let test_repo = TestRepo::init();
138 let repo = &test_repo.repo;
139
140 // G H I
141 // |X|X|
142 // D E F
143 // |X|/
144 // B C
145 // |/
146 // A
147 let mut tx = repo.start_transaction();
148 let commit_a = write_random_commit(tx.repo_mut());
149 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
150 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
151 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b, &commit_c]);
152 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b, &commit_c]);
153 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);
154 let commit_g = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d, &commit_e]);
155 let commit_h =
156 write_random_commit_with_parents(tx.repo_mut(), &[&commit_d, &commit_e, &commit_f]);
157 let commit_i = write_random_commit_with_parents(tx.repo_mut(), &[&commit_e, &commit_f]);
158
159 let commit_id_merge = find_recursive_merge_commits(
160 tx.repo().store(),
161 tx.repo().index(),
162 vec![commit_d.id().clone(), commit_e.id().clone()],
163 )
164 .block_on()?;
165 assert_eq!(
166 commit_id_merge,
167 Merge::from_vec(vec![
168 commit_d.id().clone(),
169 commit_b.id().clone(),
170 commit_a.id().clone(),
171 commit_c.id().clone(),
172 commit_e.id().clone(),
173 ])
174 );
175
176 let commit_id_merge = find_recursive_merge_commits(
177 tx.repo().store(),
178 tx.repo().index(),
179 vec![
180 commit_g.id().clone(),
181 commit_h.id().clone(),
182 commit_i.id().clone(),
183 ],
184 )
185 .block_on()?;
186 assert_eq!(
187 commit_id_merge,
188 Merge::from_vec(vec![
189 commit_g.id().clone(),
190 commit_a.id().clone(),
191 commit_b.id().clone(),
192 commit_d.id().clone(),
193 commit_c.id().clone(),
194 commit_e.id().clone(),
195 commit_h.id().clone(),
196 commit_e.id().clone(),
197 commit_c.id().clone(),
198 commit_f.id().clone(),
199 commit_i.id().clone(),
200 ])
201 );
202
203 Ok(())
204 }
205
206 #[test]
207 fn test_restore_tree() -> TestResult {
208 let test_repo = TestRepo::init();
209 let repo = &test_repo.repo;
210
211 let path1 = repo_path("file1");
212 let path2 = repo_path("dir1/file2");
213 let path3 = repo_path("dir1/file3");
214 let path4 = repo_path("dir2/file4");
215 let left = create_tree(repo, &[(path2, "left"), (path3, "left"), (path4, "left")]);
216 let right = create_tree(
217 repo,
218 &[(path1, "right"), (path2, "right"), (path3, "right")],
219 );
220
221 // Restore everything using EverythingMatcher
222 let restored = restore_tree(
223 &left,
224 &right,
225 "left side".into(),
226 "right side".into(),
227 &EverythingMatcher,
228 )
229 .block_on()?;
230 assert_tree_eq!(restored, left);
231
232 // Restore everything using FilesMatcher
233 let restored = restore_tree(
234 &left,
235 &right,
236 "left side".into(),
237 "right side".into(),
238 &FilesMatcher::new([&path1, &path2, &path3, &path4]),
239 )
240 .block_on()?;
241 assert_tree_eq!(restored, left);
242
243 // Restore some files
244 let restored = restore_tree(
245 &left,
246 &right,
247 "left side".into(),
248 "right side".into(),
249 &FilesMatcher::new([path1, path2]),
250 )
251 .block_on()?;
252 let expected = create_tree(repo, &[(path2, "left"), (path3, "right")]);
253 assert_tree_eq!(restored, expected);
254 Ok(())
255 }
256
257 #[test]
258 fn test_restore_tree_with_conflicts() -> TestResult {
259 let test_repo = TestRepo::init();
260 let repo = &test_repo.repo;
261
262 let path1 = repo_path("file1");
263 let path2 = repo_path("dir1/file2");
264 let path3 = repo_path("dir1/file3");
265
266 // Create two 2-sided conflicts.
267 let left_side1 = create_tree(repo, &[(path1, "left side 1"), (path2, "left side 1")]);
268 let left_base1 = create_tree(repo, &[(path1, "left base 1"), (path2, "left base 1")]);
269 let left_side2 = create_tree(repo, &[(path1, "left side 2"), (path2, "left side 2")]);
270 let right_side1 = create_tree(repo, &[(path1, "right side 1"), (path2, "right side 1")]);
271 let right_base1 = create_tree(repo, &[(path1, "right base"), (path2, "right base")]);
272 let right_side2 = create_tree(repo, &[(path1, "right side 2"), (path2, "right side 2")]);
273
274 let left = MergedTree::merge(Merge::from_vec(vec![
275 (left_side1, "left side 1".into()),
276 (left_base1, "left base 1".into()),
277 (left_side2, "left side 2".into()),
278 ]))
279 .block_on()?;
280 let right = MergedTree::merge(Merge::from_vec(vec![
281 (right_side1.clone(), "right side 1".into()),
282 (right_base1.clone(), "right base 1".into()),
283 (right_side2.clone(), "right side 2".into()),
284 ]))
285 .block_on()?;
286
287 // Restore everything using EverythingMatcher
288 let restored = restore_tree(
289 &left,
290 &right,
291 "left side".into(),
292 "right side".into(),
293 &EverythingMatcher,
294 )
295 .block_on()?;
296 assert_tree_eq!(restored, left);
297
298 // Restore a single conflicted file
299 let restored = restore_tree(
300 &left,
301 &right,
302 "left side".into(),
303 "right side".into(),
304 &FilesMatcher::new([path1]),
305 )
306 .block_on()?;
307
308 // After simplifying, the result should have path 1 from the left side and path
309 // 2 from the right side.
310 assert_eq!(
311 restored.path_value(path1).block_on()?.simplify(),
312 left.path_value(path1).block_on()?.simplify()
313 );
314 assert_eq!(
315 restored.path_value(path2).block_on()?.simplify(),
316 right.path_value(path2).block_on()?.simplify()
317 );
318
319 let expected_base_side1 = create_tree(repo, &[(path1, "right side 1")]);
320 let expected_base_base1 = create_tree(repo, &[(path1, "right base")]);
321 let expected_base_side2 = create_tree(repo, &[(path1, "right side 2")]);
322 let expected_left_side1 = create_tree(repo, &[(path1, "left side 1")]);
323 let expected_left_base1 = create_tree(repo, &[(path1, "left base 1")]);
324 let expected_left_side2 = create_tree(repo, &[(path1, "left side 2")]);
325 let expected = MergedTree::merge_no_resolve(Merge::from_vec(vec![
326 (right_side1.clone(), "right side 1".into()),
327 (right_base1.clone(), "right base 1".into()),
328 (right_side2.clone(), "right side 2".into()),
329 (
330 expected_base_side2.clone(),
331 "base files for restore (from right side 2)".into(),
332 ),
333 (
334 expected_base_base1.clone(),
335 "base files for restore (from right base 1)".into(),
336 ),
337 (
338 expected_base_side1.clone(),
339 "base files for restore (from right side 1)".into(),
340 ),
341 (expected_left_side1, "left side 1".into()),
342 (expected_left_base1, "left base 1".into()),
343 (expected_left_side2, "left side 2".into()),
344 ]));
345 assert_tree_eq!(restored, expected);
346
347 // Create a 3-sided conflict and a 2-sided conflict.
348 let left_side1 = create_tree(
349 repo,
350 &[
351 (path1, "left side 1"),
352 (path2, "left side 1"),
353 (path3, "left"),
354 ],
355 );
356 let left_base1 = create_tree(
357 repo,
358 &[
359 (path1, "left base"),
360 (path2, "left base 1"),
361 (path3, "left"),
362 ],
363 );
364 let left_side2 = create_tree(
365 repo,
366 &[
367 (path1, "left side 2"),
368 (path2, "left side 2"),
369 (path3, "left"),
370 ],
371 );
372 let left_base2 = create_tree(
373 repo,
374 &[
375 (path1, "left base"),
376 (path2, "left base 2"),
377 (path3, "left"),
378 ],
379 );
380 let left_side3 = create_tree(
381 repo,
382 &[
383 (path1, "left base"),
384 (path2, "left side 3"),
385 (path3, "left"),
386 ],
387 );
388 let right_side1 = create_tree(
389 repo,
390 &[
391 (path1, "right side 1"),
392 (path2, "resolved"),
393 (path3, "right"),
394 ],
395 );
396 let right_base1 = create_tree(
397 repo,
398 &[(path1, "right base"), (path2, "resolved"), (path3, "right")],
399 );
400 let right_side2 = create_tree(
401 repo,
402 &[
403 (path1, "right side 2"),
404 (path2, "resolved"),
405 (path3, "right"),
406 ],
407 );
408
409 let left = MergedTree::merge(Merge::from_vec(vec![
410 (left_side1, "left side 1".into()),
411 (left_base1, "left base 1".into()),
412 (left_side2, "left side 2".into()),
413 (left_base2, "left base 2".into()),
414 (left_side3, "left side 3".into()),
415 ]))
416 .block_on()?;
417 let right = MergedTree::merge(Merge::from_vec(vec![
418 (right_side1.clone(), "right side 1".into()),
419 (right_base1.clone(), "right base 1".into()),
420 (right_side2.clone(), "right side 2".into()),
421 ]))
422 .block_on()?;
423
424 // Restore everything using EverythingMatcher
425 let restored = restore_tree(
426 &left,
427 &right,
428 "left side".into(),
429 "right side".into(),
430 &EverythingMatcher,
431 )
432 .block_on()?;
433 assert_tree_eq!(restored, left);
434
435 // Restore a single conflicted file
436 let restored = restore_tree(
437 &left,
438 &right,
439 "left side".into(),
440 "right side".into(),
441 &FilesMatcher::new([path2]),
442 )
443 .block_on()?;
444
445 // After simplifying, the result should have paths 1 and 3 from the right side
446 // and path 2 from the left side.
447 assert_eq!(
448 restored.path_value(path1).block_on()?.simplify(),
449 right.path_value(path1).block_on()?.simplify()
450 );
451 assert_eq!(
452 restored.path_value(path2).block_on()?.simplify(),
453 left.path_value(path2).block_on()?.simplify()
454 );
455 assert_eq!(
456 restored.path_value(path3).block_on()?.simplify(),
457 right.path_value(path3).block_on()?.simplify()
458 );
459
460 // path3 is included in all trees due to `MergedTree::resolve`.
461 let expected_base = create_tree(repo, &[(path2, "resolved"), (path3, "right")]);
462 let expected_left_side1 = create_tree(repo, &[(path2, "left side 1"), (path3, "right")]);
463 let expected_left_base1 = create_tree(repo, &[(path2, "left base 1"), (path3, "right")]);
464 let expected_left_side2 = create_tree(repo, &[(path2, "left side 2"), (path3, "right")]);
465 let expected_left_base2 = create_tree(repo, &[(path2, "left base 2"), (path3, "right")]);
466 let expected_left_side3 = create_tree(repo, &[(path2, "left side 3"), (path3, "right")]);
467 let expected = MergedTree::merge_no_resolve(Merge::from_vec(vec![
468 (right_side1.clone(), "right side 1".into()),
469 (right_base1.clone(), "right base 1".into()),
470 (right_side2.clone(), "right side 2".into()),
471 (
472 expected_base,
473 "base files for restore (from right side)".into(),
474 ),
475 (expected_left_side1, "left side 1".into()),
476 (expected_left_base1, "left base 1".into()),
477 (expected_left_side2, "left side 2".into()),
478 (expected_left_base2, "left base 2".into()),
479 (expected_left_side3, "left side 3".into()),
480 ]));
481 assert_tree_eq!(restored, expected);
482
483 // Restore a single resolved file
484 let restored = restore_tree(
485 &left,
486 &right,
487 "left side".into(),
488 "right side".into(),
489 &FilesMatcher::new([path3]),
490 )
491 .block_on()?;
492
493 // After simplifying, the result should have paths 1 and 2 from the right side
494 // and path 3 from the left side.
495 assert_eq!(
496 restored.path_value(path1).block_on()?.simplify(),
497 right.path_value(path1).block_on()?.simplify()
498 );
499 assert_eq!(
500 restored.path_value(path2).block_on()?.simplify(),
501 right.path_value(path2).block_on()?.simplify()
502 );
503 assert_eq!(
504 restored.path_value(path3).block_on()?.simplify(),
505 left.path_value(path3).block_on()?.simplify()
506 );
507
508 // path3 is updated in the existing conflict terms due to `MergedTree::resolve`.
509 // If we implement https://github.com/jj-vcs/jj/issues/4152, the conflict would
510 // instead have an extra side showing the diff from the restore.
511 let expected_side1 = create_tree(
512 repo,
513 &[
514 (path1, "right side 1"),
515 (path2, "resolved"),
516 (path3, "left"),
517 ],
518 );
519 let expected_base1 = create_tree(
520 repo,
521 &[(path1, "right base"), (path2, "resolved"), (path3, "left")],
522 );
523 let expected_side2 = create_tree(
524 repo,
525 &[
526 (path1, "right side 2"),
527 (path2, "resolved"),
528 (path3, "left"),
529 ],
530 );
531 let expected = MergedTree::merge_no_resolve(Merge::from_vec(vec![
532 (expected_side1.clone(), "right side 1".into()),
533 (expected_base1.clone(), "right base 1".into()),
534 (expected_side2.clone(), "right side 2".into()),
535 ]));
536 assert_tree_eq!(restored, expected);
537 Ok(())
538 }
539
540 #[test]
541 fn test_rebase_descendants_sideways() {
542 let test_repo = TestRepo::init();
543 let repo = &test_repo.repo;
544
545 // Commit B was replaced by commit F. Commits C-E should be rebased.
546 //
547 // F
548 // | D
549 // | C E
550 // | |/
551 // | B
552 // |/
553 // A
554 let mut tx = repo.start_transaction();
555 let commit_a = write_random_commit(tx.repo_mut());
556 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
557 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
558 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);
559 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
560 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
561
562 tx.repo_mut()
563 .set_rewritten_commit(commit_b.id().clone(), commit_f.id().clone());
564 let rebase_map =
565 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
566 assert_eq!(rebase_map.len(), 3);
567 let new_commit_c = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_c, &[commit_f.id()]);
568 let new_commit_d =
569 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_d, &[new_commit_c.id()]);
570 let new_commit_e = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_e, &[commit_f.id()]);
571
572 assert_eq!(
573 *tx.repo().view().heads(),
574 hashset! {
575 new_commit_d.id().clone(),
576 new_commit_e.id().clone()
577 }
578 );
579 }
580
581 #[test]
582 fn test_rebase_descendants_forward() {
583 let test_repo = TestRepo::init();
584 let repo = &test_repo.repo;
585
586 // Commit B was replaced by commit F. Commits C and E should be rebased onto F.
587 // Commit D does not get rebased because it's an ancestor of the
588 // destination. Commit G does not get replaced because it's already in
589 // place.
590 // TODO: The above is not what actually happens! The test below shows what
591 // actually happens: D and F also get rebased onto F, so we end up with
592 // duplicates. Consider if it's worth supporting the case above better or if
593 // that decision belongs with the caller (as we currently force it to do by
594 // not supporting it in DescendantRebaser).
595 //
596 // G
597 // F E
598 // |/
599 // D C
600 // |/
601 // B
602 // A
603 let mut tx = repo.start_transaction();
604 let commit_a = write_random_commit(tx.repo_mut());
605 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
606 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
607 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
608 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d]);
609 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d]);
610 let commit_g = write_random_commit_with_parents(tx.repo_mut(), &[&commit_f]);
611
612 tx.repo_mut()
613 .set_rewritten_commit(commit_b.id().clone(), commit_f.id().clone());
614 let rebase_map =
615 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
616 let new_commit_d =
617 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_d, &[(commit_f.id())]);
618 let new_commit_f =
619 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_f, &[new_commit_d.id()]);
620 let new_commit_c =
621 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_c, &[new_commit_f.id()]);
622 let new_commit_e =
623 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_e, &[new_commit_d.id()]);
624 let new_commit_g =
625 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_g, &[new_commit_f.id()]);
626 assert_eq!(rebase_map.len(), 5);
627
628 assert_eq!(
629 *tx.repo().view().heads(),
630 hashset! {
631 new_commit_c.id().clone(),
632 new_commit_e.id().clone(),
633 new_commit_g.id().clone(),
634 }
635 );
636 }
637
638 #[test]
639 fn test_rebase_descendants_reorder() {
640 let test_repo = TestRepo::init();
641 let repo = &test_repo.repo;
642
643 // Commit E was replaced by commit D, and commit C was replaced by commit F
644 // (attempting to to reorder C and E), and commit G was replaced by commit
645 // H.
646 //
647 // I
648 // G H
649 // E F
650 // C D
651 // |/
652 // B
653 // A
654 let mut tx = repo.start_transaction();
655 let commit_a = write_random_commit(tx.repo_mut());
656 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
657 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
658 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
659 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);
660 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d]);
661 let commit_g = write_random_commit_with_parents(tx.repo_mut(), &[&commit_e]);
662 let commit_h = write_random_commit_with_parents(tx.repo_mut(), &[&commit_f]);
663 let commit_i = write_random_commit_with_parents(tx.repo_mut(), &[&commit_g]);
664
665 tx.repo_mut()
666 .set_rewritten_commit(commit_e.id().clone(), commit_d.id().clone());
667 tx.repo_mut()
668 .set_rewritten_commit(commit_c.id().clone(), commit_f.id().clone());
669 tx.repo_mut()
670 .set_rewritten_commit(commit_g.id().clone(), commit_h.id().clone());
671 let rebase_map =
672 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
673 let new_commit_i = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_i, &[commit_h.id()]);
674 assert_eq!(rebase_map.len(), 1);
675
676 assert_eq!(
677 *tx.repo().view().heads(),
678 hashset! {
679 new_commit_i.id().clone(),
680 }
681 );
682 }
683
684 #[test]
685 fn test_rebase_descendants_backward() {
686 let test_repo = TestRepo::init();
687 let repo = &test_repo.repo;
688
689 // Commit C was replaced by commit B. Commit D should be rebased.
690 //
691 // D
692 // C
693 // B
694 // A
695 let mut tx = repo.start_transaction();
696 let commit_a = write_random_commit(tx.repo_mut());
697 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
698 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
699 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);
700
701 tx.repo_mut()
702 .set_rewritten_commit(commit_c.id().clone(), commit_b.id().clone());
703 let rebase_map =
704 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
705 let new_commit_d = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_d, &[commit_b.id()]);
706 assert_eq!(rebase_map.len(), 1);
707
708 assert_eq!(
709 *tx.repo().view().heads(),
710 hashset! {new_commit_d.id().clone()}
711 );
712 }
713
714 #[test]
715 fn test_rebase_descendants_chain_becomes_branchy() {
716 let test_repo = TestRepo::init();
717 let repo = &test_repo.repo;
718
719 // Commit B was replaced by commit E and commit C was replaced by commit F.
720 // Commit F should get rebased onto E, and commit D should get rebased onto
721 // the rebased F.
722 //
723 // D
724 // C F
725 // |/
726 // B E
727 // |/
728 // A
729 let mut tx = repo.start_transaction();
730 let commit_a = write_random_commit(tx.repo_mut());
731 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
732 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
733 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);
734 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
735 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
736
737 tx.repo_mut()
738 .set_rewritten_commit(commit_b.id().clone(), commit_e.id().clone());
739 tx.repo_mut()
740 .set_rewritten_commit(commit_c.id().clone(), commit_f.id().clone());
741 let rebase_map =
742 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
743 let new_commit_f = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_f, &[commit_e.id()]);
744 let new_commit_d =
745 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_d, &[new_commit_f.id()]);
746 assert_eq!(rebase_map.len(), 2);
747
748 assert_eq!(
749 *tx.repo().view().heads(),
750 hashset! {
751 new_commit_d.id().clone(),
752 }
753 );
754 }
755
756 #[test]
757 fn test_rebase_descendants_internal_merge() {
758 let test_repo = TestRepo::init();
759 let repo = &test_repo.repo;
760
761 // Commit B was replaced by commit F. Commits C-E should be rebased.
762 //
763 // F
764 // | E
765 // | |\
766 // | C D
767 // | |/
768 // | B
769 // |/
770 // A
771 let mut tx = repo.start_transaction();
772 let commit_a = write_random_commit(tx.repo_mut());
773 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
774 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
775 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
776 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c, &commit_d]);
777 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
778
779 tx.repo_mut()
780 .set_rewritten_commit(commit_b.id().clone(), commit_f.id().clone());
781 let rebase_map =
782 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
783 let new_commit_c = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_c, &[commit_f.id()]);
784 let new_commit_d = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_d, &[commit_f.id()]);
785 let new_commit_e = assert_rebased_onto(
786 tx.repo_mut(),
787 &rebase_map,
788 &commit_e,
789 &[new_commit_c.id(), new_commit_d.id()],
790 );
791 assert_eq!(rebase_map.len(), 3);
792
793 assert_eq!(
794 *tx.repo().view().heads(),
795 hashset! { new_commit_e.id().clone() }
796 );
797 }
798
799 #[test]
800 fn test_rebase_descendants_external_merge() {
801 let test_repo = TestRepo::init();
802 let repo = &test_repo.repo;
803
804 // Commit C was replaced by commit F. Commits E should be rebased. The rebased
805 // commit E should have F as first parent and commit D as second parent.
806 //
807 // F
808 // | E
809 // | |\
810 // | C D
811 // | |/
812 // | B
813 // |/
814 // A
815 let mut tx = repo.start_transaction();
816 let commit_a = write_random_commit(tx.repo_mut());
817 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
818 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
819 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
820 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c, &commit_d]);
821 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
822
823 tx.repo_mut()
824 .set_rewritten_commit(commit_c.id().clone(), commit_f.id().clone());
825 let rebase_map =
826 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
827 let new_commit_e = assert_rebased_onto(
828 tx.repo_mut(),
829 &rebase_map,
830 &commit_e,
831 &[commit_f.id(), commit_d.id()],
832 );
833 assert_eq!(rebase_map.len(), 1);
834
835 assert_eq!(
836 *tx.repo().view().heads(),
837 hashset! {new_commit_e.id().clone()}
838 );
839 }
840
841 #[test]
842 fn test_rebase_descendants_abandon() {
843 let test_repo = TestRepo::init();
844 let repo = &test_repo.repo;
845
846 // Commit B and commit E were abandoned. Commit C and commit D should get
847 // rebased onto commit A. Commit F should get rebased onto the new commit D.
848 //
849 // F
850 // E
851 // D C
852 // |/
853 // B
854 // A
855 let mut tx = repo.start_transaction();
856 let commit_a = write_random_commit(tx.repo_mut());
857 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
858 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
859 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
860 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d]);
861 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_e]);
862
863 tx.repo_mut().record_abandoned_commit(&commit_b);
864 tx.repo_mut().record_abandoned_commit(&commit_e);
865 let rebase_map =
866 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
867 let new_commit_c = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_c, &[commit_a.id()]);
868 let new_commit_d = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_d, &[commit_a.id()]);
869 let new_commit_f =
870 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_f, &[new_commit_d.id()]);
871 assert_eq!(rebase_map.len(), 3);
872
873 assert_eq!(
874 *tx.repo().view().heads(),
875 hashset! {
876 new_commit_c.id().clone(),
877 new_commit_f.id().clone()
878 }
879 );
880 }
881
882 #[test]
883 fn test_rebase_descendants_abandon_no_descendants() {
884 let test_repo = TestRepo::init();
885 let repo = &test_repo.repo;
886
887 // Commit B and C were abandoned. Commit A should become a head.
888 //
889 // C
890 // B
891 // A
892 let mut tx = repo.start_transaction();
893 let commit_a = write_random_commit(tx.repo_mut());
894 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
895 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
896
897 tx.repo_mut().record_abandoned_commit(&commit_b);
898 tx.repo_mut().record_abandoned_commit(&commit_c);
899 let rebase_map =
900 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
901 assert_eq!(rebase_map.len(), 0);
902
903 assert_eq!(
904 *tx.repo().view().heads(),
905 hashset! {
906 commit_a.id().clone(),
907 }
908 );
909 }
910
911 #[test]
912 fn test_rebase_descendants_abandon_and_replace() {
913 let test_repo = TestRepo::init();
914 let repo = &test_repo.repo;
915
916 // Commit B was replaced by commit E. Commit C was abandoned. Commit D should
917 // get rebased onto commit E.
918 //
919 // D
920 // C
921 // E B
922 // |/
923 // A
924 let mut tx = repo.start_transaction();
925 let commit_a = write_random_commit(tx.repo_mut());
926 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
927 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
928 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);
929 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
930
931 tx.repo_mut()
932 .set_rewritten_commit(commit_b.id().clone(), commit_e.id().clone());
933 tx.repo_mut().record_abandoned_commit(&commit_c);
934 let rebase_map =
935 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
936 let new_commit_d = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_d, &[commit_e.id()]);
937 assert_eq!(rebase_map.len(), 1);
938
939 assert_eq!(
940 *tx.repo().view().heads(),
941 hashset! { new_commit_d.id().clone()}
942 );
943 }
944
945 #[test]
946 fn test_rebase_descendants_abandon_degenerate_merge_simplify() {
947 let test_repo = TestRepo::init();
948 let repo = &test_repo.repo;
949
950 // Commit B was abandoned. Commit D should get rebased to have only C as parent
951 // (not A and C).
952 //
953 // D
954 // |\
955 // B C
956 // |/
957 // A
958 let mut tx = repo.start_transaction();
959 let commit_a = write_random_commit(tx.repo_mut());
960 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
961 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
962 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b, &commit_c]);
963
964 tx.repo_mut().record_abandoned_commit(&commit_b);
965 let rebase_map = rebase_descendants_with_options_return_map(
966 tx.repo_mut(),
967 &RebaseOptions {
968 simplify_ancestor_merge: true,
969 ..Default::default()
970 },
971 );
972 let new_commit_d = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_d, &[commit_c.id()]);
973 assert_eq!(rebase_map.len(), 1);
974
975 assert_eq!(
976 *tx.repo().view().heads(),
977 hashset! {new_commit_d.id().clone()}
978 );
979 }
980
981 #[test]
982 fn test_rebase_descendants_abandon_degenerate_merge_preserve() {
983 let test_repo = TestRepo::init();
984 let repo = &test_repo.repo;
985
986 // Commit B was abandoned. Commit D should get rebased to have A and C as
987 // parents.
988 //
989 // D
990 // |\
991 // B C
992 // |/
993 // A
994 let mut tx = repo.start_transaction();
995 let commit_a = write_random_commit(tx.repo_mut());
996 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
997 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
998 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b, &commit_c]);
999
1000 tx.repo_mut().record_abandoned_commit(&commit_b);
1001 let rebase_map = rebase_descendants_with_options_return_map(
1002 tx.repo_mut(),
1003 &RebaseOptions {
1004 simplify_ancestor_merge: false,
1005 ..Default::default()
1006 },
1007 );
1008 let new_commit_d = assert_rebased_onto(
1009 tx.repo_mut(),
1010 &rebase_map,
1011 &commit_d,
1012 &[commit_a.id(), commit_c.id()],
1013 );
1014 assert_eq!(rebase_map.len(), 1);
1015
1016 assert_eq!(
1017 *tx.repo().view().heads(),
1018 hashset! {new_commit_d.id().clone()}
1019 );
1020 }
1021
1022 #[test]
1023 fn test_rebase_descendants_abandon_widen_merge() {
1024 let test_repo = TestRepo::init();
1025 let repo = &test_repo.repo;
1026
1027 // Commit E was abandoned. Commit F should get rebased to have B, C, and D as
1028 // parents (in that order).
1029 //
1030 // F
1031 // |\
1032 // E \
1033 // |\ \
1034 // B C D
1035 // \|/
1036 // A
1037 let mut tx = repo.start_transaction();
1038 let commit_a = write_random_commit(tx.repo_mut());
1039 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1040 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1041 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1042 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b, &commit_c]);
1043 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_e, &commit_d]);
1044
1045 tx.repo_mut().record_abandoned_commit(&commit_e);
1046 let rebase_map =
1047 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
1048 let new_commit_f = assert_rebased_onto(
1049 tx.repo_mut(),
1050 &rebase_map,
1051 &commit_f,
1052 &[commit_b.id(), commit_c.id(), commit_d.id()],
1053 );
1054 assert_eq!(rebase_map.len(), 1);
1055
1056 assert_eq!(
1057 *tx.repo().view().heads(),
1058 hashset! { new_commit_f.id().clone()}
1059 );
1060 }
1061
1062 #[test]
1063 fn test_rebase_descendants_multiple_sideways() {
1064 let test_repo = TestRepo::init();
1065 let repo = &test_repo.repo;
1066
1067 // Commit B and commit D were both replaced by commit F. Commit C and commit E
1068 // should get rebased onto it.
1069 //
1070 // C E
1071 // B D F
1072 // | |/
1073 // |/
1074 // A
1075 let mut tx = repo.start_transaction();
1076 let commit_a = write_random_commit(tx.repo_mut());
1077 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1078 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
1079 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1080 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d]);
1081 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1082
1083 tx.repo_mut()
1084 .set_rewritten_commit(commit_b.id().clone(), commit_f.id().clone());
1085 tx.repo_mut()
1086 .set_rewritten_commit(commit_d.id().clone(), commit_f.id().clone());
1087 let rebase_map =
1088 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
1089 let new_commit_c = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_c, &[commit_f.id()]);
1090 let new_commit_e = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_e, &[commit_f.id()]);
1091 assert_eq!(rebase_map.len(), 2);
1092
1093 assert_eq!(
1094 *tx.repo().view().heads(),
1095 hashset! {
1096 new_commit_c.id().clone(),
1097 new_commit_e.id().clone()
1098 }
1099 );
1100 }
1101
1102 #[test]
1103 #[should_panic(expected = "cycle")]
1104 fn test_rebase_descendants_multiple_swap() {
1105 let test_repo = TestRepo::init();
1106 let repo = &test_repo.repo;
1107
1108 // Commit B was replaced by commit D. Commit D was replaced by commit B.
1109 // This results in an infinite loop and a panic
1110 //
1111 // C E
1112 // B D
1113 // |/
1114 // A
1115 let mut tx = repo.start_transaction();
1116 let commit_a = write_random_commit(tx.repo_mut());
1117 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1118 let _commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
1119 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1120 let _commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d]);
1121
1122 tx.repo_mut()
1123 .set_rewritten_commit(commit_b.id().clone(), commit_d.id().clone());
1124 tx.repo_mut()
1125 .set_rewritten_commit(commit_d.id().clone(), commit_b.id().clone());
1126 tx.repo_mut().rebase_descendants().block_on().ok(); // Panics because of the cycle
1127 }
1128
1129 #[test]
1130 fn test_rebase_descendants_multiple_no_descendants() {
1131 let test_repo = TestRepo::init();
1132 let repo = &test_repo.repo;
1133
1134 // Commit B was replaced by commit C. Commit C was replaced by commit B.
1135 //
1136 // B C
1137 // |/
1138 // A
1139 let mut tx = repo.start_transaction();
1140 let commit_a = write_random_commit(tx.repo_mut());
1141 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1142 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1143
1144 tx.repo_mut()
1145 .set_rewritten_commit(commit_b.id().clone(), commit_c.id().clone());
1146 tx.repo_mut()
1147 .set_rewritten_commit(commit_c.id().clone(), commit_b.id().clone());
1148 let result = tx.repo_mut().rebase_descendants().block_on();
1149 assert_matches!(result, Err(err) if err.to_string().contains("Cycle"));
1150 }
1151
1152 #[test]
1153 fn test_rebase_descendants_divergent_rewrite() {
1154 let test_repo = TestRepo::init();
1155 let repo = &test_repo.repo;
1156
1157 // Commit B was replaced by commit B2. Commit D was replaced by commits D2 and
1158 // D3. Commit F was replaced by commit F2. Commit C should be rebased onto
1159 // B2. Commit E should not be rebased. Commit G should be rebased onto
1160 // commit F2.
1161 //
1162 // G
1163 // F
1164 // E
1165 // D
1166 // C
1167 // B
1168 // | F2
1169 // |/
1170 // | D3
1171 // |/
1172 // | D2
1173 // |/
1174 // | B2
1175 // |/
1176 // A
1177 let mut tx = repo.start_transaction();
1178 let commit_a = write_random_commit(tx.repo_mut());
1179 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1180 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
1181 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);
1182 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d]);
1183 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_e]);
1184 let commit_g = write_random_commit_with_parents(tx.repo_mut(), &[&commit_f]);
1185 let commit_b2 = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1186 let commit_d2 = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1187 let commit_d3 = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1188 let commit_f2 = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1189
1190 tx.repo_mut()
1191 .set_rewritten_commit(commit_b.id().clone(), commit_b2.id().clone());
1192 // Commit D becomes divergent
1193 tx.repo_mut().set_divergent_rewrite(
1194 commit_d.id().clone(),
1195 vec![commit_d2.id().clone(), commit_d3.id().clone()],
1196 );
1197 tx.repo_mut()
1198 .set_rewritten_commit(commit_f.id().clone(), commit_f2.id().clone());
1199 let rebase_map =
1200 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
1201 let new_commit_c =
1202 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_c, &[commit_b2.id()]);
1203 let new_commit_g =
1204 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_g, &[commit_f2.id()]);
1205 assert_eq!(rebase_map.len(), 2); // Commit E is not rebased
1206
1207 assert_eq!(
1208 *tx.repo().view().heads(),
1209 hashset! {
1210 new_commit_c.id().clone(),
1211 commit_d2.id().clone(),
1212 commit_d3.id().clone(),
1213 commit_e.id().clone(),
1214 new_commit_g.id().clone(),
1215 }
1216 );
1217 }
1218
1219 #[test]
1220 fn test_rebase_descendants_hidden() -> TestResult {
1221 let test_repo = TestRepo::init();
1222 let repo = &test_repo.repo;
1223
1224 // Commits B and C are hidden. C is then replaced by D. B should remain hidden.
1225 //
1226 // D
1227 // | C
1228 // | |
1229 // | B
1230 // |/
1231 // A
1232 let mut tx = repo.start_transaction();
1233 let commit_a = write_random_commit(tx.repo_mut());
1234 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1235 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
1236 tx.repo_mut().record_abandoned_commit(&commit_b);
1237 tx.repo_mut().record_abandoned_commit(&commit_c);
1238 tx.repo_mut().rebase_descendants().block_on()?;
1239 let repo = tx.commit("test").block_on()?;
1240 let mut tx = repo.start_transaction();
1241
1242 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1243 tx.repo_mut()
1244 .set_rewritten_commit(commit_c.id().clone(), commit_d.id().clone());
1245 let rebase_map =
1246 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
1247 assert_eq!(rebase_map.len(), 0);
1248
1249 assert_eq!(
1250 *tx.repo().view().heads(),
1251 hashset! {
1252 commit_d.id().clone(),
1253 }
1254 );
1255 Ok(())
1256 }
1257
1258 #[test]
1259 fn test_rebase_descendants_some_excluded() -> TestResult {
1260 let test_repo = TestRepo::init();
1261 let repo = &test_repo.repo;
1262
1263 // Commits A and B are immutable. A is rewritten explicitly. B shouldn't be
1264 // rebased because it's immutable. C isn't rebased because of that. D should
1265 // be rebased.
1266 //
1267 // C
1268 // |
1269 // B D
1270 // |/
1271 // A
1272 let mut tx = repo.start_transaction();
1273 let commit_a = write_random_commit(tx.repo_mut());
1274 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1275 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
1276 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1277 let repo = tx.commit("test").block_on()?;
1278
1279 let mut tx = repo.start_transaction();
1280 let commit_a2 = tx
1281 .repo_mut()
1282 .rewrite_commit(&commit_a)
1283 .set_description("a2")
1284 .write()
1285 .block_on()?;
1286 let immutable = RevsetExpression::commit(commit_b.id().clone()).ancestors();
1287 let options = RebaseOptions::default();
1288 let mut rebased_commits = Vec::new();
1289 tx.repo_mut()
1290 .rebase_descendants_with_options(
1291 &immutable,
1292 &options,
1293 |old_commit, rebased| match rebased {
1294 RebasedCommit::Rewritten(commit) => rebased_commits.push((old_commit, commit)),
1295 RebasedCommit::Abandoned { .. } => panic!("no commit should be abandoned"),
1296 },
1297 )
1298 .block_on()?;
1299 assert_eq!(rebased_commits.len(), 1);
1300 assert_eq!(rebased_commits[0].0, commit_d);
1301 let (_, commit_d2) = rebased_commits[0].clone();
1302 assert_eq!(commit_d2.parent_ids(), slice::from_ref(commit_a2.id()));
1303 assert_eq!(
1304 *tx.repo().view().heads(),
1305 hashset! {
1306 commit_c.id().clone(),
1307 commit_d2.id().clone(),
1308 }
1309 );
1310
1311 Ok(())
1312 }
1313
1314 #[test]
1315 fn test_rebase_descendants_repeated() {
1316 let test_repo = TestRepo::init();
1317 let repo = &test_repo.repo;
1318
1319 // Commit B was replaced by commit B2. Commit C should get rebased. Rebasing
1320 // descendants again should have no effect (C should not get rebased again).
1321 // We then replace B2 by B3. C should now get rebased onto B3.
1322 //
1323 // C
1324 // B
1325 // | B3
1326 // |/
1327 // | B2
1328 // |/
1329 // A
1330 let mut tx = repo.start_transaction();
1331 let commit_a = write_random_commit(tx.repo_mut());
1332 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1333 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
1334
1335 let commit_b2 = tx
1336 .repo_mut()
1337 .rewrite_commit(&commit_b)
1338 .set_description("b2")
1339 .write_unwrap();
1340 let rebase_map =
1341 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
1342 let commit_c2 = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_c, &[commit_b2.id()]);
1343 assert_eq!(rebase_map.len(), 1);
1344
1345 assert_eq!(
1346 *tx.repo().view().heads(),
1347 hashset! {
1348 commit_c2.id().clone(),
1349 }
1350 );
1351
1352 // We made no more changes, so nothing should be rebased.
1353 let rebase_map =
1354 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
1355 assert_eq!(rebase_map.len(), 0);
1356
1357 // Now mark B3 as rewritten from B2 and rebase descendants again.
1358 let commit_b3 = tx
1359 .repo_mut()
1360 .rewrite_commit(&commit_b2)
1361 .set_description("b3")
1362 .write_unwrap();
1363 let rebase_map =
1364 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
1365 let commit_c3 = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_c2, &[commit_b3.id()]);
1366 assert_eq!(rebase_map.len(), 1);
1367
1368 assert_eq!(
1369 *tx.repo().view().heads(),
1370 hashset! {
1371 // commit_b.id().clone(),
1372 commit_c3.id().clone(),
1373 }
1374 );
1375 }
1376
1377 #[test]
1378 fn test_rebase_descendants_contents() -> TestResult {
1379 let test_repo = TestRepo::init();
1380 let repo = &test_repo.repo;
1381
1382 // Commit B was replaced by commit D. Commit C should have the changes from
1383 // commit C and commit D, but not the changes from commit B.
1384 //
1385 // D
1386 // | C
1387 // | B
1388 // |/
1389 // A
1390 let mut tx = repo.start_transaction();
1391 let path1 = repo_path("file1");
1392 let tree1 = create_tree(repo, &[(path1, "content")]);
1393 let commit_a = tx
1394 .repo_mut()
1395 .new_commit(vec![repo.store().root_commit_id().clone()], tree1)
1396 .write_unwrap();
1397 let path2 = repo_path("file2");
1398 let tree2 = create_tree(repo, &[(path2, "content")]);
1399 let commit_b = tx
1400 .repo_mut()
1401 .new_commit(vec![commit_a.id().clone()], tree2)
1402 .write_unwrap();
1403 let path3 = repo_path("file3");
1404 let tree3 = create_tree(repo, &[(path3, "content")]);
1405 let commit_c = tx
1406 .repo_mut()
1407 .new_commit(vec![commit_b.id().clone()], tree3)
1408 .write_unwrap();
1409 let path4 = repo_path("file4");
1410 let tree4 = create_tree(repo, &[(path4, "content")]);
1411 let commit_d = tx
1412 .repo_mut()
1413 .new_commit(vec![commit_a.id().clone()], tree4)
1414 .write_unwrap();
1415
1416 tx.repo_mut()
1417 .set_rewritten_commit(commit_b.id().clone(), commit_d.id().clone());
1418 let rebase_map =
1419 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
1420 assert_eq!(rebase_map.len(), 1);
1421 let new_commit_c = repo
1422 .store()
1423 .get_commit(rebase_map.get(commit_c.id()).unwrap())?;
1424
1425 let tree_b = commit_b.tree();
1426 let tree_c = commit_c.tree();
1427 let tree_d = commit_d.tree();
1428 let new_tree_c = new_commit_c.tree();
1429 assert_eq!(
1430 new_tree_c.path_value(path3).block_on()?,
1431 tree_c.path_value(path3).block_on()?
1432 );
1433 assert_eq!(
1434 new_tree_c.path_value(path4).block_on()?,
1435 tree_d.path_value(path4).block_on()?
1436 );
1437 assert_ne!(
1438 new_tree_c.path_value(path2).block_on()?,
1439 tree_b.path_value(path2).block_on()?
1440 );
1441 Ok(())
1442 }
1443
1444 #[test]
1445 fn test_rebase_descendants_basic_bookmark_update() -> TestResult {
1446 let test_repo = TestRepo::init();
1447 let repo = &test_repo.repo;
1448
1449 // Bookmark "main" points to commit B. B gets rewritten as B2. Bookmark main
1450 // should be updated to point to B2.
1451 //
1452 // B main B2 main
1453 // | => |
1454 // A A
1455 let mut tx = repo.start_transaction();
1456 let commit_a = write_random_commit(tx.repo_mut());
1457 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1458 tx.repo_mut()
1459 .set_local_bookmark_target("main".as_ref(), RefTarget::normal(commit_b.id().clone()));
1460 let repo = tx.commit("test").block_on()?;
1461
1462 let mut tx = repo.start_transaction();
1463 let commit_b2 = tx.repo_mut().rewrite_commit(&commit_b).write_unwrap();
1464 tx.repo_mut().rebase_descendants().block_on()?;
1465 assert_eq!(
1466 tx.repo().get_local_bookmark("main".as_ref()),
1467 &RefTarget::normal(commit_b2.id().clone())
1468 );
1469
1470 assert_eq!(*tx.repo().view().heads(), hashset! {commit_b2.id().clone()});
1471 Ok(())
1472 }
1473
1474 #[test]
1475 fn test_rebase_descendants_bookmark_move_two_steps() -> TestResult {
1476 let test_repo = TestRepo::init();
1477 let repo = &test_repo.repo;
1478
1479 // Bookmark "main" points to bookmark C. C gets rewritten as C2 and B gets
1480 // rewritten as B2. C2 should be rebased onto B2, creating C3, and main
1481 // should be updated to point to C3.
1482 //
1483 // C2 C main C3 main
1484 // | / |
1485 // |/ => |
1486 // B B2 B2
1487 // |/ |
1488 // A A
1489 let mut tx = repo.start_transaction();
1490 let commit_a = write_random_commit(tx.repo_mut());
1491 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1492 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
1493 tx.repo_mut()
1494 .set_local_bookmark_target("main".as_ref(), RefTarget::normal(commit_c.id().clone()));
1495 let repo = tx.commit("test").block_on()?;
1496
1497 let mut tx = repo.start_transaction();
1498 let commit_b2 = tx
1499 .repo_mut()
1500 .rewrite_commit(&commit_b)
1501 .set_description("different")
1502 .write_unwrap();
1503 let commit_c2 = tx
1504 .repo_mut()
1505 .rewrite_commit(&commit_c)
1506 .set_description("more different")
1507 .write_unwrap();
1508 tx.repo_mut().rebase_descendants().block_on()?;
1509 let heads = tx.repo().view().heads();
1510 assert_eq!(heads.len(), 1);
1511 let c3_id = heads.iter().next().unwrap().clone();
1512 let commit_c3 = repo.store().get_commit(&c3_id)?;
1513 assert_ne!(commit_c3.id(), commit_c2.id());
1514 assert_eq!(commit_c3.parent_ids(), vec![commit_b2.id().clone()]);
1515 assert_eq!(
1516 tx.repo().get_local_bookmark("main".as_ref()),
1517 &RefTarget::normal(commit_c3.id().clone())
1518 );
1519 Ok(())
1520 }
1521
1522 #[test]
1523 fn test_rebase_descendants_basic_bookmark_update_with_non_local_bookmark() -> TestResult {
1524 let test_repo = TestRepo::init();
1525 let repo = &test_repo.repo;
1526
1527 // Bookmark "main" points to commit B. B gets rewritten as B2. Bookmark main
1528 // should be updated to point to B2. Remote bookmark main@origin and tag v1
1529 // should not get updated.
1530 //
1531 // B2 main
1532 // B main main@origin v1 | B main@origin v1
1533 // | => |/
1534 // A A
1535 let mut tx = repo.start_transaction();
1536 let commit_a = write_random_commit(tx.repo_mut());
1537 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1538 let commit_b_remote_ref = RemoteRef {
1539 target: RefTarget::normal(commit_b.id().clone()),
1540 state: RemoteRefState::Tracked,
1541 };
1542 tx.repo_mut()
1543 .set_local_bookmark_target("main".as_ref(), RefTarget::normal(commit_b.id().clone()));
1544 tx.repo_mut()
1545 .set_remote_bookmark(remote_symbol("main", "origin"), commit_b_remote_ref.clone());
1546 tx.repo_mut()
1547 .set_local_tag_target("v1".as_ref(), RefTarget::normal(commit_b.id().clone()));
1548 let repo = tx.commit("test").block_on()?;
1549
1550 let mut tx = repo.start_transaction();
1551 let commit_b2 = tx.repo_mut().rewrite_commit(&commit_b).write_unwrap();
1552 tx.repo_mut().rebase_descendants().block_on()?;
1553 assert_eq!(
1554 tx.repo().get_local_bookmark("main".as_ref()),
1555 &RefTarget::normal(commit_b2.id().clone())
1556 );
1557 // The remote bookmark and tag should not get updated
1558 assert_eq!(
1559 tx.repo()
1560 .get_remote_bookmark(remote_symbol("main", "origin")),
1561 &commit_b_remote_ref
1562 );
1563 assert_eq!(
1564 tx.repo().get_local_tag("v1".as_ref()),
1565 &RefTarget::normal(commit_b.id().clone())
1566 );
1567
1568 // Commit B is no longer visible even though the remote bookmark points to it.
1569 // (The user can still see it using e.g. the `remote_bookmarks()` revset.)
1570 assert_eq!(*tx.repo().view().heads(), hashset! {commit_b2.id().clone()});
1571 Ok(())
1572 }
1573
1574 #[test_case(false; "slide down abandoned")]
1575 #[test_case(true; "delete abandoned")]
1576 fn test_rebase_descendants_update_bookmark_after_abandon(
1577 delete_abandoned_bookmarks: bool,
1578 ) -> TestResult {
1579 let test_repo = TestRepo::init();
1580 let repo = &test_repo.repo;
1581
1582 // Commit B is abandoned. Local bookmarks should be deleted or moved
1583 // accordingly, whereas remote bookmarks should not get updated.
1584 //
1585 // C other
1586 // |
1587 // B main main@origin C2 other
1588 // | => |
1589 // A A main (if delete_abandoned_bookmarks = false)
1590 let mut tx = repo.start_transaction();
1591 let commit_a = write_random_commit(tx.repo_mut());
1592 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1593 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
1594 let commit_b_remote_ref = RemoteRef {
1595 target: RefTarget::normal(commit_b.id().clone()),
1596 state: RemoteRefState::Tracked,
1597 };
1598 tx.repo_mut()
1599 .set_local_bookmark_target("main".as_ref(), RefTarget::normal(commit_b.id().clone()));
1600 tx.repo_mut()
1601 .set_remote_bookmark(remote_symbol("main", "origin"), commit_b_remote_ref.clone());
1602 tx.repo_mut()
1603 .set_local_bookmark_target("other".as_ref(), RefTarget::normal(commit_c.id().clone()));
1604 let repo = tx.commit("test").block_on()?;
1605
1606 let mut tx = repo.start_transaction();
1607 tx.repo_mut().record_abandoned_commit(&commit_b);
1608 let options = RebaseOptions {
1609 rewrite_refs: RewriteRefsOptions {
1610 delete_abandoned_bookmarks,
1611 },
1612 ..Default::default()
1613 };
1614 let rebase_map = rebase_descendants_with_options_return_map(tx.repo_mut(), &options);
1615 assert_eq!(
1616 *tx.repo().get_local_bookmark("main".as_ref()),
1617 if delete_abandoned_bookmarks {
1618 RefTarget::absent()
1619 } else {
1620 RefTarget::normal(commit_a.id().clone())
1621 }
1622 );
1623 assert_eq!(
1624 tx.repo()
1625 .get_remote_bookmark(remote_symbol("main", "origin"))
1626 .target,
1627 RefTarget::normal(commit_b.id().clone())
1628 );
1629 assert_eq!(
1630 tx.repo().get_local_bookmark("other".as_ref()),
1631 &RefTarget::normal(rebase_map[commit_c.id()].clone())
1632 );
1633
1634 assert_eq!(
1635 *tx.repo().view().heads(),
1636 hashset! { rebase_map[commit_c.id()].clone() }
1637 );
1638 Ok(())
1639 }
1640
1641 #[test]
1642 fn test_rebase_descendants_update_bookmarks_after_divergent_rewrite() -> TestResult {
1643 let test_repo = TestRepo::init();
1644 let repo = &test_repo.repo;
1645
1646 // Bookmark "main" points to commit B. B gets rewritten as {B2, B3, B4}, then
1647 // B4 as {B41, B42}. Bookmark main should become a conflict pointing to {B2,
1648 // B3, B41, B42}.
1649 //
1650 // C other
1651 // C other | B42 main?
1652 // C other | B4 main? |/B41 main?
1653 // | |/B3 main? |/B3 main?
1654 // B main |/B2 main? |/B2 main?
1655 // | => |/ => |/
1656 // A A A
1657 let mut tx = repo.start_transaction();
1658 let commit_a = write_random_commit(tx.repo_mut());
1659 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1660 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
1661 tx.repo_mut()
1662 .set_local_bookmark_target("main".as_ref(), RefTarget::normal(commit_b.id().clone()));
1663 tx.repo_mut()
1664 .set_local_bookmark_target("other".as_ref(), RefTarget::normal(commit_c.id().clone()));
1665 let repo = tx.commit("test").block_on()?;
1666
1667 let mut tx = repo.start_transaction();
1668 let commit_b2 = tx.repo_mut().rewrite_commit(&commit_b).write_unwrap();
1669 // Different description so they're not the same commit
1670 let commit_b3 = tx
1671 .repo_mut()
1672 .rewrite_commit(&commit_b)
1673 .set_description("different")
1674 .write_unwrap();
1675 // Different description so they're not the same commit
1676 let commit_b4 = tx
1677 .repo_mut()
1678 .rewrite_commit(&commit_b)
1679 .set_description("more different")
1680 .write_unwrap();
1681 tx.repo_mut().set_divergent_rewrite(
1682 commit_b.id().clone(),
1683 vec![
1684 commit_b2.id().clone(),
1685 commit_b3.id().clone(),
1686 commit_b4.id().clone(),
1687 ],
1688 );
1689 let commit_b41 = tx.repo_mut().rewrite_commit(&commit_b4).write_unwrap();
1690 let commit_b42 = tx
1691 .repo_mut()
1692 .rewrite_commit(&commit_b4)
1693 .set_description("different")
1694 .write_unwrap();
1695 tx.repo_mut().set_divergent_rewrite(
1696 commit_b4.id().clone(),
1697 vec![commit_b41.id().clone(), commit_b42.id().clone()],
1698 );
1699 tx.repo_mut().rebase_descendants().block_on()?;
1700
1701 let main_target = tx.repo().get_local_bookmark("main".as_ref());
1702 assert!(main_target.has_conflict());
1703 // If the bookmark were moved at each rewrite point, there would be separate
1704 // negative terms: { commit_b => 2, commit_b4 => 1 }. Since we flatten
1705 // intermediate rewrites, commit_b4 doesn't appear in the removed_ids.
1706 assert_eq!(
1707 main_target.removed_ids().counts(),
1708 hashmap! { commit_b.id() => 3 },
1709 );
1710 assert_eq!(
1711 main_target.added_ids().counts(),
1712 hashmap! {
1713 commit_b2.id() => 1,
1714 commit_b3.id() => 1,
1715 commit_b41.id() => 1,
1716 commit_b42.id() => 1,
1717 },
1718 );
1719
1720 let other_target = tx.repo().get_local_bookmark("other".as_ref());
1721 assert_eq!(other_target.as_normal(), Some(commit_c.id()));
1722
1723 assert_eq!(
1724 *tx.repo().view().heads(),
1725 hashset! {
1726 commit_b2.id().clone(),
1727 commit_b3.id().clone(),
1728 commit_b41.id().clone(),
1729 commit_b42.id().clone(),
1730 commit_c.id().clone(),
1731 }
1732 );
1733 Ok(())
1734 }
1735
1736 #[test]
1737 fn test_rebase_descendants_rewrite_updates_bookmark_conflict() -> TestResult {
1738 let test_repo = TestRepo::init();
1739 let repo = &test_repo.repo;
1740
1741 // Bookmark "main" is a conflict removing commit A and adding commits B and C.
1742 // A gets rewritten as A2 and A3. B gets rewritten as B2 and B2. The bookmark
1743 // should become a conflict removing A and B, and adding B2, B3, C.
1744 let mut tx = repo.start_transaction();
1745 let commit_a = write_random_commit(tx.repo_mut());
1746 let commit_b = write_random_commit(tx.repo_mut());
1747 let commit_c = write_random_commit(tx.repo_mut());
1748 tx.repo_mut().set_local_bookmark_target(
1749 "main".as_ref(),
1750 RefTarget::from_legacy_form(
1751 [commit_a.id().clone()],
1752 [commit_b.id().clone(), commit_c.id().clone()],
1753 ),
1754 );
1755 let repo = tx.commit("test").block_on()?;
1756
1757 let mut tx = repo.start_transaction();
1758 let commit_a2 = tx.repo_mut().rewrite_commit(&commit_a).write_unwrap();
1759 // Different description so they're not the same commit
1760 let commit_a3 = tx
1761 .repo_mut()
1762 .rewrite_commit(&commit_a)
1763 .set_description("different")
1764 .write_unwrap();
1765 let commit_b2 = tx.repo_mut().rewrite_commit(&commit_b).write_unwrap();
1766 // Different description so they're not the same commit
1767 let commit_b3 = tx
1768 .repo_mut()
1769 .rewrite_commit(&commit_b)
1770 .set_description("different")
1771 .write_unwrap();
1772 tx.repo_mut().set_divergent_rewrite(
1773 commit_a.id().clone(),
1774 vec![commit_a2.id().clone(), commit_a3.id().clone()],
1775 );
1776 tx.repo_mut().set_divergent_rewrite(
1777 commit_b.id().clone(),
1778 vec![commit_b2.id().clone(), commit_b3.id().clone()],
1779 );
1780 tx.repo_mut().rebase_descendants().block_on()?;
1781
1782 let target = tx.repo().get_local_bookmark("main".as_ref());
1783 assert!(target.has_conflict());
1784 assert_eq!(
1785 target.removed_ids().counts(),
1786 hashmap! { commit_a.id() => 1, commit_b.id() => 1 },
1787 );
1788 assert_eq!(
1789 target.added_ids().counts(),
1790 hashmap! {
1791 commit_c.id() => 1,
1792 commit_b2.id() => 1,
1793 commit_b3.id() => 1,
1794 },
1795 );
1796
1797 assert_eq!(
1798 *tx.repo().view().heads(),
1799 hashset! {
1800 commit_a2.id().clone(),
1801 commit_a3.id().clone(),
1802 commit_b2.id().clone(),
1803 commit_b3.id().clone(),
1804 commit_c.id().clone(),
1805 }
1806 );
1807 Ok(())
1808 }
1809
1810 #[test]
1811 fn test_rebase_descendants_rewrite_resolves_bookmark_conflict() -> TestResult {
1812 let test_repo = TestRepo::init();
1813 let repo = &test_repo.repo;
1814
1815 // Bookmark "main" is a conflict removing ancestor commit A and adding commit B
1816 // and C (maybe it moved forward to B locally and moved forward to C
1817 // remotely). Now B gets rewritten as B2, which is a descendant of C (maybe
1818 // B was automatically rebased on top of the updated remote). That
1819 // would result in a conflict removing A and adding B2 and C. However, since C
1820 // is a descendant of A, and B2 is a descendant of C, the conflict gets
1821 // resolved to B2.
1822 let mut tx = repo.start_transaction();
1823 let commit_a = write_random_commit(tx.repo_mut());
1824 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1825 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1826 tx.repo_mut().set_local_bookmark_target(
1827 "main".as_ref(),
1828 RefTarget::from_legacy_form(
1829 [commit_a.id().clone()],
1830 [commit_b.id().clone(), commit_c.id().clone()],
1831 ),
1832 );
1833 let repo = tx.commit("test").block_on()?;
1834
1835 let mut tx = repo.start_transaction();
1836 let commit_b2 = tx
1837 .repo_mut()
1838 .rewrite_commit(&commit_b)
1839 .set_parents(vec![commit_c.id().clone()])
1840 .write_unwrap();
1841 tx.repo_mut().rebase_descendants().block_on()?;
1842 assert_eq!(
1843 tx.repo().get_local_bookmark("main".as_ref()),
1844 &RefTarget::normal(commit_b2.id().clone())
1845 );
1846
1847 assert_eq!(
1848 *tx.repo().view().heads(),
1849 hashset! { commit_b2.id().clone()}
1850 );
1851 Ok(())
1852 }
1853
1854 #[test_case(false; "slide down abandoned")]
1855 #[test_case(true; "delete abandoned")]
1856 fn test_rebase_descendants_bookmark_delete_modify_abandon(
1857 delete_abandoned_bookmarks: bool,
1858 ) -> TestResult {
1859 let test_repo = TestRepo::init();
1860 let repo = &test_repo.repo;
1861
1862 // Bookmark "main" initially points to commit A. One operation rewrites it to
1863 // point to B (child of A). A concurrent operation deletes the bookmark. That
1864 // leaves the bookmark pointing to "0-A+B". We now abandon B.
1865 //
1866 // - If delete_abandoned_bookmarks = false, that should result in the bookmark
1867 // pointing to "0-A+A=0".
1868 // - If delete_abandoned_bookmarks = true, that should result in the bookmark
1869 // pointing to "0-A+0=0".
1870 //
1871 // In both cases, the bookmark should be deleted.
1872 let mut tx = repo.start_transaction();
1873 let commit_a = write_random_commit(tx.repo_mut());
1874 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1875 tx.repo_mut().set_local_bookmark_target(
1876 "main".as_ref(),
1877 RefTarget::from_legacy_form([commit_a.id().clone()], [commit_b.id().clone()]),
1878 );
1879 let repo = tx.commit("test").block_on()?;
1880
1881 let mut tx = repo.start_transaction();
1882 tx.repo_mut().record_abandoned_commit(&commit_b);
1883 let options = RebaseOptions {
1884 rewrite_refs: RewriteRefsOptions {
1885 delete_abandoned_bookmarks,
1886 },
1887 ..Default::default()
1888 };
1889 let _rebase_map = rebase_descendants_with_options_return_map(tx.repo_mut(), &options);
1890 assert_eq!(
1891 tx.repo().get_local_bookmark("main".as_ref()),
1892 RefTarget::absent_ref()
1893 );
1894 Ok(())
1895 }
1896
1897 #[test_case(false; "slide down abandoned")]
1898 #[test_case(true; "delete abandoned")]
1899 fn test_rebase_descendants_bookmark_move_forward_abandon(
1900 delete_abandoned_bookmarks: bool,
1901 ) -> TestResult {
1902 let test_repo = TestRepo::init();
1903 let repo = &test_repo.repo;
1904
1905 // Bookmark "main" initially points to commit A. Two concurrent operations
1906 // rewrites it to point to A's children. That leaves the bookmark pointing
1907 // to "B-A+C". We now abandon B.
1908 //
1909 // - If delete_abandoned_bookmarks = false, that should result in the bookmark
1910 // pointing to "A-A+C=C", so the conflict should be resolved.
1911 // - If delete_abandoned_bookmarks = true, that should result in the bookmark
1912 // pointing to "0-A+C".
1913 let mut tx = repo.start_transaction();
1914 let commit_a = write_random_commit(tx.repo_mut());
1915 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1916 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
1917 tx.repo_mut().set_local_bookmark_target(
1918 "main".as_ref(),
1919 RefTarget::from_merge(Merge::from_vec(vec![
1920 Some(commit_b.id().clone()),
1921 Some(commit_a.id().clone()),
1922 Some(commit_c.id().clone()),
1923 ])),
1924 );
1925 let repo = tx.commit("test").block_on()?;
1926
1927 let mut tx = repo.start_transaction();
1928 tx.repo_mut().record_abandoned_commit(&commit_b);
1929 let options = RebaseOptions {
1930 rewrite_refs: RewriteRefsOptions {
1931 delete_abandoned_bookmarks,
1932 },
1933 ..Default::default()
1934 };
1935 let _rebase_map = rebase_descendants_with_options_return_map(tx.repo_mut(), &options);
1936 assert_eq!(
1937 *tx.repo().get_local_bookmark("main".as_ref()),
1938 if delete_abandoned_bookmarks {
1939 RefTarget::from_merge(Merge::from_vec(vec![
1940 None,
1941 Some(commit_a.id().clone()),
1942 Some(commit_c.id().clone()),
1943 ]))
1944 } else {
1945 RefTarget::normal(commit_c.id().clone())
1946 }
1947 );
1948 Ok(())
1949 }
1950
1951 #[test_case(false; "slide down abandoned")]
1952 #[test_case(true; "delete abandoned")]
1953 fn test_rebase_descendants_bookmark_move_sideways_abandon(
1954 delete_abandoned_bookmarks: bool,
1955 ) -> TestResult {
1956 let test_repo = TestRepo::init();
1957 let repo = &test_repo.repo;
1958
1959 // Bookmark "main" initially points to commit A. Two concurrent operations
1960 // rewrites it to point to A's siblings. That leaves the bookmark pointing
1961 // to "B-A+C". We now abandon B.
1962 //
1963 // - If delete_abandoned_bookmarks = false, that should result in the bookmark
1964 // pointing to "A.parent-A+C".
1965 // - If delete_abandoned_bookmarks = true, that should result in the bookmark
1966 // pointing to "0-A+C".
1967 let mut tx = repo.start_transaction();
1968 let commit_a = write_random_commit(tx.repo_mut());
1969 let commit_b = write_random_commit(tx.repo_mut());
1970 let commit_c = write_random_commit(tx.repo_mut());
1971 tx.repo_mut().set_local_bookmark_target(
1972 "main".as_ref(),
1973 RefTarget::from_merge(Merge::from_vec(vec![
1974 Some(commit_b.id().clone()),
1975 Some(commit_a.id().clone()),
1976 Some(commit_c.id().clone()),
1977 ])),
1978 );
1979 let repo = tx.commit("test").block_on()?;
1980
1981 let mut tx = repo.start_transaction();
1982 tx.repo_mut().record_abandoned_commit(&commit_b);
1983 let options = RebaseOptions {
1984 rewrite_refs: RewriteRefsOptions {
1985 delete_abandoned_bookmarks,
1986 },
1987 ..Default::default()
1988 };
1989 let _rebase_map = rebase_descendants_with_options_return_map(tx.repo_mut(), &options);
1990 assert_eq!(
1991 *tx.repo().get_local_bookmark("main".as_ref()),
1992 if delete_abandoned_bookmarks {
1993 RefTarget::from_merge(Merge::from_vec(vec![
1994 None,
1995 Some(commit_a.id().clone()),
1996 Some(commit_c.id().clone()),
1997 ]))
1998 } else {
1999 RefTarget::from_merge(Merge::from_vec(vec![
2000 Some(repo.store().root_commit_id().clone()),
2001 Some(commit_a.id().clone()),
2002 Some(commit_c.id().clone()),
2003 ]))
2004 }
2005 );
2006 Ok(())
2007 }
2008
2009 #[test]
2010 fn test_rebase_descendants_update_checkout() -> TestResult {
2011 let test_repo = TestRepo::init();
2012 let repo = &test_repo.repo;
2013
2014 // Checked-out commit B was replaced by commit C. C should become
2015 // checked out.
2016 //
2017 // C B
2018 // |/
2019 // A
2020 let mut tx = repo.start_transaction();
2021 let commit_a = write_random_commit(tx.repo_mut());
2022 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
2023 let ws1_name = WorkspaceNameBuf::from("ws1");
2024 let ws2_name = WorkspaceNameBuf::from("ws2");
2025 let ws3_name = WorkspaceNameBuf::from("ws3");
2026 tx.repo_mut()
2027 .set_wc_commit(ws1_name.clone(), commit_b.id().clone())?;
2028 tx.repo_mut()
2029 .set_wc_commit(ws2_name.clone(), commit_b.id().clone())?;
2030 tx.repo_mut()
2031 .set_wc_commit(ws3_name.clone(), commit_a.id().clone())?;
2032 let repo = tx.commit("test").block_on()?;
2033
2034 let mut tx = repo.start_transaction();
2035 let commit_c = tx
2036 .repo_mut()
2037 .rewrite_commit(&commit_b)
2038 .set_description("C")
2039 .write_unwrap();
2040 tx.repo_mut().rebase_descendants().block_on()?;
2041 let repo = tx.commit("test").block_on()?;
2042
2043 // Workspaces 1 and 2 had B checked out, so they get updated to C. Workspace 3
2044 // had A checked out, so it doesn't get updated.
2045 assert_eq!(repo.view().get_wc_commit_id(&ws1_name), Some(commit_c.id()));
2046 assert_eq!(repo.view().get_wc_commit_id(&ws2_name), Some(commit_c.id()));
2047 assert_eq!(repo.view().get_wc_commit_id(&ws3_name), Some(commit_a.id()));
2048 Ok(())
2049 }
2050
2051 #[test]
2052 fn test_rebase_descendants_update_checkout_abandoned() -> TestResult {
2053 let test_repo = TestRepo::init();
2054 let repo = &test_repo.repo;
2055
2056 // Checked-out commit B was abandoned. A child of A
2057 // should become checked out.
2058 //
2059 // B
2060 // |
2061 // A
2062 let mut tx = repo.start_transaction();
2063 let commit_a = write_random_commit(tx.repo_mut());
2064 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
2065 let ws1_name = WorkspaceNameBuf::from("ws1");
2066 let ws2_name = WorkspaceNameBuf::from("ws2");
2067 let ws3_name = WorkspaceNameBuf::from("ws3");
2068 tx.repo_mut()
2069 .set_wc_commit(ws1_name.clone(), commit_b.id().clone())?;
2070 tx.repo_mut()
2071 .set_wc_commit(ws2_name.clone(), commit_b.id().clone())?;
2072 tx.repo_mut()
2073 .set_wc_commit(ws3_name.clone(), commit_a.id().clone())?;
2074 let repo = tx.commit("test").block_on()?;
2075
2076 let mut tx = repo.start_transaction();
2077 tx.repo_mut().record_abandoned_commit(&commit_b);
2078 tx.repo_mut().rebase_descendants().block_on()?;
2079 let repo = tx.commit("test").block_on()?;
2080
2081 // Workspaces 1 and 2 had B checked out, so they get updated to the same new
2082 // commit on top of C. Workspace 3 had A checked out, so it doesn't get updated.
2083 assert_eq!(
2084 repo.view().get_wc_commit_id(&ws1_name),
2085 repo.view().get_wc_commit_id(&ws2_name)
2086 );
2087 let checkout = repo
2088 .store()
2089 .get_commit(repo.view().get_wc_commit_id(&ws1_name).unwrap())?;
2090 assert_eq!(checkout.parent_ids(), vec![commit_a.id().clone()]);
2091 assert_eq!(repo.view().get_wc_commit_id(&ws3_name), Some(commit_a.id()));
2092 Ok(())
2093 }
2094
2095 #[test]
2096 fn test_rebase_descendants_update_checkout_abandoned_merge() -> TestResult {
2097 let test_repo = TestRepo::init();
2098 let repo = &test_repo.repo;
2099
2100 // Checked-out merge commit D was abandoned. A new merge commit should become
2101 // checked out.
2102 //
2103 // D
2104 // |\
2105 // B C
2106 // |/
2107 // A
2108 let mut tx = repo.start_transaction();
2109 let commit_a = write_random_commit(tx.repo_mut());
2110 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
2111 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
2112 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b, &commit_c]);
2113 let ws_name = WorkspaceName::DEFAULT.to_owned();
2114 tx.repo_mut()
2115 .set_wc_commit(ws_name.clone(), commit_d.id().clone())?;
2116 let repo = tx.commit("test").block_on()?;
2117
2118 let mut tx = repo.start_transaction();
2119 tx.repo_mut().record_abandoned_commit(&commit_d);
2120 tx.repo_mut().rebase_descendants().block_on()?;
2121 let repo = tx.commit("test").block_on()?;
2122
2123 let new_checkout_id = repo.view().get_wc_commit_id(&ws_name).unwrap();
2124 let checkout = repo.store().get_commit(new_checkout_id)?;
2125 assert_eq!(
2126 checkout.parent_ids(),
2127 vec![commit_b.id().clone(), commit_c.id().clone()]
2128 );
2129 Ok(())
2130 }
2131
2132 #[test_case(EmptyBehavior::Keep; "keep all commits")]
2133 #[test_case(EmptyBehavior::AbandonNewlyEmpty; "abandon newly empty commits")]
2134 #[test_case(EmptyBehavior::AbandonAllEmpty ; "abandon all empty commits")]
2135 fn test_empty_commit_option(empty_behavior: EmptyBehavior) {
2136 let test_repo = TestRepo::init();
2137 let repo = &test_repo.repo;
2138
2139 // Rebase a previously empty commit, a newly empty commit, and a commit with
2140 // actual changes.
2141 //
2142 // BD (commit B joined with commit D)
2143 // | H (empty, no parent tree changes)
2144 // | |
2145 // | G
2146 // | |
2147 // | F (clean merge)
2148 // | /|\
2149 // | C D E (empty, but parent tree changes)
2150 // | \|/
2151 // | B
2152 // A__/
2153 let mut tx = repo.start_transaction();
2154 let mut_repo = tx.repo_mut();
2155 let create_fixed_tree = |paths: &[&str]| {
2156 create_tree_with(repo, |builder| {
2157 for path in paths {
2158 builder.file(repo_path(path), path);
2159 }
2160 })
2161 };
2162
2163 // The commit_with_parents function generates non-empty merge commits, so it
2164 // isn't suitable for this test case.
2165 let tree_b = create_fixed_tree(&["B"]);
2166 let tree_c = create_fixed_tree(&["B", "C"]);
2167 let tree_d = create_fixed_tree(&["B", "D"]);
2168 let tree_f = create_fixed_tree(&["B", "C", "D"]);
2169 let tree_g = create_fixed_tree(&["B", "C", "D", "G"]);
2170
2171 let commit_a = write_random_commit(mut_repo);
2172
2173 let mut create_commit = |parents: &[&Commit], tree: &MergedTree| {
2174 create_random_commit(mut_repo)
2175 .set_parents(
2176 parents
2177 .iter()
2178 .map(|commit| commit.id().clone())
2179 .collect_vec(),
2180 )
2181 .set_tree(tree.clone())
2182 .write_unwrap()
2183 };
2184 let commit_b = create_commit(&[&commit_a], &tree_b);
2185 let commit_c = create_commit(&[&commit_b], &tree_c);
2186 let commit_d = create_commit(&[&commit_b], &tree_d);
2187 let commit_e = create_commit(&[&commit_b], &tree_b);
2188 let commit_f = create_commit(&[&commit_c, &commit_d, &commit_e], &tree_f);
2189 let commit_g = create_commit(&[&commit_f], &tree_g);
2190 let commit_h = create_commit(&[&commit_g], &tree_g);
2191 let commit_bd = create_commit(&[&commit_a], &tree_d);
2192
2193 tx.repo_mut()
2194 .set_rewritten_commit(commit_b.id().clone(), commit_bd.id().clone());
2195 let rebase_map = rebase_descendants_with_options_return_map(
2196 tx.repo_mut(),
2197 &RebaseOptions {
2198 empty: empty_behavior,
2199 rewrite_refs: RewriteRefsOptions {
2200 delete_abandoned_bookmarks: false,
2201 },
2202 simplify_ancestor_merge: true,
2203 },
2204 );
2205
2206 let new_head = match empty_behavior {
2207 EmptyBehavior::Keep => {
2208 // The commit C isn't empty.
2209 let new_commit_c =
2210 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_c, &[commit_bd.id()]);
2211 let new_commit_d =
2212 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_d, &[commit_bd.id()]);
2213 let new_commit_e =
2214 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_e, &[commit_bd.id()]);
2215 let new_commit_f = assert_rebased_onto(
2216 tx.repo_mut(),
2217 &rebase_map,
2218 &commit_f,
2219 &[new_commit_c.id(), new_commit_d.id(), new_commit_e.id()],
2220 );
2221 let new_commit_g =
2222 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_g, &[new_commit_f.id()]);
2223 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_h, &[new_commit_g.id()])
2224 }
2225 EmptyBehavior::AbandonAllEmpty => {
2226 // The commit C isn't empty.
2227 let new_commit_c =
2228 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_c, &[commit_bd.id()]);
2229 // D and E are empty, and F is a clean merge with only one child. Thus, F is
2230 // also considered empty.
2231 assert_abandoned_with_parent(tx.repo_mut(), &rebase_map, &commit_d, commit_bd.id());
2232 assert_abandoned_with_parent(tx.repo_mut(), &rebase_map, &commit_e, commit_bd.id());
2233 assert_abandoned_with_parent(tx.repo_mut(), &rebase_map, &commit_f, new_commit_c.id());
2234 let new_commit_g =
2235 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_g, &[new_commit_c.id()]);
2236 assert_abandoned_with_parent(tx.repo_mut(), &rebase_map, &commit_h, new_commit_g.id())
2237 }
2238 EmptyBehavior::AbandonNewlyEmpty => {
2239 // The commit C isn't empty.
2240 let new_commit_c =
2241 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_c, &[commit_bd.id()]);
2242
2243 // The changes in D are included in BD, so D is newly empty.
2244 assert_abandoned_with_parent(tx.repo_mut(), &rebase_map, &commit_d, commit_bd.id());
2245 // E was already empty, so F is a merge commit with C and E as parents.
2246 // Although it's empty, we still keep it because we don't want to drop merge
2247 // commits.
2248 let new_commit_e =
2249 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_e, &[commit_bd.id()]);
2250 let new_commit_f = assert_rebased_onto(
2251 tx.repo_mut(),
2252 &rebase_map,
2253 &commit_f,
2254 &[new_commit_c.id(), new_commit_e.id()],
2255 );
2256 let new_commit_g =
2257 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_g, &[new_commit_f.id()]);
2258 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_h, &[new_commit_g.id()])
2259 }
2260 };
2261
2262 assert_eq!(rebase_map.len(), 6);
2263
2264 assert_eq!(
2265 *tx.repo().view().heads(),
2266 hashset! {
2267 new_head.id().clone(),
2268 }
2269 );
2270 }
2271
2272 #[test]
2273 fn test_rebase_abandoning_empty() -> TestResult {
2274 let test_repo = TestRepo::init();
2275 let repo = &test_repo.repo;
2276
2277 // Rebase B onto B2, where B2 and B have the same tree, abandoning all empty
2278 // commits.
2279 //
2280 // We expect B, D, E, and G to be skipped because they're empty. F remains
2281 // as it's not empty.
2282 // F G (empty)
2283 // |/
2284 // E (WC, empty) D (empty) F' E' (WC, empty)
2285 // | / |/
2286 // C------------- C'
2287 // | => |
2288 // B B2 B2
2289 // |/ |
2290 // A A
2291
2292 let mut tx = repo.start_transaction();
2293 let commit_a = write_random_commit(tx.repo_mut());
2294 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
2295 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
2296 let commit_d = create_random_commit(tx.repo_mut())
2297 .set_parents(vec![commit_c.id().clone()])
2298 .set_tree(commit_c.tree())
2299 .write_unwrap();
2300 let commit_e = create_random_commit(tx.repo_mut())
2301 .set_parents(vec![commit_c.id().clone()])
2302 .set_tree(commit_c.tree())
2303 .write_unwrap();
2304 let commit_b2 = create_random_commit(tx.repo_mut())
2305 .set_parents(vec![commit_a.id().clone()])
2306 .set_tree(commit_b.tree())
2307 .write_unwrap();
2308 let commit_f = create_random_commit(tx.repo_mut())
2309 .set_parents(vec![commit_e.id().clone()])
2310 .write_unwrap();
2311 let commit_g = create_random_commit(tx.repo_mut())
2312 .set_parents(vec![commit_e.id().clone()])
2313 .set_tree(commit_e.tree())
2314 .write_unwrap();
2315
2316 let workspace = WorkspaceNameBuf::from("ws");
2317 tx.repo_mut()
2318 .set_wc_commit(workspace.clone(), commit_e.id().clone())?;
2319
2320 let rebase_options = RebaseOptions {
2321 empty: EmptyBehavior::AbandonAllEmpty,
2322 rewrite_refs: RewriteRefsOptions {
2323 delete_abandoned_bookmarks: false,
2324 },
2325 simplify_ancestor_merge: true,
2326 };
2327 let rewriter = CommitRewriter::new(tx.repo_mut(), commit_b, vec![commit_b2.id().clone()]);
2328 rebase_commit_with_options(rewriter, &rebase_options).block_on()?;
2329 let rebase_map = rebase_descendants_with_options_return_map(tx.repo_mut(), &rebase_options);
2330 assert_eq!(rebase_map.len(), 5);
2331 let new_commit_c = assert_rebased_onto(tx.repo(), &rebase_map, &commit_c, &[commit_b2.id()]);
2332 assert_abandoned_with_parent(tx.repo(), &rebase_map, &commit_d, new_commit_c.id());
2333 assert_abandoned_with_parent(tx.repo(), &rebase_map, &commit_e, new_commit_c.id());
2334 let new_commit_f = assert_rebased_onto(tx.repo(), &rebase_map, &commit_f, &[new_commit_c.id()]);
2335 assert_abandoned_with_parent(tx.repo(), &rebase_map, &commit_g, new_commit_c.id());
2336
2337 let new_wc_commit_id = tx
2338 .repo()
2339 .view()
2340 .get_wc_commit_id(&workspace)
2341 .unwrap()
2342 .clone();
2343 let new_wc_commit = tx.repo().store().get_commit(&new_wc_commit_id)?;
2344 assert_eq!(new_wc_commit.parent_ids(), &[new_commit_c.id().clone()]);
2345
2346 assert_eq!(
2347 *tx.repo().view().heads(),
2348 hashset! {new_commit_f.id().clone(), new_wc_commit_id.clone()}
2349 );
2350 Ok(())
2351 }
2352
2353 #[test]
2354 fn test_commit_with_selection() {
2355 let test_repo = TestRepo::init();
2356 let repo = &test_repo.repo;
2357
2358 let mut tx = repo.start_transaction();
2359 let root_tree = repo.store().root_commit().tree();
2360 let commit = write_random_commit(tx.repo_mut());
2361 let commit_tree = commit.tree();
2362 let empty_selection = CommitWithSelection {
2363 commit: commit.clone(),
2364 selected_tree: root_tree.clone(),
2365 parent_tree: root_tree.clone(),
2366 };
2367 assert!(empty_selection.is_empty_selection());
2368 assert!(!empty_selection.is_full_selection());
2369
2370 let full_selection = CommitWithSelection {
2371 commit,
2372 selected_tree: commit_tree,
2373 parent_tree: root_tree,
2374 };
2375 assert!(!full_selection.is_empty_selection());
2376 assert!(full_selection.is_full_selection());
2377 }
2378
2379 #[test]
2380 fn test_find_duplicate_divergent_commits() -> TestResult {
2381 let test_repo = TestRepo::init();
2382 let repo = &test_repo.repo;
2383
2384 let mut tx = repo.start_transaction();
2385 let store = repo.store();
2386 // We want to create the following tree with divergent changes A, B, and C:
2387 // E
2388 // |
2389 // A2 C2
2390 // | |
2391 // C1 B2
2392 // | |
2393 // B1 D
2394 // | /
2395 // | /
2396 // A1
2397
2398 let tree_a1 = create_tree(repo, &[(repo_path("file1"), "a\n")]);
2399 // Commit b deletes file1 and adds file2
2400 let tree_b1 = create_tree(repo, &[(repo_path("file2"), "b\n")]);
2401 // Commit c appends to file2
2402 let tree_c1 = create_tree(repo, &[(repo_path("file2"), "b\nc\n")]);
2403 // Commit a2 re-adds file1, the same as commit a1. Since it already has a1
2404 // as an ancestor, it shouldn't be abandoned even though it is divergent.
2405 let tree_a2 = create_tree(
2406 repo,
2407 &[(repo_path("file1"), "a\n"), (repo_path("file2"), "b\nc\n")],
2408 );
2409 let tree_d = create_tree(
2410 repo,
2411 &[(repo_path("file1"), "a\n"), (repo_path("file3"), "d\n")],
2412 );
2413 let tree_b2 = create_tree(
2414 repo,
2415 &[(repo_path("file2"), "b\n"), (repo_path("file3"), "d\n")],
2416 );
2417 let tree_c2 = create_tree(
2418 repo,
2419 &[(repo_path("file2"), "b\nc\n"), (repo_path("file3"), "d\n")],
2420 );
2421 let tree_e = create_tree(
2422 repo,
2423 &[
2424 (repo_path("file2"), "b\nc\n"),
2425 (repo_path("file3"), "d\ne\n"),
2426 ],
2427 );
2428
2429 let mut make_commit = |change_id_byte, tree, parents| {
2430 tx.repo_mut()
2431 .new_commit(parents, tree)
2432 .set_change_id(ChangeId::new(vec![
2433 change_id_byte;
2434 store.change_id_length()
2435 ]))
2436 .write_unwrap()
2437 };
2438
2439 let commit_a1 = make_commit(0xAA, tree_a1, vec![store.root_commit_id().clone()]);
2440 let commit_b1 = make_commit(0xBB, tree_b1, vec![commit_a1.id().clone()]);
2441 let commit_c1 = make_commit(0xCC, tree_c1, vec![commit_b1.id().clone()]);
2442 let commit_a2 = make_commit(0xAA, tree_a2, vec![commit_c1.id().clone()]);
2443 let commit_d = make_commit(0xDD, tree_d, vec![commit_a1.id().clone()]);
2444 let commit_b2 = make_commit(0xBB, tree_b2, vec![commit_d.id().clone()]);
2445 let commit_c2 = make_commit(0xCC, tree_c2, vec![commit_b2.id().clone()]);
2446 let commit_e = make_commit(0xEE, tree_e, vec![commit_c2.id().clone()]);
2447
2448 // Simulate rebase of "d::" onto "a2"
2449 let duplicate_commits = find_duplicate_divergent_commits(
2450 tx.repo(),
2451 &[commit_a2.id().clone()],
2452 &MoveCommitsTarget::Roots(vec![commit_d.id().clone()]),
2453 )
2454 .block_on()?;
2455 // Commits b2 and c2 are duplicates
2456 assert_eq!(duplicate_commits, &[commit_c2.clone(), commit_b2.clone()]);
2457
2458 // Simulate rebase of "b1::" onto "e"
2459 let duplicate_commits = find_duplicate_divergent_commits(
2460 tx.repo(),
2461 &[commit_e.id().clone()],
2462 &MoveCommitsTarget::Roots(vec![commit_b1.id().clone()]),
2463 )
2464 .block_on()?;
2465 // Commits b1 and c1 are duplicates. Commit a2 is not a duplicate, because
2466 // it already had a1 as an ancestor before the rebase.
2467 assert_eq!(duplicate_commits, &[commit_c1.clone(), commit_b1.clone()]);
2468
2469 // Simulate rebase of "d | c2 | e" onto "a2"
2470 let duplicate_commits = find_duplicate_divergent_commits(
2471 tx.repo(),
2472 &[commit_a2.id().clone()],
2473 &MoveCommitsTarget::Commits(vec![
2474 commit_d.id().clone(),
2475 commit_c2.id().clone(),
2476 commit_e.id().clone(),
2477 ]),
2478 )
2479 .block_on()?;
2480 // Commit c2 is a duplicate
2481 assert_eq!(duplicate_commits, std::slice::from_ref(&commit_c2));
2482 Ok(())
2483 }