diff --git a/cli/src/template_builder.rs b/cli/src/template_builder.rs index 1d65f152f..cc40ae866 100644 --- a/cli/src/template_builder.rs +++ b/cli/src/template_builder.rs @@ -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" => { diff --git a/cli/src/templater.rs b/cli/src/templater.rs index cb97e6207..30c909355 100644 --- a/cli/src/templater.rs +++ b/cli/src/templater.rs @@ -57,9 +57,17 @@ impl + ?Sized> Template for Box { 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(()) } diff --git a/cli/tests/test_commit_template.rs b/cli/tests/test_commit_template.rs index 7725c98f0..d21f84ef2 100644 --- a/cli/tests/test_commit_template.rs +++ b/cli/tests/test_commit_template.rs @@ -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) diff --git a/cli/tests/test_templater.rs b/cli/tests/test_templater.rs index 583a8c8f3..4d6a8b0c1 100644 --- a/cli/tests/test_templater.rs +++ b/cli/tests/test_templater.rs @@ -418,6 +418,20 @@ fn test_templater_signature() { insta::assert_snapshot!(render(r#"author"#), @"Test User "); 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) "); + 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)"#), @" <>true"); + insta::assert_snapshot!(render(r#"concat(author, empty)"#), @"(no name available) <(no email available)>true"); insta::assert_snapshot!( render(r#"concat(label("error", ""), label("warning", "a"), "b")"#), @"ab"); @@ -651,11 +665,11 @@ fn test_templater_separate_function() { // Separate keywords 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 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]