From ffc73110ced3affb077f0ef08f0a99107669beab Mon Sep 17 00:00:00 2001 From: Evan Mesterhazy Date: Sat, 30 Mar 2024 19:10:33 -0400 Subject: [PATCH] Split rebase_descendants into two functions This refactor will allow us to reuse new `rebase_descendants` function for the `jj split --siblings` feature (#2274) and later possibly for `jj parallelize` (#1079). --- cli/src/commands/rebase.rs | 50 ++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/cli/src/commands/rebase.rs b/cli/src/commands/rebase.rs index af3991b719..5a55a36167 100644 --- a/cli/src/commands/rebase.rs +++ b/cli/src/commands/rebase.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::borrow::Borrow; use std::collections::HashMap; use std::io::Write; use std::sync::Arc; @@ -30,7 +31,7 @@ use tracing::instrument; use crate::cli_util::{ resolve_multiple_nonempty_revsets_default_single, short_commit_hash, CommandHelper, - RevisionArg, WorkspaceCommandHelper, + RevisionArg, WorkspaceCommandHelper, WorkspaceCommandTransaction, }; use crate::command_error::{user_error, CommandError}; use crate::ui::Ui; @@ -221,7 +222,7 @@ Please use `jj rebase -d 'all:x|y'` instead of `jj rebase --allow-large-revsets } else if !args.source.is_empty() { let source_commits = resolve_multiple_nonempty_revsets_default_single(&workspace_command, &args.source)?; - rebase_descendants( + rebase_descendants_transaction( ui, command.settings(), &mut workspace_command, @@ -272,7 +273,7 @@ fn rebase_branch( .iter() .commits(workspace_command.repo().store()) .try_collect()?; - rebase_descendants( + rebase_descendants_transaction( ui, settings, workspace_command, @@ -282,7 +283,32 @@ fn rebase_branch( ) } -fn rebase_descendants( +/// Rebases `old_commits` onto `new_parents`. +pub fn rebase_descendants( + tx: &mut WorkspaceCommandTransaction, + settings: &UserSettings, + new_parents: &[Commit], + old_commits: &[impl Borrow], + rebase_options: RebaseOptions, +) -> Result { + // `rebase_descendants` takes care of sorting in reverse topological order, so + // no need to do it here. + for old_commit in old_commits.iter() { + rebase_commit_with_options( + settings, + tx.mut_repo(), + old_commit.borrow(), + new_parents, + &rebase_options, + )?; + } + let num_rebased = old_commits.len() + + tx.mut_repo() + .rebase_descendants_with_options(settings, rebase_options)?; + Ok(num_rebased) +} + +fn rebase_descendants_transaction( ui: &mut Ui, settings: &UserSettings, workspace_command: &mut WorkspaceCommandHelper, @@ -304,20 +330,8 @@ fn rebase_descendants( check_rebase_destinations(workspace_command.repo(), new_parents, old_commit)?; } let mut tx = workspace_command.start_transaction(); - // `rebase_descendants` takes care of sorting in reverse topological order, so - // no need to do it here. - for old_commit in old_commits.iter() { - rebase_commit_with_options( - settings, - tx.mut_repo(), - old_commit, - new_parents, - &rebase_options, - )?; - } - let num_rebased = old_commits.len() - + tx.mut_repo() - .rebase_descendants_with_options(settings, rebase_options)?; + let num_rebased = + rebase_descendants(&mut tx, settings, new_parents, &old_commits, rebase_options)?; writeln!(ui.stderr(), "Rebased {num_rebased} commits")?; let tx_message = if old_commits.len() == 1 { format!(