Skip to content

Commit

Permalink
cli: migrate "tag list" to template
Browse files Browse the repository at this point in the history
I'm going to add more detailed output there. This is a step towards "branch
list" template. "tag list -T" wouldn't be that useful, but it shares primitives
with "branch list -T".
  • Loading branch information
yuja committed May 1, 2024
1 parent b2aa58d commit 00e1827
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
26 changes: 22 additions & 4 deletions cli/src/commands/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use jj_lib::str_util::StringPattern;

use crate::cli_util::CommandHelper;
use crate::command_error::CommandError;
use crate::commit_templater::{CommitTemplateLanguage, RefName};
use crate::ui::Ui;

/// Manage tags.
Expand All @@ -35,6 +36,13 @@ pub struct TagListArgs {
/// https://github.com/martinvonz/jj/blob/main/docs/revsets.md#string-patterns.
#[arg(value_parser = StringPattern::parse)]
pub names: Vec<StringPattern>,
/// Render each tag using the given template
///
/// All 0-argument methods of the `RefName` type are available as keywords.
///
/// For the syntax, see https://github.com/martinvonz/jj/blob/main/docs/templates.md
#[arg(long, short = 'T')]
template: Option<String>,
}

pub fn cmd_tag(
Expand All @@ -56,16 +64,26 @@ fn cmd_tag_list(
let repo = workspace_command.repo();
let view = repo.view();

let template = {
let language = workspace_command.commit_template_language()?;
let text = match &args.template {
Some(value) => value.to_owned(),
None => command.settings().config().get("templates.tag_list")?,
};
workspace_command
.parse_template(&language, &text, CommitTemplateLanguage::wrap_ref_name)?
.labeled("tag_list")
};

ui.request_pager();
let mut formatter = ui.stdout_formatter();
let formatter = formatter.as_mut();

for name in view.tags().keys() {
for (name, target) in view.tags() {
if !args.names.is_empty() && !args.names.iter().any(|pattern| pattern.matches(name)) {
continue;
}

writeln!(formatter.labeled("tag"), "{name}")?;
let ref_name = RefName::local_only(name, target.clone());
template.format(&ref_name, formatter.as_mut())?;
}

Ok(())
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 @@ -17,6 +17,10 @@ log = 'builtin_log_compact'
op_log = 'builtin_op_log_compact'
show = 'builtin_log_detailed'

tag_list = '''
label("tag", name) ++ "\n"
'''

[template-aliases]
builtin_log_oneline = '''
if(root,
Expand Down
6 changes: 5 additions & 1 deletion cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -1824,12 +1824,16 @@ Manage tags
List tags
**Usage:** `jj tag list [NAMES]...`
**Usage:** `jj tag list [OPTIONS] [NAMES]...`
###### **Arguments:**
* `<NAMES>` — Show tags whose local name matches
###### **Options:**
* `-T`, `--template <TEMPLATE>` — Render each tag using the given template
## `jj util`
Expand Down
8 changes: 8 additions & 0 deletions cli/tests/test_tag_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ fn test_tag_list() {
@r###"
test_tag2
"###);

let template = r#"'name: ' ++ name ++ "\n""#;
insta::assert_snapshot!(
test_env.jj_cmd_success(&repo_path, &["tag", "list", "-T", template]),
@r###"
name: test_tag
name: test_tag2
"###);
}

0 comments on commit 00e1827

Please sign in to comment.