Skip to content

Commit ba48317

Browse files
committed
templater: add commit.local/remote_tags() methods for completeness
1 parent 0fb99c1 commit ba48317

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6262
* New `jj tag set`/`delete` commands to create/update/delete tags locally.
6363
Updated tags will be exported to Git as lightweight tags.
6464

65+
* New commit template keywords `local`/`remote_tags` to show only local/remote
66+
tags. These keywords may be useful in non-colocated Git repositories where
67+
local and exported `@git` tags can point to different revisions.
68+
6569
### Fixed bugs
6670

6771
* `jj metaedit --author-timestamp` twice with the same value no longer

cli/src/commit_templater.rs

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,14 +1073,8 @@ fn builtin_commit_methods<'repo>() -> CommitTemplateBuildMethodFnMap<'repo, Comm
10731073
.keyword_cache
10741074
.bookmarks_index(language.repo)
10751075
.clone();
1076-
let out_property = self_property.map(move |commit| {
1077-
index
1078-
.get(commit.id())
1079-
.iter()
1080-
.filter(|commit_ref| commit_ref.is_local())
1081-
.cloned()
1082-
.collect_vec()
1083-
});
1076+
let out_property =
1077+
self_property.map(move |commit| collect_local_refs(index.get(commit.id())));
10841078
Ok(out_property.into_dyn_wrapped())
10851079
},
10861080
);
@@ -1092,14 +1086,8 @@ fn builtin_commit_methods<'repo>() -> CommitTemplateBuildMethodFnMap<'repo, Comm
10921086
.keyword_cache
10931087
.bookmarks_index(language.repo)
10941088
.clone();
1095-
let out_property = self_property.map(move |commit| {
1096-
index
1097-
.get(commit.id())
1098-
.iter()
1099-
.filter(|commit_ref| commit_ref.is_remote())
1100-
.cloned()
1101-
.collect_vec()
1102-
});
1089+
let out_property =
1090+
self_property.map(move |commit| collect_remote_refs(index.get(commit.id())));
11031091
Ok(out_property.into_dyn_wrapped())
11041092
},
11051093
);
@@ -1113,6 +1101,26 @@ fn builtin_commit_methods<'repo>() -> CommitTemplateBuildMethodFnMap<'repo, Comm
11131101
Ok(out_property.into_dyn_wrapped())
11141102
},
11151103
);
1104+
map.insert(
1105+
"local_tags",
1106+
|language, _diagnostics, _build_ctx, self_property, function| {
1107+
function.expect_no_arguments()?;
1108+
let index = language.keyword_cache.tags_index(language.repo).clone();
1109+
let out_property =
1110+
self_property.map(move |commit| collect_local_refs(index.get(commit.id())));
1111+
Ok(out_property.into_dyn_wrapped())
1112+
},
1113+
);
1114+
map.insert(
1115+
"remote_tags",
1116+
|language, _diagnostics, _build_ctx, self_property, function| {
1117+
function.expect_no_arguments()?;
1118+
let index = language.keyword_cache.tags_index(language.repo).clone();
1119+
let out_property =
1120+
self_property.map(move |commit| collect_remote_refs(index.get(commit.id())));
1121+
Ok(out_property.into_dyn_wrapped())
1122+
},
1123+
);
11161124
map.insert(
11171125
"git_refs",
11181126
|language, _diagnostics, _build_ctx, self_property, function| {
@@ -1823,6 +1831,22 @@ fn collect_distinct_refs(commit_refs: &[Rc<CommitRef>]) -> Vec<Rc<CommitRef>> {
18231831
.collect()
18241832
}
18251833

1834+
fn collect_local_refs(commit_refs: &[Rc<CommitRef>]) -> Vec<Rc<CommitRef>> {
1835+
commit_refs
1836+
.iter()
1837+
.filter(|commit_ref| commit_ref.is_local())
1838+
.cloned()
1839+
.collect()
1840+
}
1841+
1842+
fn collect_remote_refs(commit_refs: &[Rc<CommitRef>]) -> Vec<Rc<CommitRef>> {
1843+
commit_refs
1844+
.iter()
1845+
.filter(|commit_ref| commit_ref.is_remote())
1846+
.cloned()
1847+
.collect()
1848+
}
1849+
18261850
/// Wrapper to render ref/remote name in revset syntax.
18271851
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
18281852
#[serde(transparent)]

cli/tests/test_commit_template.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,16 @@ fn test_log_tags() {
765765
◆ 000000000000 (no tags)
766766
[EOF]
767767
");
768+
769+
let template = r#"separate(" ", "L:", local_tags, "R:", remote_tags) ++ "\n""#;
770+
let output = work_dir.run_jj(["log", "-rall()", "-T", template]);
771+
insta::assert_snapshot!(output, @r"
772+
@ L: R:
773+
◆ L: bar baz foo* R: bar@git
774+
◆ L: R: foo@git
775+
◆ L: R:
776+
[EOF]
777+
");
768778
}
769779

770780
#[test]

docs/templates.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ This type cannot be printed. The following methods are defined.
156156
* `.tags() -> List<CommitRef>`: Local and remote tags pointing to the commit. A
157157
tracked remote tag will be included only if its target is different from the
158158
local one.
159+
* `.local_tags() -> List<CommitRef>`: All local tags pointing to the commit.
160+
* `.remote_tags() -> List<CommitRef>`: All remote tags pointing to the commit.
159161
* `.git_refs() -> List<CommitRef>`
160162
* `.git_head() -> Boolean`: True for the Git `HEAD` commit.
161163
* `.divergent() -> Boolean`: True if the commit's change id corresponds to multiple

0 commit comments

Comments
 (0)