template: add contained_in method to commit object in templates

this allows to conditionally display or label elements depending on 
whether the given commit is contained within the revset
This commit is contained in:
Noah Mayr 2024-04-26 16:56:40 +02:00
parent 0e2e09a593
commit dc693e7b8f
4 changed files with 146 additions and 10 deletions

View file

@ -65,6 +65,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `jj rebase -r` now accepts `--insert-after` and `--insert-before` options to
customize the location of the rebased revisions.
* Commit objects in templates now have a `containted_in(revset: String) -> Boolean` method.
### Fixed bugs
* Revsets now support `\`-escapes in string literal.

View file

@ -27,7 +27,7 @@ use jj_lib::id_prefix::IdPrefixContext;
use jj_lib::object_id::ObjectId as _;
use jj_lib::op_store::{RefTarget, WorkspaceId};
use jj_lib::repo::Repo;
use jj_lib::revset::{Revset, RevsetParseContext};
use jj_lib::revset::{self, Revset, RevsetExpression, RevsetParseContext};
use jj_lib::{git, rewrite};
use once_cell::unsync::OnceCell;
@ -620,6 +620,20 @@ fn builtin_commit_methods<'repo>() -> CommitTemplateBuildMethodFnMap<'repo, Comm
Ok(L::wrap_boolean(out_property))
},
);
map.insert(
"contained_in",
|language, _build_ctx, self_property, function| {
let [revset_node] = template_parser::expect_exact_arguments(function)?;
let is_contained =
template_parser::expect_string_literal_with(revset_node, |revset, span| {
Ok(evaluate_user_revset(language, span, revset)?.containing_fn())
})?;
let out_property = self_property.map(move |commit| is_contained(commit.id()));
Ok(L::wrap_boolean(out_property))
},
);
map.insert(
"conflict",
|_language, _build_ctx, self_property, function| {
@ -666,11 +680,27 @@ fn extract_working_copies(repo: &dyn Repo, commit: &Commit) -> String {
type RevsetContainingFn<'repo> = dyn Fn(&CommitId) -> bool + 'repo;
fn evaluate_revset_expression<'repo>(
language: &CommitTemplateLanguage<'repo>,
span: pest::Span<'_>,
expression: Rc<RevsetExpression>,
) -> Result<Box<dyn Revset + 'repo>, TemplateParseError> {
let symbol_resolver = revset_util::default_symbol_resolver(
language.repo,
language.revset_parse_context.extensions.symbol_resolvers(),
language.id_prefix_context,
);
let revset =
revset_util::evaluate(language.repo, &symbol_resolver, expression).map_err(|err| {
TemplateParseError::expression("Failed to evaluate revset", span).with_source(err)
})?;
Ok(revset)
}
fn evaluate_immutable_revset<'repo>(
language: &CommitTemplateLanguage<'repo>,
span: pest::Span<'_>,
) -> Result<Box<dyn Revset + 'repo>, TemplateParseError> {
let repo = language.repo;
// Alternatively, a negated (i.e. visible mutable) set could be computed.
// It's usually smaller than the immutable set. The revset engine can also
// optimize "::<recent_heads>" query to use bitset-based implementation.
@ -678,15 +708,20 @@ fn evaluate_immutable_revset<'repo>(
.map_err(|err| {
TemplateParseError::expression("Failed to parse revset", span).with_source(err)
})?;
let symbol_resolver = revset_util::default_symbol_resolver(
repo,
language.revset_parse_context.extensions.symbol_resolvers(),
language.id_prefix_context,
);
let revset = revset_util::evaluate(repo, &symbol_resolver, expression).map_err(|err| {
TemplateParseError::expression("Failed to evaluate revset", span).with_source(err)
evaluate_revset_expression(language, span, expression)
}
fn evaluate_user_revset<'repo>(
language: &CommitTemplateLanguage<'repo>,
span: pest::Span<'_>,
revset: &str,
) -> Result<Box<dyn Revset + 'repo>, TemplateParseError> {
let expression = revset::parse(revset, &language.revset_parse_context).map_err(|err| {
TemplateParseError::expression("Failed to parse revset", span).with_source(err)
})?;
Ok(revset)
evaluate_revset_expression(language, span, expression)
}
/// Branch or tag name with metadata.

View file

@ -658,3 +658,101 @@ fn test_log_immutable() {
2: Revision "unknown_symbol" doesn't exist
"###);
}
#[test]
fn test_log_contained_in() {
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, &["new", "-mA", "root()"]);
test_env.jj_cmd_ok(&repo_path, &["new", "-mB"]);
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "main"]);
test_env.jj_cmd_ok(&repo_path, &["new", "-mC"]);
test_env.jj_cmd_ok(&repo_path, &["new", "-mD", "root()"]);
let template_for_revset = |revset: &str| {
format!(
r#"
separate(" ",
description.first_line(),
branches,
if(self.contained_in("{revset}"), "[contained_in]"),
) ++ "\n"
"#
)
};
let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"log",
"-r::",
"-T",
&template_for_revset(r#"description(A)::"#),
],
);
insta::assert_snapshot!(stdout, @r###"
@ D
C [contained_in]
B main [contained_in]
A [contained_in]
"###);
let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"log",
"-r::",
"-T",
&template_for_revset(r#"visible_heads()"#),
],
);
insta::assert_snapshot!(stdout, @r###"
@ D [contained_in]
C [contained_in]
B main
A
"###);
// Suppress error that could be detected earlier
let stderr = test_env.jj_cmd_failure(
&repo_path,
&["log", "-r::", "-T", &template_for_revset("unknown_fn()")],
);
insta::assert_snapshot!(stderr, @r###"
Error: Failed to parse template: Failed to parse revset
Caused by:
1: --> 5:28
|
5 | if(self.contained_in("unknown_fn()"), "[contained_in]"),
| ^------------^
|
= Failed to parse revset
2: --> 1:1
|
1 | unknown_fn()
| ^--------^
|
= Function "unknown_fn" doesn't exist
"###);
let stderr = test_env.jj_cmd_failure(
&repo_path,
&["log", "-r::", "-T", &template_for_revset("unknown_symbol")],
);
insta::assert_snapshot!(stderr, @r###"
Error: Failed to parse template: Failed to evaluate revset
Caused by:
1: --> 5:28
|
5 | if(self.contained_in("unknown_symbol"), "[contained_in]"),
| ^--------------^
|
= Failed to evaluate revset
2: Revision "unknown_symbol" doesn't exist
"###);
}

View file

@ -91,6 +91,7 @@ This type cannot be printed. The following methods are defined.
* `hidden() -> Boolean`: True if the commit is not visible (a.k.a. abandoned).
* `immutable() -> Boolean`: True if the commit is included in [the set of
immutable commits](config.md#set-of-immutable-commits).
* `contained_in(revset: String) -> Boolean`: True if the commit is included in [the provided revset](revsets.md).
* `conflict() -> Boolean`: True if the commit contains merge conflicts.
* `empty() -> Boolean`: True if the commit modifies no files.
* `root() -> Boolean`: True if the commit is the root commit.