cli: add "op log -T" option and basic tests for template keywords

This commit is contained in:
Yuya Nishihara 2023-02-19 18:15:54 +09:00
parent b5f1728ffb
commit c2df989fe9
2 changed files with 30 additions and 3 deletions

View file

@ -21,7 +21,12 @@ pub enum OperationCommands {
/// Show the operation log
#[derive(clap::Args, Clone, Debug)]
pub struct OperationLogArgs {}
pub struct OperationLogArgs {
/// Render each operation using the given template (the syntax is not yet
/// documented and is likely to change)
#[arg(long, short = 'T')]
template: Option<String>,
}
/// Restore to the state at an operation
#[derive(clap::Args, Clone, Debug)]
@ -41,14 +46,17 @@ pub struct OperationUndoArgs {
fn cmd_op_log(
ui: &mut Ui,
command: &CommandHelper,
_args: &OperationLogArgs,
args: &OperationLogArgs,
) -> Result<(), CommandError> {
let workspace_command = command.workspace_helper(ui)?;
let repo = workspace_command.repo();
let head_op = repo.operation().clone();
let head_op_id = head_op.id().clone();
let template_string = command.settings().config().get_string("templates.op_log")?;
let template_string = match &args.template {
Some(value) => value.to_owned(),
None => command.settings().config().get_string("templates.op_log")?,
};
let template = operation_templater::parse(
repo,
&template_string,

View file

@ -128,6 +128,25 @@ fn test_op_log() {
"###);
}
#[test]
fn test_op_log_template() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
let render = |template| test_env.jj_cmd_success(&repo_path, &["op", "log", "-T", template]);
insta::assert_snapshot!(render(r#"id "\n""#), @r###"
@ a99a3fd5c51e8f7ccb9ae2f9fb749612a23f0a7cf25d8c644f36c35c077449ce3c66f49d098a5a704ca5e47089a7f019563a5b8cbc7d451619e0f90c82241ceb
o 56b94dfc38e7d54340377f566e96ab97dc6163ea7841daf49fb2e1d1ceb27e26274db1245835a1a421fb9d06e6e0fe1e4f4aa1b0258c6e86df676ad9111d0dab
"###);
insta::assert_snapshot!(
render(r#"separate(" ", id.short(5), current_operation, user,
time.start(), time.end(), time.duration()) "\n""#), @r###"
@ a99a3 true test-username@host.example.com 2001-02-03 04:05:07.000 +07:00 2001-02-03 04:05:07.000 +07:00 less than a microsecond
o 56b94 false test-username@host.example.com 2001-02-03 04:05:07.000 +07:00 2001-02-03 04:05:07.000 +07:00 less than a microsecond
"###);
}
#[test]
fn test_op_log_configurable() {
let test_env = TestEnvironment::default();