diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index 13166011f0..f7ea55b348 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -130,6 +130,24 @@ impl ErrorWithMessage { } } +pub fn user_error_missing_template(workspace_command: &WorkspaceCommandHelper) -> CommandError { + let mut template_names = workspace_command + .template_aliases_map() + .symbol_aliases_keys(); + template_names.sort_unstable(); + let mut hint = String::new(); + let padding = " "; + for name in template_names { + hint.push_str("\n"); + hint.push_str(padding); + hint.push_str(name); + } + user_error_with_hint( + String::from("Template is required"), + format!("The following template-aliases are defined:{}", hint), + ) +} + pub fn user_error(err: impl Into>) -> CommandError { user_error_with_hint_opt(err, None) } diff --git a/cli/src/commands/log.rs b/cli/src/commands/log.rs index 83ea7f2fec..094d83d432 100644 --- a/cli/src/commands/log.rs +++ b/cli/src/commands/log.rs @@ -21,7 +21,9 @@ use jj_lib::revset_graph::{ }; use tracing::instrument; -use crate::cli_util::{CommandError, CommandHelper, LogContentFormat, RevisionArg}; +use crate::cli_util::{ + user_error_missing_template, CommandError, CommandHelper, LogContentFormat, RevisionArg, +}; use crate::diff_util::{self, DiffFormatArgs}; use crate::graphlog::{get_graphlog, Edge}; use crate::ui::Ui; @@ -51,7 +53,7 @@ pub(crate) struct LogArgs { /// /// For the syntax, see https://github.com/martinvonz/jj/blob/main/docs/templates.md #[arg(long, short = 'T')] - template: Option, + template: Option>, /// Show patch #[arg(long, short = 'p')] patch: bool, @@ -100,7 +102,8 @@ pub(crate) fn cmd_log( diff_util::diff_formats_for_log(command.settings(), &args.diff_format, args.patch)?; let template_string = match &args.template { - Some(value) => value.to_string(), + Some(Some(value)) => value.to_string(), + Some(None) => return Err(user_error_missing_template(&workspace_command)), None => command.settings().config().get_string("templates.log")?, }; let use_elided_nodes = command diff --git a/cli/src/template_parser.rs b/cli/src/template_parser.rs index 8bad4af9a6..1fbf8caa1a 100644 --- a/cli/src/template_parser.rs +++ b/cli/src/template_parser.rs @@ -561,6 +561,10 @@ impl TemplateAliasesMap { Self::default() } + pub fn symbol_aliases_keys(&self) -> Vec<&str> { + self.symbol_aliases.keys().map(|s| s.as_str()).collect_vec() + } + /// Adds new substitution rule `decl = defn`. /// /// Returns error if `decl` is invalid. The `defn` part isn't checked. A bad diff --git a/cli/tests/test_log_command.rs b/cli/tests/test_log_command.rs index b0997b68da..2f88eab324 100644 --- a/cli/tests/test_log_command.rs +++ b/cli/tests/test_log_command.rs @@ -179,7 +179,7 @@ fn test_log_with_or_without_diff() { insta::assert_snapshot!(stderr, @r###" error: the argument '--git' cannot be used with '--color-words' - Usage: jj log --template