Skip to content

Commit

Permalink
commands: move rebase_to_dest_parent to jj_lib::rewrite::rebase_source
Browse files Browse the repository at this point in the history
What make rebase_to_dest_parent a good candidate for jj_lib::rewrite module:

- It is used both in obslog and interdiff. It's a sign that it may be moved to a lower layer
- CommandError is returned by converting from TreeMergeError. Not explicitly.
- It only use jj_lib::rewrite fonctions.
  • Loading branch information
AntoineCezar committed Nov 3, 2023
1 parent 4f84c6b commit bed6a14
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
4 changes: 2 additions & 2 deletions cli/src/commands/interdiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
// limitations under the License.

use clap::ArgGroup;
use jj_lib::rewrite::rebase_to_dest_parent;
use tracing::instrument;

use super::rebase_to_dest_parent;
use crate::cli_util::{CommandError, CommandHelper, RevisionArg};
use crate::diff_util::{self, DiffFormatArgs};
use crate::ui::Ui;
Expand Down Expand Up @@ -51,7 +51,7 @@ pub(crate) fn cmd_interdiff(
let from = workspace_command.resolve_single_rev(args.from.as_deref().unwrap_or("@"), ui)?;
let to = workspace_command.resolve_single_rev(args.to.as_deref().unwrap_or("@"), ui)?;

let from_tree = rebase_to_dest_parent(&workspace_command, &from, &to)?;
let from_tree = rebase_to_dest_parent(workspace_command.repo().as_ref(), &from, &to)?;
let to_tree = to.tree()?;
let matcher = workspace_command.matcher_from_values(&args.paths)?;
let diff_formats = diff_util::diff_formats_for(command.settings(), &args.format)?;
Expand Down
20 changes: 0 additions & 20 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ use clap::{Command, CommandFactory, FromArgMatches, Subcommand};
use itertools::Itertools;
use jj_lib::commit::Commit;
use jj_lib::matchers::EverythingMatcher;
use jj_lib::merged_tree::MergedTree;
use jj_lib::repo::ReadonlyRepo;
use jj_lib::rewrite::merge_commit_trees;
use jj_lib::settings::UserSettings;
use tracing::instrument;

Expand Down Expand Up @@ -147,24 +145,6 @@ enum Commands {
Workspace(workspace::WorkspaceCommands),
}

fn rebase_to_dest_parent(
workspace_command: &WorkspaceCommandHelper,
source: &Commit,
destination: &Commit,
) -> Result<MergedTree, CommandError> {
if source.parent_ids() == destination.parent_ids() {
Ok(source.tree()?)
} else {
let destination_parent_tree =
merge_commit_trees(workspace_command.repo().as_ref(), &destination.parents())?;
let source_parent_tree =
merge_commit_trees(workspace_command.repo().as_ref(), &source.parents())?;
let source_tree = source.tree()?;
let rebased_tree = destination_parent_tree.merge(&source_parent_tree, &source_tree)?;
Ok(rebased_tree)
}
}

fn edit_description(
repo: &ReadonlyRepo,
description: &str,
Expand Down
5 changes: 3 additions & 2 deletions cli/src/commands/obslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
use jj_lib::commit::Commit;
use jj_lib::dag_walk::topo_order_reverse;
use jj_lib::matchers::EverythingMatcher;
use jj_lib::rewrite::rebase_to_dest_parent;
use tracing::instrument;

use super::rebase_to_dest_parent;
use crate::cli_util::{
CommandError, CommandHelper, LogContentFormat, RevisionArg, WorkspaceCommandHelper,
};
Expand Down Expand Up @@ -153,7 +153,8 @@ fn show_predecessor_patch(
Some(predecessor) => predecessor,
None => return Ok(()),
};
let predecessor_tree = rebase_to_dest_parent(workspace_command, predecessor, commit)?;
let predecessor_tree =
rebase_to_dest_parent(workspace_command.repo().as_ref(), predecessor, commit)?;
let tree = commit.tree()?;
diff_util::show_diff(
ui,
Expand Down
16 changes: 16 additions & 0 deletions lib/src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ pub fn rebase_commit(
.write()?)
}

pub fn rebase_to_dest_parent(
repo: &dyn Repo,
source: &Commit,
destination: &Commit,
) -> Result<MergedTree, TreeMergeError> {
if source.parent_ids() == destination.parent_ids() {
Ok(source.tree()?)
} else {
let destination_parent_tree = merge_commit_trees(repo, &destination.parents())?;
let source_parent_tree = merge_commit_trees(repo, &source.parents())?;
let source_tree = source.tree()?;
let rebased_tree = destination_parent_tree.merge(&source_parent_tree, &source_tree)?;
Ok(rebased_tree)
}
}

pub fn back_out_commit(
settings: &UserSettings,
mut_repo: &mut MutableRepo,
Expand Down

0 comments on commit bed6a14

Please sign in to comment.