Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {
yuja marked this conversation as resolved.
Show resolved Hide resolved
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