1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
|
// Copyright 2020 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::HashSet;
use std::fs;
use std::sync::Arc;
use assert_matches::assert_matches;
use jj_lib::backend::ChangeId;
use jj_lib::backend::CommitId;
use jj_lib::commit::Commit;
use jj_lib::commit_builder::CommitBuilder;
use jj_lib::default_index::AsCompositeIndex as _;
use jj_lib::default_index::CompositeIndex;
use jj_lib::default_index::DefaultIndexStore;
use jj_lib::default_index::DefaultIndexStoreError;
use jj_lib::default_index::DefaultMutableIndex;
use jj_lib::default_index::DefaultReadonlyIndex;
use jj_lib::index::Index as _;
use jj_lib::object_id::HexPrefix;
use jj_lib::object_id::ObjectId as _;
use jj_lib::object_id::PrefixResolution;
use jj_lib::op_store::RefTarget;
use jj_lib::op_store::RemoteRef;
use jj_lib::ref_name::RefName;
use jj_lib::ref_name::RemoteName;
use jj_lib::ref_name::RemoteRefSymbol;
use jj_lib::repo::MutableRepo;
use jj_lib::repo::ReadonlyRepo;
use jj_lib::repo::Repo as _;
use jj_lib::revset::ResolvedExpression;
use jj_lib::revset::GENERATION_RANGE_FULL;
use maplit::hashset;
use testutils::commit_transactions;
use testutils::create_random_commit;
use testutils::test_backend::TestBackend;
use testutils::write_random_commit;
use testutils::CommitGraphBuilder;
use testutils::TestRepo;
fn child_commit<'repo>(mut_repo: &'repo mut MutableRepo, commit: &Commit) -> CommitBuilder<'repo> {
create_random_commit(mut_repo).set_parents(vec![commit.id().clone()])
}
// Helper just to reduce line wrapping
fn generation_number(index: &CompositeIndex, commit_id: &CommitId) -> u32 {
index.entry_by_id(commit_id).unwrap().generation_number()
}
fn remote_symbol<'a, N, M>(name: &'a N, remote: &'a M) -> RemoteRefSymbol<'a>
where
N: AsRef<RefName> + ?Sized,
M: AsRef<RemoteName> + ?Sized,
{
RemoteRefSymbol {
name: name.as_ref(),
remote: remote.as_ref(),
}
}
#[test]
fn test_index_commits_empty_repo() {
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
let index = as_readonly_composite(repo);
// There should be just the root commit
assert_eq!(index.num_commits(), 1);
// Check the generation numbers of the root and the working copy
assert_eq!(generation_number(index, repo.store().root_commit_id()), 0);
}
#[test]
fn test_index_commits_standard_cases() {
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
// o H
// o | G
// o | F
// |\|
// | o E
// | o D
// | o C
// o | B
// |/
// o A
// | o working copy
// |/
// o root
let root_commit_id = repo.store().root_commit_id();
let mut tx = repo.start_transaction();
let mut graph_builder = CommitGraphBuilder::new(tx.repo_mut());
let commit_a = graph_builder.initial_commit();
let commit_b = graph_builder.commit_with_parents(&[&commit_a]);
let commit_c = graph_builder.commit_with_parents(&[&commit_a]);
let commit_d = graph_builder.commit_with_parents(&[&commit_c]);
let commit_e = graph_builder.commit_with_parents(&[&commit_d]);
let commit_f = graph_builder.commit_with_parents(&[&commit_b, &commit_e]);
let commit_g = graph_builder.commit_with_parents(&[&commit_f]);
let commit_h = graph_builder.commit_with_parents(&[&commit_e]);
let repo = tx.commit("test").unwrap();
let index = as_readonly_composite(&repo);
// There should be the root commit, plus 8 more
assert_eq!(index.num_commits(), 1 + 8);
let stats = index.stats();
assert_eq!(stats.num_commits, 1 + 8);
assert_eq!(stats.num_merges, 1);
assert_eq!(stats.max_generation_number, 6);
assert_eq!(generation_number(index, root_commit_id), 0);
assert_eq!(generation_number(index, commit_a.id()), 1);
assert_eq!(generation_number(index, commit_b.id()), 2);
assert_eq!(generation_number(index, commit_c.id()), 2);
assert_eq!(generation_number(index, commit_d.id()), 3);
assert_eq!(generation_number(index, commit_e.id()), 4);
assert_eq!(generation_number(index, commit_f.id()), 5);
assert_eq!(generation_number(index, commit_g.id()), 6);
assert_eq!(generation_number(index, commit_h.id()), 5);
assert!(index.is_ancestor(root_commit_id, commit_a.id()));
assert!(!index.is_ancestor(commit_a.id(), root_commit_id));
assert!(index.is_ancestor(root_commit_id, commit_b.id()));
assert!(!index.is_ancestor(commit_b.id(), root_commit_id));
assert!(!index.is_ancestor(commit_b.id(), commit_c.id()));
assert!(index.is_ancestor(commit_a.id(), commit_b.id()));
assert!(index.is_ancestor(commit_a.id(), commit_e.id()));
assert!(index.is_ancestor(commit_a.id(), commit_f.id()));
assert!(index.is_ancestor(commit_a.id(), commit_g.id()));
assert!(index.is_ancestor(commit_a.id(), commit_h.id()));
}
#[test]
fn test_index_commits_criss_cross() {
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
let num_generations = 50;
// Create a long chain of criss-crossed merges. If they were traversed without
// keeping track of visited nodes, it would be 2^50 visits, so if this test
// finishes in reasonable time, we know that we don't do a naive traversal.
let mut tx = repo.start_transaction();
let mut graph_builder = CommitGraphBuilder::new(tx.repo_mut());
let mut left_commits = vec![graph_builder.initial_commit()];
let mut right_commits = vec![graph_builder.initial_commit()];
for gen in 1..num_generations {
let new_left =
graph_builder.commit_with_parents(&[&left_commits[gen - 1], &right_commits[gen - 1]]);
let new_right =
graph_builder.commit_with_parents(&[&left_commits[gen - 1], &right_commits[gen - 1]]);
left_commits.push(new_left);
right_commits.push(new_right);
}
let repo = tx.commit("test").unwrap();
let index = as_readonly_composite(&repo);
// There should the root commit, plus 2 for each generation
assert_eq!(index.num_commits(), 1 + 2 * (num_generations as u32));
let stats = index.stats();
assert_eq!(stats.num_commits, 1 + 2 * (num_generations as u32));
// The first generations are not merges
assert_eq!(stats.num_merges, 2 * (num_generations as u32 - 1));
assert_eq!(stats.max_generation_number, num_generations as u32);
// Check generation numbers
for gen in 0..num_generations {
assert_eq!(
generation_number(index, left_commits[gen].id()),
(gen as u32) + 1
);
assert_eq!(
generation_number(index, right_commits[gen].id()),
(gen as u32) + 1
);
}
// The left and right commits of the same generation should not be ancestors of
// each other
for gen in 0..num_generations {
assert!(!index.is_ancestor(left_commits[gen].id(), right_commits[gen].id()));
assert!(!index.is_ancestor(right_commits[gen].id(), left_commits[gen].id()));
}
// Both sides of earlier generations should be ancestors. Check a few different
// earlier generations.
for gen in 1..num_generations {
for ancestor_side in &[&left_commits, &right_commits] {
for descendant_side in &[&left_commits, &right_commits] {
assert!(index.is_ancestor(ancestor_side[0].id(), descendant_side[gen].id()));
assert!(index.is_ancestor(ancestor_side[gen - 1].id(), descendant_side[gen].id()));
assert!(index.is_ancestor(ancestor_side[gen / 2].id(), descendant_side[gen].id()));
}
}
}
let count_revs = |wanted: &[CommitId], unwanted: &[CommitId], generation| {
// Constructs ResolvedExpression directly to bypass tree optimization.
let expression = ResolvedExpression::Range {
roots: ResolvedExpression::Commits(unwanted.to_vec()).into(),
heads: ResolvedExpression::Commits(wanted.to_vec()).into(),
generation,
};
let revset = index.evaluate_revset(&expression, repo.store()).unwrap();
// Don't switch to more efficient .count() implementation. Here we're
// testing the iterator behavior.
revset.iter().count()
};
// RevWalk deduplicates chains by entry.
assert_eq!(
count_revs(
&[left_commits[num_generations - 1].id().clone()],
&[],
GENERATION_RANGE_FULL,
),
2 * num_generations
);
assert_eq!(
count_revs(
&[right_commits[num_generations - 1].id().clone()],
&[],
GENERATION_RANGE_FULL,
),
2 * num_generations
);
assert_eq!(
count_revs(
&[left_commits[num_generations - 1].id().clone()],
&[left_commits[num_generations - 2].id().clone()],
GENERATION_RANGE_FULL,
),
2
);
assert_eq!(
count_revs(
&[right_commits[num_generations - 1].id().clone()],
&[right_commits[num_generations - 2].id().clone()],
GENERATION_RANGE_FULL,
),
2
);
// RevWalkGenerationRange deduplicates chains by (entry, generation), which may
// be more expensive than RevWalk, but should still finish in reasonable time.
assert_eq!(
count_revs(
&[left_commits[num_generations - 1].id().clone()],
&[],
0..(num_generations + 1) as u64,
),
2 * num_generations
);
assert_eq!(
count_revs(
&[right_commits[num_generations - 1].id().clone()],
&[],
0..(num_generations + 1) as u64,
),
2 * num_generations
);
assert_eq!(
count_revs(
&[left_commits[num_generations - 1].id().clone()],
&[left_commits[num_generations - 2].id().clone()],
0..(num_generations + 1) as u64,
),
2
);
assert_eq!(
count_revs(
&[right_commits[num_generations - 1].id().clone()],
&[right_commits[num_generations - 2].id().clone()],
0..(num_generations + 1) as u64,
),
2
);
}
#[test]
fn test_index_commits_previous_operations() {
// Test that commits visible only in previous operations are indexed.
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let test_env = &test_repo.env;
let repo = &test_repo.repo;
// Remove commit B and C in one operation and make sure they're still
// visible in the index after that operation.
// o C
// o B
// o A
// | o working copy
// |/
// o root
let mut tx = repo.start_transaction();
let mut graph_builder = CommitGraphBuilder::new(tx.repo_mut());
let commit_a = graph_builder.initial_commit();
let commit_b = graph_builder.commit_with_parents(&[&commit_a]);
let commit_c = graph_builder.commit_with_parents(&[&commit_b]);
let repo = tx.commit("test").unwrap();
let mut tx = repo.start_transaction();
tx.repo_mut().remove_head(commit_c.id());
let repo = tx.commit("test").unwrap();
// Delete index from disk
let default_index_store: &DefaultIndexStore =
repo.index_store().as_any().downcast_ref().unwrap();
default_index_store.reinit().unwrap();
let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
let index = as_readonly_composite(&repo);
// There should be the root commit, plus 3 more
assert_eq!(index.num_commits(), 1 + 3);
let stats = index.stats();
assert_eq!(stats.num_commits, 1 + 3);
assert_eq!(stats.num_merges, 0);
assert_eq!(stats.max_generation_number, 3);
assert_eq!(generation_number(index, commit_a.id()), 1);
assert_eq!(generation_number(index, commit_b.id()), 2);
assert_eq!(generation_number(index, commit_c.id()), 3);
}
#[test]
fn test_index_commits_hidden_but_referenced() {
// Test that hidden-but-referenced commits are indexed.
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let test_env = &test_repo.env;
let repo = &test_repo.repo;
// Remote bookmarks are usually visible at a certain point in operation
// history, but that's not guaranteed if old operations have been discarded.
// This can also happen if imported remote bookmarks get immediately
// abandoned because the other bookmark has moved.
let mut tx = repo.start_transaction();
let commit_a = write_random_commit(tx.repo_mut());
let commit_b = write_random_commit(tx.repo_mut());
let commit_c = write_random_commit(tx.repo_mut());
tx.repo_mut().remove_head(commit_a.id());
tx.repo_mut().remove_head(commit_b.id());
tx.repo_mut().remove_head(commit_c.id());
tx.repo_mut().set_remote_bookmark(
remote_symbol("bookmark", "origin"),
RemoteRef {
target: RefTarget::from_legacy_form(
[commit_a.id().clone()],
[commit_b.id().clone(), commit_c.id().clone()],
),
state: jj_lib::op_store::RemoteRefState::New,
},
);
let repo = tx.commit("test").unwrap();
// All commits should be indexed
assert!(repo.index().has_id(commit_a.id()));
assert!(repo.index().has_id(commit_b.id()));
assert!(repo.index().has_id(commit_c.id()));
// Delete index from disk
let default_index_store: &DefaultIndexStore =
repo.index_store().as_any().downcast_ref().unwrap();
default_index_store.reinit().unwrap();
let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
// All commits should be reindexed
assert!(repo.index().has_id(commit_a.id()));
assert!(repo.index().has_id(commit_b.id()));
assert!(repo.index().has_id(commit_c.id()));
}
#[test]
fn test_index_commits_incremental() {
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let test_env = &test_repo.env;
let repo = &test_repo.repo;
// Create A in one operation, then B and C in another. Check that the index is
// valid after.
// o C
// o B
// o A
// | o working copy
// |/
// o root
let root_commit = repo.store().root_commit();
let mut tx = repo.start_transaction();
let commit_a = child_commit(tx.repo_mut(), &root_commit).write().unwrap();
let repo = tx.commit("test").unwrap();
let index = as_readonly_composite(&repo);
// There should be the root commit, plus 1 more
assert_eq!(index.num_commits(), 1 + 1);
let mut tx = repo.start_transaction();
let commit_b = child_commit(tx.repo_mut(), &commit_a).write().unwrap();
let commit_c = child_commit(tx.repo_mut(), &commit_b).write().unwrap();
tx.commit("test").unwrap();
let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
let index = as_readonly_composite(&repo);
// There should be the root commit, plus 3 more
assert_eq!(index.num_commits(), 1 + 3);
let stats = index.stats();
assert_eq!(stats.num_commits, 1 + 3);
assert_eq!(stats.num_merges, 0);
assert_eq!(stats.max_generation_number, 3);
assert_eq!(stats.levels.len(), 1);
assert_eq!(stats.levels[0].num_commits, 4);
assert_eq!(generation_number(index, root_commit.id()), 0);
assert_eq!(generation_number(index, commit_a.id()), 1);
assert_eq!(generation_number(index, commit_b.id()), 2);
assert_eq!(generation_number(index, commit_c.id()), 3);
}
#[test]
fn test_index_commits_incremental_empty_transaction() {
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let test_env = &test_repo.env;
let repo = &test_repo.repo;
// Create A in one operation, then just an empty transaction. Check that the
// index is valid after.
// o A
// | o working copy
// |/
// o root
let root_commit = repo.store().root_commit();
let mut tx = repo.start_transaction();
let commit_a = child_commit(tx.repo_mut(), &root_commit).write().unwrap();
let repo = tx.commit("test").unwrap();
let index = as_readonly_composite(&repo);
// There should be the root commit, plus 1 more
assert_eq!(index.num_commits(), 1 + 1);
repo.start_transaction().commit("test").unwrap();
let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
let index = as_readonly_composite(&repo);
// There should be the root commit, plus 1 more
assert_eq!(index.num_commits(), 1 + 1);
let stats = index.stats();
assert_eq!(stats.num_commits, 1 + 1);
assert_eq!(stats.num_merges, 0);
assert_eq!(stats.max_generation_number, 1);
assert_eq!(stats.levels.len(), 1);
assert_eq!(stats.levels[0].num_commits, 2);
assert_eq!(generation_number(index, root_commit.id()), 0);
assert_eq!(generation_number(index, commit_a.id()), 1);
}
#[test]
fn test_index_commits_incremental_already_indexed() {
// Tests that trying to add a commit that's already been added is a no-op.
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
// Create A in one operation, then try to add it again an new transaction.
// o A
// | o working copy
// |/
// o root
let root_commit = repo.store().root_commit();
let mut tx = repo.start_transaction();
let commit_a = child_commit(tx.repo_mut(), &root_commit).write().unwrap();
let repo = tx.commit("test").unwrap();
assert!(repo.index().has_id(commit_a.id()));
assert_eq!(as_readonly_composite(&repo).num_commits(), 1 + 1);
let mut tx = repo.start_transaction();
let mut_repo = tx.repo_mut();
mut_repo.add_head(&commit_a).unwrap();
assert_eq!(as_mutable_composite(mut_repo).num_commits(), 1 + 1);
}
#[must_use]
fn create_n_commits(repo: &Arc<ReadonlyRepo>, num_commits: i32) -> Arc<ReadonlyRepo> {
let mut tx = repo.start_transaction();
for _ in 0..num_commits {
write_random_commit(tx.repo_mut());
}
tx.commit("test").unwrap()
}
fn as_readonly_composite(repo: &Arc<ReadonlyRepo>) -> &CompositeIndex {
repo.readonly_index()
.as_any()
.downcast_ref::<DefaultReadonlyIndex>()
.unwrap()
.as_composite()
}
fn as_mutable_composite(repo: &MutableRepo) -> &CompositeIndex {
repo.mutable_index()
.as_any()
.downcast_ref::<DefaultMutableIndex>()
.unwrap()
.as_composite()
}
fn commits_by_level(repo: &Arc<ReadonlyRepo>) -> Vec<u32> {
as_readonly_composite(repo)
.stats()
.levels
.iter()
.map(|level| level.num_commits)
.collect()
}
#[test]
fn test_index_commits_incremental_squashed() {
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
let repo = create_n_commits(repo, 1);
assert_eq!(commits_by_level(&repo), vec![2]);
let repo = create_n_commits(&repo, 1);
assert_eq!(commits_by_level(&repo), vec![3]);
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
let repo = create_n_commits(repo, 2);
assert_eq!(commits_by_level(&repo), vec![3]);
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
let repo = create_n_commits(repo, 100);
assert_eq!(commits_by_level(&repo), vec![101]);
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
let repo = create_n_commits(repo, 1);
let repo = create_n_commits(&repo, 2);
let repo = create_n_commits(&repo, 4);
let repo = create_n_commits(&repo, 8);
let repo = create_n_commits(&repo, 16);
let repo = create_n_commits(&repo, 32);
assert_eq!(commits_by_level(&repo), vec![64]);
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
let repo = create_n_commits(repo, 32);
let repo = create_n_commits(&repo, 16);
let repo = create_n_commits(&repo, 8);
let repo = create_n_commits(&repo, 4);
let repo = create_n_commits(&repo, 2);
assert_eq!(commits_by_level(&repo), vec![57, 6]);
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
let repo = create_n_commits(repo, 30);
let repo = create_n_commits(&repo, 15);
let repo = create_n_commits(&repo, 7);
let repo = create_n_commits(&repo, 3);
let repo = create_n_commits(&repo, 1);
assert_eq!(commits_by_level(&repo), vec![31, 15, 7, 3, 1]);
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
let repo = create_n_commits(repo, 10);
let repo = create_n_commits(&repo, 10);
let repo = create_n_commits(&repo, 10);
let repo = create_n_commits(&repo, 10);
let repo = create_n_commits(&repo, 10);
let repo = create_n_commits(&repo, 10);
let repo = create_n_commits(&repo, 10);
let repo = create_n_commits(&repo, 10);
let repo = create_n_commits(&repo, 10);
assert_eq!(commits_by_level(&repo), vec![71, 20]);
}
#[test]
fn test_reindex_no_segments_dir() {
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let test_env = &test_repo.env;
let repo = &test_repo.repo;
let mut tx = repo.start_transaction();
let commit_a = write_random_commit(tx.repo_mut());
let repo = tx.commit("test").unwrap();
assert!(repo.index().has_id(commit_a.id()));
// jj <= 0.14 doesn't have "segments" directory
let segments_dir = test_repo.repo_path().join("index").join("segments");
assert!(segments_dir.is_dir());
fs::remove_dir_all(&segments_dir).unwrap();
let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
assert!(repo.index().has_id(commit_a.id()));
}
#[test]
fn test_reindex_corrupt_segment_files() {
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let test_env = &test_repo.env;
let repo = &test_repo.repo;
let mut tx = repo.start_transaction();
let commit_a = write_random_commit(tx.repo_mut());
let repo = tx.commit("test").unwrap();
assert!(repo.index().has_id(commit_a.id()));
// Corrupt the index files
let segments_dir = test_repo.repo_path().join("index").join("segments");
for entry in segments_dir.read_dir().unwrap() {
let entry = entry.unwrap();
// u32: file format version
// u32: parent segment file name length (0 means root)
// u32: number of local commit entries
// u32: number of local change ids
// u32: number of overflow parent entries
// u32: number of overflow change id positions
fs::write(entry.path(), b"\0".repeat(24)).unwrap();
}
let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
assert!(repo.index().has_id(commit_a.id()));
}
#[test]
fn test_reindex_from_merged_operation() {
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
// The following operation log:
// x (add head, index will be missing)
// x (add head, index will be missing)
// |\
// o o (remove head)
// o o (add head)
// |/
// o
let mut txs = Vec::new();
for _ in 0..2 {
let mut tx = repo.start_transaction();
let commit = write_random_commit(tx.repo_mut());
let repo = tx.commit("test").unwrap();
let mut tx = repo.start_transaction();
tx.repo_mut().remove_head(commit.id());
txs.push(tx);
}
let repo = commit_transactions(txs);
let mut op_ids_to_delete = Vec::new();
op_ids_to_delete.push(repo.op_id());
let mut tx = repo.start_transaction();
write_random_commit(tx.repo_mut());
let repo = tx.commit("test").unwrap();
op_ids_to_delete.push(repo.op_id());
let operation_to_reload = repo.operation();
// Sanity check before corrupting the index store
let index = as_readonly_composite(&repo);
assert_eq!(index.num_commits(), 4);
let index_operations_dir = test_repo.repo_path().join("index").join("operations");
for &op_id in &op_ids_to_delete {
fs::remove_file(index_operations_dir.join(op_id.hex())).unwrap();
}
// When re-indexing, one of the merge parent operations will be selected as
// the parent index segment. The commits in the other side should still be
// reachable.
let repo = repo.reload_at(operation_to_reload).unwrap();
let index = as_readonly_composite(&repo);
assert_eq!(index.num_commits(), 4);
}
#[test]
fn test_reindex_missing_commit() {
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let test_env = &test_repo.env;
let repo = &test_repo.repo;
let mut tx = repo.start_transaction();
let missing_commit = write_random_commit(tx.repo_mut());
let repo = tx.commit("test").unwrap();
let bad_op_id = repo.op_id();
let mut tx = repo.start_transaction();
tx.repo_mut().remove_head(missing_commit.id());
let repo = tx.commit("test").unwrap();
// Remove historical head commit to simulate bad GC.
let test_backend: &TestBackend = repo.store().backend_impl().downcast_ref().unwrap();
test_backend.remove_commit_unchecked(missing_commit.id());
let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path()); // discard cache
assert!(repo.store().get_commit(missing_commit.id()).is_err());
// Reindexing error should include the operation id where the commit
// couldn't be found.
let default_index_store: &DefaultIndexStore =
repo.index_store().as_any().downcast_ref().unwrap();
default_index_store.reinit().unwrap();
let err = default_index_store
.build_index_at_operation(repo.operation(), repo.store())
.unwrap_err();
assert_matches!(err, DefaultIndexStoreError::IndexCommits { op_id, .. } if op_id == *bad_op_id);
}
/// Test that .jj/repo/index/type is created when the repo is created.
#[test]
fn test_index_store_type() {
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
assert_eq!(as_readonly_composite(repo).num_commits(), 1);
let index_store_type_path = test_repo.repo_path().join("index").join("type");
assert_eq!(
std::fs::read_to_string(index_store_type_path).unwrap(),
"default"
);
}
#[test]
fn test_change_id_index() {
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
let mut tx = repo.start_transaction();
let root_commit = repo.store().root_commit();
let mut commit_number = 0;
let mut commit_with_change_id = |change_id| {
commit_number += 1;
tx.repo_mut()
.new_commit(
vec![root_commit.id().clone()],
root_commit.tree_id().clone(),
)
.set_change_id(ChangeId::from_hex(change_id))
.set_description(format!("commit {commit_number}"))
.write()
.unwrap()
};
let commit_1 = commit_with_change_id("abbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
let commit_2 = commit_with_change_id("aaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
let commit_3 = commit_with_change_id("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
let commit_4 = commit_with_change_id("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
let commit_5 = commit_with_change_id("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
let index_for_heads = |commits: &[&Commit]| {
tx.repo()
.mutable_index()
.change_id_index(&mut commits.iter().map(|commit| commit.id()))
};
let change_id_index = index_for_heads(&[&commit_1, &commit_2, &commit_3, &commit_4, &commit_5]);
let prefix_len =
|commit: &Commit| change_id_index.shortest_unique_prefix_len(commit.change_id());
assert_eq!(prefix_len(&root_commit), 1);
assert_eq!(prefix_len(&commit_1), 2);
assert_eq!(prefix_len(&commit_2), 6);
assert_eq!(prefix_len(&commit_3), 6);
assert_eq!(prefix_len(&commit_4), 1);
assert_eq!(prefix_len(&commit_5), 1);
let resolve_prefix = |prefix: &str| {
change_id_index
.resolve_prefix(&HexPrefix::new(prefix).unwrap())
.map(HashSet::from_iter)
};
// Ambiguous matches
assert_eq!(resolve_prefix("a"), PrefixResolution::AmbiguousMatch);
assert_eq!(resolve_prefix("aaaaa"), PrefixResolution::AmbiguousMatch);
// Exactly the necessary length
assert_eq!(
resolve_prefix("0"),
PrefixResolution::SingleMatch(hashset! {root_commit.id().clone()})
);
assert_eq!(
resolve_prefix("aaaaaa"),
PrefixResolution::SingleMatch(hashset! {commit_3.id().clone()})
);
assert_eq!(
resolve_prefix("aaaaab"),
PrefixResolution::SingleMatch(hashset! {commit_2.id().clone()})
);
assert_eq!(
resolve_prefix("ab"),
PrefixResolution::SingleMatch(hashset! {commit_1.id().clone()})
);
assert_eq!(
resolve_prefix("b"),
PrefixResolution::SingleMatch(hashset! {commit_4.id().clone(), commit_5.id().clone()})
);
// Longer than necessary
assert_eq!(
resolve_prefix("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
PrefixResolution::SingleMatch(hashset! {commit_3.id().clone()})
);
// No match
assert_eq!(resolve_prefix("ba"), PrefixResolution::NoMatch);
// Test with an index containing only some of the commits. The shortest
// length doesn't have to be minimized further, but unreachable commits
// should never be included in the resolved set.
let change_id_index = index_for_heads(&[&commit_1, &commit_2]);
let resolve_prefix = |prefix: &str| {
change_id_index
.resolve_prefix(&HexPrefix::new(prefix).unwrap())
.map(HashSet::from_iter)
};
assert_eq!(
resolve_prefix("0"),
PrefixResolution::SingleMatch(hashset! {root_commit.id().clone()})
);
assert_eq!(
resolve_prefix("aaaaab"),
PrefixResolution::SingleMatch(hashset! {commit_2.id().clone()})
);
assert_eq!(resolve_prefix("aaaaaa"), PrefixResolution::NoMatch);
assert_eq!(resolve_prefix("a"), PrefixResolution::AmbiguousMatch);
assert_eq!(resolve_prefix("b"), PrefixResolution::NoMatch);
}
|