Skip to content

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

feat(completion): complete remote-qualified switch branches (#4613)

Worktrunk Botbc8ad63e232b
diff --git a/docs/src/content/docs/switch.md b/docs/src/content/docs/switch.md
index 6ec3eca1c..d9cc1858f 100644
--- a/docs/src/content/docs/switch.md
+++ b/docs/src/content/docs/switch.md
@@ -33,6 +33,8 @@ $ wt switch https://github.com/owner/repo/pull/123 # ...or paste the PR's URL
The `--create` flag creates a new branch from `--base` — the default branch unless specified. Without `--create`, the branch must already exist. Switching to a remote branch (e.g., `wt switch feature` when only `origin/feature` exists) creates a local tracking branch.
+To complete branches from a specific remote, type its prefix before pressing Tab (e.g., `wt switch origin/`).
+
A new branch tracks the remote branch it starts from only when the two share a name: `--create release --base origin/release` tracks `origin/release`, while `--create feature --base origin/release` gets no upstream. Publish such a branch with `git push --set-upstream origin <branch>`, or set git's `push.autoSetupRemote = true` once.
## Creating worktrees
diff --git a/plugins/worktrunk/skills/worktrunk/reference/switch.md b/plugins/worktrunk/skills/worktrunk/reference/switch.md
index 7730ef06b..ad6cb6c26 100644
--- a/plugins/worktrunk/skills/worktrunk/reference/switch.md
+++ b/plugins/worktrunk/skills/worktrunk/reference/switch.md
@@ -19,6 +19,8 @@ $ wt switch https://github.com/owner/repo/pull/123 # ...or paste the PR's URL
The `--create` flag creates a new branch from `--base` — the default branch unless specified. Without `--create`, the branch must already exist. Switching to a remote branch (e.g., `wt switch feature` when only `origin/feature` exists) creates a local tracking branch.
+To complete branches from a specific remote, type its prefix before pressing Tab (e.g., `wt switch origin/`).
+
A new branch tracks the remote branch it starts from only when the two share a name: `--create release --base origin/release` tracks `origin/release`, while `--create feature --base origin/release` gets no upstream. Publish such a branch with `git push --set-upstream origin <branch>`, or set git's `push.autoSetupRemote = true` once.
## Creating worktrees
diff --git a/skills/worktrunk/reference/switch.md b/skills/worktrunk/reference/switch.md
index 7730ef06b..ad6cb6c26 100644
--- a/skills/worktrunk/reference/switch.md
+++ b/skills/worktrunk/reference/switch.md
@@ -19,6 +19,8 @@ $ wt switch https://github.com/owner/repo/pull/123 # ...or paste the PR's URL
The `--create` flag creates a new branch from `--base` — the default branch unless specified. Without `--create`, the branch must already exist. Switching to a remote branch (e.g., `wt switch feature` when only `origin/feature` exists) creates a local tracking branch.
+To complete branches from a specific remote, type its prefix before pressing Tab (e.g., `wt switch origin/`).
+
A new branch tracks the remote branch it starts from only when the two share a name: `--create release --base origin/release` tracks `origin/release`, while `--create feature --base origin/release` gets no upstream. Publish such a branch with `git push --set-upstream origin <branch>`, or set git's `push.autoSetupRemote = true` once.
## Creating worktrees
diff --git a/src/cli/mod.rs b/src/cli/mod.rs
index e05d4ddfe..b6c3e3a00 100644
--- a/src/cli/mod.rs
+++ b/src/cli/mod.rs
@@ -648,6 +648,8 @@ $ wt switch https://github.com/owner/repo/pull/123 # ...or paste the PR's URL
The `--create` flag creates a new branch from `--base` — the default branch unless specified. Without `--create`, the branch must already exist. Switching to a remote branch (e.g., `wt switch feature` when only `origin/feature` exists) creates a local tracking branch.
+To complete branches from a specific remote, type its prefix before pressing Tab (e.g., `wt switch origin/`).
+
A new branch tracks the remote branch it starts from only when the two share a name: `--create release --base origin/release` tracks `origin/release`, while `--create feature --base origin/release` gets no upstream. Publish such a branch with `git push --set-upstream origin <branch>`, or set git's `push.autoSetupRemote = true` once.
## Creating worktrees
diff --git a/src/completion.rs b/src/completion.rs
index 5fc2911c4..4d81d970a 100644
--- a/src/completion.rs
+++ b/src/completion.rs
@@ -30,6 +30,7 @@
//! skips prewarm's rev-parse batch (benches/AGENTS.md, "Analyzing a trace").
use std::cell::RefCell;
+use std::collections::HashSet;
use std::ffi::{OsStr, OsString};
use std::io::Write;
@@ -275,6 +276,7 @@ pub(crate) fn branch_value_completer() -> ArgValueCompleter {
suppress_with_create: false,
exclude_remote_only: false,
worktree_only: false,
+ qualify_remote_prefix: false,
})
}
@@ -285,6 +287,7 @@ pub(crate) fn worktree_branch_completer() -> ArgValueCompleter {
suppress_with_create: true,
exclude_remote_only: false,
worktree_only: false,
+ qualify_remote_prefix: true,
})
}
@@ -294,6 +297,7 @@ pub(crate) fn local_branches_completer() -> ArgValueCompleter {
suppress_with_create: false,
exclude_remote_only: true,
worktree_only: false,
+ qualify_remote_prefix: false,
})
}
@@ -303,6 +307,7 @@ pub(crate) fn worktree_only_completer() -> ArgValueCompleter {
suppress_with_create: false,
exclude_remote_only: false,
worktree_only: true,
+ qualify_remote_prefix: false,
})
}
@@ -405,6 +410,7 @@ struct BranchCompleter {
suppress_with_create: bool,
exclude_remote_only: bool,
worktree_only: bool,
+ qualify_remote_prefix: bool,
}
impl ValueCompleter for BranchCompleter {
@@ -422,6 +428,58 @@ impl ValueCompleter for BranchCompleter {
return Vec::new();
}
+ // A typed remote prefix is an explicit request for remote-tracking
+ // names. Keep the qualified name so the shell can complete `origin/…`,
+ // including in repositories where the general list omits remotes.
+ if self.qualify_remote_prefix
+ && let Some(current) = current.to_str()
+ && current.contains('/')
+ && let Ok(repo) = Repository::current()
+ && repo.all_remote_urls().iter().any(|(name, _)| {
+ current
+ .strip_prefix(name)
+ .is_some_and(|suffix| suffix.starts_with('/'))
+ })
+ {
+ std::thread::scope(|scope| {
+ scope.spawn(|| {
+ let _ = repo.local_branches();
+ });
+ scope.spawn(|| {
+ let _ = repo.remote_branches();
+ });
+ });
+ let remotes = repo.remote_branches().unwrap_or_default();
+ let locals = repo.local_branches().unwrap_or_default();
+ let local_name_set: HashSet<_> =
+ locals.iter().map(|branch| branch.name.as_str()).collect();
+ let local_names: Vec<_> = locals
+ .iter()
+ .filter(|branch| branch.name.starts_with(current))
+ .collect();
+ let mut candidates: Vec<_> = local_names
+ .iter()
+ .map(|branch| {
+ let help = format!("/ {}", format_relative_time_short(branch.committer_ts));
+ CompletionCandidate::new(branch.name.clone()).help(Some(help.into()))
+ })
+ .collect();
+ candidates.extend(
+ remotes
+ .iter()
+ .filter(|remote| {
+ remote.short_name.starts_with(current)
+ && !local_name_set.contains(remote.short_name.as_str())
+ && !local_name_set.contains(remote.local_name.as_str())
+ })
+ .map(|remote| {
+ let help = format!("⇣ {}", format_relative_time_short(remote.committer_ts));
+ CompletionCandidate::new(remote.short_name.clone()).help(Some(help.into()))
+ }),
+ );
+ return candidates;
+ }
+
// Whether a remote-only branch could reach the user from this argument
// at all. It decides the scan as well as the filter: a completer that
// answers "no" never runs `for-each-ref refs/remotes/`, which on a
diff --git a/tests/integration_tests/completion.rs b/tests/integration_tests/completion.rs
index 6052876df..f8f07ec1b 100644
--- a/tests/integration_tests/completion.rs
+++ b/tests/integration_tests/completion.rs
@@ -1870,7 +1870,10 @@ fn test_complete_switch_excludes_remote_branches_when_over_threshold(mut repo: T
repo.run_git(&delete);
repo.run_git(&["fetch", "origin"]);
- // Total branches: 1 (main worktree) + 50 local + 60 remote = 111 > 100
+ // A local branch can literally contain the same remote-looking prefix.
+ repo.run_git(&["branch", "origin/local-only"]);
+
+ // The branch total exceeds the 100-candidate cutoff.
let output = repo.completion_cmd(&["wt", "switch", ""]).output().unwrap();
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
@@ -1887,6 +1890,42 @@ fn test_complete_switch_excludes_remote_branches_when_over_threshold(mut repo: T
!suggestions.iter().any(|s| s.contains("remote/branch-")),
"Remote-only branches should be excluded when total > 100: {stdout}"
);
+
+ // `wt switch origin/remote/branch-0` resolves to this local branch.
+ repo.run_git(&["branch", "remote/branch-0"]);
+
+ // An explicit remote prefix still gives access to that remote's branches.
+ let output = repo
+ .completion_cmd_for_shell(&["wt", "switch", "origin/remote/"], "fish")
+ .output()
+ .unwrap();
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ assert!(
+ value_suggestions(&stdout)
+ .iter()
+ .any(|s| s.split('\t').next() == Some("origin/remote/branch-1")),
+ "Remote-qualified branch should complete: {stdout}"
+ );
+ assert!(
+ !value_suggestions(&stdout)
+ .iter()
+ .any(|s| s.split('\t').next() == Some("origin/remote/branch-0")),
+ "A remote-qualified name that selects a local branch should not be offered: {stdout}"
+ );
+
+ let output = repo
+ .completion_cmd_for_shell(&["wt", "switch", "origin/"], "fish")
+ .output()
+ .unwrap();
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ assert!(
+ value_suggestions(&stdout)
+ .iter()
+ .any(|s| s.split('\t').next() == Some("origin/local-only")),
+ "A local branch with a remote-looking name should still complete: {stdout}"
+ );
}
#[rstest]
diff --git a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap b/tests/snapshots/integration__integration_tests__help__help_switch_long.snap
index a4c0ce97f..20c98d53d 100644
--- a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap
+++ b/tests/snapshots/integration__integration_tests__help__help_switch_long.snap
@@ -160,6 +160,8 @@ Worktrees are addressed by branch name; paths are computed from a configurable t
The --create flag creates a new branch from --base — the default branch unless specified. Without --create, the branch must already exist. Switching to a remote branch (e.g., wt switch feature when only origin/feature exists) creates a local tracking branch.
+To complete branches from a specific remote, type its prefix before pressing Tab (e.g., wt switch origin/).
+
A new branch tracks the remote branch it starts from only when the two share a name: --create release --base origin/release tracks origin/release, while --create feature --base origin/release gets no upstream. Publish such a branch with git push --set-upstream origin <branch>, or set git's push.autoSetupRemote = true once.
Creating worktrees