Skip to content

Commit

Permalink
EXAMPLE: Rewrite commit to multiple commits
Browse files Browse the repository at this point in the history
  • Loading branch information
emesterhazy committed Apr 11, 2024
1 parent 4c3c546 commit bb838e7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 49 deletions.
49 changes: 8 additions & 41 deletions cli/src/commands/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,16 @@ the operation will be aborted.
// second commit after the command finishes. This also means that any
// branches pointing to the commit being split are moved to the second
// commit.
tx.mut_repo()
.set_rewritten_commit(commit.id().clone(), second_commit.id().clone());
let num_rebased = if args.siblings {
rebase_children_for_siblings_split(
&mut tx,
command.settings(),
&commit,
vec![first_commit.clone(), second_commit.clone()],
)?
if args.siblings {
tx.mut_repo().set_rewritten_commit_multiple(
commit.id().clone(),
vec![first_commit.id().clone(), second_commit.id().clone()],
);
} else {
tx.mut_repo().rebase_descendants(command.settings())?
tx.mut_repo()
.set_rewritten_commit(commit.id().clone(), second_commit.id().clone());
};
let num_rebased = tx.mut_repo().rebase_descendants(command.settings())?;

if let Some(mut formatter) = ui.status_formatter() {
if num_rebased > 0 {
Expand All @@ -201,34 +199,3 @@ the operation will be aborted.
tx.finish(ui, format!("split commit {}", commit.id().hex()))?;
Ok(())
}

// Rebases the children of `original_commit` by replacing `original_commit` with
// `new_siblings`. Any parents other than `original_commit` will remain after
// the rebase.
fn rebase_children_for_siblings_split(
tx: &mut WorkspaceCommandTransaction,
settings: &UserSettings,
original_commit: &Commit,
new_siblings: Vec<Commit>,
) -> Result<usize, CommandError> {
let children: Vec<Commit> = RevsetExpression::commit(original_commit.id().clone())
.children()
.evaluate_programmatic(tx.base_repo().as_ref())?
.iter()
.commits(tx.base_repo().store())
.try_collect()?;
let mut num_rebased = 0;
for child in children {
let mut new_parents = new_siblings.clone();
new_parents.extend(
child
.parents()
.into_iter()
// Filter out the commit being split.
.filter(|c| c.id() != original_commit.id()),
);
num_rebased +=
rebase_descendants(tx, settings, &new_parents, &[child], Default::default())?;
}
Ok(num_rebased)
}
16 changes: 8 additions & 8 deletions cli/tests/test_split_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ JJ: Lines starting with "JJ: " (like this one) will be removed.
);
assert!(!test_env.env_root().join("editor2").exists());
insta::assert_snapshot!(get_log_output(&test_env, &workspace_path), @r###"
@ zsuskulnrvyr false test_branch
qpvuntsmwlqt false TESTED=TODO
├─╯
zsuskulnrvyr false test_branch??
@ qpvuntsmwlqt false TESTED=TODO
├─╯ test_branch??
◉ zzzzzzzzzzzz true
"###);
}
Expand Down Expand Up @@ -371,8 +371,8 @@ JJ: Lines starting with "JJ: " (like this one) will be removed.
◉ kkmpptxzrspx false Add file4
◉ rlvkpnrzqnoo false Add file3
├─╮
@ yqosqzytrlsw false Add file2
│ qpvuntsmwlqt false Add file1
yqosqzytrlsw false Add file2
@ │ qpvuntsmwlqt false Add file1
├─╯
◉ zzzzzzzzzzzz true
"###);
Expand Down Expand Up @@ -416,10 +416,10 @@ fn test_split_siblings_with_merge_child() {
insta::assert_snapshot!(get_log_output(&test_env, &workspace_path), @r###"
@ zsuskulnrvyr true 2
├─┬─╮
│ │ ◉ qpvuntsmwlqt true 1
│ ◉ │ royxmykxtrkr false Add file2
│ │ ◉ royxmykxtrkr false Add file2
│ ◉ │ kkmpptxzrspx false Add file1
│ ├─╯
◉ │ kkmpptxzrspx false Add file1
◉ │ qpvuntsmwlqt true 1
├─╯
◉ zzzzzzzzzzzz true
"###);
Expand Down
6 changes: 6 additions & 0 deletions lib/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,12 @@ impl MutableRepo {
.insert(old_id, (RewriteType::Rewritten, vec![new_id]));
}

pub fn set_rewritten_commit_multiple(&mut self, old_id: CommitId, new_ids: Vec<CommitId>) {
assert_ne!(old_id, *self.store().root_commit_id());
self.parent_mapping
.insert(old_id, (RewriteType::Rewritten, new_ids));
}

/// Record a commit as being rewritten into multiple other commits in this
/// transaction.
///
Expand Down

0 comments on commit bb838e7

Please sign in to comment.