Skip to content

Commit f1ef217

Browse files
committed
cli: introduce 'ui.diff-viewer' setting
'ui.diff-viewer' combines 'ui.diff.tool' and 'ui.diff.format' into a single setting, which in turn allows repository level configuration to override global settings (see jj-vcs#3327).
1 parent 575bcbf commit f1ef217

File tree

4 files changed

+95
-12
lines changed

4 files changed

+95
-12
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Deprecations
1111

12+
* `ui.diff.format` and `ui.diff.tool` were deprecated in favor of `ui.diff-formatter`
13+
1214
* `jj move` was deprecated in favor of `jj squash`.
1315

1416
### Breaking changes
@@ -18,6 +20,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1820

1921
### New features
2022

23+
* Add `ui.diff-formatter` combining the both `ui.diff.format` and `ui.diff.tool` into a single setting. Built-in formats previously set via `ui.diff.format` can now be specified prefixed with `:`. Having a single option allows a repository level built-in format to override a global tool format.
24+
25+
* `"ui.diff-formatter" = ":color-words"`
26+
* `"ui.diff-formatter" = ["difft", "--color=always", "$left", "$right"]`
27+
2128
* Config now supports rgb hex colors (in the form `#rrggbb`) wherever existing color names are supported.
2229

2330
* `ui.default-command` now accepts multiple string arguments, for more complex

cli/tests/test_diff_command.rs

+79-2
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,83 @@ fn test_diff_skipped_context_nondefault() {
740740
"###);
741741
}
742742

743+
#[test]
744+
fn test_diff_formatter_setting() {
745+
let mut test_env = TestEnvironment::default();
746+
test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
747+
let repo_path = test_env.env_root().join("repo");
748+
749+
std::fs::write(repo_path.join("file1"), "foo\n").unwrap();
750+
751+
insta::assert_snapshot!(
752+
test_env.jj_cmd_success(&repo_path, &["diff", "--config-toml=ui.diff-viewer=':summary'"]), @r###"
753+
A file1
754+
"###);
755+
756+
insta::assert_snapshot!(
757+
test_env.jj_cmd_success(&repo_path, &["diff", "--config-toml=ui.diff-viewer=':types'"]), @r###"
758+
-F file1
759+
"###);
760+
761+
insta::assert_snapshot!(
762+
test_env.jj_cmd_success(&repo_path, &["diff", "--config-toml=ui.diff-viewer=':git'"]), @r###"
763+
diff --git a/file1 b/file1
764+
new file mode 100644
765+
index 0000000000..257cc5642c
766+
--- /dev/null
767+
+++ b/file1
768+
@@ -1,0 +1,1 @@
769+
+foo
770+
"###);
771+
772+
insta::assert_snapshot!(
773+
test_env.jj_cmd_success(&repo_path, &["diff", "--config-toml=ui.diff-viewer=':color-words'"]), @r###"
774+
Added regular file file1:
775+
1: foo
776+
"###);
777+
778+
insta::assert_snapshot!(
779+
test_env.jj_cmd_success(&repo_path, &["diff", "--config-toml=ui.diff-viewer=':stat'"]), @r###"
780+
file1 | 1 +
781+
1 file changed, 1 insertion(+), 0 deletions(-)
782+
"###);
783+
784+
insta::assert_snapshot!(
785+
test_env.jj_cmd_failure(&repo_path, &["diff", "--config-toml=ui.diff-viewer=':unknown'"]), @r###"
786+
Config error: Unknown format setting for 'ui.diff-viewer', built-in formats are ':summary', ':types', ':git', ':color-words', and ':stat', or use an external tool
787+
For help, see https://github.com/martinvonz/jj/blob/main/docs/config.md.
788+
"###);
789+
790+
let edit_script = test_env.set_up_fake_diff_editor();
791+
std::fs::write(
792+
edit_script,
793+
"print-files-before\0print --\0print-files-after",
794+
)
795+
.unwrap();
796+
797+
let command = escaped_fake_diff_editor_path();
798+
799+
insta::assert_snapshot!(
800+
test_env.jj_cmd_success(&repo_path, &["diff", "--config-toml=ui.diff-viewer='fake-diff-editor'"]), @r###"
801+
--
802+
file1
803+
"###);
804+
805+
insta::assert_snapshot!(
806+
test_env.jj_cmd_success(&repo_path, &["diff", &format!(r#"--config-toml=ui.diff-viewer=["{command}"]"#)]), @r###"
807+
--
808+
file1
809+
"###);
810+
811+
insta::assert_snapshot!(
812+
test_env.jj_cmd_failure(&repo_path, &["diff", r#"--config-toml=ui.diff-viewer=["unknown-diff-binary"]"#]), @r###"
813+
Error: Failed to generate diff
814+
Caused by:
815+
1: Error executing 'unknown-diff-binary' (run with --debug to see the exact invocation)
816+
2: No such file or directory (os error 2)
817+
"###);
818+
}
819+
743820
#[test]
744821
fn test_diff_external_tool() {
745822
let mut test_env = TestEnvironment::default();
@@ -812,7 +889,7 @@ fn test_diff_external_tool() {
812889
"###);
813890

814891
// Enabled by default, looks up the merge-tools table
815-
let config = "--config-toml=ui.diff.tool='fake-diff-editor'";
892+
let config = "--config-toml=ui.diff-viewer='fake-diff-editor'";
816893
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff", config]), @r###"
817894
file1
818895
file2
@@ -823,7 +900,7 @@ fn test_diff_external_tool() {
823900

824901
// Inlined command arguments
825902
let command = escaped_fake_diff_editor_path();
826-
let config = format!(r#"--config-toml=ui.diff.tool=["{command}", "$right", "$left"]"#);
903+
let config = format!(r#"--config-toml=ui.diff-viewer=["{command}", "$right", "$left"]"#);
827904
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff", &config]), @r###"
828905
file2
829906
file3

cli/tests/test_log_command.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn test_log_with_or_without_diff() {
138138
"description",
139139
"-p",
140140
"-s",
141-
"--config-toml=ui.diff.format='summary'",
141+
"--config-toml=ui.diff-viewer=':summary'",
142142
],
143143
);
144144
insta::assert_snapshot!(stdout, @r###"

docs/config.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -165,24 +165,23 @@ useful reminder to fill in things like BUG=, TESTED= etc.
165165
ui.default-description = "\n\nTESTED=TODO"
166166
```
167167

168-
### Diff format
168+
### Generating diffs with built-in and external formatters
169+
170+
Different diff formatters can be chosen with the `ui.diff-viewer` setting. `jj` provides three built-in formatters, `:color-words` (the default), `:git`, and `:summary`.
169171

170172
```toml
171-
# Possible values: "color-words" (default), "git", "summary"
172-
ui.diff.format = "git"
173+
[ui]
174+
diff-viewer = ":git"
173175
```
174176

175-
### Generating diffs by external command
176-
177-
If `ui.diff.tool` is set, the specified diff command will be called instead of
178-
the internal diff function.
177+
An external diff command can be used instead of a built-in formatter, specifying the command and its arguments.
179178

180179
```toml
181180
[ui]
182181
# Use Difftastic by default
183-
diff.tool = ["difft", "--color=always", "$left", "$right"]
182+
diff-viewer = ["difft", "--color=always", "$left", "$right"]
184183
# Use tool named "<name>" (see below)
185-
diff.tool = "<name>"
184+
diff-viewer = "<name>"
186185
```
187186

188187
The external diff tool can also be enabled by `diff --tool <name>` argument.

0 commit comments

Comments
 (0)