Debian Codegraph @

lib/tests/test_index.rs

raw
1 // Copyright 2020 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::fs;
16 use std::sync::Arc;
17
18 use assert_matches::assert_matches;
19 use futures::StreamExt as _;
20 use itertools::Itertools as _;
21 use jj_lib::backend::ChangeId;
22 use jj_lib::backend::CommitId;
23 use jj_lib::commit::Commit;
24 use jj_lib::default_index::DefaultIndexStore;
25 use jj_lib::default_index::DefaultIndexStoreError;
26 use jj_lib::default_index::DefaultMutableIndex;
27 use jj_lib::default_index::DefaultReadonlyIndex;
28 use jj_lib::index::Index;
29 use jj_lib::index::ResolvedChangeState;
30 use jj_lib::index::ResolvedChangeTargets;
31 use jj_lib::object_id::HexPrefix;
32 use jj_lib::object_id::ObjectId as _;
33 use jj_lib::object_id::PrefixResolution;
34 use jj_lib::op_store::RefTarget;
35 use jj_lib::op_store::RemoteRef;
36 use jj_lib::ref_name::RefName;
37 use jj_lib::ref_name::RemoteName;
38 use jj_lib::ref_name::RemoteRefSymbol;
39 use jj_lib::repo::MutableRepo;
40 use jj_lib::repo::ReadonlyRepo;
41 use jj_lib::repo::Repo as _;
42 use jj_lib::repo_path::RepoPathBuf;
43 use jj_lib::revset::GENERATION_RANGE_FULL;
44 use jj_lib::revset::PARENTS_RANGE_FULL;
45 use jj_lib::revset::ResolvedExpression;
46 use pollster::FutureExt as _;
47 use test_case::test_case;
48 use testutils::CommitBuilderExt as _;
49 use testutils::TestRepo;
50 use testutils::TestResult;
51 use testutils::assert_tree_eq;
52 use testutils::commit_transactions;
53 use testutils::create_tree;
54 use testutils::repo_path;
55 use testutils::repo_path_buf;
56 use testutils::test_backend::TestBackend;
57 use testutils::write_random_commit;
58 use testutils::write_random_commit_with_parents;
59
60 fn remote_symbol<'a, N, M>(name: &'a N, remote: &'a M) -> RemoteRefSymbol<'a>
61 where
62 N: AsRef<RefName> + ?Sized,
63 M: AsRef<RemoteName> + ?Sized,
64 {
65 RemoteRefSymbol {
66 name: name.as_ref(),
67 remote: remote.as_ref(),
68 }
69 }
70
71 fn enable_changed_path_index(repo: &ReadonlyRepo) -> Arc<ReadonlyRepo> {
72 let default_index_store: &DefaultIndexStore = repo.index_store().downcast_ref().unwrap();
73 default_index_store
74 .build_changed_path_index_at_operation(repo.op_id(), repo.store(), 0, |_| ())
75 .block_on()
76 .unwrap();
77 repo.reload_at(repo.operation()).block_on().unwrap()
78 }
79
80 fn collect_changed_paths(repo: &ReadonlyRepo, commit_id: &CommitId) -> Option<Vec<RepoPathBuf>> {
81 repo.index()
82 .changed_paths_in_commit(commit_id)
83 .block_on()
84 .unwrap()
85 .map(|paths| paths.collect())
86 }
87
88 fn index_has_id(index: &dyn Index, commit_id: &CommitId) -> bool {
89 index.has_id(commit_id).block_on().unwrap()
90 }
91
92 fn is_ancestor(
93 index: &DefaultReadonlyIndex,
94 ancestor_id: &CommitId,
95 descendant_id: &CommitId,
96 ) -> bool {
97 index
98 .is_ancestor(ancestor_id, descendant_id)
99 .block_on()
100 .unwrap()
101 }
102
103 #[test]
104 fn test_index_commits_empty_repo() -> TestResult {
105 let test_repo = TestRepo::init();
106 let repo = &test_repo.repo;
107
108 let index = as_readonly_index(repo);
109 // There should be just the root commit
110 assert_eq!(index.num_commits(), 1);
111
112 // Check the generation numbers of the root and the working copy
113 assert_eq!(
114 index
115 .generation_number(repo.store().root_commit_id())
116 .unwrap(),
117 0
118 );
119 Ok(())
120 }
121
122 #[test]
123 fn test_index_commits_standard_cases() -> TestResult {
124 let test_repo = TestRepo::init();
125 let repo = &test_repo.repo;
126
127 // o H
128 // o | G
129 // o | F
130 // |\|
131 // | o E
132 // | o D
133 // | o C
134 // o | B
135 // |/
136 // o A
137 // | o working copy
138 // |/
139 // o root
140
141 let root_commit_id = repo.store().root_commit_id();
142 let mut tx = repo.start_transaction();
143 let commit_a = write_random_commit(tx.repo_mut());
144 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
145 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
146 let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);
147 let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d]);
148 let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b, &commit_e]);
149 let commit_g = write_random_commit_with_parents(tx.repo_mut(), &[&commit_f]);
150 let commit_h = write_random_commit_with_parents(tx.repo_mut(), &[&commit_e]);
151 let repo = tx.commit("test").block_on()?;
152
153 let index = as_readonly_index(&repo);
154 // There should be the root commit, plus 8 more
155 assert_eq!(index.num_commits(), 1 + 8);
156
157 let stats = index.stats();
158 assert_eq!(stats.num_commits, 1 + 8);
159 assert_eq!(stats.num_merges, 1);
160 assert_eq!(stats.max_generation_number, 6);
161
162 assert_eq!(index.generation_number(root_commit_id).unwrap(), 0);
163 assert_eq!(index.generation_number(commit_a.id()).unwrap(), 1);
164 assert_eq!(index.generation_number(commit_b.id()).unwrap(), 2);
165 assert_eq!(index.generation_number(commit_c.id()).unwrap(), 2);
166 assert_eq!(index.generation_number(commit_d.id()).unwrap(), 3);
167 assert_eq!(index.generation_number(commit_e.id()).unwrap(), 4);
168 assert_eq!(index.generation_number(commit_f.id()).unwrap(), 5);
169 assert_eq!(index.generation_number(commit_g.id()).unwrap(), 6);
170 assert_eq!(index.generation_number(commit_h.id()).unwrap(), 5);
171
172 assert!(is_ancestor(index, root_commit_id, commit_a.id()));
173 assert!(!is_ancestor(index, commit_a.id(), root_commit_id));
174
175 assert!(is_ancestor(index, root_commit_id, commit_b.id()));
176 assert!(!is_ancestor(index, commit_b.id(), root_commit_id));
177
178 assert!(!is_ancestor(index, commit_b.id(), commit_c.id()));
179
180 assert!(is_ancestor(index, commit_a.id(), commit_b.id()));
181 assert!(is_ancestor(index, commit_a.id(), commit_e.id()));
182 assert!(is_ancestor(index, commit_a.id(), commit_f.id()));
183 assert!(is_ancestor(index, commit_a.id(), commit_g.id()));
184 assert!(is_ancestor(index, commit_a.id(), commit_h.id()));
185 Ok(())
186 }
187
188 #[test]
189 fn test_index_commits_criss_cross() -> TestResult {
190 let test_repo = TestRepo::init();
191 let repo = &test_repo.repo;
192
193 let num_generations = 50;
194
195 // Create a long chain of criss-crossed merges. If they were traversed without
196 // keeping track of visited nodes, it would be 2^50 visits, so if this test
197 // finishes in reasonable time, we know that we don't do a naive traversal.
198 let mut tx = repo.start_transaction();
199 let mut left_commits = vec![write_random_commit(tx.repo_mut())];
200 let mut right_commits = vec![write_random_commit(tx.repo_mut())];
201 for generation in 1..num_generations {
202 let new_left = write_random_commit_with_parents(
203 tx.repo_mut(),
204 &[
205 &left_commits[generation - 1],
206 &right_commits[generation - 1],
207 ],
208 );
209 let new_right = write_random_commit_with_parents(
210 tx.repo_mut(),
211 &[
212 &left_commits[generation - 1],
213 &right_commits[generation - 1],
214 ],
215 );
216 left_commits.push(new_left);
217 right_commits.push(new_right);
218 }
219 let repo = tx.commit("test").block_on()?;
220
221 let index = as_readonly_index(&repo);
222 // There should the root commit, plus 2 for each generation
223 assert_eq!(index.num_commits(), 1 + 2 * (num_generations as u32));
224
225 let stats = index.stats();
226 assert_eq!(stats.num_commits, 1 + 2 * (num_generations as u32));
227 // The first generations are not merges
228 assert_eq!(stats.num_merges, 2 * (num_generations as u32 - 1));
229 assert_eq!(stats.max_generation_number, num_generations as u32);
230
231 // Check generation numbers
232 for generation in 0..num_generations {
233 assert_eq!(
234 index
235 .generation_number(left_commits[generation].id())
236 .unwrap(),
237 (generation as u32) + 1
238 );
239 assert_eq!(
240 index
241 .generation_number(right_commits[generation].id())
242 .unwrap(),
243 (generation as u32) + 1
244 );
245 }
246
247 // The left and right commits of the same generation should not be ancestors of
248 // each other
249 for generation in 0..num_generations {
250 assert!(!is_ancestor(
251 index,
252 left_commits[generation].id(),
253 right_commits[generation].id()
254 ));
255 assert!(!is_ancestor(
256 index,
257 right_commits[generation].id(),
258 left_commits[generation].id()
259 ));
260 }
261
262 // Both sides of earlier generations should be ancestors. Check a few different
263 // earlier generations.
264 for generation in 1..num_generations {
265 for ancestor_side in &[&left_commits, &right_commits] {
266 for descendant_side in &[&left_commits, &right_commits] {
267 assert!(is_ancestor(
268 index,
269 ancestor_side[0].id(),
270 descendant_side[generation].id()
271 ));
272 assert!(is_ancestor(
273 index,
274 ancestor_side[generation - 1].id(),
275 descendant_side[generation].id()
276 ));
277 assert!(is_ancestor(
278 index,
279 ancestor_side[generation / 2].id(),
280 descendant_side[generation].id()
281 ));
282 }
283 }
284 }
285
286 let count_revs = |wanted: &[CommitId], unwanted: &[CommitId], generation| {
287 // Constructs ResolvedExpression directly to bypass tree optimization.
288 let expression = ResolvedExpression::Range {
289 roots: ResolvedExpression::Commits(unwanted.to_vec()).into(),
290 heads: ResolvedExpression::Commits(wanted.to_vec()).into(),
291 generation,
292 parents_range: PARENTS_RANGE_FULL,
293 };
294 let revset = index.evaluate_revset(&expression, repo.store()).unwrap();
295 // Don't switch to more efficient .count() implementation. Here we're
296 // testing the iterator behavior.
297 revset.stream().count().block_on()
298 };
299
300 // RevWalk deduplicates chains by entry.
301 assert_eq!(
302 count_revs(
303 &[left_commits[num_generations - 1].id().clone()],
304 &[],
305 GENERATION_RANGE_FULL,
306 ),
307 2 * num_generations
308 );
309 assert_eq!(
310 count_revs(
311 &[right_commits[num_generations - 1].id().clone()],
312 &[],
313 GENERATION_RANGE_FULL,
314 ),
315 2 * num_generations
316 );
317 assert_eq!(
318 count_revs(
319 &[left_commits[num_generations - 1].id().clone()],
320 &[left_commits[num_generations - 2].id().clone()],
321 GENERATION_RANGE_FULL,
322 ),
323 2
324 );
325 assert_eq!(
326 count_revs(
327 &[right_commits[num_generations - 1].id().clone()],
328 &[right_commits[num_generations - 2].id().clone()],
329 GENERATION_RANGE_FULL,
330 ),
331 2
332 );
333
334 // RevWalkGenerationRange deduplicates chains by (entry, generation), which may
335 // be more expensive than RevWalk, but should still finish in reasonable time.
336 assert_eq!(
337 count_revs(
338 &[left_commits[num_generations - 1].id().clone()],
339 &[],
340 0..(num_generations + 1) as u64,
341 ),
342 2 * num_generations
343 );
344 assert_eq!(
345 count_revs(
346 &[right_commits[num_generations - 1].id().clone()],
347 &[],
348 0..(num_generations + 1) as u64,
349 ),
350 2 * num_generations
351 );
352 assert_eq!(
353 count_revs(
354 &[left_commits[num_generations - 1].id().clone()],
355 &[left_commits[num_generations - 2].id().clone()],
356 0..(num_generations + 1) as u64,
357 ),
358 2
359 );
360 assert_eq!(
361 count_revs(
362 &[right_commits[num_generations - 1].id().clone()],
363 &[right_commits[num_generations - 2].id().clone()],
364 0..(num_generations + 1) as u64,
365 ),
366 2
367 );
368 Ok(())
369 }
370
371 #[test]
372 fn test_index_commits_previous_operations() -> TestResult {
373 // Test that commits visible only in previous operations are indexed.
374 let settings = testutils::user_settings();
375 let test_repo = TestRepo::init();
376 let test_env = &test_repo.env;
377 let repo = &test_repo.repo;
378
379 // Remove commit B and C in one operation and make sure they're still
380 // visible in the index after that operation.
381 // o C
382 // o B
383 // o A
384 // | o working copy
385 // |/
386 // o root
387
388 let mut tx = repo.start_transaction();
389 let commit_a = write_random_commit(tx.repo_mut());
390 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
391 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
392 let repo = tx.commit("test").block_on()?;
393
394 let mut tx = repo.start_transaction();
395 tx.repo_mut().remove_head(commit_c.id());
396 let repo = tx.commit("test").block_on()?;
397
398 // Delete index from disk
399 let default_index_store: &DefaultIndexStore = repo.index_store().downcast_ref().unwrap();
400 default_index_store.reinit()?;
401
402 let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
403 let index = as_readonly_index(&repo);
404 // There should be the root commit, plus 3 more
405 assert_eq!(index.num_commits(), 1 + 3);
406
407 let stats = index.stats();
408 assert_eq!(stats.num_commits, 1 + 3);
409 assert_eq!(stats.num_merges, 0);
410 assert_eq!(stats.max_generation_number, 3);
411
412 assert_eq!(index.generation_number(commit_a.id()).unwrap(), 1);
413 assert_eq!(index.generation_number(commit_b.id()).unwrap(), 2);
414 assert_eq!(index.generation_number(commit_c.id()).unwrap(), 3);
415 Ok(())
416 }
417
418 #[test]
419 fn test_index_commits_hidden_but_referenced() -> TestResult {
420 // Test that hidden-but-referenced commits are indexed.
421 let settings = testutils::user_settings();
422 let test_repo = TestRepo::init();
423 let test_env = &test_repo.env;
424 let repo = &test_repo.repo;
425
426 // Remote bookmarks are usually visible at a certain point in operation
427 // history, but that's not guaranteed if old operations have been discarded.
428 // This can also happen if imported remote bookmarks get immediately
429 // abandoned because the other bookmark has moved.
430 let mut tx = repo.start_transaction();
431 let commit_a = write_random_commit(tx.repo_mut());
432 let commit_b = write_random_commit(tx.repo_mut());
433 let commit_c = write_random_commit(tx.repo_mut());
434 tx.repo_mut().remove_head(commit_a.id());
435 tx.repo_mut().remove_head(commit_b.id());
436 tx.repo_mut().remove_head(commit_c.id());
437 tx.repo_mut().set_remote_bookmark(
438 remote_symbol("bookmark", "origin"),
439 RemoteRef {
440 target: RefTarget::from_legacy_form(
441 [commit_a.id().clone()],
442 [commit_b.id().clone(), commit_c.id().clone()],
443 ),
444 state: jj_lib::op_store::RemoteRefState::New,
445 },
446 );
447
448 let repo = tx.commit("test").block_on()?;
449 // All commits should be indexed
450 assert!(index_has_id(repo.index(), commit_a.id()));
451 assert!(index_has_id(repo.index(), commit_b.id()));
452 assert!(index_has_id(repo.index(), commit_c.id()));
453
454 // Delete index from disk
455 let default_index_store: &DefaultIndexStore = repo.index_store().downcast_ref().unwrap();
456 default_index_store.reinit()?;
457
458 let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
459 // All commits should be reindexed
460 assert!(index_has_id(repo.index(), commit_a.id()));
461 assert!(index_has_id(repo.index(), commit_b.id()));
462 assert!(index_has_id(repo.index(), commit_c.id()));
463 Ok(())
464 }
465
466 #[test]
467 fn test_index_commits_incremental() -> TestResult {
468 let settings = testutils::user_settings();
469 let test_repo = TestRepo::init();
470 let test_env = &test_repo.env;
471 let repo = &test_repo.repo;
472
473 // Create A in one operation, then B and C in another. Check that the index is
474 // valid after.
475 // o C
476 // o B
477 // o A
478 // | o working copy
479 // |/
480 // o root
481
482 let root_commit = repo.store().root_commit();
483 let mut tx = repo.start_transaction();
484 let commit_a = write_random_commit_with_parents(tx.repo_mut(), &[]);
485 let repo = tx.commit("test").block_on()?;
486
487 let index = as_readonly_index(&repo);
488 // There should be the root commit, plus 1 more
489 assert_eq!(index.num_commits(), 1 + 1);
490
491 let mut tx = repo.start_transaction();
492 let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);
493 let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);
494 tx.commit("test").block_on()?;
495
496 let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
497 let index = as_readonly_index(&repo);
498 // There should be the root commit, plus 3 more
499 assert_eq!(index.num_commits(), 1 + 3);
500
501 let stats = index.stats();
502 assert_eq!(stats.num_commits, 1 + 3);
503 assert_eq!(stats.num_merges, 0);
504 assert_eq!(stats.max_generation_number, 3);
505 assert_eq!(stats.commit_levels.len(), 1);
506 assert_eq!(stats.commit_levels[0].num_commits, 4);
507
508 assert_eq!(index.generation_number(root_commit.id()).unwrap(), 0);
509 assert_eq!(index.generation_number(commit_a.id()).unwrap(), 1);
510 assert_eq!(index.generation_number(commit_b.id()).unwrap(), 2);
511 assert_eq!(index.generation_number(commit_c.id()).unwrap(), 3);
512 Ok(())
513 }
514
515 #[test]
516 fn test_index_commits_incremental_empty_transaction() -> TestResult {
517 let settings = testutils::user_settings();
518 let test_repo = TestRepo::init();
519 let test_env = &test_repo.env;
520 let repo = &test_repo.repo;
521
522 // Create A in one operation, then just an empty transaction. Check that the
523 // index is valid after.
524 // o A
525 // | o working copy
526 // |/
527 // o root
528
529 let root_commit = repo.store().root_commit();
530 let mut tx = repo.start_transaction();
531 let commit_a = write_random_commit_with_parents(tx.repo_mut(), &[]);
532 let repo = tx.commit("test").block_on()?;
533
534 let index = as_readonly_index(&repo);
535 // There should be the root commit, plus 1 more
536 assert_eq!(index.num_commits(), 1 + 1);
537
538 repo.start_transaction().commit("test").block_on()?;
539
540 let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
541 let index = as_readonly_index(&repo);
542 // There should be the root commit, plus 1 more
543 assert_eq!(index.num_commits(), 1 + 1);
544
545 let stats = index.stats();
546 assert_eq!(stats.num_commits, 1 + 1);
547 assert_eq!(stats.num_merges, 0);
548 assert_eq!(stats.max_generation_number, 1);
549 assert_eq!(stats.commit_levels.len(), 1);
550 assert_eq!(stats.commit_levels[0].num_commits, 2);
551
552 assert_eq!(index.generation_number(root_commit.id()).unwrap(), 0);
553 assert_eq!(index.generation_number(commit_a.id()).unwrap(), 1);
554 Ok(())
555 }
556
557 #[test]
558 fn test_index_commits_incremental_already_indexed() -> TestResult {
559 // Tests that trying to add a commit that's already been added is a no-op.
560 let test_repo = TestRepo::init();
561 let repo = &test_repo.repo;
562
563 // Create A in one operation, then try to add it again an new transaction.
564 // o A
565 // | o working copy
566 // |/
567 // o root
568
569 let mut tx = repo.start_transaction();
570 let commit_a = write_random_commit_with_parents(tx.repo_mut(), &[]);
571 let repo = tx.commit("test").block_on()?;
572
573 assert!(index_has_id(repo.index(), commit_a.id()));
574 assert_eq!(as_readonly_index(&repo).num_commits(), 1 + 1);
575 let mut tx = repo.start_transaction();
576 let mut_repo = tx.repo_mut();
577 mut_repo.add_head(&commit_a).block_on()?;
578 assert_eq!(as_mutable_index(mut_repo).num_commits(), 1 + 1);
579 Ok(())
580 }
581
582 #[must_use]
583 fn create_n_commits(repo: &Arc<ReadonlyRepo>, num_commits: i32) -> Arc<ReadonlyRepo> {
584 let mut tx = repo.start_transaction();
585 for _ in 0..num_commits {
586 write_random_commit(tx.repo_mut());
587 }
588 tx.commit("test").block_on().unwrap()
589 }
590
591 fn as_readonly_index(repo: &Arc<ReadonlyRepo>) -> &DefaultReadonlyIndex {
592 repo.readonly_index().downcast_ref().unwrap()
593 }
594
595 fn as_mutable_index(repo: &MutableRepo) -> &DefaultMutableIndex {
596 repo.mutable_index().downcast_ref().unwrap()
597 }
598
599 fn commits_by_level(repo: &Arc<ReadonlyRepo>) -> Vec<u32> {
600 as_readonly_index(repo)
601 .stats()
602 .commit_levels
603 .iter()
604 .map(|level| level.num_commits)
605 .collect()
606 }
607
608 #[test]
609 fn test_index_commits_incremental_squashed() -> TestResult {
610 let test_repo = TestRepo::init();
611 let repo = &test_repo.repo;
612 let repo = create_n_commits(repo, 1);
613 assert_eq!(commits_by_level(&repo), vec![2]);
614 let repo = create_n_commits(&repo, 1);
615 assert_eq!(commits_by_level(&repo), vec![3]);
616
617 let test_repo = TestRepo::init();
618 let repo = &test_repo.repo;
619 let repo = create_n_commits(repo, 2);
620 assert_eq!(commits_by_level(&repo), vec![3]);
621
622 let test_repo = TestRepo::init();
623 let repo = &test_repo.repo;
624 let repo = create_n_commits(repo, 100);
625 assert_eq!(commits_by_level(&repo), vec![101]);
626
627 let test_repo = TestRepo::init();
628 let repo = &test_repo.repo;
629 let repo = create_n_commits(repo, 1);
630 let repo = create_n_commits(&repo, 2);
631 let repo = create_n_commits(&repo, 4);
632 let repo = create_n_commits(&repo, 8);
633 let repo = create_n_commits(&repo, 16);
634 let repo = create_n_commits(&repo, 32);
635 assert_eq!(commits_by_level(&repo), vec![64]);
636
637 let test_repo = TestRepo::init();
638 let repo = &test_repo.repo;
639 let repo = create_n_commits(repo, 32);
640 let repo = create_n_commits(&repo, 16);
641 let repo = create_n_commits(&repo, 8);
642 let repo = create_n_commits(&repo, 4);
643 let repo = create_n_commits(&repo, 2);
644 assert_eq!(commits_by_level(&repo), vec![57, 6]);
645
646 let test_repo = TestRepo::init();
647 let repo = &test_repo.repo;
648 let repo = create_n_commits(repo, 30);
649 let repo = create_n_commits(&repo, 15);
650 let repo = create_n_commits(&repo, 7);
651 let repo = create_n_commits(&repo, 3);
652 let repo = create_n_commits(&repo, 1);
653 assert_eq!(commits_by_level(&repo), vec![31, 15, 7, 3, 1]);
654
655 let test_repo = TestRepo::init();
656 let repo = &test_repo.repo;
657 let repo = create_n_commits(repo, 10);
658 let repo = create_n_commits(&repo, 10);
659 let repo = create_n_commits(&repo, 10);
660 let repo = create_n_commits(&repo, 10);
661 let repo = create_n_commits(&repo, 10);
662 let repo = create_n_commits(&repo, 10);
663 let repo = create_n_commits(&repo, 10);
664 let repo = create_n_commits(&repo, 10);
665 let repo = create_n_commits(&repo, 10);
666 assert_eq!(commits_by_level(&repo), vec![71, 20]);
667 Ok(())
668 }
669
670 #[test]
671 fn test_reindex_no_segments_dir() -> TestResult {
672 let settings = testutils::user_settings();
673 let test_repo = TestRepo::init();
674 let test_env = &test_repo.env;
675 let repo = &test_repo.repo;
676
677 let mut tx = repo.start_transaction();
678 let commit_a = write_random_commit(tx.repo_mut());
679 let repo = tx.commit("test").block_on()?;
680 assert!(index_has_id(repo.index(), commit_a.id()));
681
682 // jj <= 0.14 doesn't have "segments" directory
683 let segments_dir = test_repo.repo_path().join("index").join("segments");
684 assert!(segments_dir.is_dir());
685 fs::remove_dir_all(&segments_dir)?;
686
687 let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
688 assert!(index_has_id(repo.index(), commit_a.id()));
689 Ok(())
690 }
691
692 #[test]
693 fn test_reindex_corrupt_segment_files() -> TestResult {
694 let settings = testutils::user_settings();
695 let test_repo = TestRepo::init();
696 let test_env = &test_repo.env;
697 let repo = &test_repo.repo;
698
699 let mut tx = repo.start_transaction();
700 let commit_a = write_random_commit(tx.repo_mut());
701 let repo = tx.commit("test").block_on()?;
702 assert!(index_has_id(repo.index(), commit_a.id()));
703
704 // Corrupt the index files
705 let segments_dir = test_repo.repo_path().join("index").join("segments");
706 for entry in segments_dir.read_dir()? {
707 let entry = entry?;
708 // u32: file format version
709 // u32: parent segment file name length (0 means root)
710 // u32: number of local commit entries
711 // u32: number of local change ids
712 // u32: number of overflow parent entries
713 // u32: number of overflow change id positions
714 fs::write(entry.path(), b"\0".repeat(24))?;
715 }
716
717 let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
718 assert!(index_has_id(repo.index(), commit_a.id()));
719 Ok(())
720 }
721
722 #[test]
723 fn test_reindex_from_merged_operation() -> TestResult {
724 let test_repo = TestRepo::init();
725 let repo = &test_repo.repo;
726
727 // The following operation log:
728 // x (add head, index will be missing)
729 // x (add head, index will be missing)
730 // |\
731 // o o (remove head)
732 // o o (add head)
733 // |/
734 // o
735 let mut txs = Vec::new();
736 for _ in 0..2 {
737 let mut tx = repo.start_transaction();
738 let commit = write_random_commit(tx.repo_mut());
739 let repo = tx.commit("test").block_on()?;
740 let mut tx = repo.start_transaction();
741 tx.repo_mut().remove_head(commit.id());
742 txs.push(tx);
743 }
744 let repo = commit_transactions(txs);
745 let mut op_ids_to_delete = Vec::new();
746 op_ids_to_delete.push(repo.op_id());
747 let mut tx = repo.start_transaction();
748 write_random_commit(tx.repo_mut());
749 let repo = tx.commit("test").block_on()?;
750 op_ids_to_delete.push(repo.op_id());
751 let operation_to_reload = repo.operation();
752
753 // Sanity check before corrupting the index store
754 let index = as_readonly_index(&repo);
755 assert_eq!(index.num_commits(), 4);
756
757 let op_links_dir = test_repo.repo_path().join("index").join("op_links");
758 for &op_id in &op_ids_to_delete {
759 fs::remove_file(op_links_dir.join(op_id.hex()))?;
760 }
761
762 // When re-indexing, one of the merge parent operations will be selected as
763 // the parent index segment. The commits in the other side should still be
764 // reachable.
765 let repo = repo.reload_at(operation_to_reload).block_on()?;
766 let index = as_readonly_index(&repo);
767 assert_eq!(index.num_commits(), 4);
768 Ok(())
769 }
770
771 #[test]
772 fn test_reindex_missing_commit() -> TestResult {
773 let settings = testutils::user_settings();
774 let test_repo = TestRepo::init();
775 let test_env = &test_repo.env;
776 let repo = &test_repo.repo;
777
778 let mut tx = repo.start_transaction();
779 let missing_commit = write_random_commit(tx.repo_mut());
780 let repo = tx.commit("test").block_on()?;
781 let bad_op_id = repo.op_id();
782
783 let mut tx = repo.start_transaction();
784 tx.repo_mut().remove_head(missing_commit.id());
785 let repo = tx.commit("test").block_on()?;
786
787 // Remove historical head commit to simulate bad GC.
788 let test_backend: &TestBackend = repo.store().backend_impl().unwrap();
789 test_backend.remove_commit_unchecked(missing_commit.id());
790 let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path()); // discard cache
791 assert!(repo.store().get_commit(missing_commit.id()).is_err());
792
793 // Reindexing error should include the operation id where the commit
794 // couldn't be found.
795 let default_index_store: &DefaultIndexStore = repo.index_store().downcast_ref().unwrap();
796 default_index_store.reinit()?;
797 let err = default_index_store
798 .build_index_at_operation(repo.operation(), repo.store())
799 .block_on()
800 .unwrap_err();
801 assert_matches!(err, DefaultIndexStoreError::IndexCommits { op_id, .. } if op_id == *bad_op_id);
802 Ok(())
803 }
804
805 /// Test that .jj/repo/index/type is created when the repo is created.
806 #[test]
807 fn test_index_store_type() -> TestResult {
808 let test_repo = TestRepo::init();
809 let repo = &test_repo.repo;
810
811 assert_eq!(as_readonly_index(repo).num_commits(), 1);
812 let index_store_type_path = test_repo.repo_path().join("index").join("type");
813 assert_eq!(std::fs::read_to_string(index_store_type_path)?, "default");
814 Ok(())
815 }
816
817 #[test]
818 fn test_changed_path_segments() -> TestResult {
819 let test_repo = TestRepo::init();
820 let repo = &test_repo.repo;
821 let root_commit_id = repo.store().root_commit_id();
822
823 // Changed-path index should be disabled by default
824 let segments_dir = test_repo.repo_path().join("index").join("changed_paths");
825 let count_segment_files = || {
826 let entries = segments_dir.read_dir().unwrap();
827 entries.process_results(|entries| entries.count()).unwrap()
828 };
829 assert_eq!(count_segment_files(), 0);
830 let stats = as_readonly_index(repo).stats();
831 assert_eq!(stats.changed_path_commits_range, None);
832 assert_eq!(stats.changed_path_levels.len(), 0);
833
834 let repo = enable_changed_path_index(repo);
835 let stats = as_readonly_index(&repo).stats();
836 assert_eq!(stats.changed_path_commits_range, Some(1..1));
837 assert_eq!(stats.changed_path_levels.len(), 0);
838
839 let tree1 = create_tree(&repo, &[(repo_path("a"), "")]);
840 let tree2 = create_tree(&repo, &[(repo_path("a"), ""), (repo_path("b"), "")]);
841
842 // Add new commit with changed-path index enabled
843 let mut tx = repo.start_transaction();
844 let commit1 = tx
845 .repo_mut()
846 .new_commit(vec![root_commit_id.clone()], tree1)
847 .write_unwrap();
848 let repo = tx.commit("test").block_on()?;
849 let stats = as_readonly_index(&repo).stats();
850 assert_eq!(count_segment_files(), 1);
851 assert_eq!(stats.changed_path_commits_range, Some(1..2));
852 assert_eq!(stats.changed_path_levels.len(), 1);
853 assert_eq!(stats.changed_path_levels[0].num_commits, 1);
854 assert_eq!(stats.changed_path_levels[0].num_changed_paths, 1);
855 assert_eq!(stats.changed_path_levels[0].num_paths, 1);
856 assert_eq!(collect_changed_paths(&repo, root_commit_id), None);
857 assert_eq!(
858 collect_changed_paths(&repo, commit1.id()),
859 Some(vec![repo_path_buf("a")])
860 );
861
862 // Add one more commit, segment files should be squashed
863 let mut tx = repo.start_transaction();
864 let commit2 = tx
865 .repo_mut()
866 .new_commit(vec![root_commit_id.clone()], tree2)
867 .write_unwrap();
868 let repo = tx.commit("test").block_on()?;
869 let stats = as_readonly_index(&repo).stats();
870 assert_eq!(count_segment_files(), 2);
871 assert_eq!(stats.changed_path_commits_range, Some(1..3));
872 assert_eq!(stats.changed_path_levels.len(), 1);
873 assert_eq!(stats.changed_path_levels[0].num_commits, 2);
874 assert_eq!(stats.changed_path_levels[0].num_changed_paths, 3);
875 assert_eq!(stats.changed_path_levels[0].num_paths, 2);
876 assert_eq!(collect_changed_paths(&repo, root_commit_id), None);
877 assert_eq!(
878 collect_changed_paths(&repo, commit1.id()),
879 Some(vec![repo_path_buf("a")])
880 );
881 assert_eq!(
882 collect_changed_paths(&repo, commit2.id()),
883 Some(vec![repo_path_buf("a"), repo_path_buf("b")])
884 );
885 Ok(())
886 }
887
888 #[test]
889 fn test_build_changed_path_segments() -> TestResult {
890 let test_repo = TestRepo::init();
891 let repo = test_repo.repo;
892 let root_commit_id = repo.store().root_commit_id();
893 let default_index_store: &DefaultIndexStore = repo.index_store().downcast_ref().unwrap();
894
895 let mut tx = repo.start_transaction();
896 for i in 1..10 {
897 let tree = create_tree(&repo, &[(repo_path(&i.to_string()), "")]);
898 tx.repo_mut()
899 .new_commit(vec![root_commit_id.clone()], tree)
900 .write_unwrap();
901 }
902 let repo = tx.commit("test").block_on()?;
903
904 // Index the last 4 commits
905 default_index_store
906 .build_changed_path_index_at_operation(repo.op_id(), repo.store(), 4, |_| ())
907 .block_on()?;
908 let repo = repo.reload_at(repo.operation()).block_on()?;
909 let stats = as_readonly_index(&repo).stats();
910 assert_eq!(stats.changed_path_commits_range, Some(6..10));
911 assert_eq!(stats.changed_path_levels.len(), 1);
912 assert_eq!(stats.changed_path_levels[0].num_commits, 4);
913 assert_eq!(stats.changed_path_levels[0].num_changed_paths, 4);
914 assert_eq!(stats.changed_path_levels[0].num_paths, 4);
915
916 // Index remainders
917 default_index_store
918 .build_changed_path_index_at_operation(repo.op_id(), repo.store(), u32::MAX, |_| ())
919 .block_on()?;
920 let repo = repo.reload_at(repo.operation()).block_on()?;
921 let stats = as_readonly_index(&repo).stats();
922 assert_eq!(stats.changed_path_commits_range, Some(0..10));
923 assert_eq!(stats.changed_path_levels.len(), 2);
924 assert_eq!(stats.changed_path_levels[0].num_commits, 6);
925 assert_eq!(stats.changed_path_levels[0].num_changed_paths, 5);
926 assert_eq!(stats.changed_path_levels[0].num_paths, 5);
927 assert_eq!(stats.changed_path_levels[1].num_commits, 4);
928 assert_eq!(stats.changed_path_levels[1].num_changed_paths, 4);
929 assert_eq!(stats.changed_path_levels[1].num_paths, 4);
930 Ok(())
931 }
932
933 #[test]
934 fn test_build_changed_path_segments_partially_enabled() -> TestResult {
935 let test_repo = TestRepo::init();
936 let repo = test_repo.repo;
937 let root_commit_id = repo.store().root_commit_id();
938 let default_index_store: &DefaultIndexStore = repo.index_store().downcast_ref().unwrap();
939
940 // Partially enable index by merging two operations
941 let tx1 = {
942 let mut tx = repo.start_transaction();
943 for i in 1..5 {
944 let tree = create_tree(&repo, &[(repo_path(&i.to_string()), "")]);
945 tx.repo_mut()
946 .new_commit(vec![root_commit_id.clone()], tree)
947 .write_unwrap();
948 }
949 let repo = tx.commit("test").block_on()?;
950 let repo = enable_changed_path_index(&repo);
951 let mut tx = repo.start_transaction();
952 let tree = create_tree(&repo, &[(repo_path("5"), "")]);
953 tx.repo_mut()
954 .new_commit(vec![root_commit_id.clone()], tree)
955 .write_unwrap();
956 tx
957 };
958 let mut tx2 = repo.start_transaction();
959 for i in 6..10 {
960 let tree = create_tree(&repo, &[(repo_path(&i.to_string()), "")]);
961 tx2.repo_mut()
962 .new_commit(vec![root_commit_id.clone()], tree)
963 .write_unwrap();
964 }
965 let repo = commit_transactions(vec![tx1, tx2]);
966 let stats = as_readonly_index(&repo).stats();
967 assert_eq!(stats.num_commits, 10);
968 assert_eq!(stats.changed_path_commits_range, Some(5..6));
969 assert_eq!(stats.changed_path_levels.len(), 1);
970 assert_eq!(stats.changed_path_levels[0].num_commits, 1);
971 assert_eq!(stats.changed_path_levels[0].num_changed_paths, 1);
972 assert_eq!(stats.changed_path_levels[0].num_paths, 1);
973
974 // Index later commits from the mid point
975 default_index_store
976 .build_changed_path_index_at_operation(repo.op_id(), repo.store(), 2, |_| ())
977 .block_on()?;
978 let repo = repo.reload_at(repo.operation()).block_on()?;
979 let stats = as_readonly_index(&repo).stats();
980 assert_eq!(stats.changed_path_commits_range, Some(5..8));
981 assert_eq!(stats.changed_path_levels.len(), 1);
982 assert_eq!(stats.changed_path_levels[0].num_commits, 3);
983 assert_eq!(stats.changed_path_levels[0].num_changed_paths, 3);
984 assert_eq!(stats.changed_path_levels[0].num_paths, 3);
985
986 // Index later and earlier commits from the mid point
987 default_index_store
988 .build_changed_path_index_at_operation(repo.op_id(), repo.store(), 3, |_| ())
989 .block_on()?;
990 let repo = repo.reload_at(repo.operation()).block_on()?;
991 let stats = as_readonly_index(&repo).stats();
992 assert_eq!(stats.changed_path_commits_range, Some(4..10));
993 assert_eq!(stats.changed_path_levels.len(), 1);
994 assert_eq!(stats.changed_path_levels[0].num_commits, 6);
995 assert_eq!(stats.changed_path_levels[0].num_changed_paths, 6);
996 assert_eq!(stats.changed_path_levels[0].num_paths, 6);
997 Ok(())
998 }
999
1000 #[test]
1001 fn test_merge_changed_path_segments_both_enabled() -> TestResult {
1002 let test_repo = TestRepo::init();
1003 let repo = enable_changed_path_index(&test_repo.repo);
1004 let root_commit_id = repo.store().root_commit_id();
1005
1006 let tree1 = create_tree(&repo, &[(repo_path("a"), "")]);
1007 let tree2 = create_tree(&repo, &[(repo_path("a"), ""), (repo_path("b"), "")]);
1008 let tree3 = create_tree(&repo, &[(repo_path("c"), ""), (repo_path("d"), "")]);
1009
1010 // Add index segment that will be squashed
1011 let mut tx = repo.start_transaction();
1012 tx.repo_mut()
1013 .new_commit(vec![root_commit_id.clone()], tree1)
1014 .write_unwrap();
1015 let repo = tx.commit("test").block_on()?;
1016
1017 // Merge concurrent index segments without the common base segment
1018 let mut tx1 = repo.start_transaction();
1019 tx1.repo_mut()
1020 .new_commit(vec![root_commit_id.clone()], tree2)
1021 .write_unwrap();
1022 let mut tx2 = repo.start_transaction();
1023 tx2.repo_mut()
1024 .new_commit(vec![root_commit_id.clone()], tree3)
1025 .write_unwrap();
1026 let repo = commit_transactions(vec![tx1, tx2]);
1027 let stats = as_readonly_index(&repo).stats();
1028 assert_eq!(stats.num_commits, 4);
1029 assert_eq!(stats.changed_path_commits_range, Some(1..4));
1030 assert_eq!(stats.changed_path_levels.len(), 1);
1031 assert_eq!(stats.changed_path_levels[0].num_commits, 3);
1032 assert_eq!(stats.changed_path_levels[0].num_changed_paths, 5);
1033 assert_eq!(stats.changed_path_levels[0].num_paths, 4);
1034 Ok(())
1035 }
1036
1037 #[test]
1038 fn test_merge_changed_path_segments_enabled_and_disabled() -> TestResult {
1039 let test_repo = TestRepo::init();
1040 let repo = test_repo.repo;
1041 let root_commit_id = repo.store().root_commit_id();
1042
1043 let tree1 = create_tree(&repo, &[(repo_path("a"), "")]);
1044 let tree2 = create_tree(&repo, &[(repo_path("b"), "")]);
1045 let tree3 = create_tree(&repo, &[(repo_path("c"), "")]);
1046
1047 // Enable changed-path index only in tx1
1048 let tx1 = {
1049 let mut tx = repo.start_transaction();
1050 tx.repo_mut()
1051 .new_commit(vec![root_commit_id.clone()], tree1)
1052 .write_unwrap();
1053 let repo = tx.commit("test").block_on()?;
1054 let repo = enable_changed_path_index(&repo);
1055 let mut tx = repo.start_transaction();
1056 tx.repo_mut()
1057 .new_commit(vec![root_commit_id.clone()], tree2)
1058 .write_unwrap();
1059 tx
1060 };
1061 let mut tx2 = repo.start_transaction();
1062 tx2.repo_mut()
1063 .new_commit(vec![root_commit_id.clone()], tree3)
1064 .write_unwrap();
1065 let repo = commit_transactions(vec![tx1, tx2]);
1066 let stats = as_readonly_index(&repo).stats();
1067 assert_eq!(stats.num_commits, 4);
1068 assert_eq!(stats.changed_path_commits_range, Some(2..3));
1069 assert_eq!(stats.changed_path_levels.len(), 1);
1070 assert_eq!(stats.changed_path_levels[0].num_commits, 1);
1071 assert_eq!(stats.changed_path_levels[0].num_changed_paths, 1);
1072 assert_eq!(stats.changed_path_levels[0].num_paths, 1);
1073
1074 // Changed paths in new commit can no longer be indexed
1075 let mut tx = repo.start_transaction();
1076 write_random_commit(tx.repo_mut());
1077 let repo = tx.commit("test").block_on()?;
1078 let stats = as_readonly_index(&repo).stats();
1079 assert_eq!(stats.num_commits, 5);
1080 assert_eq!(stats.changed_path_commits_range, Some(2..3));
1081 Ok(())
1082 }
1083
1084 #[test_case(false; "without changed-path index")]
1085 #[test_case(true; "with changed-path index")]
1086 fn test_commit_is_empty(indexed: bool) -> TestResult {
1087 let test_repo = TestRepo::init();
1088 let repo = if indexed {
1089 enable_changed_path_index(&test_repo.repo)
1090 } else {
1091 test_repo.repo
1092 };
1093 let root_commit_id = repo.store().root_commit_id();
1094 let root_tree = repo.store().empty_merged_tree();
1095
1096 let tree2 = create_tree(&repo, &[(repo_path("a"), "")]);
1097 let tree3 = create_tree(&repo, &[(repo_path("b"), "")]);
1098 let tree4 = create_tree(&repo, &[(repo_path("a"), ""), (repo_path("b"), "")]);
1099
1100 let mut tx = repo.start_transaction();
1101 let commit1 = tx
1102 .repo_mut()
1103 .new_commit(vec![root_commit_id.clone()], root_tree.clone())
1104 .write_unwrap();
1105 let commit2 = tx
1106 .repo_mut()
1107 .new_commit(vec![root_commit_id.clone()], tree2)
1108 .write_unwrap();
1109 let commit3 = tx
1110 .repo_mut()
1111 .new_commit(vec![root_commit_id.clone()], tree3)
1112 .write_unwrap();
1113 let commit4 = tx
1114 .repo_mut()
1115 .new_commit(
1116 vec![
1117 commit1.id().clone(),
1118 commit2.id().clone(),
1119 commit3.id().clone(),
1120 ],
1121 tree4.clone(),
1122 )
1123 .write_unwrap();
1124 let repo = tx.commit("test").block_on()?;
1125
1126 // Sanity check
1127 let stats = as_readonly_index(&repo).stats();
1128 if indexed {
1129 assert_eq!(stats.changed_path_commits_range, Some(1..5));
1130 } else {
1131 assert_eq!(stats.changed_path_commits_range, None);
1132 }
1133
1134 assert!(commit1.is_empty(repo.as_ref()).block_on()?);
1135 assert!(!commit2.is_empty(repo.as_ref()).block_on()?);
1136 assert!(!commit3.is_empty(repo.as_ref()).block_on()?);
1137 assert!(commit4.is_empty(repo.as_ref()).block_on()?);
1138
1139 assert_tree_eq!(commit1.parent_tree(repo.as_ref()).block_on()?, root_tree);
1140 assert_tree_eq!(commit2.parent_tree(repo.as_ref()).block_on()?, root_tree);
1141 assert_tree_eq!(commit3.parent_tree(repo.as_ref()).block_on()?, root_tree);
1142 assert_tree_eq!(commit4.parent_tree(repo.as_ref()).block_on()?, tree4);
1143 Ok(())
1144 }
1145
1146 #[test]
1147 fn test_change_id_index() {
1148 let test_repo = TestRepo::init();
1149 let repo = &test_repo.repo;
1150
1151 let mut tx = repo.start_transaction();
1152
1153 let root_commit = repo.store().root_commit();
1154 let mut commit_number = 0;
1155 let mut commit_with_change_id = |change_id| {
1156 commit_number += 1;
1157 tx.repo_mut()
1158 .new_commit(vec![root_commit.id().clone()], root_commit.tree())
1159 .set_change_id(ChangeId::from_hex(change_id))
1160 .set_description(format!("commit {commit_number}"))
1161 .write_unwrap()
1162 };
1163 let commit_1 = commit_with_change_id("abbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
1164 let commit_2 = commit_with_change_id("aaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
1165 let commit_3 = commit_with_change_id("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
1166 let commit_4 = commit_with_change_id("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
1167 let commit_5 = commit_with_change_id("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
1168
1169 let index_for_heads = |commits: &[&Commit]| {
1170 tx.repo()
1171 .mutable_index()
1172 .change_id_index(&mut commits.iter().map(|commit| commit.id()))
1173 };
1174 let change_id_index = index_for_heads(&[&commit_1, &commit_2, &commit_3, &commit_4, &commit_5]);
1175 let prefix_len = |commit: &Commit| {
1176 change_id_index
1177 .shortest_unique_prefix_len(commit.change_id())
1178 .block_on()
1179 .unwrap()
1180 };
1181 assert_eq!(prefix_len(&root_commit), 1);
1182 assert_eq!(prefix_len(&commit_1), 2);
1183 assert_eq!(prefix_len(&commit_2), 6);
1184 assert_eq!(prefix_len(&commit_3), 6);
1185 assert_eq!(prefix_len(&commit_4), 1);
1186 assert_eq!(prefix_len(&commit_5), 1);
1187 let resolve_prefix = |prefix: &str| {
1188 change_id_index
1189 .resolve_prefix(&HexPrefix::try_from_hex(prefix).unwrap())
1190 .block_on()
1191 .unwrap()
1192 };
1193 // Ambiguous matches
1194 assert_eq!(resolve_prefix("a"), PrefixResolution::AmbiguousMatch);
1195 assert_eq!(resolve_prefix("aaaaa"), PrefixResolution::AmbiguousMatch);
1196 // Exactly the necessary length
1197 assert_eq!(
1198 resolve_prefix("0"),
1199 PrefixResolution::SingleMatch(ResolvedChangeTargets {
1200 targets: vec![(root_commit.id().clone(), ResolvedChangeState::Visible)],
1201 })
1202 );
1203 assert_eq!(
1204 resolve_prefix("aaaaaa"),
1205 PrefixResolution::SingleMatch(ResolvedChangeTargets {
1206 targets: vec![(commit_3.id().clone(), ResolvedChangeState::Visible)],
1207 })
1208 );
1209 assert_eq!(
1210 resolve_prefix("aaaaab"),
1211 PrefixResolution::SingleMatch(ResolvedChangeTargets {
1212 targets: vec![(commit_2.id().clone(), ResolvedChangeState::Visible)],
1213 })
1214 );
1215 assert_eq!(
1216 resolve_prefix("ab"),
1217 PrefixResolution::SingleMatch(ResolvedChangeTargets {
1218 targets: vec![(commit_1.id().clone(), ResolvedChangeState::Visible)],
1219 })
1220 );
1221 assert_eq!(
1222 resolve_prefix("b"),
1223 PrefixResolution::SingleMatch(ResolvedChangeTargets {
1224 targets: vec![
1225 (commit_5.id().clone(), ResolvedChangeState::Visible),
1226 (commit_4.id().clone(), ResolvedChangeState::Visible),
1227 ],
1228 })
1229 );
1230 // Longer than necessary
1231 assert_eq!(
1232 resolve_prefix("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
1233 PrefixResolution::SingleMatch(ResolvedChangeTargets {
1234 targets: vec![(commit_3.id().clone(), ResolvedChangeState::Visible)],
1235 })
1236 );
1237 // No match
1238 assert_eq!(resolve_prefix("ba"), PrefixResolution::NoMatch);
1239
1240 // Test with an index containing only some of the commits. The shortest
1241 // length doesn't have to be minimized further, but unreachable commits
1242 // should never be included in the resolved set.
1243 let change_id_index = index_for_heads(&[&commit_1, &commit_2, &commit_5]);
1244 let resolve_prefix = |prefix: &str| {
1245 change_id_index
1246 .resolve_prefix(&HexPrefix::try_from_hex(prefix).unwrap())
1247 .block_on()
1248 .unwrap()
1249 };
1250 assert_eq!(
1251 resolve_prefix("0"),
1252 PrefixResolution::SingleMatch(ResolvedChangeTargets {
1253 targets: vec![(root_commit.id().clone(), ResolvedChangeState::Visible)],
1254 })
1255 );
1256 assert_eq!(
1257 resolve_prefix("aaaaab"),
1258 PrefixResolution::SingleMatch(ResolvedChangeTargets {
1259 targets: vec![(commit_2.id().clone(), ResolvedChangeState::Visible)],
1260 })
1261 );
1262 assert_eq!(
1263 resolve_prefix("aaaaaa"),
1264 PrefixResolution::SingleMatch(ResolvedChangeTargets {
1265 targets: vec![(commit_3.id().clone(), ResolvedChangeState::Hidden)],
1266 })
1267 );
1268 assert_eq!(resolve_prefix("a"), PrefixResolution::AmbiguousMatch);
1269 assert_eq!(
1270 resolve_prefix("b"),
1271 PrefixResolution::SingleMatch(ResolvedChangeTargets {
1272 targets: vec![
1273 (commit_5.id().clone(), ResolvedChangeState::Visible),
1274 (commit_4.id().clone(), ResolvedChangeState::Hidden),
1275 ],
1276 })
1277 );
1278 assert_eq!(resolve_prefix("ba"), PrefixResolution::NoMatch);
1279 }