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:
parent
3f8ac2198d
commit
2062abdc9d
4 changed files with 45 additions and 11 deletions
|
@ -382,20 +382,32 @@ fn build_signature_method<'a, L: TemplateLanguage<'a>>(
|
|||
"name" => {
|
||||
template_parser::expect_no_arguments(function)?;
|
||||
language.wrap_string(TemplateFunction::new(self_property, |signature| {
|
||||
signature.name
|
||||
if !signature.name.is_empty() {
|
||||
signature.name
|
||||
} else {
|
||||
"(no name available)".to_string()
|
||||
}
|
||||
}))
|
||||
}
|
||||
"email" => {
|
||||
template_parser::expect_no_arguments(function)?;
|
||||
language.wrap_string(TemplateFunction::new(self_property, |signature| {
|
||||
signature.email
|
||||
if !signature.email.is_empty() {
|
||||
signature.email
|
||||
} else {
|
||||
"(no email available)".to_string()
|
||||
}
|
||||
}))
|
||||
}
|
||||
"username" => {
|
||||
template_parser::expect_no_arguments(function)?;
|
||||
language.wrap_string(TemplateFunction::new(self_property, |signature| {
|
||||
let (username, _) = text_util::split_email(&signature.email);
|
||||
username.to_owned()
|
||||
if !signature.email.is_empty() {
|
||||
let (username, _) = text_util::split_email(&signature.email);
|
||||
username.to_owned()
|
||||
} else {
|
||||
"(no username available)".to_string()
|
||||
}
|
||||
}))
|
||||
}
|
||||
"timestamp" => {
|
||||
|
|
|
@ -57,9 +57,17 @@ impl<C, T: Template<C> + ?Sized> Template<C> for Box<T> {
|
|||
|
||||
impl Template<()> for Signature {
|
||||
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||
write!(formatter.labeled("name"), "{}", self.name)?;
|
||||
if !self.name.is_empty() {
|
||||
write!(formatter.labeled("name"), "{}", self.name)?;
|
||||
} else {
|
||||
write!(formatter.labeled("name"), "(no name available)")?;
|
||||
}
|
||||
write!(formatter, " <")?;
|
||||
write!(formatter.labeled("email"), "{}", self.email)?;
|
||||
if !self.email.is_empty() {
|
||||
write!(formatter.labeled("email"), "{}", self.email)?;
|
||||
} else {
|
||||
write!(formatter.labeled("email"), "(no email available)")?;
|
||||
}
|
||||
write!(formatter, ">")?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -187,8 +187,8 @@ fn test_log_builtin_templates() {
|
|||
│
|
||||
◉ Commit ID: 0000000000000000000000000000000000000000
|
||||
Change ID: zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
|
||||
Author: <> (1970-01-01 00:00:00.000 +00:00)
|
||||
Committer: <> (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: (no name available) <(no email available)> (1970-01-01 00:00:00.000 +00:00)
|
||||
|
||||
(no description set)
|
||||
|
||||
|
|
|
@ -418,6 +418,20 @@ fn test_templater_signature() {
|
|||
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.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]
|
||||
|
@ -601,7 +615,7 @@ fn test_templater_concat_function() {
|
|||
let render = |template| get_colored_template_output(&test_env, &repo_path, "@-", template);
|
||||
|
||||
insta::assert_snapshot!(render(r#"concat()"#), @"");
|
||||
insta::assert_snapshot!(render(r#"concat(author, empty)"#), @" <>[38;5;2mtrue[39m");
|
||||
insta::assert_snapshot!(render(r#"concat(author, empty)"#), @"(no name available) <[38;5;3m(no email available)[39m>[38;5;2mtrue[39m");
|
||||
insta::assert_snapshot!(
|
||||
render(r#"concat(label("error", ""), label("warning", "a"), "b")"#),
|
||||
@"[38;5;3ma[39mb");
|
||||
|
@ -651,11 +665,11 @@ fn test_templater_separate_function() {
|
|||
|
||||
// Separate keywords
|
||||
insta::assert_snapshot!(
|
||||
render(r#"separate(" ", author, description, empty)"#), @" <> [38;5;2mtrue[39m");
|
||||
render(r#"separate(" ", author, description, empty)"#), @"(no name available) <[38;5;3m(no email available)[39m> [38;5;2mtrue[39m");
|
||||
|
||||
// Keyword as separator
|
||||
insta::assert_snapshot!(
|
||||
render(r#"separate(author, "X", "Y", "Z")"#), @"X <>Y <>Z");
|
||||
render(r#"separate(author, "X", "Y", "Z")"#), @"X(no name available) <[38;5;3m(no email available)[39m>Y(no name available) <[38;5;3m(no email available)[39m>Z");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in a new issue