mirror of
https://github.com/martinvonz/jj.git
synced 2025-05-09 01:12:50 +00:00
There are several existing commands that would benefit from an API that makes it easier to rewrite a whole graph of commits while transforming them in some way. `jj squash` is one example. When squashing into an ancestor, that command currently rewrites the ancestor, then rebases descendants, and then rewrites the rewritten source commit. It would be better to rewrite the source commit (and any descendants) only once. Another example is the future `jj fix`. That command will want to rewrite a graph while updating the trees. There's currently no good API for that; you have to manually iterate over descendants and rewrite them. This patch adds a new `MutableRepo::transform_descendants()` method that takes a callback which gets a `CommitRewriter` passed to it. The callback can then decide to change the parents, the tree, etc. The callback is also free to leave the commit in place or to abandon it. I updated the regular `rebase_descendants()` to use the new function in order to exercise it. I hope we can replace all of the `rebase_descendant_*()` flavors later. I added a `replace_parent()` method that was a bit useful for the test case. It could easily be hard-coded in the test case instead, but I think the method will be useful for `jj git sync` and similar in the future.
85 lines
3.2 KiB
Rust
85 lines
3.2 KiB
Rust
// Copyright 2024 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::HashMap;
|
|
|
|
use jj_lib::repo::Repo;
|
|
use maplit::hashset;
|
|
use testutils::{CommitGraphBuilder, TestRepo};
|
|
|
|
// Simulate some `jj sync` command that rebases B:: onto G while abandoning C
|
|
// (because it's presumably already in G).
|
|
//
|
|
// G
|
|
// | E
|
|
// | D F
|
|
// | |/
|
|
// | C
|
|
// | B
|
|
// |/
|
|
// A
|
|
#[test]
|
|
fn test_transform_descendants_sync() {
|
|
let settings = testutils::user_settings();
|
|
let test_repo = TestRepo::init();
|
|
let repo = &test_repo.repo;
|
|
|
|
let mut tx = repo.start_transaction(&settings);
|
|
let mut graph_builder = CommitGraphBuilder::new(&settings, tx.mut_repo());
|
|
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 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_c]);
|
|
let commit_g = graph_builder.commit_with_parents(&[&commit_a]);
|
|
|
|
let mut rebased = HashMap::new();
|
|
tx.mut_repo()
|
|
.transform_descendants(&settings, vec![commit_b.id().clone()], |mut rewriter| {
|
|
rewriter.replace_parent(commit_a.id(), &[commit_g.id().clone()]);
|
|
if *rewriter.old_commit() == commit_c {
|
|
let old_id = rewriter.old_commit().id().clone();
|
|
let new_parent_ids = rewriter.new_parents().to_vec();
|
|
rewriter
|
|
.mut_repo()
|
|
.record_abandoned_commit_with_parents(old_id, new_parent_ids);
|
|
} else {
|
|
let old_commit_id = rewriter.old_commit().id().clone();
|
|
let new_commit = rewriter.rebase(&settings)?.write()?;
|
|
rebased.insert(old_commit_id, new_commit);
|
|
}
|
|
Ok(())
|
|
})
|
|
.unwrap();
|
|
assert_eq!(rebased.len(), 4);
|
|
let new_commit_b = rebased.get(commit_b.id()).unwrap();
|
|
let new_commit_d = rebased.get(commit_d.id()).unwrap();
|
|
let new_commit_e = rebased.get(commit_e.id()).unwrap();
|
|
let new_commit_f = rebased.get(commit_f.id()).unwrap();
|
|
|
|
assert_eq!(
|
|
*tx.mut_repo().view().heads(),
|
|
hashset! {
|
|
new_commit_e.id().clone(),
|
|
new_commit_f.id().clone(),
|
|
}
|
|
);
|
|
|
|
assert_eq!(new_commit_b.parent_ids(), vec![commit_g.id().clone()]);
|
|
assert_eq!(new_commit_d.parent_ids(), vec![new_commit_b.id().clone()]);
|
|
assert_eq!(new_commit_e.parent_ids(), vec![new_commit_d.id().clone()]);
|
|
assert_eq!(new_commit_f.parent_ids(), vec![new_commit_b.id().clone()]);
|
|
}
|