From 90e78a1424de75dca3fd53e2ca6afbb30e35ca54 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 27 Aug 2023 07:11:50 -0700 Subject: [PATCH] rewrite: return `MergedTree` from `merge_commit_trees()` --- cli/src/commands/mod.rs | 45 +++++++++++-------------------------- cli/src/commit_templater.rs | 2 +- cli/src/diff_util.rs | 5 +---- lib/src/rewrite.rs | 26 ++++++++++++++------- 4 files changed, 33 insertions(+), 45 deletions(-) diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index 36bdf1621..684b870ec 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -1436,10 +1436,7 @@ fn cmd_diff(ui: &mut Ui, command: &CommandHelper, args: &DiffArgs) -> Result<(), let commit = workspace_command.resolve_single_rev(args.revision.as_deref().unwrap_or("@"), ui)?; let parents = commit.parents(); - from_tree = MergedTree::legacy(merge_commit_trees( - workspace_command.repo().as_ref(), - &parents, - )?); + from_tree = merge_commit_trees(workspace_command.repo().as_ref(), &parents)?; to_tree = commit.merged_tree()? } let matcher = workspace_command.matcher_from_values(&args.paths)?; @@ -1496,8 +1493,7 @@ fn cmd_status( let formatter = formatter.as_mut(); if let Some(wc_commit) = &maybe_wc_commit { - let parent_tree = - MergedTree::legacy(merge_commit_trees(repo.as_ref(), &wc_commit.parents())?); + let parent_tree = merge_commit_trees(repo.as_ref(), &wc_commit.parents())?; let tree = wc_commit.merged_tree()?; if tree.id() == parent_tree.id() { formatter.write_str("The working copy is clean\n")?; @@ -1883,14 +1879,10 @@ fn rebase_to_dest_parent( if source.parent_ids() == destination.parent_ids() { Ok(source.merged_tree()?) } else { - let destination_parent_tree = MergedTree::legacy(merge_commit_trees( - workspace_command.repo().as_ref(), - &destination.parents(), - )?); - let source_parent_tree = MergedTree::legacy(merge_commit_trees( - workspace_command.repo().as_ref(), - &source.parents(), - )?); + 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.merged_tree()?; let rebased_tree = destination_parent_tree.merge(&source_parent_tree, &source_tree)?; Ok(rebased_tree) @@ -2291,11 +2283,7 @@ Please use `jj new 'all:x|y'` instead of `jj new --allow-large-revsets x y`.", let new_parents_commit_id = new_parents_commits.iter().map(|c| c.id().clone()).collect(); new_commit = tx .mut_repo() - .new_commit( - command.settings(), - new_parents_commit_id, - merged_tree.legacy_id(), - ) + .new_commit(command.settings(), new_parents_commit_id, merged_tree.id()) .set_description(cli_util::join_message_paragraphs(&args.message_paragraphs)) .write()?; num_rebased = target_ids.len(); @@ -2311,11 +2299,7 @@ Please use `jj new 'all:x|y'` instead of `jj new --allow-large-revsets x y`.", let merged_tree = merge_commit_trees(tx.repo(), &target_commits)?; new_commit = tx .mut_repo() - .new_commit( - command.settings(), - target_ids.clone(), - merged_tree.legacy_id(), - ) + .new_commit(command.settings(), target_ids.clone(), merged_tree.id()) .set_description(cli_util::join_message_paragraphs(&args.message_paragraphs)) .write()?; if args.insert_after { @@ -2403,7 +2387,7 @@ fn cmd_move(ui: &mut Ui, command: &CommandHelper, args: &MoveArgs) -> Result<(), source.id().hex(), destination.id().hex() )); - let parent_tree = MergedTree::legacy(merge_commit_trees(tx.repo(), &source.parents())?); + let parent_tree = merge_commit_trees(tx.repo(), &source.parents())?; let source_tree = source.merged_tree()?; let instructions = format!( "\ @@ -2586,7 +2570,7 @@ fn cmd_unsquash( workspace_command.check_rewritable(parent)?; let mut tx = workspace_command.start_transaction(&format!("unsquash commit {}", commit.id().hex())); - let parent_base_tree = MergedTree::legacy(merge_commit_trees(tx.repo(), &parent.parents())?); + let parent_base_tree = merge_commit_trees(tx.repo(), &parent.parents())?; let new_parent_tree_id; if args.interactive { let instructions = format!( @@ -2889,10 +2873,7 @@ fn cmd_restore( } else { to_commit = workspace_command.resolve_single_rev(args.changes_in.as_deref().unwrap_or("@"), ui)?; - from_tree = MergedTree::legacy(merge_commit_trees( - workspace_command.repo().as_ref(), - &to_commit.parents(), - )?); + from_tree = merge_commit_trees(workspace_command.repo().as_ref(), &to_commit.parents())?; } workspace_command.check_rewritable(&to_commit)?; @@ -2966,7 +2947,7 @@ Adjust the right side until it shows the contents you want. If you don't make any changes, then the operation will be aborted.", tx.format_commit_summary(&target_commit), ); - let base_tree = MergedTree::legacy(merge_commit_trees(tx.repo(), base_commits.as_slice())?); + let base_tree = merge_commit_trees(tx.repo(), base_commits.as_slice())?; let tree = target_commit.merged_tree()?; let tree_id = tx.edit_diff(ui, &base_tree, &tree, &instructions)?; if tree_id == *target_commit.merged_tree_id() { @@ -3057,7 +3038,7 @@ fn cmd_split(ui: &mut Ui, command: &CommandHelper, args: &SplitArgs) -> Result<( let mut tx = workspace_command.start_transaction(&format!("split commit {}", commit.id().hex())); let end_tree = commit.merged_tree()?; - let base_tree = MergedTree::legacy(merge_commit_trees(tx.repo(), &commit.parents())?); + let base_tree = merge_commit_trees(tx.repo(), &commit.parents())?; let interactive = args.paths.is_empty(); let instructions = format!( "\ diff --git a/cli/src/commit_templater.rs b/cli/src/commit_templater.rs index 68885559b..736f4f295 100644 --- a/cli/src/commit_templater.rs +++ b/cli/src/commit_templater.rs @@ -304,7 +304,7 @@ fn build_commit_keyword_opt<'repo>( })), "empty" => language.wrap_boolean(wrap_fn(property, |commit| { let parent_tree = rewrite::merge_commit_trees(repo, &commit.parents()).unwrap(); - commit.tree_id() == parent_tree.id() + *commit.merged_tree_id() == parent_tree.id() })), "root" => language.wrap_boolean(wrap_fn(property, move |commit| { commit.id() == repo.store().root_commit_id() diff --git a/cli/src/diff_util.rs b/cli/src/diff_util.rs index 8b23d5624..8b304db20 100644 --- a/cli/src/diff_util.rs +++ b/cli/src/diff_util.rs @@ -203,10 +203,7 @@ pub fn show_patch( formats: &[DiffFormat], ) -> Result<(), CommandError> { let parents = commit.parents(); - let from_tree = MergedTree::legacy(rewrite::merge_commit_trees( - workspace_command.repo().as_ref(), - &parents, - )?); + let from_tree = rewrite::merge_commit_trees(workspace_command.repo().as_ref(), &parents)?; let to_tree = commit.merged_tree()?; show_diff( ui, diff --git a/lib/src/rewrite.rs b/lib/src/rewrite.rs index 8eb53cd91..e1d18600d 100644 --- a/lib/src/rewrite.rs +++ b/lib/src/rewrite.rs @@ -24,6 +24,7 @@ use crate::backend::{BackendError, CommitId, ObjectId}; use crate::commit::Commit; use crate::dag_walk; use crate::index::Index; +use crate::merged_tree::MergedTree; use crate::op_store::RefTarget; use crate::repo::{MutableRepo, Repo}; use crate::repo_path::RepoPath; @@ -34,8 +35,15 @@ use crate::tree::{merge_trees, Tree, TreeMergeError}; use crate::view::RefName; #[instrument(skip(repo))] -pub fn merge_commit_trees(repo: &dyn Repo, commits: &[Commit]) -> Result { - merge_commit_trees_without_repo(repo.store(), repo.index(), commits) +pub fn merge_commit_trees( + repo: &dyn Repo, + commits: &[Commit], +) -> Result { + Ok(MergedTree::legacy(merge_commit_trees_without_repo( + repo.store(), + repo.index(), + commits, + )?)) } #[instrument(skip(index))] @@ -82,12 +90,13 @@ pub fn rebase_commit( .collect_vec(); let new_tree_id = if new_parent_trees == old_parent_trees { // Optimization - old_commit.tree_id().clone() + old_commit.merged_tree_id().clone() } else { let old_base_tree = merge_commit_trees(mut_repo, &old_parents)?; let new_base_tree = merge_commit_trees(mut_repo, new_parents)?; - let merged_tree = merge_trees(&new_base_tree, &old_base_tree, &old_commit.tree())?; - merged_tree.id().clone() + let old_tree = old_commit.merged_tree()?; + let merged_tree = new_base_tree.merge(&old_base_tree, &old_tree)?; + merged_tree.id() }; let new_parent_ids = new_parents .iter() @@ -96,7 +105,7 @@ pub fn rebase_commit( Ok(mut_repo .rewrite_commit(settings, old_commit) .set_parents(new_parent_ids) - .set_tree(new_tree_id) + .set_tree_id(new_tree_id) .write()?) } @@ -108,14 +117,15 @@ pub fn back_out_commit( ) -> Result { let old_base_tree = merge_commit_trees(mut_repo, &old_commit.parents())?; let new_base_tree = merge_commit_trees(mut_repo, new_parents)?; - let new_tree = merge_trees(&new_base_tree, &old_commit.tree(), &old_base_tree)?; + let old_tree = old_commit.merged_tree()?; + let new_tree = new_base_tree.merge(&old_tree, &old_base_tree)?; let new_parent_ids = new_parents .iter() .map(|commit| commit.id().clone()) .collect(); // TODO: i18n the description based on repo language Ok(mut_repo - .new_commit(settings, new_parent_ids, new_tree.legacy_id()) + .new_commit(settings, new_parent_ids, new_tree.id()) .set_description(format!("backout of commit {}", &old_commit.id().hex())) .write()?) }