Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Complete effective hook names and source-qualified filters (#4549)
Maximilian Roosad7c68ba5536
diff --git a/src/commands/mod.rs b/src/commands/mod.rsindex 0dce1e957..9c102da34 100644--- a/src/commands/mod.rs+++ b/src/commands/mod.rs@@ -53,6 +53,7 @@ pub(crate) use custom::handle_custom_command;pub(crate) use eval::step_eval;pub(crate) use for_each::step_for_each;pub(crate) use hook_commands::{HookCliArgs, handle_hook_show, run_hook};+pub(crate) use hook_filter::ParsedFilter;pub(crate) use init::{handle_completions, handle_init};pub(crate) use list::handle_list;pub(crate) use merge::{MergeFlagOverrides, MergeOptions, handle_merge};diff --git a/src/completion.rs b/src/completion.rsindex 8dab93ab6..f05089645 100644--- a/src/completion.rs+++ b/src/completion.rs@@ -38,10 +38,10 @@ use clap_complete::engine::{ArgValueCompleter, CompletionCandidate, ValueCompletuse clap_complete::env::CompleteEnv;use crate::cli;-use crate::commands::{AliasMap, load_aliases};+use crate::commands::{AliasMap, ParsedFilter, load_aliases};use crate::display::format_relative_time_short;use worktrunk::config::{- ALIAS_ARGS_KEY, CommandConfig, ProjectConfig, UserConfig, template_references_var,+ ALIAS_ARGS_KEY, CommandConfig, HookSource, ProjectConfig, UserConfig, template_references_var,};use worktrunk::git::{BranchCategory, HookType, Repository};@@ -290,7 +290,7 @@ pub(crate) fn worktree_only_completer() -> ArgValueCompleter {}/// Hook command name completion for `wt hook <hook-type> <name>`.-/// Completes with command names from the project config for the hook type being invoked.+/// Completes with effective user and project command names for the hook type being invoked.pub(crate) fn hook_command_name_completer() -> ArgValueCompleter {ArgValueCompleter::new(HookCommandCompleter)}@@ -345,30 +345,42 @@ impl ValueCompleter for HookCommandCompleter {let mut candidates = Vec::new();+ let filter = ParsedFilter::parse(current.to_str().unwrap_or_default());let add_named_commands =- |candidates: &mut Vec<_>, config: &worktrunk::config::CommandConfig| {- candidates.extend(- config- .commands()- .filter_map(|cmd| cmd.name.as_ref())- .map(|name| CompletionCandidate::new(name.clone())),- );+ |candidates: &mut Vec<_>, config: &CommandConfig, source: HookSource| {+ if !filter.matches_source(source) {+ return;+ }+ candidates.extend(config.commands().filter_map(|cmd| {+ let name = cmd.name.as_ref()?;+ let name = if filter.source.is_some() {+ source.command_label(Some(name))+ } else {+ name.clone()+ };+ Some(CompletionCandidate::new(name))+ }));};- // Load user config and add user hook names+ let repo = Repository::current().ok();+ let project_id = repo+ .as_ref()+ .and_then(|repo| repo.project_identifier().ok());++ // Use the same global + per-project merge as hook execution.if let Ok(user_config) = UserConfig::load()- && let Some(config) = user_config.hooks.get(hook_type)+ && let Some(config) = user_config.hooks(project_id.as_ref()).get(hook_type){- add_named_commands(&mut candidates, config);+ add_named_commands(&mut candidates, config, HookSource::User);}// Load project config and add project hook names// Pass write_hints=false to avoid side effects during completion- if let Ok(repo) = Repository::current()+ if let Some(repo) = repo&& let Ok(Some(project_config)) = ProjectConfig::load(&repo, false)&& let Some(config) = project_config.hooks.get(hook_type){- add_named_commands(&mut candidates, config);+ add_named_commands(&mut candidates, config, HookSource::Project);}candidatesdiff --git a/tests/integration_tests/completion.rs b/tests/integration_tests/completion.rsindex d63795727..ede6b8ec5 100644--- a/tests/integration_tests/completion.rs+++ b/tests/integration_tests/completion.rs@@ -892,6 +892,60 @@ fn test_complete_hook_subcommands(repo: TestRepo) {assert!(!subcommands.contains(&"pre-merge"));}+/// Hook names include the same effective user settings used at execution:+/// global hooks, matching wildcard keys, and the exact project key.+#[rstest]+fn test_hook_command_completion_includes_project_user_settings(repo: TestRepo) {+ repo.run_git(&["remote", "remove", "origin"]);+ repo.write_project_config(+ r#"+[pre-merge]+project-file = "echo project"+[post-merge]+other-hook = "echo other hook"+"#,+ );+ repo.write_test_config(&format!(+ r#"+[pre-merge]+global = "echo global"+[projects."*".pre-merge]+wildcard = "echo wildcard"+[projects.'{}'.pre-merge]+exact = "echo exact"+[projects."nonmatching-project".pre-merge]+unrelated = "echo unrelated"+"#,+ repo.project_id()+ ));++ let output = repo+ .completion_cmd(&["wt", "hook", "pre-merge", ""])+ .output()+ .unwrap();+ assert!(output.status.success());+ let stdout = String::from_utf8(output.stdout).unwrap();+ let mut names = value_suggestions(&stdout);+ names.sort_unstable();+ assert_eq!(names, ["exact", "global", "project-file", "wildcard"]);++ for (prefix, expected) in [+ ("user:", vec!["user:exact", "user:global", "user:wildcard"]),+ ("project:", vec!["project:project-file"]),+ ("user:ex", vec!["user:exact"]),+ ] {+ let output = repo+ .completion_cmd(&["wt", "hook", "pre-merge", prefix])+ .output()+ .unwrap();+ assert!(output.status.success());+ let stdout = String::from_utf8(output.stdout).unwrap();+ let mut names = value_suggestions(&stdout);+ names.sort_unstable();+ assert_eq!(names, expected, "{prefix}: {stdout}");+ }+}+/// Cross-shell completion contract for hook command names.////// Same contract as branch completions (test_completion_cross_shell_filtering_contract):