Skip to content

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

fix: strip remote prefix when switching to remote branch from picker (#1266)

worktrunk-bot70b96e9672e0
diff --git a/src/commands/worktree/switch.rs b/src/commands/worktree/switch.rs
index a0ccb3ef3..736c4fa3b 100644
--- a/src/commands/worktree/switch.rs
+++ b/src/commands/worktree/switch.rs
@@ -353,10 +353,16 @@ fn resolve_switch_target(
}
// Regular branch switch
- let resolved_branch = repo
+ let mut resolved_branch = repo
.resolve_worktree_name(branch)
.context("Failed to resolve branch name")?;
+ // Handle remote-tracking ref names (e.g., "origin/username/feature-1" from the picker).
+ // Strip the remote prefix so DWIM can create a local tracking branch.
+ if !create && let Some(local_name) = repo.strip_remote_prefix(&resolved_branch) {
+ resolved_branch = local_name;
+ }
+
// Resolve and validate base (only when --create is set)
let resolved_base = if let Some(base_str) = base {
if !create {
diff --git a/src/git/repository/remotes.rs b/src/git/repository/remotes.rs
index 7079db6dc..e8cb51064 100644
--- a/src/git/repository/remotes.rs
+++ b/src/git/repository/remotes.rs
@@ -214,4 +214,34 @@ impl Repository {
])
.is_ok()
}
+
+ /// Strip the remote prefix from a remote-tracking branch name.
+ ///
+ /// Given a name like `origin/username/feature-1`, returns `Some("username/feature-1")`
+ /// if it's a valid remote-tracking ref. Returns `None` if the name isn't a remote ref
+ /// or the remote can't be identified.
+ ///
+ /// This handles remote names that don't contain `/` (the common case). It lists
+ /// all configured remotes and finds the one that matches the prefix.
+ ///
+ /// TODO: A cleaner approach would be to strip the prefix upstream — either have
+ /// `list_remote_branches()` return `(remote, local_branch, sha)` tuples, or track
+ /// `is_remote` on `ListItem` so the picker outputs just the local branch name.
+ /// Either would eliminate this runtime `git remote` call. See #1260.
+ pub fn strip_remote_prefix(&self, ref_name: &str) -> Option<String> {
+ // Quick check: is this actually a remote-tracking ref?
+ if !self.is_remote_tracking_branch(ref_name) {
+ return None;
+ }
+
+ // List all remotes and find the one that is a prefix of ref_name
+ let output = self.run_command(&["remote"]).ok()?;
+ output.lines().find_map(|remote| {
+ let prefix = format!("{}/", remote.trim());
+ ref_name
+ .strip_prefix(&prefix)
+ .filter(|branch| !branch.is_empty())
+ .map(|branch| branch.to_string())
+ })
+ }
}
diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rs
index b0eac95fd..747f0c1e1 100644
--- a/tests/integration_tests/switch.rs
+++ b/tests/integration_tests/switch.rs
@@ -149,6 +149,42 @@ fn test_switch_dwim_from_remote(#[from(repo_with_remote)] repo: TestRepo) {
snapshot_switch("switch_dwim_from_remote", &repo, &["dwim-feature"]);
}
+/// When the branch argument includes the remote prefix (e.g., "origin/feature"),
+/// strip the prefix and switch to the local branch via DWIM.
+/// This happens when the interactive picker returns a remote branch name.
+#[rstest]
+fn test_switch_remote_prefix_stripped(#[from(repo_with_remote)] repo: TestRepo) {
+ // Create a branch on the remote only (no local branch)
+ repo.run_git(&["branch", "remote-feature"]);
+ repo.run_git(&["push", "origin", "remote-feature"]);
+ repo.run_git(&["branch", "-D", "remote-feature"]);
+
+ // Passing "origin/remote-feature" should strip the prefix and DWIM to local branch
+ snapshot_switch(
+ "switch_remote_prefix_stripped",
+ &repo,
+ &["origin/remote-feature"],
+ );
+}
+
+/// When the branch name contains slashes (e.g., "username/feature-1") and the picker
+/// returns it with the remote prefix ("origin/username/feature-1"), the remote prefix
+/// should be stripped correctly. Regression test for #1260.
+#[rstest]
+fn test_switch_remote_prefix_stripped_slash_in_branch(#[from(repo_with_remote)] repo: TestRepo) {
+ // Create a branch with / in the name on the remote only
+ repo.run_git(&["branch", "username/feature-1"]);
+ repo.run_git(&["push", "origin", "username/feature-1"]);
+ repo.run_git(&["branch", "-D", "username/feature-1"]);
+
+ // Passing "origin/username/feature-1" should strip "origin/" and DWIM correctly
+ snapshot_switch(
+ "switch_remote_prefix_slash_branch",
+ &repo,
+ &["origin/username/feature-1"],
+ );
+}
+
/// When a branch exists on multiple remotes, DWIM should fail with an error
/// since git can't determine which remote to track.
#[rstest]
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_remote_prefix_slash_branch.snap b/tests/snapshots/integration__integration_tests__switch__switch_remote_prefix_slash_branch.snap
new file mode 100644
index 000000000..636f07afa
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_remote_prefix_slash_branch.snap
@@ -0,0 +1,47 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - origin/username/feature-1
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"
+ NO_COLOR: ""
+ PATH: "[PATH]"
+ PSModulePath: ""
+ RUST_LOG: warn
+ SHELL: ""
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
+ WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
+ WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_NUSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+
+----- stderr -----
+✓ Created branch username/feature-1 (tracking origin/username/feature-1) and worktree @ _REPO_.username-feature-1
+▲ Cannot change directory — shell integration not installed
+↳ To enable automatic cd, run wt config shell install
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_remote_prefix_stripped.snap b/tests/snapshots/integration__integration_tests__switch__switch_remote_prefix_stripped.snap
new file mode 100644
index 000000000..afe71771a
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_remote_prefix_stripped.snap
@@ -0,0 +1,47 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - origin/remote-feature
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"
+ NO_COLOR: ""
+ PATH: "[PATH]"
+ PSModulePath: ""
+ RUST_LOG: warn
+ SHELL: ""
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
+ WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
+ WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_NUSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+
+----- stderr -----
+✓ Created branch remote-feature (tracking origin/remote-feature) and worktree @ _REPO_.remote-feature
+▲ Cannot change directory — shell integration not installed
+↳ To enable automatic cd, run wt config shell install