diff --git a/cli/src/command_error.rs b/cli/src/command_error.rs index 2621fb0ad0..8361e03d4b 100644 --- a/cli/src/command_error.rs +++ b/cli/src/command_error.rs @@ -35,6 +35,7 @@ use jj_lib::working_copy::{ResetError, SnapshotError, WorkingCopyStateError}; use jj_lib::workspace::WorkspaceInitError; use thiserror::Error; +use crate::cli_util::WorkspaceCommandHelper; use crate::merge_tools::{ ConflictResolveError, DiffEditError, DiffGenerateError, MergeToolConfigError, }; @@ -76,6 +77,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('\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 8bf661fff1..9a61df8d46 100644 --- a/cli/src/commands/log.rs +++ b/cli/src/commands/log.rs @@ -22,7 +22,7 @@ use jj_lib::revset_graph::{ use tracing::instrument; use crate::cli_util::{CommandHelper, LogContentFormat, RevisionArg}; -use crate::command_error::CommandError; +use crate::command_error::{user_error_missing_template, CommandError}; use crate::diff_util::{self, DiffFormatArgs}; use crate::graphlog::{get_graphlog, Edge}; use crate::ui::Ui; @@ -52,7 +52,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, @@ -101,7 +101,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