mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-12 07:14:38 +00:00
templater: make .short(negative_len) return 0-length string
I think this is less surprising than falling back to the default length. i64-to-usize conversion can also overflow on 32 bit environment, but I'm not bothered to handle overflow scenario.
This commit is contained in:
parent
23509e939e
commit
3bf92a0914
4 changed files with 24 additions and 15 deletions
|
@ -480,7 +480,7 @@ fn build_commit_or_change_id_method<'repo>(
|
|||
let len_property = parse_optional_integer(function)?;
|
||||
language.wrap_string(TemplateFunction::new(
|
||||
(self_property, len_property),
|
||||
|(id, len)| id.short(len.and_then(|l| l.try_into().ok()).unwrap_or(12)),
|
||||
|(id, len)| id.short(len.map_or(12, |l| l.try_into().unwrap_or(0))),
|
||||
))
|
||||
}
|
||||
"shortest" => {
|
||||
|
|
|
@ -176,7 +176,7 @@ fn build_operation_id_method(
|
|||
(self_property, len_property),
|
||||
|(id, len)| {
|
||||
let mut hex = id.hex();
|
||||
hex.truncate(len.and_then(|l| l.try_into().ok()).unwrap_or(12));
|
||||
hex.truncate(len.map_or(12, |l| l.try_into().unwrap_or(0)));
|
||||
hex
|
||||
},
|
||||
))
|
||||
|
|
|
@ -622,30 +622,31 @@ fn test_log_prefix_highlight_counts_hidden_commits() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_log_shortest_length_parameter() {
|
||||
fn test_log_short_shortest_length_parameter() {
|
||||
let test_env = TestEnvironment::default();
|
||||
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
|
||||
let repo_path = test_env.env_root().join("repo");
|
||||
let render = |template| test_env.jj_cmd_success(&repo_path, &["log", "-T", template]);
|
||||
|
||||
insta::assert_snapshot!(
|
||||
test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id.shortest(0)"]), @r###"
|
||||
@ 2
|
||||
◉ 0
|
||||
render(r#"commit_id.short(0) ++ "|" ++ commit_id.shortest(0)"#), @r###"
|
||||
@ |2
|
||||
◉ |0
|
||||
"###);
|
||||
insta::assert_snapshot!(
|
||||
test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id.shortest(-0)"]), @r###"
|
||||
@ 2
|
||||
◉ 0
|
||||
render(r#"commit_id.short(-0) ++ "|" ++ commit_id.shortest(-0)"#), @r###"
|
||||
@ |2
|
||||
◉ |0
|
||||
"###);
|
||||
insta::assert_snapshot!(
|
||||
test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id.shortest(-100)"]), @r###"
|
||||
@ 2
|
||||
◉ 0
|
||||
render(r#"commit_id.short(-100) ++ "|" ++ commit_id.shortest(-100)"#), @r###"
|
||||
@ |2
|
||||
◉ |0
|
||||
"###);
|
||||
insta::assert_snapshot!(
|
||||
test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id.shortest(100)"]), @r###"
|
||||
@ 230dd059e1b059aefc0da06a2e5a7dbf22362f22
|
||||
◉ 0000000000000000000000000000000000000000
|
||||
render(r#"commit_id.short(100) ++ "|" ++ commit_id.shortest(100)"#), @r###"
|
||||
@ 230dd059e1b059aefc0da06a2e5a7dbf22362f22|230dd059e1b059aefc0da06a2e5a7dbf22362f22
|
||||
◉ 0000000000000000000000000000000000000000|0000000000000000000000000000000000000000
|
||||
"###);
|
||||
}
|
||||
|
||||
|
|
|
@ -147,6 +147,14 @@ fn test_op_log_template() {
|
|||
@ 19b80 true test-username@host.example.com 2001-02-03 04:05:07.000 +07:00 2001-02-03 04:05:07.000 +07:00 less than a microsecond
|
||||
◉ f1c46 false test-username@host.example.com 2001-02-03 04:05:07.000 +07:00 2001-02-03 04:05:07.000 +07:00 less than a microsecond
|
||||
"###);
|
||||
|
||||
// Negative length shouldn't cause panic (and is clamped.)
|
||||
// TODO: If we add runtime error, this will probably error out.
|
||||
insta::assert_snapshot!(render(r#"id.short(-1) ++ "|""#), @r###"
|
||||
@ |
|
||||
◉ |
|
||||
"###);
|
||||
|
||||
// Test the default template, i.e. with relative start time and duration. We
|
||||
// don't generally use that template because it depends on the current time,
|
||||
// so we need to reset the time range format here.
|
||||
|
|
Loading…
Reference in a new issue