cli: extract function for showing diff style based on args and config

I'm about to add a `jj show` command (like `git show`), and that'll
have the same arguments and config for deciding which style of diff to
show.
This commit is contained in:
Martin von Zweigbergk 2021-12-17 11:18:53 -08:00
parent 277f42d98a
commit 8d3ad34009

View File

@ -1744,6 +1744,18 @@ fn cmd_diff(ui: &mut Ui, command: &CommandHelper, args: &ArgMatches) -> Result<(
let repo = workspace_command.repo(); let repo = workspace_command.repo();
let workspace_root = workspace_command.workspace_root(); let workspace_root = workspace_command.workspace_root();
let matcher = matcher_from_values(ui, workspace_root, args.values_of("paths"))?; let matcher = matcher_from_values(ui, workspace_root, args.values_of("paths"))?;
let diff_iterator = from_tree.diff(&to_tree, matcher.as_ref());
show_diff(ui, repo, workspace_root, args, diff_iterator)?;
Ok(())
}
fn show_diff(
ui: &mut Ui,
repo: &Arc<ReadonlyRepo>,
workspace_root: &Path,
args: &ArgMatches,
tree_diff: TreeDiffIterator,
) -> Result<(), CommandError> {
enum Format { enum Format {
Summary, Summary,
Git, Git,
@ -1765,21 +1777,19 @@ fn cmd_diff(ui: &mut Ui, command: &CommandHelper, args: &ArgMatches) -> Result<(
} }
} }
}; };
let diff_iterator = from_tree.diff(&to_tree, matcher.as_ref());
match format { match format {
Format::Summary => { Format::Summary => {
show_diff_summary(ui, workspace_root, diff_iterator)?; show_diff_summary(ui, workspace_root, tree_diff)?;
} }
Format::Git => { Format::Git => {
show_git_diff(ui, repo, diff_iterator)?; show_git_diff(ui, repo, tree_diff)?;
} }
Format::ColorWords => { Format::ColorWords => {
show_color_words_diff(ui, workspace_root, repo, diff_iterator)?; show_color_words_diff(ui, workspace_root, repo, tree_diff)?;
} }
} }
Ok(()) Ok(())
} }
fn diff_content( fn diff_content(
repo: &Arc<ReadonlyRepo>, repo: &Arc<ReadonlyRepo>,
path: &RepoPath, path: &RepoPath,