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

author/committer templates: add a username method

I haven't used a proper email address parser as I'm not really sure if it's
worth the extra dependency and effort -- thoughts?
This commit is contained in:
Vamsi Avula 2023-02-10 22:31:28 +05:30 committed by Vamsi Avula
parent 3744ae4508
commit aacdcf629b
3 changed files with 83 additions and 0 deletions

View file

@ -142,6 +142,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `jj new --insert-after` inserts the new commit between the target commit and
its children.
* `author`/`committer` templates now support `.username()`, which leaves out the
domain information of `.email()`.
### Fixed bugs
* When sharing the working copy with a Git repo, we used to forget to export

View file

@ -360,6 +360,14 @@ fn expect_arguments<const N: usize, const M: usize>(
}
}
fn split_email(email: &str) -> (&str, Option<&str>) {
if let Some((username, rest)) = email.split_once('@') {
(username, Some(rest))
} else {
(email, None)
}
}
fn parse_method_chain<'a, I: 'a>(
input_property: PropertyAndLabels<'a, I>,
method_pairs: Pairs<Rule>,
@ -556,6 +564,16 @@ fn parse_signature_method<'a, I: 'a>(
TemplatePropertyFn(|signature: &Signature| signature.email.clone()),
))
}
"username" => {
expect_no_arguments(args_pair)?;
Property::String(chain_properties(
self_property,
TemplatePropertyFn(|signature: &Signature| {
let (username, _) = split_email(&signature.email);
username.to_owned()
}),
))
}
"timestamp" => {
expect_no_arguments(args_pair)?;
Property::Timestamp(chain_properties(

View file

@ -257,6 +257,68 @@ fn test_templater_string_method() {
insta::assert_snapshot!(render(r#""foo\nbar".first_line()"#), @"foo");
}
#[test]
fn test_templater_signature() {
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_template_output(&test_env, &repo_path, "@", template);
test_env.jj_cmd_success(&repo_path, &["new"]);
insta::assert_snapshot!(render(r#"author"#), @"Test User <test.user@example.com>");
insta::assert_snapshot!(render(r#"author.name()"#), @"Test User");
insta::assert_snapshot!(render(r#"author.email()"#), @"test.user@example.com");
insta::assert_snapshot!(render(r#"author.username()"#), @"test.user");
test_env.jj_cmd_success(
&repo_path,
&["--config-toml=user.name='Another Test User'", "new"],
);
insta::assert_snapshot!(render(r#"author"#), @"Another Test User <test.user@example.com>");
insta::assert_snapshot!(render(r#"author.name()"#), @"Another Test User");
insta::assert_snapshot!(render(r#"author.email()"#), @"test.user@example.com");
insta::assert_snapshot!(render(r#"author.username()"#), @"test.user");
test_env.jj_cmd_success(
&repo_path,
&[
"--config-toml=user.email='test.user@invalid@example.com'",
"new",
],
);
insta::assert_snapshot!(render(r#"author"#), @"Test User <test.user@invalid@example.com>");
insta::assert_snapshot!(render(r#"author.name()"#), @"Test User");
insta::assert_snapshot!(render(r#"author.email()"#), @"test.user@invalid@example.com");
insta::assert_snapshot!(render(r#"author.username()"#), @"test.user");
test_env.jj_cmd_success(&repo_path, &["--config-toml=user.email='test.user'", "new"]);
insta::assert_snapshot!(render(r#"author"#), @"Test User <test.user>");
insta::assert_snapshot!(render(r#"author.email()"#), @"test.user");
insta::assert_snapshot!(render(r#"author.username()"#), @"test.user");
test_env.jj_cmd_success(
&repo_path,
&[
"--config-toml=user.email='test.user+tag@example.com'",
"new",
],
);
insta::assert_snapshot!(render(r#"author"#), @"Test User <test.user+tag@example.com>");
insta::assert_snapshot!(render(r#"author.email()"#), @"test.user+tag@example.com");
insta::assert_snapshot!(render(r#"author.username()"#), @"test.user+tag");
test_env.jj_cmd_success(&repo_path, &["--config-toml=user.email='x@y'", "new"]);
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]
fn test_templater_label_function() {
let test_env = TestEnvironment::default();