ok/jj
1
0
Fork 0
forked from mirrors/jj

templates: replace empty name and email strings with placeholders

New placeholders say "(no name availalbe)" and "(no email available)",
because empty strings aren't _necessarily_ a configuration issue.
This commit is contained in:
Emily Fox 2023-08-18 13:46:22 -05:00 committed by Emily Kyle Fox
parent 3f8ac2198d
commit 2062abdc9d
4 changed files with 45 additions and 11 deletions

View file

@ -382,20 +382,32 @@ fn build_signature_method<'a, L: TemplateLanguage<'a>>(
"name" => { "name" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_string(TemplateFunction::new(self_property, |signature| { language.wrap_string(TemplateFunction::new(self_property, |signature| {
if !signature.name.is_empty() {
signature.name signature.name
} else {
"(no name available)".to_string()
}
})) }))
} }
"email" => { "email" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_string(TemplateFunction::new(self_property, |signature| { language.wrap_string(TemplateFunction::new(self_property, |signature| {
if !signature.email.is_empty() {
signature.email signature.email
} else {
"(no email available)".to_string()
}
})) }))
} }
"username" => { "username" => {
template_parser::expect_no_arguments(function)?; template_parser::expect_no_arguments(function)?;
language.wrap_string(TemplateFunction::new(self_property, |signature| { language.wrap_string(TemplateFunction::new(self_property, |signature| {
if !signature.email.is_empty() {
let (username, _) = text_util::split_email(&signature.email); let (username, _) = text_util::split_email(&signature.email);
username.to_owned() username.to_owned()
} else {
"(no username available)".to_string()
}
})) }))
} }
"timestamp" => { "timestamp" => {

View file

@ -57,9 +57,17 @@ impl<C, T: Template<C> + ?Sized> Template<C> for Box<T> {
impl Template<()> for Signature { impl Template<()> for Signature {
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
if !self.name.is_empty() {
write!(formatter.labeled("name"), "{}", self.name)?; write!(formatter.labeled("name"), "{}", self.name)?;
} else {
write!(formatter.labeled("name"), "(no name available)")?;
}
write!(formatter, " <")?; write!(formatter, " <")?;
if !self.email.is_empty() {
write!(formatter.labeled("email"), "{}", self.email)?; write!(formatter.labeled("email"), "{}", self.email)?;
} else {
write!(formatter.labeled("email"), "(no email available)")?;
}
write!(formatter, ">")?; write!(formatter, ">")?;
Ok(()) Ok(())
} }

View file

@ -187,8 +187,8 @@ fn test_log_builtin_templates() {
Commit ID: 0000000000000000000000000000000000000000 Commit ID: 0000000000000000000000000000000000000000
Change ID: zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz Change ID: zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
Author: <> (1970-01-01 00:00:00.000 +00:00) Author: (no name available) <(no email available)> (1970-01-01 00:00:00.000 +00:00)
Committer: <> (1970-01-01 00:00:00.000 +00:00) Committer: (no name available) <(no email available)> (1970-01-01 00:00:00.000 +00:00)
(no description set) (no description set)

View file

@ -418,6 +418,20 @@ fn test_templater_signature() {
insta::assert_snapshot!(render(r#"author"#), @"Test User <x@y>"); insta::assert_snapshot!(render(r#"author"#), @"Test User <x@y>");
insta::assert_snapshot!(render(r#"author.email()"#), @"x@y"); insta::assert_snapshot!(render(r#"author.email()"#), @"x@y");
insta::assert_snapshot!(render(r#"author.username()"#), @"x"); insta::assert_snapshot!(render(r#"author.username()"#), @"x");
test_env.jj_cmd_ok(&repo_path, &["--config-toml=user.name=''", "new"]);
insta::assert_snapshot!(render(r#"author"#), @"(no name available) <test.user@example.com>");
insta::assert_snapshot!(render(r#"author.name()"#), @"(no name available)");
insta::assert_snapshot!(render(r#"author.email()"#), @"test.user@example.com");
insta::assert_snapshot!(render(r#"author.username()"#), @"test.user");
test_env.jj_cmd_ok(&repo_path, &["--config-toml=user.email=''", "new"]);
insta::assert_snapshot!(render(r#"author"#), @"Test User <(no email available)>");
insta::assert_snapshot!(render(r#"author.name()"#), @"Test User");
insta::assert_snapshot!(render(r#"author.email()"#), @"(no email available)");
insta::assert_snapshot!(render(r#"author.username()"#), @"(no username available)");
} }
#[test] #[test]
@ -601,7 +615,7 @@ fn test_templater_concat_function() {
let render = |template| get_colored_template_output(&test_env, &repo_path, "@-", template); let render = |template| get_colored_template_output(&test_env, &repo_path, "@-", template);
insta::assert_snapshot!(render(r#"concat()"#), @""); insta::assert_snapshot!(render(r#"concat()"#), @"");
insta::assert_snapshot!(render(r#"concat(author, empty)"#), @" <>true"); insta::assert_snapshot!(render(r#"concat(author, empty)"#), @"(no name available) <(no email available)>true");
insta::assert_snapshot!( insta::assert_snapshot!(
render(r#"concat(label("error", ""), label("warning", "a"), "b")"#), render(r#"concat(label("error", ""), label("warning", "a"), "b")"#),
@"ab"); @"ab");
@ -651,11 +665,11 @@ fn test_templater_separate_function() {
// Separate keywords // Separate keywords
insta::assert_snapshot!( insta::assert_snapshot!(
render(r#"separate(" ", author, description, empty)"#), @" <> true"); render(r#"separate(" ", author, description, empty)"#), @"(no name available) <(no email available)> true");
// Keyword as separator // Keyword as separator
insta::assert_snapshot!( insta::assert_snapshot!(
render(r#"separate(author, "X", "Y", "Z")"#), @"X <>Y <>Z"); render(r#"separate(author, "X", "Y", "Z")"#), @"X(no name available) <(no email available)>Y(no name available) <(no email available)>Z");
} }
#[test] #[test]