Skip to content

Commit

Permalink
log: add glyph for private revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
jennings committed Jul 19, 2024
1 parent 002ff36 commit c6bbc65
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ pub struct CommitKeywordCache<'repo> {
tags_index: OnceCell<Rc<RefNamesIndex>>,
git_refs_index: OnceCell<Rc<RefNamesIndex>>,
is_immutable_fn: OnceCell<Rc<RevsetContainingFn<'repo>>>,
is_private_fn: OnceCell<Rc<RevsetContainingFn<'repo>>>,
}

impl<'repo> CommitKeywordCache<'repo> {
Expand Down Expand Up @@ -458,6 +459,17 @@ impl<'repo> CommitKeywordCache<'repo> {
Ok(revset.containing_fn().into())
})
}

pub fn is_private_fn(
&self,
language: &CommitTemplateLanguage<'repo>,
span: pest::Span<'_>,
) -> TemplateParseResult<&Rc<RevsetContainingFn<'repo>>> {
self.is_private_fn.get_or_try_init(|| {
let revset = evaluate_private_revset(language, span)?;
Ok(revset.containing_fn().into())
})
}
}

fn builtin_commit_methods<'repo>() -> CommitTemplateBuildMethodFnMap<'repo, Commit> {
Expand Down Expand Up @@ -650,6 +662,18 @@ fn builtin_commit_methods<'repo>() -> CommitTemplateBuildMethodFnMap<'repo, Comm
Ok(L::wrap_boolean(out_property))
},
);
map.insert(
"private",
|language, _build_ctx, self_property, function| {
function.expect_no_arguments()?;
let is_private = language
.keyword_cache
.is_private_fn(language, function.name_span)?
.clone();
let out_property = self_property.map(move |commit| is_private(commit.id()));
Ok(L::wrap_boolean(out_property))
},
);
map.insert(
"contained_in",
|language, _build_ctx, self_property, function| {
Expand Down Expand Up @@ -762,6 +786,18 @@ fn evaluate_immutable_revset<'repo>(
evaluate_revset_expression(language, span, expression)
}

fn evaluate_private_revset<'repo>(
language: &CommitTemplateLanguage<'repo>,
span: pest::Span<'_>,
) -> Result<Box<dyn Revset + 'repo>, TemplateParseError> {
let expression = revset_util::parse_private_expression(&language.revset_parse_context)
.map_err(|err| {
TemplateParseError::expression("Failed to parse revset", span).with_source(err)
})?;

evaluate_revset_expression(language, span, expression)
}

fn evaluate_user_revset<'repo>(
language: &CommitTemplateLanguage<'repo>,
span: pest::Span<'_>,
Expand Down
4 changes: 4 additions & 0 deletions cli/src/config/templates.toml
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,13 @@ coalesce(
if(current_working_copy, "working_copy"),
if(immutable, "immutable"),
if(conflict, "conflict"),
if(private, "private"),
),
coalesce(
if(current_working_copy, "@"),
if(immutable, "◆"),
if(conflict, "×"),
if(private, "◌"),
"○",
)
)
Expand All @@ -230,11 +232,13 @@ coalesce(
if(current_working_copy, "working_copy"),
if(immutable, "immutable"),
if(conflict, "conflict"),
if(private, "private"),
),
coalesce(
if(current_working_copy, "@"),
if(immutable, "+"),
if(conflict, "x"),
if(private, "#"),
"o",
)
)
Expand Down
63 changes: 63 additions & 0 deletions cli/tests/test_log_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1541,3 +1541,66 @@ fn test_log_with_custom_symbols() {
^
"###);
}

#[test]
fn test_log_with_private_commits() {
// Test that elided commits are shown as synthetic nodes.
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let repo_path = test_env.env_root().join("repo");

test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "initial"]);
test_env.jj_cmd_ok(&repo_path, &["new", "-m", "main branch 1"]);
test_env.jj_cmd_ok(&repo_path, &["new", "-m", "main branch 2"]);
test_env.jj_cmd_ok(&repo_path, &["new", "@--", "-m", "side branch 1"]);
test_env.jj_cmd_ok(&repo_path, &["new", "-m", "private 1"]);
test_env.jj_cmd_ok(
&repo_path,
&["new", "-m", "merge", r#"description("main branch 2")"#, "@"],
);

test_env.add_config(r#"revset-aliases."private_roots()" = "description(glob:'private*')""#);

let get_log = |revs: &str| -> String {
test_env.jj_cmd_success(
&repo_path,
&["log", "-T", r#"description ++ "\n""#, "-r", revs],
)
};

// Simple test with showing default and elided nodes.
test_env.add_config("ui.graph.style = 'curved'");
insta::assert_snapshot!(get_log(".."), @r###"
@ merge
├─╮
│ ◌ private 1
│ │
│ ○ side branch 1
│ │
○ │ main branch 2
│ │
○ │ main branch 1
├─╯
○ initial
~
"###);

// Simple test with showing default and elided nodes, ascii style.
test_env.add_config("ui.graph.style = 'ascii'");
insta::assert_snapshot!(get_log(".."), @r###"
@ merge
|\
| # private 1
| |
| o side branch 1
| |
o | main branch 2
| |
o | main branch 1
|/
o initial
|
~
"###);
}

0 comments on commit c6bbc65

Please sign in to comment.