Debian Codegraph @

lib/tests/test_rewrite_transform.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 jj_lib::commit::Commit;
18 use jj_lib::repo::Repo as _;
19 use jj_lib::revset::RevsetExpression;
20 use jj_lib::rewrite::RewriteRefsOptions;
21 use maplit::hashmap;
22 use maplit::hashset;
23 use pollster::FutureExt as _;
24 use testutils::TestRepo;
25 use testutils::TestResult;
26 use testutils::write_random_commit;
27 use testutils::write_random_commit_with_parents;
28
29 // Simulate some `jj sync` command that rebases B:: onto G while abandoning C
30 // (because it's presumably already in G).
31 //
32 // G
33 // | E
34 // | D F
35 // | |/
36 // | C
37 // | B
38 // |/
39 // A
40 #[test]
41 fn test_transform_descendants_sync() -> TestResult {
42 let test_repo = TestRepo::init();
43 let repo = &test_repo.repo;
44
45 let mut tx = repo.start_transaction();
46 let commit_a = write_random_commit(tx.repo_mut());
47 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
48 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
49 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);
50 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d]);
51 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);
52 let commit_g = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
53
54 let mut rebased = HashMap::new();
55 tx.repo_mut()
56 .transform_descendants(vec![commit_b.id().clone()], async |mut rewriter| {
57 rewriter.replace_parent(commit_a.id(), [commit_g.id()]);
58 if *rewriter.old_commit() == commit_c {
59 rewriter.abandon();
60 } else {
61 let old_commit_id = rewriter.old_commit().id().clone();
62 let new_commit = rewriter.rebase().await?.write().await?;
63 rebased.insert(old_commit_id, new_commit);
64 }
65 Ok(())
66 })
67 .block_on()?;
68 assert_eq!(rebased.len(), 4);
69 let new_commit_b = rebased.get(commit_b.id()).unwrap();
70 let new_commit_d = rebased.get(commit_d.id()).unwrap();
71 let new_commit_e = rebased.get(commit_e.id()).unwrap();
72 let new_commit_f = rebased.get(commit_f.id()).unwrap();
73
74 assert_eq!(
75 *tx.repo().view().heads(),
76 hashset! {
77 new_commit_e.id().clone(),
78 new_commit_f.id().clone(),
79 }
80 );
81
82 assert_eq!(new_commit_b.parent_ids(), vec![commit_g.id().clone()]);
83 assert_eq!(new_commit_d.parent_ids(), vec![new_commit_b.id().clone()]);
84 assert_eq!(new_commit_e.parent_ids(), vec![new_commit_d.id().clone()]);
85 assert_eq!(new_commit_f.parent_ids(), vec![new_commit_b.id().clone()]);
86 Ok(())
87 }
88
89 // Transform just commit C replacing parent A by parent B. The parents should be
90 // deduplicated.
91 //
92 // C
93 // /|
94 // B |
95 // |/
96 // A
97 #[test]
98 fn test_transform_descendants_sync_linearize_merge() -> TestResult {
99 let test_repo = TestRepo::init();
100 let repo = &test_repo.repo;
101
102 let mut tx = repo.start_transaction();
103 let commit_a = write_random_commit(tx.repo_mut());
104 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
105 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a, &commit_b]);
106
107 let mut rebased = HashMap::new();
108 tx.repo_mut()
109 .transform_descendants(vec![commit_c.id().clone()], async |mut rewriter| {
110 rewriter.replace_parent(commit_a.id(), [commit_b.id()]);
111 let old_commit_id = rewriter.old_commit().id().clone();
112 let new_commit = rewriter.rebase().await?.write().await?;
113 rebased.insert(old_commit_id, new_commit);
114 Ok(())
115 })
116 .block_on()?;
117 assert_eq!(rebased.len(), 1);
118 let new_commit_c = rebased.get(commit_c.id()).unwrap();
119
120 assert_eq!(
121 *tx.repo().view().heads(),
122 hashset! {
123 new_commit_c.id().clone(),
124 }
125 );
126
127 assert_eq!(new_commit_c.parent_ids(), vec![commit_b.id().clone()]);
128 Ok(())
129 }
130
131 // Reorder commits B and C by using the `new_parents_map`. Reordering has to be
132 // done outside of the typical callback since we must ensure that the new
133 // traversal order of the commits is valid.
134 //
135 // G
136 // | E
137 // | D F
138 // | |/
139 // | C
140 // | B
141 // |/
142 // A
143 #[test]
144 fn test_transform_descendants_new_parents_map() -> TestResult {
145 let test_repo = TestRepo::init();
146 let repo = &test_repo.repo;
147
148 let mut tx = repo.start_transaction();
149 let commit_a = write_random_commit(tx.repo_mut());
150 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
151 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
152 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);
153 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d]);
154 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);
155 let commit_g = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
156
157 let options = RewriteRefsOptions::default();
158 let mut rebased = HashMap::new();
159 tx.repo_mut()
160 .transform_descendants_with_options(
161 vec![commit_b.id().clone()],
162 &RevsetExpression::none(),
163 &hashmap! {
164 commit_b.id().clone() => vec![commit_c.id().clone()],
165 commit_c.id().clone() => vec![commit_a.id().clone()],
166 },
167 &options,
168 async |mut rewriter| {
169 let old_commit_id = rewriter.old_commit().id().clone();
170 if old_commit_id != *commit_b.id()
171 && let Some(new_commit_c) = rebased.get(commit_c.id())
172 {
173 let new_commit_b: &Commit = rebased.get(commit_b.id()).unwrap();
174 rewriter.replace_parent(new_commit_c.id(), [new_commit_b.id()]);
175 }
176 let new_commit = rewriter.rebase().await?.write().await?;
177 rebased.insert(old_commit_id, new_commit);
178 Ok(())
179 },
180 )
181 .block_on()?;
182 assert_eq!(rebased.len(), 5);
183 let new_commit_b = rebased.get(commit_b.id()).unwrap();
184 let new_commit_c = rebased.get(commit_c.id()).unwrap();
185 let new_commit_d = rebased.get(commit_d.id()).unwrap();
186 let new_commit_e = rebased.get(commit_e.id()).unwrap();
187 let new_commit_f = rebased.get(commit_f.id()).unwrap();
188
189 assert_eq!(
190 *tx.repo().view().heads(),
191 hashset! {
192 commit_g.id().clone(),
193 new_commit_e.id().clone(),
194 new_commit_f.id().clone(),
195 }
196 );
197
198 assert_eq!(new_commit_c.parent_ids(), vec![commit_a.id().clone()]);
199 assert_eq!(new_commit_b.parent_ids(), vec![new_commit_c.id().clone()]);
200 assert_eq!(new_commit_d.parent_ids(), vec![new_commit_b.id().clone()]);
201 assert_eq!(new_commit_e.parent_ids(), vec![new_commit_d.id().clone()]);
202 assert_eq!(new_commit_f.parent_ids(), vec![new_commit_b.id().clone()]);
203 Ok(())
204 }