Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template: add method mine() to commit type #3456

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* The list of conflicted paths is printed whenever the working copy changes.
This can be disabled with the `--quiet` option.

* Commit objects in templates now have a `mine() -> Boolean` method analog to the same function in revsets.
It evaluates to true if the email of the commit author matches the current `user.email`.

### Fixed bugs

## [0.16.0] - 2024-04-03
Expand Down
6 changes: 6 additions & 0 deletions cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,12 @@ fn builtin_commit_methods<'repo>() -> CommitTemplateBuildMethodFnMap<'repo, Comm
Ok(L::wrap_signature(out_property))
},
);
map.insert("mine", |language, _build_ctx, self_property, function| {
template_parser::expect_no_arguments(function)?;
let user_email = language.revset_parse_context.user_email.clone();
let out_property = self_property.map(move |commit| commit.author().email == user_email);
Ok(L::wrap_boolean(out_property))
});
map.insert(
"working_copies",
|language, _build_ctx, self_property, function| {
Expand Down
29 changes: 29 additions & 0 deletions cli/tests/test_commit_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,35 @@ fn test_log_author_timestamp_local() {
"###);
}

#[test]
fn test_mine_is_true_when_author_is_user() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
test_env.jj_cmd_ok(
&repo_path,
&[
"--config-toml=user.email='[email protected]'",
"--config-toml=user.name='John Doe'",
"new",
],
);

let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"log",
"-T",
r#"coalesce(if(mine, "mine"), author.email(), email_placeholder)"#,
],
);
insta::assert_snapshot!(stdout, @r###"
@ [email protected]
◉ mine
◉ (no email set)
"###);
}

#[test]
fn test_log_default() {
let test_env = TestEnvironment::default();
Expand Down
2 changes: 2 additions & 0 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ This type cannot be printed. The following methods are defined.
* `parents() -> List<Commit>`
* `author() -> Signature`
* `committer() -> Signature`
* `mine() -> Boolean`: Commits where the author's email matches the email of the current
user.
* `working_copies() -> String`: For multi-workspace repository, indicate
working-copy commit as `<workspace name>@`.
* `current_working_copy() -> Boolean`: True for the working-copy commit of the
Expand Down