cli: refactor out rebase_to_destination_parent()

Refactor out the parent-rebasing parts of show_predecessor_patch() to
create rebase_to_destination_parent(). This will be reused for `jj
interdiff`.
This commit is contained in:
Glen Choo 2022-08-30 11:47:38 -07:00
parent d53d9020c9
commit ed415c17b0

View file

@ -3225,29 +3225,41 @@ fn show_predecessor_patch(
commit: &Commit, commit: &Commit,
diff_format: DiffFormat, diff_format: DiffFormat,
) -> Result<(), CommandError> { ) -> Result<(), CommandError> {
if let Some(predecessor) = commit.predecessors().first() { let predecessors = commit.predecessors();
let predecessor_tree = if predecessor.parent_ids() == commit.parent_ids() { let predecessor = match predecessors.first() {
predecessor.tree() Some(predecessor) => predecessor,
} else { None => return Ok(()),
// Rebase the predecessor to have the current commit's parent(s) and use that };
// tree as base let predecessor_tree = rebase_to_dest_parent(workspace_command, predecessor, commit)?;
let new_parent_tree = let diff_iterator = predecessor_tree.diff(&commit.tree(), &EverythingMatcher);
merge_commit_trees(workspace_command.repo().as_repo_ref(), &commit.parents()); show_diff(formatter, workspace_command, diff_iterator, diff_format)
let old_parent_tree = merge_commit_trees( }
workspace_command.repo().as_repo_ref(),
&predecessor.parents(), fn rebase_to_dest_parent(
); workspace_command: &WorkspaceCommandHelper,
let rebased_tree_id = source: &Commit,
merge_trees(&new_parent_tree, &old_parent_tree, &predecessor.tree())?; destination: &Commit,
workspace_command ) -> Result<Tree, CommandError> {
.repo() if source.parent_ids() == destination.parent_ids() {
.store() Ok(source.tree())
.get_tree(&RepoPath::root(), &rebased_tree_id)? } else {
}; let destination_parent_tree = merge_commit_trees(
let diff_iterator = predecessor_tree.diff(&commit.tree(), &EverythingMatcher); workspace_command.repo().as_repo_ref(),
show_diff(formatter, workspace_command, diff_iterator, diff_format)?; &destination.parents(),
);
let source_parent_tree =
merge_commit_trees(workspace_command.repo().as_repo_ref(), &source.parents());
let rebased_tree_id = merge_trees(
&destination_parent_tree,
&source_parent_tree,
&source.tree(),
)?;
let tree = workspace_command
.repo()
.store()
.get_tree(&RepoPath::root(), &rebased_tree_id)?;
Ok(tree)
} }
Ok(())
} }
fn edit_description( fn edit_description(