diff --git a/src/commit_templater.rs b/src/commit_templater.rs index 7dc1374a1..507024948 100644 --- a/src/commit_templater.rs +++ b/src/commit_templater.rs @@ -382,6 +382,21 @@ impl Template<()> for ShortestIdPrefix { } } +impl ShortestIdPrefix { + fn to_upper(&self) -> Self { + Self { + prefix: self.prefix.to_ascii_uppercase(), + rest: self.rest.to_ascii_uppercase(), + } + } + fn to_lower(&self) -> Self { + Self { + prefix: self.prefix.to_ascii_lowercase(), + rest: self.rest.to_ascii_lowercase(), + } + } +} + fn build_shortest_id_prefix_method<'repo>( language: &CommitTemplateLanguage<'repo, '_>, self_property: impl TemplateProperty + 'repo, @@ -402,6 +417,20 @@ fn build_shortest_id_prefix_method<'repo>( TemplatePropertyFn(|id: &ShortestIdPrefix| id.rest.clone()), )) } + "upper" => { + template_parser::expect_no_arguments(function)?; + language.wrap_shortest_id_prefix(template_parser::chain_properties( + self_property, + TemplatePropertyFn(|id: &ShortestIdPrefix| id.to_upper()), + )) + } + "lower" => { + template_parser::expect_no_arguments(function)?; + language.wrap_shortest_id_prefix(template_parser::chain_properties( + self_property, + TemplatePropertyFn(|id: &ShortestIdPrefix| id.to_lower()), + )) + } _ => { return Err(TemplateParseError::no_such_method( "ShortestIdPrefix", diff --git a/src/template_parser.rs b/src/template_parser.rs index ad032e6e4..e7e752fad 100644 --- a/src/template_parser.rs +++ b/src/template_parser.rs @@ -906,6 +906,20 @@ fn build_string_method<'a, L: TemplateLanguage<'a>>( TemplatePropertyFn(|s: &String| s.lines().next().unwrap_or_default().to_string()), )) } + "upper" => { + expect_no_arguments(function)?; + language.wrap_string(chain_properties( + self_property, + TemplatePropertyFn(|s: &String| s.to_uppercase()), + )) + } + "lower" => { + expect_no_arguments(function)?; + language.wrap_string(chain_properties( + self_property, + TemplatePropertyFn(|s: &String| s.to_lowercase()), + )) + } _ => return Err(TemplateParseError::no_such_method("String", function)), }; Ok(property) diff --git a/tests/test_templater.rs b/tests/test_templater.rs index 93f7e739f..09143ad69 100644 --- a/tests/test_templater.rs +++ b/tests/test_templater.rs @@ -388,6 +388,20 @@ fn test_templater_separate_function() { render(r#"separate(author, "X", "Y", "Z")"#), @"X <>Y <>Z"); } +#[test] +fn test_templater_upper_lower() { + 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| get_colored_template_output(&test_env, &repo_path, "@-", template); + + insta::assert_snapshot!( + render(r#"change_id.shortest(4).upper() change_id.shortest(4).upper().lower()"#), + @"ZZZZzzzz"); + insta::assert_snapshot!( + render(r#""Hello".upper() "Hello".lower()"#), @"HELLOhello"); +} + #[test] fn test_templater_alias() { let test_env = TestEnvironment::default();