cli: don't ignore 'diff --tool=:builtin', report error

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 409356fa5b "merge_tools: enable `:builtin` as default
diff/merge editor."
This commit is contained in:
Yuya Nishihara 2024-02-28 20:46:57 +09:00
parent 8148daa229
commit 9698a13747
3 changed files with 17 additions and 22 deletions

View file

@ -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;
@ -129,15 +129,10 @@ 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) => {
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)
}
@ -146,18 +141,13 @@ 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) => {
.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
} else if let Some(name) = config.get_string("diff.format").optional()? {

View file

@ -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 {

View file

@ -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() {
@ -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)]