From 5fb17925eb866ef64f46446c1bbed2b57180fbe3 Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Sun, 5 Feb 2023 16:10:25 -0800 Subject: [PATCH] `jj log`: option to specify preferred id length The new option is `ui.log-id-preferred-length`. Setting it to 6 is quite convenient for the `jj` repo, for example. Screenshot: https://user-images.githubusercontent.com/4123047/216535699-ad1e2ac8-73dd-44be-b28a-ebdebc00c63c.png --- CHANGELOG.md | 3 ++ docs/config.md | 14 +++++++- lib/src/settings.rs | 7 ++++ src/commands/mod.rs | 7 ++-- src/config-schema.json | 6 +++- tests/test_commit_template.rs | 61 +++++++++++++++++++++++++++++++++-- 6 files changed, 90 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cf93fc0d..01818cd3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -95,6 +95,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 option to `none`. For a presentation that doesn't rely on color, set it to `brackets`. +* It is now possible to change the lengths of the ids `jj log` prints with the + new `ui.log-id-preferred-length` option. + * `jj print` was renamed to `jj cat`. `jj print` remains as an alias. * In content that goes to the terminal, the ANSI escape byte (0x1b) is replaced diff --git a/docs/config.md b/docs/config.md index 4b210edbc..da7a72818 100644 --- a/docs/config.md +++ b/docs/config.md @@ -105,7 +105,8 @@ for some examples of what's possible. ui.graph.style = "curved" ``` -### Shortest unique prefixes for ids +### Display of commit and change ids + ```toml ui.unique-prefixes = "brackets" # Does not rely on color ``` @@ -113,6 +114,17 @@ ui.unique-prefixes = "brackets" # Does not rely on color Whether to highlight a unique prefix for commit & change ids. Possible values are `styled`, `brackets` and `none` (default: `styled`). +```toml +ui.log-id-preferred-length = 6 +``` + +Determines the number of characters displayed for `jj log` for change or commit +ids. The default is 12. If the `ui.unique-prefixes` option is not set to `none`, +this option will be ignored if the number of characters it specifies is +insufficient to print the entire unique prefix of an id. + +This option can be convenient to set on a per-repository level. + ### Relative timestamps ```toml diff --git a/lib/src/settings.rs b/lib/src/settings.rs index 9734a7f96..b57e0a94a 100644 --- a/lib/src/settings.rs +++ b/lib/src/settings.rs @@ -170,6 +170,13 @@ impl UserSettings { .unwrap_or_else(|_| "styled".to_string()) } + pub fn log_id_preferred_length(&self) -> Option { + self.config + .get_int("ui.log-id-preferred-length") + .ok() + .and_then(|l| l.try_into().ok()) + } + pub fn config(&self) -> &config::Config { &self.config } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 4d767d3a2..5e029c286 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1413,12 +1413,13 @@ fn log_template(settings: &UserSettings) -> String { } else { "committer.timestamp()" }; + let desired_id_len = settings.log_id_preferred_length().unwrap_or(12); // TODO: If/when this logic is relevant in the `lib` crate, make this into // and enum similar to `ColorChoice`. let prefix_format = match settings.unique_prefixes().as_str() { - "brackets" => "shortest_prefix_and_brackets()", - "styled" => "shortest_styled_prefix()", - _ => "short()", + "brackets" => format!("shortest_prefix_and_brackets({desired_id_len})"), + "styled" => format!("shortest_styled_prefix({desired_id_len})"), + _ => format!("short({desired_id_len})"), }; let default_template = format!( r#" diff --git a/src/config-schema.json b/src/config-schema.json index eb5612379..e5f5d92a5 100644 --- a/src/config-schema.json +++ b/src/config-schema.json @@ -86,7 +86,11 @@ "enum": ["none", "brackets", "styled"], "description": "How formatter indicates the unique prefix part of a revision or change ID", "default": "styled" - + }, + "log-id-preferred-length": { + "type": "integer", + "description": "Determines the number of characters displayed for `jj log` for change or commit ids.", + "default": 12 }, "editor": { "type": "string", diff --git a/tests/test_commit_template.rs b/tests/test_commit_template.rs index 14582d1d4..ffd93f5b2 100644 --- a/tests/test_commit_template.rs +++ b/tests/test_commit_template.rs @@ -79,11 +79,29 @@ fn test_log_default() { &["log", "--config-toml", "ui.unique-prefixes='brackets'"], ); insta::assert_snapshot!(stdout, @r###" - @ f[fdaa62087] test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9d[e54178d5] + @ f[fdaa62087a2] test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9d[e54178d59d] | (empty) description 1 - o 9a[45c67d3e] test.user@example.com 2001-02-03 04:05:08.000 +07:00 4[291e264ae] + o 9a[45c67d3e96] test.user@example.com 2001-02-03 04:05:08.000 +07:00 4[291e264ae97] | add a file - o 0[000000000] 1970-01-01 00:00:00.000 +00:00 0[000000000] + o 0[00000000000] 1970-01-01 00:00:00.000 +00:00 0[00000000000] + (empty) (no description set) + "###); + let stdout = test_env.jj_cmd_success( + &repo_path, + &[ + "log", + "--config-toml", + "ui.unique-prefixes='brackets'", + "--config-toml", + "ui.log-id-preferred-length=2", + ], + ); + insta::assert_snapshot!(stdout, @r###" + @ f[f] test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9d + | (empty) description 1 + o 9a test.user@example.com 2001-02-03 04:05:08.000 +07:00 4[2] + | add a file + o 0[0] 1970-01-01 00:00:00.000 +00:00 0[0] (empty) (no description set) "###); @@ -105,6 +123,25 @@ fn test_log_default() { o 000000000000 1970-01-01 00:00:00.000 +00:00 000000000000 (empty) (no description set) "###); + let stdout = test_env.jj_cmd_success( + &repo_path, + &[ + "log", + "--color=always", + "--config-toml", + "ui.unique-prefixes='styled'", + "--config-toml", + "ui.log-id-preferred-length=1", + ], + ); + insta::assert_snapshot!(stdout, @r###" + @ f test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9d + | (empty) description 1 + o 9a test.user@example.com 2001-02-03 04:05:08.000 +07:00 4 + | add a file + o 0 1970-01-01 00:00:00.000 +00:00 0 + (empty) (no description set) + "###); // Test default log output format with prefixes explicitly disabled let stdout = test_env.jj_cmd_success( @@ -119,6 +156,24 @@ fn test_log_default() { o 000000000000 1970-01-01 00:00:00.000 +00:00 000000000000 (empty) (no description set) "###); + let stdout = test_env.jj_cmd_success( + &repo_path, + &[ + "log", + "--config-toml", + "ui.unique-prefixes='none'", + "--config-toml", + "ui.log-id-preferred-length=1", + ], + ); + insta::assert_snapshot!(stdout, @r###" + @ f test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9 + | (empty) description 1 + o 9 test.user@example.com 2001-02-03 04:05:08.000 +07:00 4 + | add a file + o 0 1970-01-01 00:00:00.000 +00:00 0 + (empty) (no description set) + "###); // Color let stdout = test_env.jj_cmd_success(&repo_path, &["log", "--color=always"]);