diff: extract inter-diff helper

This isn't as primitive as show_diff() or show_patch(), but there are three
callers. Let's add a helper method to remove repo argument.
This commit is contained in:
Yuya Nishihara 2024-09-05 11:59:42 +09:00
parent f473d7bb7e
commit 03487af192
4 changed files with 39 additions and 41 deletions

View file

@ -16,8 +16,6 @@ use itertools::Itertools;
use jj_lib::commit::Commit;
use jj_lib::dag_walk::topo_order_reverse_ok;
use jj_lib::matchers::EverythingMatcher;
use jj_lib::repo::Repo;
use jj_lib::rewrite::rebase_to_dest_parent;
use tracing::instrument;
use super::log::get_node_template;
@ -80,7 +78,6 @@ pub(crate) fn cmd_evolog(
args: &EvologArgs,
) -> Result<(), CommandError> {
let workspace_command = command.workspace_helper(ui)?;
let repo = workspace_command.repo().as_ref();
let start_commit = workspace_command.resolve_single_rev(&args.revision)?;
@ -164,7 +161,7 @@ pub(crate) fn cmd_evolog(
if let Some(renderer) = &diff_renderer {
let mut formatter = ui.new_formatter(&mut buffer);
let width = usize::saturating_sub(ui.term_width(), graph_width());
show_predecessor_patch(ui, repo, renderer, formatter.as_mut(), &commit, width)?;
show_predecessor_patch(ui, renderer, formatter.as_mut(), &commit, width)?;
}
let node_symbol = format_template(ui, &Some(commit.clone()), &node_template);
graph.add_node(
@ -180,7 +177,7 @@ pub(crate) fn cmd_evolog(
.write(formatter, |formatter| template.format(&commit, formatter))?;
if let Some(renderer) = &diff_renderer {
let width = ui.term_width();
show_predecessor_patch(ui, repo, renderer, formatter, &commit, width)?;
show_predecessor_patch(ui, renderer, formatter, &commit, width)?;
}
}
}
@ -190,22 +187,18 @@ pub(crate) fn cmd_evolog(
fn show_predecessor_patch(
ui: &Ui,
repo: &dyn Repo,
renderer: &DiffRenderer,
formatter: &mut dyn Formatter,
commit: &Commit,
width: usize,
) -> Result<(), CommandError> {
let predecessors: Vec<_> = commit.predecessors().try_collect()?;
let predecessor_tree = rebase_to_dest_parent(repo, &predecessors, commit)?;
let tree = commit.tree()?;
renderer.show_diff(
renderer.show_inter_diff(
ui,
formatter,
&predecessor_tree,
&tree,
&predecessors,
&commit,
&EverythingMatcher,
&Default::default(),
width,
)?;
Ok(())

View file

@ -15,7 +15,6 @@
use std::slice;
use clap::ArgGroup;
use jj_lib::rewrite::rebase_to_dest_parent;
use tracing::instrument;
use crate::cli_util::CommandHelper;
@ -55,25 +54,17 @@ pub(crate) fn cmd_interdiff(
let from =
workspace_command.resolve_single_rev(args.from.as_ref().unwrap_or(&RevisionArg::AT))?;
let to = workspace_command.resolve_single_rev(args.to.as_ref().unwrap_or(&RevisionArg::AT))?;
let from_tree = rebase_to_dest_parent(
workspace_command.repo().as_ref(),
slice::from_ref(&from),
&to,
)?;
let to_tree = to.tree()?;
let matcher = workspace_command
.parse_file_patterns(&args.paths)?
.to_matcher();
let diff_renderer = workspace_command.diff_renderer_for(&args.format)?;
ui.request_pager();
diff_renderer.show_diff(
diff_renderer.show_inter_diff(
ui,
ui.stdout_formatter().as_mut(),
&from_tree,
&to_tree,
slice::from_ref(&from),
&to,
matcher.as_ref(),
&Default::default(),
ui.term_width(),
)?;
Ok(())

View file

@ -34,7 +34,6 @@ use jj_lib::repo::ReadonlyRepo;
use jj_lib::repo::Repo;
use jj_lib::revset;
use jj_lib::revset::RevsetIteratorExt as _;
use jj_lib::rewrite::rebase_to_dest_parent;
use crate::cli_util::short_change_hash;
use crate::cli_util::short_operation_hash;
@ -260,7 +259,6 @@ pub fn show_op_diff(
show_change_diff(
ui,
formatter.as_mut(),
current_repo,
diff_renderer,
modified_change,
width,
@ -287,14 +285,7 @@ pub fn show_op_diff(
)?;
if let Some(diff_renderer) = &diff_renderer {
let width = ui.term_width();
show_change_diff(
ui,
formatter,
current_repo,
diff_renderer,
modified_change,
width,
)?;
show_change_diff(ui, formatter, diff_renderer, modified_change, width)?;
}
}
}
@ -563,7 +554,6 @@ fn compute_operation_commits_diff(
fn show_change_diff(
ui: &Ui,
formatter: &mut dyn Formatter,
repo: &dyn Repo,
diff_renderer: &DiffRenderer,
change: &ModifiedChange,
width: usize,
@ -572,15 +562,12 @@ fn show_change_diff(
(predecessors @ ([] | [_]), [commit]) => {
// New or modified change. If the modification involved a rebase,
// show diffs from the rebased tree.
let predecessor_tree = rebase_to_dest_parent(repo, predecessors, commit)?;
let tree = commit.tree()?;
diff_renderer.show_diff(
diff_renderer.show_inter_diff(
ui,
formatter,
&predecessor_tree,
&tree,
predecessors,
commit,
&EverythingMatcher,
&Default::default(),
width,
)?;
}

View file

@ -51,6 +51,7 @@ use jj_lib::object_id::ObjectId;
use jj_lib::repo::Repo;
use jj_lib::repo_path::RepoPath;
use jj_lib::repo_path::RepoPathUiConverter;
use jj_lib::rewrite::rebase_to_dest_parent;
use jj_lib::settings::ConfigResultExt as _;
use jj_lib::settings::UserSettings;
use jj_lib::store::Store;
@ -357,6 +358,32 @@ impl<'a> DiffRenderer<'a> {
Ok(())
}
/// Generates diff between `from_commits` and `to_commit` based off their
/// parents. The `from_commits` will temporarily be rebased onto the
/// `to_commit` parents to exclude unrelated changes.
pub fn show_inter_diff(
&self,
ui: &Ui,
formatter: &mut dyn Formatter,
from_commits: &[Commit],
to_commit: &Commit,
matcher: &dyn Matcher,
width: usize,
) -> Result<(), DiffRenderError> {
let from_tree = rebase_to_dest_parent(self.repo, from_commits, to_commit)?;
let to_tree = to_commit.tree()?;
let copy_records = CopyRecords::default(); // TODO
self.show_diff(
ui,
formatter,
&from_tree,
&to_tree,
matcher,
&copy_records,
width,
)
}
/// Generates diff of the given `commit` compared to its parents.
pub fn show_patch(
&self,