2042 lines · 72.5 KB · Rust
RawDownload
// Copyright 2021 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 assert_matches::assert_matches;use itertools::Itertools as _;use jj_lib::backend::ChangeId;use jj_lib::commit::Commit;use jj_lib::matchers::EverythingMatcher;use jj_lib::matchers::FilesMatcher;use jj_lib::merge::Merge;use jj_lib::merged_tree::MergedTree;use jj_lib::op_store::RefTarget;use jj_lib::op_store::RemoteRef;use jj_lib::op_store::RemoteRefState;use jj_lib::ref_name::RefName;use jj_lib::ref_name::RemoteName;use jj_lib::ref_name::RemoteRefSymbol;use jj_lib::ref_name::WorkspaceName;use jj_lib::ref_name::WorkspaceNameBuf;use jj_lib::repo::Repo as _;use jj_lib::rewrite::CommitRewriter;use jj_lib::rewrite::CommitWithSelection;use jj_lib::rewrite::EmptyBehavior;use jj_lib::rewrite::MoveCommitsTarget;use jj_lib::rewrite::RebaseOptions;use jj_lib::rewrite::RewriteRefsOptions;use jj_lib::rewrite::find_duplicate_divergent_commits;use jj_lib::rewrite::find_recursive_merge_commits;use jj_lib::rewrite::merge_commit_trees;use jj_lib::rewrite::rebase_commit_with_options;use jj_lib::rewrite::restore_tree;use maplit::hashmap;use maplit::hashset;use pollster::FutureExt as _;use test_case::test_case;use testutils::TestRepo;use testutils::assert_abandoned_with_parent;use testutils::assert_rebased_onto;use testutils::create_random_commit;use testutils::create_tree;use testutils::create_tree_with;use testutils::rebase_descendants_with_options_return_map;use testutils::repo_path;use testutils::write_random_commit;use testutils::write_random_commit_with_parents; 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(),    }} /// Based on https://lore.kernel.org/git/Pine.LNX.4.44.0504271254120.4678-100000@wax.eds.org//// (found in t/t6401-merge-criss-cross.sh in the git.git repo).#[test]fn test_merge_criss_cross() {    let test_repo = TestRepo::init();    let repo = &test_repo.repo;     let path = repo_path("file");    let tree_a = create_tree(repo, &[(path, "1\n2\n3\n4\n5\n6\n7\n8\n9\n")]);    let tree_b = create_tree(repo, &[(path, "1\n2\n3\n4\n5\n6\n7\n8B\n9\n")]);    let tree_c = create_tree(repo, &[(path, "1\n2\n3C\n4\n5\n6\n7\n8\n9\n")]);    let tree_d = create_tree(repo, &[(path, "1\n2\n3C\n4\n5\n6\n7\n8D\n9\n")]);    let tree_e = create_tree(repo, &[(path, "1\n2\n3E\n4\n5\n6\n7\n8B\n9\n")]);    let tree_expected = create_tree(repo, &[(path, "1\n2\n3E\n4\n5\n6\n7\n8D\n9\n")]);     let mut tx = repo.start_transaction();    let mut make_commit = |description, parents, tree_id| {        tx.repo_mut()            .new_commit(parents, tree_id)            .set_description(description)            .write()            .unwrap()    };    let commit_a = make_commit(        "A",        vec![repo.store().root_commit_id().clone()],        tree_a.id(),    );    let commit_b = make_commit("B", vec![commit_a.id().clone()], tree_b.id());    let commit_c = make_commit("C", vec![commit_a.id().clone()], tree_c.id());    let commit_d = make_commit(        "D",        vec![commit_b.id().clone(), commit_c.id().clone()],        tree_d.id(),    );    let commit_e = make_commit(        "E",        vec![commit_b.id().clone(), commit_c.id().clone()],        tree_e.id(),    );    let merged = merge_commit_trees(tx.repo_mut(), &[commit_d, commit_e])        .block_on()        .unwrap();     assert_eq!(merged, tree_expected);} #[test]fn test_find_recursive_merge_commits() {    let test_repo = TestRepo::init();    let repo = &test_repo.repo;     let mut tx = repo.start_transaction();    let commit_a = write_random_commit(tx.repo_mut());    let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);    let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);    let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b, &commit_c]);    let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b, &commit_c]);     let commit_id_merge = find_recursive_merge_commits(        tx.repo().store(),        tx.repo().index(),        vec![commit_d.id().clone(), commit_e.id().clone()],    )    .unwrap();     assert_eq!(        commit_id_merge,        Merge::from_vec(vec![            commit_d.id().clone(),            commit_b.id().clone(),            commit_a.id().clone(),            commit_c.id().clone(),            commit_e.id().clone(),        ])    );} #[test]fn test_restore_tree() {    let test_repo = TestRepo::init();    let repo = &test_repo.repo;     let path1 = repo_path("file1");    let path2 = repo_path("dir1/file2");    let path3 = repo_path("dir1/file3");    let path4 = repo_path("dir2/file4");    let left = create_tree(repo, &[(path2, "left"), (path3, "left"), (path4, "left")]);    let right = create_tree(        repo,        &[(path1, "right"), (path2, "right"), (path3, "right")],    );     // Restore everything using EverythingMatcher    let restored = restore_tree(&left, &right, &EverythingMatcher)        .block_on()        .unwrap();    assert_eq!(restored, left.id());     // Restore everything using FilesMatcher    let restored = restore_tree(        &left,        &right,        &FilesMatcher::new([&path1, &path2, &path3, &path4]),    )    .block_on()    .unwrap();    assert_eq!(restored, left.id());     // Restore some files    let restored = restore_tree(&left, &right, &FilesMatcher::new([path1, path2]))        .block_on()        .unwrap();    let expected = create_tree(repo, &[(path2, "left"), (path3, "right")]);    assert_eq!(restored, expected.id());} #[test]fn test_rebase_descendants_sideways() {    let test_repo = TestRepo::init();    let repo = &test_repo.repo;     // Commit B was replaced by commit F. Commits C-E should be rebased.    //    // F    // | D    // | C E    // | |/    // | B    // |/    // A    let mut tx = repo.start_transaction();    let commit_a = write_random_commit(tx.repo_mut());    let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);    let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);    let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);    let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);    let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);     tx.repo_mut()        .set_rewritten_commit(commit_b.id().clone(), commit_f.id().clone());    let rebase_map =        rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());    assert_eq!(rebase_map.len(), 3);    let new_commit_c = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_c, &[commit_f.id()]);    let new_commit_d =        assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_d, &[new_commit_c.id()]);    let new_commit_e = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_e, &[commit_f.id()]);     assert_eq!(        *tx.repo().view().heads(),        hashset! {            new_commit_d.id().clone(),            new_commit_e.id().clone()        }    );} #[test]fn test_rebase_descendants_forward() {    let test_repo = TestRepo::init();    let repo = &test_repo.repo;     // Commit B was replaced by commit F. Commits C and E should be rebased onto F.    // Commit D does not get rebased because it's an ancestor of the    // destination. Commit G does not get replaced because it's already in    // place.    // TODO: The above is not what actually happens! The test below shows what    // actually happens: D and F also get rebased onto F, so we end up with    // duplicates. Consider if it's worth supporting the case above better or if    // that decision belongs with the caller (as we currently force it to do by    // not supporting it in DescendantRebaser).    //    // G    // F E    // |/    // D C    // |/    // B    // A    let mut tx = repo.start_transaction();    let commit_a = write_random_commit(tx.repo_mut());    let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);    let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);    let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);    let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d]);    let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d]);    let commit_g = write_random_commit_with_parents(tx.repo_mut(), &[&commit_f]);     tx.repo_mut()        .set_rewritten_commit(commit_b.id().clone(), commit_f.id().clone());    let rebase_map =        rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());    let new_commit_d =        assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_d, &[(commit_f.id())]);    let new_commit_f =        assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_f, &[new_commit_d.id()]);    let new_commit_c =        assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_c, &[new_commit_f.id()]);    let new_commit_e =        assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_e, &[new_commit_d.id()]);    let new_commit_g =        assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_g, &[new_commit_f.id()]);    assert_eq!(rebase_map.len(), 5);     assert_eq!(        *tx.repo().view().heads(),        hashset! {            new_commit_c.id().clone(),            new_commit_e.id().clone(),            new_commit_g.id().clone(),        }    );} #[test]fn test_rebase_descendants_reorder() {    let test_repo = TestRepo::init();    let repo = &test_repo.repo;     // Commit E was replaced by commit D, and commit C was replaced by commit F    // (attempting to to reorder C and E), and commit G was replaced by commit    // H.    //    // I    // G H    // E F    // C D    // |/    // B    // A    let mut tx = repo.start_transaction();    let commit_a = write_random_commit(tx.repo_mut());    let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);    let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);    let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);    let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);    let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_d]);    let commit_g = write_random_commit_with_parents(tx.repo_mut(), &[&commit_e]);    let commit_h = write_random_commit_with_parents(tx.repo_mut(), &[&commit_f]);    let commit_i = write_random_commit_with_parents(tx.repo_mut(), &[&commit_g]);     tx.repo_mut()        .set_rewritten_commit(commit_e.id().clone(), commit_d.id().clone());    tx.repo_mut()        .set_rewritten_commit(commit_c.id().clone(), commit_f.id().clone());    tx.repo_mut()        .set_rewritten_commit(commit_g.id().clone(), commit_h.id().clone());    let rebase_map =        rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());    let new_commit_i = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_i, &[commit_h.id()]);    assert_eq!(rebase_map.len(), 1);     assert_eq!(        *tx.repo().view().heads(),        hashset! {            new_commit_i.id().clone(),        }    );} #[test]fn test_rebase_descendants_backward() {    let test_repo = TestRepo::init();    let repo = &test_repo.repo;     // Commit C was replaced by commit B. Commit D should be rebased.    //    // D    // C    // B    // A    let mut tx = repo.start_transaction();    let commit_a = write_random_commit(tx.repo_mut());    let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);    let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);    let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);     tx.repo_mut()        .set_rewritten_commit(commit_c.id().clone(), commit_b.id().clone());    let rebase_map =        rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());    let new_commit_d = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_d, &[commit_b.id()]);    assert_eq!(rebase_map.len(), 1);     assert_eq!(        *tx.repo().view().heads(),        hashset! {new_commit_d.id().clone()}    );} #[test]fn test_rebase_descendants_chain_becomes_branchy() {    let test_repo = TestRepo::init();    let repo = &test_repo.repo;     // Commit B was replaced by commit E and commit C was replaced by commit F.    // Commit F should get rebased onto E, and commit D should get rebased onto    // the rebased F.    //    // D    // C F    // |/    // B E    // |/    // A    let mut tx = repo.start_transaction();    let commit_a = write_random_commit(tx.repo_mut());    let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);    let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);    let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c]);    let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);    let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);     tx.repo_mut()        .set_rewritten_commit(commit_b.id().clone(), commit_e.id().clone());    tx.repo_mut()        .set_rewritten_commit(commit_c.id().clone(), commit_f.id().clone());    let rebase_map =        rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());    let new_commit_f = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_f, &[commit_e.id()]);    let new_commit_d =        assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_d, &[new_commit_f.id()]);    assert_eq!(rebase_map.len(), 2);     assert_eq!(        *tx.repo().view().heads(),        hashset! {            new_commit_d.id().clone(),        }    );} #[test]fn test_rebase_descendants_internal_merge() {    let test_repo = TestRepo::init();    let repo = &test_repo.repo;     // Commit B was replaced by commit F. Commits C-E should be rebased.    //    // F    // | E    // | |\    // | C D    // | |/    // | B    // |/    // A    let mut tx = repo.start_transaction();    let commit_a = write_random_commit(tx.repo_mut());    let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);    let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);    let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);    let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c, &commit_d]);    let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);     tx.repo_mut()        .set_rewritten_commit(commit_b.id().clone(), commit_f.id().clone());    let rebase_map =        rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());    let new_commit_c = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_c, &[commit_f.id()]);    let new_commit_d = assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit_d, &[commit_f.id()]);    let new_commit_e = assert_rebased_onto(        tx.repo_mut(),        &rebase_map,        &commit_e,        &[new_commit_c.id(), new_commit_d.id()],    );    assert_eq!(rebase_map.len(), 3);     assert_eq!(        *tx.repo().view().heads(),        hashset! { new_commit_e.id().clone() }    );} #[test]fn test_rebase_descendants_external_merge() {    let test_repo = TestRepo::init();    let repo = &test_repo.repo;     // Commit C was replaced by commit F. Commits E should be rebased. The rebased    // commit E should have F as first parent and commit D as second parent.    //    // F    // | E    // | |\    // | C D    // | |/    // | B    // |/    // A    let mut tx = repo.start_transaction();    let commit_a = write_random_commit(tx.repo_mut());    let commit_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);    let commit_c = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);    let commit_d = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]);    let commit_e = write_random_commit_with_parents(tx.repo_mut(), &[&commit_c, &commit_d]);    let commit_f = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]);     tx.repo_mut()        .set_rewritten_commit(commit_c.id().clone(), commit_f.id().clone());    let rebase_map =        rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());    let new_commit_e = assert_rebased_onto(        tx.repo_mut(),        &rebase_map,        &commit_e,        &[commit_f.id(), commit_d.id()],    );    assert_eq!(rebase_map.len(), 1);     assert_eq!(        *tx.repo().view().heads(),        hashset! {new_commit_e.id().clone()}    );} #[test]fn test_rebase_descendants_abandon() {    let test_repo = TestRepo::init();    let repo = &test_repo.repo;     // Commit B and commit E were abandoned. Commit C and commit D should get    // rebased onto commit A. Commit F should get rebased onto the new commit D.    //    // F    // E    // D C    // |/    // B    // A

Lines 1–500 of 2042.