diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index 3715d4a0bb..1d4f131129 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -787,6 +787,7 @@ impl WorkspaceCommandEnvironment { now.into(), self.command.revset_extensions(), Some(workspace_context), + &self.fileset_aliases_map, ) } diff --git a/cli/src/commands/fix.rs b/cli/src/commands/fix.rs index 98e620539b..b6dcd241ac 100644 --- a/cli/src/commands/fix.rs +++ b/cli/src/commands/fix.rs @@ -48,6 +48,7 @@ use tracing::instrument; use crate::cli_util::CommandHelper; use crate::cli_util::RevisionArg; +use crate::cli_util::WorkspaceCommandHelper; use crate::command_error::config_error; use crate::command_error::print_parse_diagnostics; use crate::command_error::CommandError; @@ -145,7 +146,7 @@ pub(crate) fn cmd_fix( args: &FixArgs, ) -> Result<(), CommandError> { let mut workspace_command = command.workspace_helper(ui)?; - let tools_config = get_tools_config(ui, command.settings())?; + let tools_config = get_tools_config(ui, &workspace_command, command.settings())?; let root_commits: Vec = if args.source.is_empty() { let revs = command.settings().get_string("revsets.fix")?; workspace_command.parse_revset(ui, &RevisionArg::from(revs))? @@ -446,7 +447,11 @@ struct RawToolConfig { /// Fails if any of the commands or patterns are obviously unusable, but does /// not check for issues that might still occur later like missing executables. /// This is a place where we could fail earlier in some cases, though. -fn get_tools_config(ui: &mut Ui, settings: &UserSettings) -> Result { +fn get_tools_config( + ui: &mut Ui, + workspace_command: &WorkspaceCommandHelper, + settings: &UserSettings, +) -> Result { let mut tools_config = ToolsConfig { tools: Vec::new() }; // TODO: Remove this block of code and associated documentation after at least // one release where the feature is marked deprecated. @@ -475,6 +480,7 @@ fn get_tools_config(ui: &mut Ui, settings: &UserSettings) -> Result = tools_table .into_iter() .sorted_by(|a, b| a.0.cmp(&b.0)) @@ -492,7 +498,7 @@ fn get_tools_config(ui: &mut Ui, settings: &UserSettings) -> Result() -> CommitTemplateBuildMethodFnMap<'repo, Comm |language, diagnostics, _build_ctx, self_property, function| { let ([], [files_node]) = function.expect_arguments()?; let files = if let Some(node) = files_node { - expect_fileset_literal(diagnostics, node, language.path_converter)? + expect_fileset_literal( + diagnostics, + node, + language.path_converter, + language.revset_parse_context.fileset_aliases_map(), + )? } else { // TODO: defaults to CLI path arguments? // https://github.com/martinvonz/jj/issues/2933#issuecomment-1925870731 @@ -851,11 +857,11 @@ fn expect_fileset_literal( diagnostics: &mut TemplateDiagnostics, node: &ExpressionNode, path_converter: &RepoPathUiConverter, + aliases_map: &FilesetAliasesMap, ) -> Result { template_parser::expect_string_literal_with(node, |text, span| { let mut inner_diagnostics = FilesetDiagnostics::new(); - let aliases_map = Default::default(); - let expression = fileset::parse(&mut inner_diagnostics, text, path_converter, &aliases_map) + let expression = fileset::parse(&mut inner_diagnostics, text, path_converter, aliases_map) .map_err(|err| { TemplateParseError::expression("In fileset expression", span).with_source(err) })?; diff --git a/lib/src/fileset.pest b/lib/src/fileset.pest index 8329e33b89..0ef3d15f13 100644 --- a/lib/src/fileset.pest +++ b/lib/src/fileset.pest @@ -90,4 +90,4 @@ program_or_bare_string = _{ | bare_string ~ EOI ) } -alias_declaration = _{ SOI ~ identifier ~ EOI } +alias_declaration = _{ SOI ~ strict_identifier ~ EOI } diff --git a/lib/src/fileset_parser.rs b/lib/src/fileset_parser.rs index 569c32fa95..5a119d6825 100644 --- a/lib/src/fileset_parser.rs +++ b/lib/src/fileset_parser.rs @@ -427,6 +427,8 @@ pub fn parse_program_or_bare_string(text: &str) -> FilesetParseResult; #[derive(Clone, Debug, Default)] @@ -439,7 +441,7 @@ impl AliasDeclarationParser for FilesetAliasParser { let mut pairs = FilesetParser::parse(Rule::alias_declaration, source)?; let first = pairs.next().unwrap(); match first.as_rule() { - Rule::identifier => Ok(AliasDeclaration::Symbol(first.as_str().to_owned())), + Rule::strict_identifier => Ok(AliasDeclaration::Symbol(first.as_str().to_owned())), r => panic!("unexpected alias declaration rule {r:?}"), } } diff --git a/lib/src/revset.rs b/lib/src/revset.rs index 40c9dcc6b7..4c02f272e5 100644 --- a/lib/src/revset.rs +++ b/lib/src/revset.rs @@ -896,7 +896,12 @@ static BUILTIN_FUNCTION_MAP: Lazy> = Lazy: } let file_expressions = itertools::chain([arg], args) .map(|arg| { - expect_fileset_expression(diagnostics, arg, ctx.path_converter, &Default::default()) + expect_fileset_expression( + diagnostics, + arg, + ctx.path_converter, + context.fileset_aliases_map, + ) }) .try_collect()?; let expr = FilesetExpression::union_all(file_expressions); @@ -918,7 +923,7 @@ static BUILTIN_FUNCTION_MAP: Lazy> = Lazy: diagnostics, files_arg, ctx.path_converter, - &Default::default(), + context.fileset_aliases_map, )? } else { // TODO: defaults to CLI path arguments? @@ -2647,6 +2652,7 @@ pub struct RevsetParseContext<'a> { date_pattern_context: DatePatternContext, extensions: &'a RevsetExtensions, workspace: Option>, + fileset_aliases_map: &'a FilesetAliasesMap, } impl<'a> RevsetParseContext<'a> { @@ -2656,6 +2662,7 @@ impl<'a> RevsetParseContext<'a> { date_pattern_context: DatePatternContext, extensions: &'a RevsetExtensions, workspace: Option>, + fileset_aliases_map: &'a FilesetAliasesMap, ) -> Self { Self { aliases_map, @@ -2663,6 +2670,7 @@ impl<'a> RevsetParseContext<'a> { date_pattern_context, extensions, workspace, + fileset_aliases_map, } } @@ -2670,6 +2678,10 @@ impl<'a> RevsetParseContext<'a> { self.aliases_map } + pub fn fileset_aliases_map(&self) -> &'a FilesetAliasesMap { + self.fileset_aliases_map + } + pub fn user_email(&self) -> &str { &self.user_email } @@ -2695,28 +2707,43 @@ mod tests { use std::path::PathBuf; use assert_matches::assert_matches; + use dsl_util::AliasesMap; use super::*; + const EMPTY: [(&str, &str); 0] = []; + + fn get_alias_map( + aliases: impl IntoIterator, impl Into)>, + ) -> AliasesMap + where + T: dsl_util::AliasDeclarationParser + Default, + T::Error: std::fmt::Debug, + { + let mut aliases_map = AliasesMap::new(); + for (decl, defn) in aliases { + aliases_map.insert(decl, defn).unwrap(); + } + aliases_map + } + fn parse(revset_str: &str) -> Result, RevsetParseError> { - parse_with_aliases(revset_str, [] as [(&str, &str); 0]) + parse_with_aliases(revset_str, EMPTY) } fn parse_with_workspace( revset_str: &str, workspace_id: &WorkspaceId, ) -> Result, RevsetParseError> { - parse_with_aliases_and_workspace(revset_str, [] as [(&str, &str); 0], workspace_id) + parse_with_aliases_and_workspace(revset_str, EMPTY, workspace_id) } fn parse_with_aliases( revset_str: &str, aliases: impl IntoIterator, impl Into)>, ) -> Result, RevsetParseError> { - let mut aliases_map = RevsetAliasesMap::new(); - for (decl, defn) in aliases { - aliases_map.insert(decl, defn).unwrap(); - } + let aliases_map = get_alias_map(aliases); + let fileset_aliases_map = get_alias_map(EMPTY); let extensions = RevsetExtensions::default(); let context = RevsetParseContext::new( &aliases_map, @@ -2724,6 +2751,7 @@ mod tests { chrono::Utc::now().fixed_offset().into(), &extensions, None, + &fileset_aliases_map, ); super::parse(&mut RevsetDiagnostics::new(), revset_str, &context) } @@ -2742,10 +2770,8 @@ mod tests { path_converter: &path_converter, workspace_id, }; - let mut aliases_map = RevsetAliasesMap::new(); - for (decl, defn) in aliases { - aliases_map.insert(decl, defn).unwrap(); - } + let aliases_map = get_alias_map(aliases); + let fileset_aliases_map = get_alias_map(EMPTY); let extensions = RevsetExtensions::default(); let context = RevsetParseContext::new( &aliases_map, @@ -2753,6 +2779,7 @@ mod tests { chrono::Utc::now().fixed_offset().into(), &extensions, Some(workspace_ctx), + &fileset_aliases_map, ); super::parse(&mut RevsetDiagnostics::new(), revset_str, &context) } @@ -2760,17 +2787,15 @@ mod tests { fn parse_with_modifier( revset_str: &str, ) -> Result<(Rc, Option), RevsetParseError> { - parse_with_aliases_and_modifier(revset_str, [] as [(&str, &str); 0]) + parse_with_aliases_and_modifier(revset_str, EMPTY) } fn parse_with_aliases_and_modifier( revset_str: &str, aliases: impl IntoIterator, impl Into)>, ) -> Result<(Rc, Option), RevsetParseError> { - let mut aliases_map = RevsetAliasesMap::new(); - for (decl, defn) in aliases { - aliases_map.insert(decl, defn).unwrap(); - } + let aliases_map = get_alias_map(aliases); + let fileset_aliases_map = get_alias_map(EMPTY); let extensions = RevsetExtensions::default(); let context = RevsetParseContext::new( &aliases_map, @@ -2778,6 +2803,7 @@ mod tests { chrono::Utc::now().fixed_offset().into(), &extensions, None, + &fileset_aliases_map, ); super::parse_with_modifier(&mut RevsetDiagnostics::new(), revset_str, &context) } diff --git a/lib/tests/test_revset.rs b/lib/tests/test_revset.rs index 51ee358de4..eb31d2ec89 100644 --- a/lib/tests/test_revset.rs +++ b/lib/tests/test_revset.rs @@ -23,6 +23,7 @@ use jj_lib::backend::MillisSinceEpoch; use jj_lib::backend::Signature; use jj_lib::backend::Timestamp; use jj_lib::commit::Commit; +use jj_lib::fileset::FilesetAliasesMap; use jj_lib::fileset::FilesetExpression; use jj_lib::git; use jj_lib::git_backend::GitBackend; @@ -68,9 +69,16 @@ fn resolve_symbol_with_extensions( symbol: &str, ) -> Result, RevsetResolutionError> { let aliases_map = RevsetAliasesMap::default(); + let fileset_aliases_map = FilesetAliasesMap::default(); let now = chrono::Local::now(); - let context = - RevsetParseContext::new(&aliases_map, String::new(), now.into(), extensions, None); + let context = RevsetParseContext::new( + &aliases_map, + String::new(), + now.into(), + extensions, + None, + &fileset_aliases_map, + ); let expression = parse(&mut RevsetDiagnostics::new(), symbol, &context).unwrap(); assert_matches!(*expression, RevsetExpression::CommitRef(_)); let symbol_resolver = DefaultSymbolResolver::new(repo, extensions.symbol_resolvers()); @@ -206,6 +214,7 @@ fn test_resolve_symbol_commit_id() { &([] as [&Box; 0]), ); let aliases_map = RevsetAliasesMap::default(); + let fileset_aliases_map = FilesetAliasesMap::default(); let extensions = RevsetExtensions::default(); let context = RevsetParseContext::new( &aliases_map, @@ -213,6 +222,7 @@ fn test_resolve_symbol_commit_id() { chrono::Utc::now().fixed_offset().into(), &extensions, None, + &fileset_aliases_map, ); assert_matches!( parse(&mut RevsetDiagnostics::new(), "present(04)", &context).unwrap() @@ -921,6 +931,7 @@ fn try_resolve_commit_ids( ) -> Result, RevsetResolutionError> { let settings = testutils::user_settings(); let aliases_map = RevsetAliasesMap::default(); + let fileset_aliases_map = FilesetAliasesMap::default(); let revset_extensions = RevsetExtensions::default(); let context = RevsetParseContext::new( &aliases_map, @@ -928,6 +939,7 @@ fn try_resolve_commit_ids( chrono::Utc::now().fixed_offset().into(), &revset_extensions, None, + &fileset_aliases_map, ); let expression = parse(&mut RevsetDiagnostics::new(), revset_str, &context).unwrap(); let symbol_resolver = DefaultSymbolResolver::new(repo, revset_extensions.symbol_resolvers()); @@ -956,6 +968,7 @@ fn resolve_commit_ids_in_workspace( workspace_id: workspace.workspace_id(), }; let aliases_map = RevsetAliasesMap::default(); + let fileset_aliases_map = FilesetAliasesMap::default(); let extensions = RevsetExtensions::default(); let context = RevsetParseContext::new( &aliases_map, @@ -963,6 +976,7 @@ fn resolve_commit_ids_in_workspace( chrono::Utc::now().fixed_offset().into(), &extensions, Some(workspace_ctx), + &fileset_aliases_map, ); let expression = parse(&mut RevsetDiagnostics::new(), revset_str, &context).unwrap(); let symbol_resolver =