Skip to content

Commit

Permalink
templater: make "git_head" return boolean, render it as "git_head()"
Browse files Browse the repository at this point in the history
For the same reason as the root revision is indicated by "root()".
  • Loading branch information
yuja committed Oct 21, 2024
1 parent 3d31928 commit 5dd6185
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 184 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
* Evaluation error of `revsets.short-prefixes` configuration is now reported.

* The `HEAD@git` symbol no longer resolves to the Git HEAD revision. Use
`git_head()` or `@-` revset expression instead.
`git_head()` or `@-` revset expression instead. The `git_head` template
keyword now returns a boolean.

* Help command doesn't work recursively anymore, i.e. `jj workspace help root`
doesn't work anymore.
Expand Down
16 changes: 5 additions & 11 deletions cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use jj_lib::extensions_map::ExtensionsMap;
use jj_lib::fileset;
use jj_lib::fileset::FilesetDiagnostics;
use jj_lib::fileset::FilesetExpression;
use jj_lib::git;
use jj_lib::id_prefix::IdPrefixContext;
use jj_lib::id_prefix::IdPrefixIndex;
use jj_lib::matchers::Matcher;
Expand Down Expand Up @@ -703,8 +702,11 @@ fn builtin_commit_methods<'repo>() -> CommitTemplateBuildMethodFnMap<'repo, Comm
|language, _diagnostics, _build_ctx, self_property, function| {
function.expect_no_arguments()?;
let repo = language.repo;
let out_property = self_property.map(|commit| extract_git_head(repo, &commit));
Ok(L::wrap_ref_name_opt(out_property))
let out_property = self_property.map(|commit| {
let target = repo.view().git_head();
target.added_ids().contains(commit.id())
});
Ok(L::wrap_boolean(out_property))
},
);
map.insert(
Expand Down Expand Up @@ -1230,14 +1232,6 @@ fn build_ref_names_index<'a>(
index
}

fn extract_git_head(repo: &dyn Repo, commit: &Commit) -> Option<Rc<RefName>> {
let target = repo.view().git_head();
target
.added_ids()
.contains(commit.id())
.then(|| RefName::remote_only("HEAD", git::REMOTE_NAME_FOR_LOCAL_GIT_REPO, target.clone()))
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CommitOrChangeId {
Commit(CommitId),
Expand Down
4 changes: 2 additions & 2 deletions cli/src/config/templates.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ if(root,
bookmarks,
tags,
working_copies,
git_head,
if(git_head, label("git_head", "git_head()")),
format_short_commit_id(commit_id),
if(conflict, label("conflict", "conflict")),
if(empty, label("empty", "(empty)")),
Expand All @@ -103,7 +103,7 @@ if(root,
bookmarks,
tags,
working_copies,
git_head,
if(git_head, label("git_head", "git_head()")),
format_short_commit_id(commit_id),
if(conflict, label("conflict", "conflict")),
) ++ "\n",
Expand Down
24 changes: 9 additions & 15 deletions cli/tests/test_commit_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,27 +671,21 @@ fn test_log_git_head() {
test_env.jj_cmd_ok(&repo_path, &["new", "-m=initial"]);
std::fs::write(repo_path.join("file"), "foo\n").unwrap();

let template = r#"
separate(", ",
if(git_head, "name: " ++ git_head.name()),
"remote: " ++ git_head.remote(),
) ++ "\n"
"#;
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", template]);
insta::assert_snapshot!(stdout, @r###"
@ remote: <Error: No RefName available>
○ name: HEAD, remote: git
◆ remote: <Error: No RefName available>
"###);
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "git_head"]);
insta::assert_snapshot!(stdout, @r#"
@ false
○ true
◆ false
"#);

let stdout = test_env.jj_cmd_success(&repo_path, &["log", "--color=always"]);
insta::assert_snapshot!(stdout, @r###"
insta::assert_snapshot!(stdout, @r#"
@ rlvkpnrz [38;5;[email protected] 2001-02-03 08:05:09 50aaf475
│ initial
○ [1m[38;5;5mq[0m[38;5;8mpvuntsm[39m [38;5;[email protected][39m [38;5;6m2001-02-03 08:05:07[39m [38;5;2mHEAD@git[39m [1m[38;5;4m2[0m[38;5;8m30dd059[39m
○ [1m[38;5;5mq[0m[38;5;8mpvuntsm[39m [38;5;[email protected][39m [38;5;6m2001-02-03 08:05:07[39m [38;5;2mgit_head()[39m [1m[38;5;4m2[0m[38;5;8m30dd059[39m
│ (empty) (no description set)
◆ zzzzzzzz root() 00000000
"###);
"#);
}

#[test]
Expand Down
Loading

0 comments on commit 5dd6185

Please sign in to comment.