Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Exclude remote branches from `get_all_branches_in` for completions

Maximilian Roos54d0944dbf7f
diff --git a/src/git.rs b/src/git.rs
index c61f84128..818919388 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -521,10 +521,11 @@ pub fn get_all_branches() -> Result<Vec<String>, GitError> {
get_all_branches_in(std::path::Path::new("."))
}
-/// Get all branch names in a specific directory
+/// Get all branch names in a specific directory (local branches only)
+/// Note: This excludes remote-tracking branches (e.g., origin/main)
pub fn get_all_branches_in(path: &std::path::Path) -> Result<Vec<String>, GitError> {
let stdout = run_git_command(
- &["branch", "--all", "--format=%(refname:short)"],
+ &["branch", "--format=%(refname:short)"], // Removed --all to exclude remotes
Some(path),
)?;
Ok(stdout
diff --git a/src/main.rs b/src/main.rs
index 25a8d8b26..c708f8fdb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -941,4 +941,64 @@ mod tests {
let args = vec!["wt".to_string()];
assert_eq!(parse_completion_context(&args), CompletionContext::Unknown);
}
+
+ #[test]
+ fn test_parse_completion_context_base_flag_short() {
+ let args = vec![
+ "wt".to_string(),
+ "switch".to_string(),
+ "--create".to_string(),
+ "new".to_string(),
+ "-b".to_string(),
+ "dev".to_string(),
+ ];
+ assert_eq!(parse_completion_context(&args), CompletionContext::BaseFlag);
+ }
+
+ #[test]
+ fn test_parse_completion_context_base_at_end() {
+ // --base at the end with empty string (what shell sends when completing)
+ let args = vec![
+ "wt".to_string(),
+ "switch".to_string(),
+ "--create".to_string(),
+ "new".to_string(),
+ "--base".to_string(),
+ "".to_string(), // Shell sends empty string for cursor position
+ ];
+ // Should detect BaseFlag context
+ assert_eq!(parse_completion_context(&args), CompletionContext::BaseFlag);
+ }
+
+ #[test]
+ fn test_parse_completion_context_multiple_base_flags() {
+ // Multiple --base flags (last one wins)
+ let args = vec![
+ "wt".to_string(),
+ "switch".to_string(),
+ "--create".to_string(),
+ "new".to_string(),
+ "--base".to_string(),
+ "main".to_string(),
+ "--base".to_string(),
+ "develop".to_string(),
+ ];
+ assert_eq!(parse_completion_context(&args), CompletionContext::BaseFlag);
+ }
+
+ #[test]
+ fn test_parse_completion_context_empty_args() {
+ let args = vec![];
+ assert_eq!(parse_completion_context(&args), CompletionContext::Unknown);
+ }
+
+ #[test]
+ fn test_parse_completion_context_switch_only() {
+ // Just "wt switch" with no other args
+ let args = vec!["wt".to_string(), "switch".to_string()];
+ assert_eq!(
+ parse_completion_context(&args),
+ CompletionContext::SwitchBranch
+ );
+ }
}
diff --git a/tests/integration_tests/completion.rs b/tests/integration_tests/completion.rs
index 49e1eb7e4..b74f4bbd1 100644
--- a/tests/integration_tests/completion.rs
+++ b/tests/integration_tests/completion.rs
@@ -345,3 +345,231 @@ fn test_complete_switch_no_available_branches() {
assert!(!stdout.contains("hotfix/bug"));
// May include main if it doesn't have a worktree
}
+
+#[test]
+fn test_complete_excludes_remote_branches() {
+ let temp = TestRepo::new();
+ temp.commit("initial");
+
+ // Create local branches
+ StdCommand::new("git")
+ .args(&["branch", "feature/local"])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ // Set up a fake remote
+ StdCommand::new("git")
+ .args(&["remote", "add", "origin", "https://example.com/repo.git"])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ // Create a remote-tracking branch by fetching from a local "remote"
+ // First, create a bare repo to act as remote
+ let remote_dir = temp.root_path().parent().unwrap().join("remote.git");
+ StdCommand::new("git")
+ .args(&["init", "--bare", remote_dir.to_str().unwrap()])
+ .output()
+ .unwrap();
+
+ // Update remote URL to point to our bare repo
+ StdCommand::new("git")
+ .args(&["remote", "set-url", "origin", remote_dir.to_str().unwrap()])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ // Push to create remote branches
+ StdCommand::new("git")
+ .args(&["push", "origin", "main"])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ StdCommand::new("git")
+ .args(&["push", "origin", "feature/local:feature/remote"])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ // Fetch to create remote-tracking branches
+ StdCommand::new("git")
+ .args(&["fetch", "origin"])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ // Test completion
+ let mut cmd = Command::cargo_bin("wt").unwrap();
+ let output = cmd
+ .current_dir(temp.root_path())
+ .args(&["complete", "wt", "switch", ""])
+ .output()
+ .unwrap();
+
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+
+ // Should include local branch without worktree
+ assert!(
+ stdout.contains("feature/local"),
+ "Should include feature/local branch, but got: {}",
+ stdout
+ );
+
+ // main branch has a worktree (the root repo), so it may or may not be included
+ // depending on switch context - not critical for this test
+
+ // Should NOT include remote-tracking branches (origin/*)
+ assert!(
+ !stdout.contains("origin/"),
+ "Completion should not include remote-tracking branches, but found: {}",
+ stdout
+ );
+}
+
+#[test]
+fn test_complete_merge_shows_branches() {
+ let mut temp = TestRepo::new();
+ temp.commit("initial");
+
+ // Create worktree (creates "feature/new" branch)
+ temp.add_worktree("feature-worktree", "feature/new");
+
+ // Create another branch without worktree
+ StdCommand::new("git")
+ .args(&["branch", "hotfix/bug"])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ // Test completion for merge (should show ALL branches, including those with worktrees)
+ let mut cmd = Command::cargo_bin("wt").unwrap();
+ let output = cmd
+ .current_dir(temp.root_path())
+ .args(&["complete", "wt", "merge", ""])
+ .output()
+ .unwrap();
+
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let branches: Vec<&str> = stdout.lines().collect();
+
+ // Should include both branches (merge shows all)
+ assert!(branches.iter().any(|b| b.contains("feature/new")));
+ assert!(branches.iter().any(|b| b.contains("hotfix/bug")));
+}
+
+#[test]
+fn test_complete_unknown_command_returns_empty() {
+ let temp = TestRepo::new();
+ temp.commit("initial");
+
+ // Create some branches
+ StdCommand::new("git")
+ .args(&["branch", "feature/test"])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ // Test completion for unknown command
+ let mut cmd = Command::cargo_bin("wt").unwrap();
+ let output = cmd
+ .current_dir(temp.root_path())
+ .args(&["complete", "wt", "unknown-command", ""])
+ .output()
+ .unwrap();
+
+ // Should succeed but return no completions
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ assert_eq!(stdout.trim(), "");
+}
+
+#[test]
+fn test_complete_with_special_characters_in_branch_names() {
+ let temp = TestRepo::new();
+ temp.commit("initial");
+
+ // Create branches with various special characters
+ let branch_names = vec![
+ "feature/FOO-123", // Uppercase + dash + numbers
+ "release/v1.2.3", // Dots
+ "hotfix/bug_fix", // Underscore
+ "feature/multi-part-name", // Multiple dashes
+ ];
+
+ for branch in &branch_names {
+ StdCommand::new("git")
+ .args(&["branch", branch])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+ }
+
+ // Test completion
+ let mut cmd = Command::cargo_bin("wt").unwrap();
+ let output = cmd
+ .current_dir(temp.root_path())
+ .args(&["complete", "wt", "switch", ""])
+ .output()
+ .unwrap();
+
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+
+ // All branches should be present
+ for branch in &branch_names {
+ assert!(
+ stdout.contains(branch),
+ "Branch {} should be in completion output",
+ branch
+ );
+ }
+}
+
+#[test]
+fn test_complete_list_command_returns_empty() {
+ let temp = TestRepo::new();
+ temp.commit("initial");
+
+ // Create branches
+ StdCommand::new("git")
+ .args(&["branch", "feature/test"])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ // Test completion for list command (doesn't take branch arguments)
+ let mut cmd = Command::cargo_bin("wt").unwrap();
+ let output = cmd
+ .current_dir(temp.root_path())
+ .args(&["complete", "wt", "list", ""])
+ .output()
+ .unwrap();
+
+ // Should succeed but return no completions
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ assert_eq!(stdout.trim(), "");
+}
+
+#[test]
+fn test_complete_finish_command_returns_empty() {
+ let temp = TestRepo::new();
+ temp.commit("initial");
+
+ // Test completion for finish command (doesn't take arguments)
+ let mut cmd = Command::cargo_bin("wt").unwrap();
+ let output = cmd
+ .current_dir(temp.root_path())
+ .args(&["complete", "wt", "finish", ""])
+ .output()
+ .unwrap();
+
+ // Should succeed but return no completions
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ assert_eq!(stdout.trim(), "");
+}