Debian Codegraph @

lib/tests/test_rewrite_duplicate.rs

raw
1 // Copyright 2024 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::collections::HashMap;
16
17 use itertools::Itertools as _;
18 use jj_lib::backend::CommitId;
19 use jj_lib::repo::Repo as _;
20 use jj_lib::rewrite::duplicate_commits;
21 use jj_lib::transaction::Transaction;
22 use pollster::FutureExt as _;
23 use testutils::CommitBuilderExt as _;
24 use testutils::TestRepo;
25 use testutils::TestResult;
26 use testutils::assert_tree_eq;
27 use testutils::create_tree;
28 use testutils::repo_path;
29
30 #[test]
31 fn test_duplicate_linear_contents() -> TestResult {
32 let test_repo = TestRepo::init();
33 let repo = &test_repo.repo;
34
35 let path_1 = repo_path("file1");
36 let path_2 = repo_path("file2");
37 let empty_tree = repo.store().empty_merged_tree();
38 let tree_1 = create_tree(repo, &[(path_1, "content1")]);
39 let tree_2 = create_tree(repo, &[(path_2, "content2")]);
40 let tree_1_2 = create_tree(repo, &[(path_1, "content1"), (path_2, "content2")]);
41
42 // E [=file2]
43 // D [-file1, =file2]
44 // C [=file1, +file2]
45 // B [+file1]
46 // A []
47 let mut tx = repo.start_transaction();
48 let commit_a = tx
49 .repo_mut()
50 .new_commit(
51 vec![repo.store().root_commit_id().clone()],
52 empty_tree.clone(),
53 )
54 .write_unwrap();
55 let commit_b = tx
56 .repo_mut()
57 .new_commit(vec![commit_a.id().clone()], tree_1.clone())
58 .write_unwrap();
59 let commit_c = tx
60 .repo_mut()
61 .new_commit(vec![commit_b.id().clone()], tree_1_2.clone())
62 .write_unwrap();
63 let commit_d = tx
64 .repo_mut()
65 .new_commit(vec![commit_c.id().clone()], tree_2.clone())
66 .write_unwrap();
67 let commit_e = tx
68 .repo_mut()
69 .new_commit(vec![commit_d.id().clone()], tree_2.clone())
70 .write_unwrap();
71 let repo = tx.commit("test").block_on()?;
72
73 let duplicate_in_between = |tx: &mut Transaction,
74 target_commits: &[&CommitId],
75 parent_commit_ids: &[&CommitId],
76 children_commit_ids: &[&CommitId]| {
77 duplicate_commits(
78 tx.repo_mut(),
79 &target_commits.iter().copied().cloned().collect_vec(),
80 &HashMap::new(),
81 &parent_commit_ids.iter().copied().cloned().collect_vec(),
82 &children_commit_ids.iter().copied().cloned().collect_vec(),
83 )
84 .block_on()
85 .unwrap()
86 };
87 let duplicate_onto =
88 |tx: &mut Transaction, target_commits: &[&CommitId], parent_commit_ids: &[&CommitId]| {
89 duplicate_in_between(tx, target_commits, parent_commit_ids, &[])
90 };
91
92 // Duplicate empty commit onto empty ancestor tree
93 let mut tx = repo.start_transaction();
94 let stats = duplicate_onto(&mut tx, &[commit_e.id()], &[commit_a.id()]);
95 assert_tree_eq!(stats.duplicated_commits[commit_e.id()].tree(), empty_tree);
96
97 // Duplicate empty commit onto non-empty ancestor tree
98 let mut tx = repo.start_transaction();
99 let stats = duplicate_onto(&mut tx, &[commit_e.id()], &[commit_b.id()]);
100 assert_tree_eq!(stats.duplicated_commits[commit_e.id()].tree(), tree_1);
101
102 // Duplicate non-empty commit onto empty ancestor tree
103 let mut tx = repo.start_transaction();
104 let stats = duplicate_onto(&mut tx, &[commit_c.id()], &[commit_a.id()]);
105 assert_tree_eq!(stats.duplicated_commits[commit_c.id()].tree(), tree_2);
106
107 // Duplicate non-empty commit onto non-empty ancestor tree
108 let mut tx = repo.start_transaction();
109 let stats = duplicate_onto(&mut tx, &[commit_d.id()], &[commit_b.id()]);
110 assert_tree_eq!(stats.duplicated_commits[commit_d.id()].tree(), empty_tree);
111
112 // Duplicate non-empty commit onto non-empty descendant tree
113 let mut tx = repo.start_transaction();
114 let stats = duplicate_onto(&mut tx, &[commit_b.id()], &[commit_d.id()]);
115 assert_tree_eq!(stats.duplicated_commits[commit_b.id()].tree(), tree_1_2);
116
117 // Duplicate multiple contiguous commits
118 let mut tx = repo.start_transaction();
119 let stats = duplicate_onto(&mut tx, &[commit_e.id(), commit_d.id()], &[commit_b.id()]);
120 assert_tree_eq!(stats.duplicated_commits[commit_d.id()].tree(), empty_tree);
121 assert_tree_eq!(stats.duplicated_commits[commit_e.id()].tree(), empty_tree);
122
123 // Duplicate multiple non-contiguous commits
124 let mut tx = repo.start_transaction();
125 let stats = duplicate_onto(&mut tx, &[commit_e.id(), commit_c.id()], &[commit_a.id()]);
126 assert_tree_eq!(stats.duplicated_commits[commit_c.id()].tree(), tree_2);
127 assert_tree_eq!(stats.duplicated_commits[commit_e.id()].tree(), tree_2);
128
129 // Duplicate onto multiple parents
130 let mut tx = repo.start_transaction();
131 let stats = duplicate_onto(&mut tx, &[commit_d.id()], &[commit_c.id(), commit_b.id()]);
132 assert_tree_eq!(stats.duplicated_commits[commit_d.id()].tree(), tree_2);
133
134 // Insert duplicated commit
135 let mut tx = repo.start_transaction();
136 let stats = duplicate_in_between(
137 &mut tx,
138 &[commit_b.id()],
139 &[commit_d.id()],
140 &[commit_e.id()],
141 );
142 assert_tree_eq!(stats.duplicated_commits[commit_b.id()].tree(), tree_1_2);
143 let [head_id] = tx.repo().view().heads().iter().collect_array().unwrap();
144 assert_ne!(head_id, commit_e.id());
145 assert_tree_eq!(tx.repo().store().get_commit(head_id)?.tree(), tree_1_2);
146 Ok(())
147 }