Skip to content

Commit

Permalink
cli: don't ignore 'diff --tool=:builtin', report error
Browse files Browse the repository at this point in the history
Before, --tool=:builtin argument was ignored and the tool was loaded from
"ui.diff.tool" option. Since there is no single builtin diff format, :builtin
doesn't make sense here. Maybe we can translate ":<format>" to the internal
diff format instead, but that will also mean "ui.diff.tool" and ".format" can
be merged.

This partially reverts 409356f "merge_tools: enable `:builtin` as default
diff/merge editor."
  • Loading branch information
yuja committed Feb 29, 2024
1 parent 3575c9f commit 96ccb87
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
24 changes: 7 additions & 17 deletions cli/src/diff_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use unicode_width::UnicodeWidthStr as _;
use crate::cli_util::{CommandError, WorkspaceCommandHelper};
use crate::config::CommandNameAndArgs;
use crate::formatter::Formatter;
use crate::merge_tools::{self, ExternalMergeTool, MergeTool};
use crate::merge_tools::{self, ExternalMergeTool};
use crate::text_util;
use crate::ui::Ui;

Expand Down Expand Up @@ -129,14 +129,9 @@ fn diff_formats_from_args(
.filter_map(|(arg, format)| arg.then_some(format))
.collect_vec();
if let Some(name) = &args.tool {
let tool = merge_tools::get_tool_config(settings, name)?
.unwrap_or_else(|| MergeTool::External(ExternalMergeTool::with_program(name)));
match tool {
MergeTool::Builtin => {}
MergeTool::External(tool) => {
formats.push(DiffFormat::Tool(Box::new(tool)));
}
}
let tool = merge_tools::get_external_tool_config(settings, name)?
.unwrap_or_else(|| ExternalMergeTool::with_program(name));
formats.push(DiffFormat::Tool(Box::new(tool)));
}
Ok(formats)
}
Expand All @@ -146,17 +141,12 @@ fn default_diff_format(settings: &UserSettings) -> Result<DiffFormat, config::Co
if let Some(args) = config.get("ui.diff.tool").optional()? {
// External "tool" overrides the internal "format" option.
let tool = if let CommandNameAndArgs::String(name) = &args {
merge_tools::get_tool_config(settings, name)?
merge_tools::get_external_tool_config(settings, name)?
} else {
None
}
.unwrap_or_else(|| MergeTool::External(ExternalMergeTool::with_diff_args(&args)));
match tool {
MergeTool::Builtin => {}
MergeTool::External(tool) => {
return Ok(DiffFormat::Tool(Box::new(tool)));
}
}
.unwrap_or_else(|| ExternalMergeTool::with_diff_args(&args));
return Ok(DiffFormat::Tool(Box::new(tool)));
}
let name = if let Some(name) = config.get_string("ui.diff.format").optional()? {
name
Expand Down
5 changes: 1 addition & 4 deletions cli/src/merge_tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,7 @@ fn editor_args_from_settings(

/// Resolves builtin merge tool name or loads external tool options from
/// `[merge-tools.<name>]`.
pub fn get_tool_config(
settings: &UserSettings,
name: &str,
) -> Result<Option<MergeTool>, ConfigError> {
fn get_tool_config(settings: &UserSettings, name: &str) -> Result<Option<MergeTool>, ConfigError> {
if name == BUILTIN_EDITOR_NAME {
Ok(Some(MergeTool::Builtin))
} else {
Expand Down
10 changes: 9 additions & 1 deletion cli/tests/test_diff_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use itertools::Itertools;

use crate::common::{escaped_fake_diff_editor_path, TestEnvironment};
use crate::common::{escaped_fake_diff_editor_path, strip_last_line, TestEnvironment};

#[test]
fn test_diff_basic() {
Expand Down Expand Up @@ -753,6 +753,14 @@ fn test_diff_external_tool() {
insta::assert_snapshot!(stderr, @r###"
Tool exited with a non-zero code (run with --debug to see the exact invocation). Exit code: 1.
"###);

// --tool=:builtin shouldn't be ignored
let stderr = test_env.jj_cmd_failure(&repo_path, &["diff", "--tool=:builtin"]);
insta::assert_snapshot!(strip_last_line(&stderr), @r###"
Error: Failed to generate diff
Caused by:
1: Error executing ':builtin' (run with --debug to see the exact invocation)
"###);
}

#[cfg(unix)]
Expand Down

0 comments on commit 96ccb87

Please sign in to comment.