Skip to content

Commit

Permalink
templater: add local/remote_branches keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Oct 28, 2023
1 parent 5486d72 commit 0e14a1f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* The `branches`/`tags`/`git_refs`/`git_head` template keywords now return a
list of `RefName`s. They were previously pre-formatted strings.

* The new template keywords `local_branches`/`remote_branches` are added to show
only local/remote branches.

### Fixed bugs

* Updating the working copy to a commit where a file that's currently ignored
Expand Down
26 changes: 26 additions & 0 deletions cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,28 @@ fn build_commit_keyword_opt<'repo>(
.collect()
}))
}
"local_branches" => {
let index = cache.branches_index(repo).clone();
language.wrap_ref_name_list(wrap_fn(property, move |commit| {
index
.get(commit.id())
.iter()
.filter(|ref_name| ref_name.is_local())
.cloned()
.collect()
}))
}
"remote_branches" => {
let index = cache.branches_index(repo).clone();
language.wrap_ref_name_list(wrap_fn(property, move |commit| {
index
.get(commit.id())
.iter()
.filter(|ref_name| ref_name.is_remote())
.cloned()
.collect()
}))
}
"tags" => {
let index = cache.tags_index(repo).clone();
language.wrap_ref_name_list(wrap_fn(property, move |commit| {
Expand Down Expand Up @@ -393,6 +415,10 @@ impl RefName {
fn is_local(&self) -> bool {
self.remote.is_none()
}

fn is_remote(&self) -> bool {
self.remote.is_some()
}
}

impl Template<()> for RefName {
Expand Down
20 changes: 17 additions & 3 deletions cli/tests/test_commit_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ fn test_log_branches() {
test_env.jj_cmd_ok(&origin_path, &["describe", "-m=description 1"]);
test_env.jj_cmd_ok(&origin_path, &["branch", "create", "branch1"]);
test_env.jj_cmd_ok(&origin_path, &["new", "root()", "-m=description 2"]);
test_env.jj_cmd_ok(&origin_path, &["branch", "create", "branch2"]);
test_env.jj_cmd_ok(&origin_path, &["branch", "create", "branch2", "unchanged"]);
test_env.jj_cmd_ok(&origin_path, &["new", "root()", "-m=description 3"]);
test_env.jj_cmd_ok(&origin_path, &["branch", "create", "branch3"]);
test_env.jj_cmd_ok(&origin_path, &["git", "export"]);
Expand Down Expand Up @@ -417,7 +417,7 @@ fn test_log_branches() {
│ ◉ 21c33875443e branch1*
├─╯
│ @ a5b4d15489cc branch2* new-branch
│ ◉ 8476341eb395 branch2@origin
│ ◉ 8476341eb395 branch2@origin unchanged
├─╯
◉ 000000000000 (no branches)
"###);
Expand All @@ -431,10 +431,24 @@ fn test_log_branches() {
│ ◉ branch1
├─╯
│ @ branch2, new-branch
│ ◉ origin/branch2
│ ◉ origin/branch2, unchanged
├─╯
"###);

let template = r#"separate(" ", "L:", local_branches, "R:", remote_branches)"#;
let output = test_env.jj_cmd_success(&workspace_root, &["log", "-T", template]);
insta::assert_snapshot!(output, @r###"
◉ L: branch3?? R: branch3@origin
│ ◉ L: branch3?? R:
├─╯
│ ◉ L: branch1* R:
├─╯
│ @ L: branch2* new-branch R:
│ ◉ L: unchanged R: branch2@origin unchanged@origin
├─╯
◉ L: R:
"###);
}

#[test]
Expand Down
6 changes: 5 additions & 1 deletion docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ The following keywords can be used in `jj log`/`jj obslog` templates.
working-copy commit as `<workspace name>@`.
* `current_working_copy: Boolean`: True for the working-copy commit of the
current workspace.
* `branches: List<RefName>`
* `branches: List<RefName>`: Local and remote branches pointing to the commit.
A tracking remote branch will be included only if its target is different
from the local one.
* `local_branches: List<RefName>`: All local branches pointing to the commit.
* `remote_branches: List<RefName>`: All remote branches pointing to the commit.
* `tags: List<RefName>`
* `git_refs: List<RefName>`
* `git_head: List<RefName>`
Expand Down

0 comments on commit 0e14a1f

Please sign in to comment.