Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: filter completions by prefix for bash shell (#1622)
worktrunk-bot76d886633492
diff --git a/src/completion.rs b/src/completion.rsindex 8031af037..2e3fab334 100644--- a/src/completion.rs+++ b/src/completion.rs@@ -60,7 +60,7 @@ pub(crate) fn maybe_handle_env_completion() -> bool {// Check if the current word is exactly "-" (single dash)// If so, we want to show both short flags (-h) AND long flags (--help)// clap only returns matches for the prefix, so we call complete twice- let current_word = args.get(index).map(|s| s.to_string_lossy());+ let current_word = args.get(index).map(|s| s.to_string_lossy().into_owned());let include_long_flags = current_word.as_deref() == Some("-");let completions = match clap_complete::engine::complete(@@ -104,8 +104,25 @@ pub(crate) fn maybe_handle_env_completion() -> bool {completions};- // Write completions in the appropriate format for the shell+ // Bash does not filter COMPREPLY by prefix — its programmable completion+ // (-F) passes the array as-is. Fish/zsh apply their own matching (substring,+ // fuzzy), so they receive all candidates. For bash, we must filter here.let shell_name = shell_name.to_string_lossy();+ let completions = if shell_name.as_ref() == "bash" {+ let prefix = current_word.as_deref().unwrap_or("").to_owned();+ if prefix.is_empty() {+ completions+ } else {+ completions+ .into_iter()+ .filter(|c| c.get_value().to_string_lossy().starts_with(&*prefix))+ .collect()+ }+ } else {+ completions+ };++ // Write completions in the appropriate format for the shelllet ifs = std::env::var("_CLAP_IFS").ok();let separator = ifs.as_deref().unwrap_or("\n");diff --git a/tests/integration_tests/completion.rs b/tests/integration_tests/completion.rsindex ba3889b12..0f4f1ea2f 100644--- a/tests/integration_tests/completion.rs+++ b/tests/integration_tests/completion.rs@@ -141,7 +141,8 @@ fn test_complete_base_flag_all_formats(repo: TestRepo) {);}- // Test partial completion --base=m (shell handles filtering, we return all)+ // Test partial completion --base=m (clap returns "--base=<value>" form,+ // so bash prefix filter matches correctly: "--base=main".starts_with("--base=m"))let output = repo.completion_cmd(&["wt", "switch", "--create", "new-branch", "--base=m"]).output()@@ -289,7 +290,7 @@ fn test_init_fish_no_inline_completions() {}#[rstest]-fn test_complete_with_partial_prefix_returns_all_branches(repo: TestRepo) {+fn test_complete_with_partial_prefix_returns_all_branches_in_fish(repo: TestRepo) {repo.commit("initial");// Create branches with common prefix@@ -297,18 +298,17 @@ fn test_complete_with_partial_prefix_returns_all_branches(repo: TestRepo) {repo.run_git(&["branch", "feature/two"]);repo.run_git(&["branch", "hotfix/bug"]);- // Complete with partial prefix — binary returns ALL branches, shell does its own- // matching (substring in fish, fuzzy in zsh, prefix in bash). This enables- // fish/zsh substring matching for e.g. `wt switch auth<TAB>` → feature/user-auth.+ // Fish/zsh apply their own matching (substring, fuzzy), so the binary returns+ // ALL candidates. This enables fish/zsh substring matching.let output = repo- .completion_cmd(&["wt", "switch", "feat"])+ .completion_cmd_for_shell(&["wt", "switch", "feat"], "fish").output().unwrap();assert!(output.status.success());let stdout = String::from_utf8_lossy(&output.stdout);let values = value_suggestions(&stdout);- // All branches returned, not just prefix matches+ // All branches returned for fish (no prefix filtering)assert!(values.iter().any(|v| v.contains("feature/one")));assert!(values.iter().any(|v| v.contains("feature/two")));assert!(values.iter().any(|v| v.contains("hotfix/bug")));@@ -317,8 +317,9 @@ fn test_complete_with_partial_prefix_returns_all_branches(repo: TestRepo) {/// Typing a substring that appears mid-branch (e.g. "auth") should still return/// branches containing that substring, because the binary no longer prefix-filters.-/// This is the core use case from #1468: `wt switch auth<TAB>` should let fish/zsh-/// match `feature/user-auth`.+/// Fish/zsh apply their own matching (substring, fuzzy), so the binary returns+/// all candidates for those shells. This is the core use case from #1468:+/// `wt switch auth<TAB>` should let fish/zsh match `feature/user-auth`.#[rstest]fn test_complete_switch_returns_candidates_for_substring_matching(repo: TestRepo) {repo.commit("initial");@@ -327,10 +328,9 @@ fn test_complete_switch_returns_candidates_for_substring_matching(repo: TestReporepo.run_git(&["branch", "bugfix/user-auth-timeout"]);repo.run_git(&["branch", "release/2024-q1"]);- // Type "auth" — not a prefix of any branch, but the binary returns all candidates- // so the shell can apply substring matching+ // Type "auth" in fish — not a prefix of any branch, but fish does substring matchinglet output = repo- .completion_cmd(&["wt", "switch", "auth"])+ .completion_cmd_for_shell(&["wt", "switch", "auth"], "fish").output().unwrap();assert!(output.status.success());@@ -353,6 +353,146 @@ fn test_complete_switch_returns_candidates_for_substring_matching(repo: TestRepo);}+/// Bash does not filter COMPREPLY by prefix — the binary must return only+/// prefix-matching candidates. (#1621)+#[rstest]+fn test_complete_switch_bash_filters_by_prefix(repo: TestRepo) {+ repo.commit("initial");++ repo.run_git(&["branch", "feature/user-auth"]);+ repo.run_git(&["branch", "feature/login"]);+ repo.run_git(&["branch", "bugfix/crash"]);+ repo.run_git(&["branch", "release/2024-q1"]);++ // Type "feat" in bash — should only return branches starting with "feat"+ let output = repo+ .completion_cmd_for_shell(&["wt", "switch", "feat"], "bash")+ .output()+ .unwrap();+ assert!(output.status.success());+ let stdout = String::from_utf8_lossy(&output.stdout);+ let values = value_suggestions(&stdout);++ assert!(+ values.iter().any(|v| v.contains("feature/user-auth")),+ "should include feature/user-auth (prefix match)\n{stdout}"+ );+ assert!(+ values.iter().any(|v| v.contains("feature/login")),+ "should include feature/login (prefix match)\n{stdout}"+ );+ assert!(+ !values.iter().any(|v| v.contains("bugfix/crash")),+ "should NOT include bugfix/crash (not a prefix match)\n{stdout}"+ );+ assert!(+ !values.iter().any(|v| v.contains("release/2024-q1")),+ "should NOT include release/2024-q1 (not a prefix match)\n{stdout}"+ );+}++/// Cross-shell completion contract: each shell gets the filtering it needs.+///+/// This captures the tension between #1468 (fish/zsh need all candidates for+/// substring/fuzzy matching) and #1621 (bash needs prefix filtering because+/// its programmable completion doesn't filter COMPREPLY).+///+/// The same set of branches with the same typed prefix must produce different+/// results depending on the shell:+/// - bash: only prefix matches (binary filters)+/// - fish: all candidates (fish does substring matching)+/// - zsh: all candidates (zsh does fuzzy matching)+#[rstest]+fn test_completion_cross_shell_filtering_contract(repo: TestRepo) {+ repo.commit("initial");++ repo.run_git(&["branch", "feature/user-auth"]);+ repo.run_git(&["branch", "bugfix/auth-timeout"]);+ repo.run_git(&["branch", "release/2024-q1"]);++ // Prefix "feat" — matches feature/* but not bugfix/* or release/*+ for shell in ["fish", "zsh"] {+ let output = repo+ .completion_cmd_for_shell(&["wt", "switch", "feat"], shell)+ .output()+ .unwrap();+ let stdout = String::from_utf8_lossy(&output.stdout);+ let values = value_suggestions(&stdout);+ assert!(+ values.iter().any(|v| v.contains("bugfix/auth-timeout")),+ "{shell} should return ALL candidates (shell does its own matching)\n{stdout}"+ );+ assert!(+ values.iter().any(|v| v.contains("release/2024-q1")),+ "{shell} should return ALL candidates\n{stdout}"+ );+ }++ let output = repo+ .completion_cmd_for_shell(&["wt", "switch", "feat"], "bash")+ .output()+ .unwrap();+ let stdout = String::from_utf8_lossy(&output.stdout);+ let values = value_suggestions(&stdout);+ assert!(+ values.iter().any(|v| v.contains("feature/user-auth")),+ "bash should return prefix matches\n{stdout}"+ );+ assert!(+ !values.iter().any(|v| v.contains("bugfix/auth-timeout")),+ "bash should NOT return non-prefix matches\n{stdout}"+ );+ assert!(+ !values.iter().any(|v| v.contains("release/2024-q1")),+ "bash should NOT return non-prefix matches\n{stdout}"+ );++ // Substring "auth" — appears mid-branch, not as a prefix+ for shell in ["fish", "zsh"] {+ let output = repo+ .completion_cmd_for_shell(&["wt", "switch", "auth"], shell)+ .output()+ .unwrap();+ let stdout = String::from_utf8_lossy(&output.stdout);+ let values = value_suggestions(&stdout);+ assert!(+ values.iter().any(|v| v.contains("feature/user-auth")),+ "{shell} should return all candidates so shell can substring-match 'auth'\n{stdout}"+ );+ }++ let output = repo+ .completion_cmd_for_shell(&["wt", "switch", "auth"], "bash")+ .output()+ .unwrap();+ let stdout = String::from_utf8_lossy(&output.stdout);+ let values = value_suggestions(&stdout);+ assert!(+ !values.iter().any(|v| v.contains("feature/user-auth")),+ "bash should not return 'feature/user-auth' — 'auth' is not a prefix\n{stdout}"+ );+}++/// Bash with empty prefix should still return all branches.+#[rstest]+fn test_complete_switch_bash_empty_prefix_shows_all(repo: TestRepo) {+ repo.commit("initial");++ repo.run_git(&["branch", "feature/new"]);+ repo.run_git(&["branch", "bugfix/crash"]);++ let output = repo+ .completion_cmd_for_shell(&["wt", "switch", ""], "bash")+ .output()+ .unwrap();+ assert!(output.status.success());+ let stdout = String::from_utf8_lossy(&output.stdout);++ assert!(stdout.contains("feature/new"));+ assert!(stdout.contains("bugfix/crash"));+ assert!(stdout.contains("main"));+}+#[rstest]fn test_complete_switch_shows_all_branches_even_with_worktrees(mut repo: TestRepo) {repo.commit("initial");