Debian Codegraph @

lib/tests/test_default_revset_graph_iterator.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 itertools::Itertools as _;
16 use jj_lib::backend::CommitId;
17 use jj_lib::commit::Commit;
18 use jj_lib::default_index::DefaultReadonlyIndex;
19 use jj_lib::default_index::DefaultReadonlyIndexRevset;
20 use jj_lib::graph::GraphEdge;
21 use jj_lib::repo::ReadonlyRepo;
22 use jj_lib::repo::Repo as _;
23 use jj_lib::revset::ResolvedExpression;
24 use pollster::FutureExt as _;
25 use test_case::test_case;
26 use testutils::TestRepo;
27 use testutils::TestResult;
28 use testutils::write_random_commit;
29 use testutils::write_random_commit_with_parents;
30
31 fn revset_for_commits(repo: &ReadonlyRepo, commits: &[&Commit]) -> DefaultReadonlyIndexRevset {
32 let index: &DefaultReadonlyIndex = repo.readonly_index().downcast_ref().unwrap();
33 let expression =
34 ResolvedExpression::Commits(commits.iter().map(|commit| commit.id().clone()).collect());
35 index
36 .evaluate_revset_impl(&expression, repo.store())
37 .unwrap()
38 }
39
40 fn direct(commit: &Commit) -> GraphEdge<CommitId> {
41 GraphEdge::direct(commit.id().clone())
42 }
43
44 fn indirect(commit: &Commit) -> GraphEdge<CommitId> {
45 GraphEdge::indirect(commit.id().clone())
46 }
47
48 fn missing(commit: &Commit) -> GraphEdge<CommitId> {
49 GraphEdge::missing(commit.id().clone())
50 }
51
52 #[test_case(false, 0; "keep transitive edges")]
53 #[test_case(true, 0; "skip transitive edges")]
54 #[test_matrix(true, 60..64; "skip transitive edges")]
55 fn test_graph_iterator_linearized(skip_transitive_edges: bool, padding: u32) -> TestResult {
56 let test_repo = TestRepo::init();
57 let repo = &test_repo.repo;
58
59 // Tests that a fork and a merge becomes a single edge:
60 // D
61 // |\ D
62 // b c => :
63 // |/ A
64 // A ~
65 // |
66 // root
67 let mut tx = repo.start_transaction();
68 // Pad commits to move interesting parts to boundary of u64 bit set
69 for _ in 0..padding {
70 write_random_commit(tx.repo_mut());
71 }
72 let commit_a = write_random_commit(tx.repo_mut());
73 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
74 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
75 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b, &commit_c]);
76 let repo = tx.commit("test").block_on()?;
77 let root_commit = repo.store().root_commit();
78
79 let revset = revset_for_commits(repo.as_ref(), &[&commit_a, &commit_d]);
80 let commits: Vec<_> = revset
81 .iter_graph_impl(skip_transitive_edges)
82 .try_collect()?;
83 assert_eq!(commits.len(), 2);
84 assert_eq!(commits[0].0, *commit_d.id());
85 assert_eq!(commits[1].0, *commit_a.id());
86 assert_eq!(commits[0].1, vec![indirect(&commit_a)]);
87 assert_eq!(commits[1].1, vec![missing(&root_commit)]);
88 Ok(())
89 }
90
91 #[test_case(false, 0; "keep transitive edges")]
92 #[test_case(true, 0; "skip transitive edges")]
93 #[test_matrix(true, 58..64; "skip transitive edges")]
94 fn test_graph_iterator_virtual_octopus(skip_transitive_edges: bool, padding: u32) -> TestResult {
95 let test_repo = TestRepo::init();
96 let repo = &test_repo.repo;
97
98 // Tests that merges outside the set can result in more parent edges than there
99 // was in the input:
100 //
101 // F
102 // |\
103 // d e F
104 // |\|\ => /|\
105 // A B C A B C
106 // \|/ ~ ~ ~
107 // root
108 let mut tx = repo.start_transaction();
109 // Pad commits to move interesting parts to boundary of u64 bit set
110 for _ in 0..padding {
111 write_random_commit(tx.repo_mut());
112 }
113 let commit_a = write_random_commit(tx.repo_mut());
114 let commit_b = write_random_commit(tx.repo_mut());
115 let commit_c = write_random_commit(tx.repo_mut());
116 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a, &commit_b]);
117 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b, &commit_c]);
118 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d, &commit_e]);
119 let repo = tx.commit("test").block_on()?;
120 let root_commit = repo.store().root_commit();
121
122 let revset = revset_for_commits(repo.as_ref(), &[&commit_a, &commit_b, &commit_c, &commit_f]);
123 let commits: Vec<_> = revset
124 .iter_graph_impl(skip_transitive_edges)
125 .try_collect()?;
126 assert_eq!(commits.len(), 4);
127 assert_eq!(commits[0].0, *commit_f.id());
128 assert_eq!(commits[1].0, *commit_c.id());
129 assert_eq!(commits[2].0, *commit_b.id());
130 assert_eq!(commits[3].0, *commit_a.id());
131 assert_eq!(
132 commits[0].1,
133 vec![
134 indirect(&commit_a),
135 indirect(&commit_b),
136 indirect(&commit_c),
137 ]
138 );
139 assert_eq!(commits[1].1, vec![missing(&root_commit)]);
140 assert_eq!(commits[2].1, vec![missing(&root_commit)]);
141 assert_eq!(commits[3].1, vec![missing(&root_commit)]);
142 Ok(())
143 }
144
145 #[test_case(false, 0; "keep transitive edges")]
146 #[test_case(true, 0; "skip transitive edges")]
147 #[test_matrix(true, 59..64; "skip transitive edges")]
148 fn test_graph_iterator_simple_fork(skip_transitive_edges: bool, padding: u32) -> TestResult {
149 let test_repo = TestRepo::init();
150 let repo = &test_repo.repo;
151
152 // Tests that the branch with "C" gets emitted correctly:
153 // E
154 // |
155 // d
156 // | C E C
157 // |/ |/
158 // b => A
159 // | ~
160 // A
161 // |
162 // root
163 let mut tx = repo.start_transaction();
164 // Pad commits to move interesting parts to boundary of u64 bit set
165 for _ in 0..padding {
166 write_random_commit(tx.repo_mut());
167 }
168 let commit_a = write_random_commit(tx.repo_mut());
169 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
170 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
171 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
172 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d]);
173 let repo = tx.commit("test").block_on()?;
174 let root_commit = repo.store().root_commit();
175
176 let revset = revset_for_commits(repo.as_ref(), &[&commit_a, &commit_c, &commit_e]);
177 let commits: Vec<_> = revset
178 .iter_graph_impl(skip_transitive_edges)
179 .try_collect()?;
180 assert_eq!(commits.len(), 3);
181 assert_eq!(commits[0].0, *commit_e.id());
182 assert_eq!(commits[1].0, *commit_c.id());
183 assert_eq!(commits[2].0, *commit_a.id());
184 assert_eq!(commits[0].1, vec![indirect(&commit_a)]);
185 assert_eq!(commits[1].1, vec![indirect(&commit_a)]);
186 assert_eq!(commits[2].1, vec![missing(&root_commit)]);
187 Ok(())
188 }
189
190 #[test_case(false, 0; "keep transitive edges")]
191 #[test_case(true, 0; "skip transitive edges")]
192 #[test_matrix(true, 58..64; "skip transitive edges")]
193 fn test_graph_iterator_multiple_missing(skip_transitive_edges: bool, padding: u32) -> TestResult {
194 let test_repo = TestRepo::init();
195 let repo = &test_repo.repo;
196
197 // Tests that we get missing edges to "a" and "c" and not just one missing edge
198 // to the root.
199 // F
200 // / \ F
201 // d e => /|\
202 // |\ /| ~ B ~
203 // a B c ~
204 // \|/
205 // root
206 let mut tx = repo.start_transaction();
207 // Pad commits to move interesting parts to boundary of u64 bit set
208 for _ in 0..padding {
209 write_random_commit(tx.repo_mut());
210 }
211 let commit_a = write_random_commit(tx.repo_mut());
212 let commit_b = write_random_commit(tx.repo_mut());
213 let commit_c = write_random_commit(tx.repo_mut());
214 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a, &commit_b]);
215 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b, &commit_c]);
216 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d, &commit_e]);
217 let repo = tx.commit("test").block_on()?;
218 let root_commit = repo.store().root_commit();
219
220 let revset = revset_for_commits(repo.as_ref(), &[&commit_b, &commit_f]);
221 let commits: Vec<_> = revset
222 .iter_graph_impl(skip_transitive_edges)
223 .try_collect()?;
224 assert_eq!(commits.len(), 2);
225 assert_eq!(commits[0].0, *commit_f.id());
226 assert_eq!(commits[1].0, *commit_b.id());
227 assert_eq!(
228 commits[0].1,
229 vec![missing(&commit_a), indirect(&commit_b), missing(&commit_c)]
230 );
231 assert_eq!(commits[1].1, vec![missing(&root_commit)]);
232 Ok(())
233 }
234
235 #[test_case(false, 0; "keep transitive edges")]
236 #[test_case(true, 0; "skip transitive edges")]
237 #[test_matrix(true, 58..64; "skip transitive edges")]
238 fn test_graph_iterator_edge_to_ancestor(skip_transitive_edges: bool, padding: u32) -> TestResult {
239 let test_repo = TestRepo::init();
240 let repo = &test_repo.repo;
241
242 // Tests that we get both an edge from F to D and to D's ancestor C if we keep
243 // transitive edges and only the edge from F to D if we skip transitive
244 // edges:
245 // F F
246 // |\ |\
247 // D e D :
248 // |\| => |\:
249 // b C ~ C
250 // | ~
251 // a
252 // |
253 // root
254 let mut tx = repo.start_transaction();
255 // Pad commits to move interesting parts to boundary of u64 bit set
256 for _ in 0..padding {
257 write_random_commit(tx.repo_mut());
258 }
259 let commit_a = write_random_commit(tx.repo_mut());
260 let commit_b = write_random_commit(tx.repo_mut());
261 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
262 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b, &commit_c]);
263 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);
264 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d, &commit_e]);
265 let repo = tx.commit("test").block_on()?;
266
267 let revset = revset_for_commits(repo.as_ref(), &[&commit_c, &commit_d, &commit_f]);
268 let commits: Vec<_> = revset
269 .iter_graph_impl(skip_transitive_edges)
270 .try_collect()?;
271 assert_eq!(commits.len(), 3);
272 assert_eq!(commits[0].0, *commit_f.id());
273 assert_eq!(commits[1].0, *commit_d.id());
274 assert_eq!(commits[2].0, *commit_c.id());
275 if skip_transitive_edges {
276 assert_eq!(commits[0].1, vec![direct(&commit_d)]);
277 } else {
278 assert_eq!(commits[0].1, vec![direct(&commit_d), indirect(&commit_c),]);
279 }
280 assert_eq!(commits[1].1, vec![missing(&commit_b), direct(&commit_c)]);
281 assert_eq!(commits[2].1, vec![missing(&commit_a)]);
282 Ok(())
283 }
284
285 #[test_case(false, 0; "keep transitive edges")]
286 #[test_case(true, 0; "skip transitive edges")]
287 #[test_matrix(true, 54..64; "skip transitive edges")]
288 fn test_graph_iterator_edge_escapes_from_(skip_transitive_edges: bool, padding: u32) -> TestResult {
289 let test_repo = TestRepo::init();
290 let repo = &test_repo.repo;
291
292 // Tests a more complex case for skipping transitive edges.
293 // J
294 // /|
295 // | i J
296 // | |\ /:
297 // | | H | H
298 // G | | G :
299 // | e f => : D
300 // | \|\ :/
301 // | D | A
302 // \ / c |
303 // b / root
304 // |/
305 // A
306 // |
307 // root
308 let mut tx = repo.start_transaction();
309 // Pad commits to move interesting parts to boundary of u64 bit set
310 for _ in 0..padding {
311 write_random_commit(tx.repo_mut());
312 }
313 let commit_a = write_random_commit(tx.repo_mut());
314 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
315 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
316 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
317 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d]);
318 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d, &commit_c]);
319 let commit_g = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
320 let commit_h = write_random_commit_with_parents(tx.repo_mut(), &[&commit_f]);
321 let commit_i = write_random_commit_with_parents(tx.repo_mut(), &[&commit_e, &commit_h]);
322 let commit_j = write_random_commit_with_parents(tx.repo_mut(), &[&commit_g, &commit_i]);
323 let repo = tx.commit("test").block_on()?;
324 let root_commit = repo.store().root_commit();
325
326 let revset = revset_for_commits(
327 repo.as_ref(),
328 &[&commit_a, &commit_d, &commit_g, &commit_h, &commit_j],
329 );
330 let commits: Vec<_> = revset
331 .iter_graph_impl(skip_transitive_edges)
332 .try_collect()?;
333 assert_eq!(commits.len(), 5);
334 assert_eq!(commits[0].0, *commit_j.id());
335 assert_eq!(commits[1].0, *commit_h.id());
336 assert_eq!(commits[2].0, *commit_g.id());
337 assert_eq!(commits[3].0, *commit_d.id());
338 assert_eq!(commits[4].0, *commit_a.id());
339 if skip_transitive_edges {
340 assert_eq!(commits[0].1, vec![direct(&commit_g), indirect(&commit_h)]);
341 assert_eq!(commits[1].1, vec![indirect(&commit_d)]);
342 } else {
343 assert_eq!(
344 commits[0].1,
345 vec![direct(&commit_g), indirect(&commit_d), indirect(&commit_h)]
346 );
347 assert_eq!(commits[1].1, vec![indirect(&commit_d), indirect(&commit_a)]);
348 }
349 assert_eq!(commits[2].1, vec![indirect(&commit_a)]);
350 assert_eq!(commits[3].1, vec![indirect(&commit_a)]);
351 assert_eq!(commits[4].1, vec![missing(&root_commit)]);
352 Ok(())
353 }