Repository navigation
feat(switch): allow opt-in navigation to branch-mismatched worktrees - #4480
BenJurewicz wants to merge 6 commits into
Conversation
Add the opt-in switch.allow-branch-mismatch setting with a false default. Preserve explicit false values when project settings override global configuration. Validation at this commit on Linux aarch64: all six switch configuration unit tests pass (cargo test --locked --lib test_switch_config).
Add paired branch-mismatch flags and honor the switch configuration preference. Navigate to a usable occupying worktree without changing its checkout, and use its actual branch for hooks, JSON, and execution. Keep creation, explicit placement, and PR/MR selection strict. Include the generated command help and documentation for the flags. Validation at this commit on Linux aarch64: 255 existing switch tests, 56 help tests, and documentation sync pass, using cargo test --locked --features shell-integration-tests --test integration with the integration_tests::switch::, test_help, and test_docs_are_in_sync filters.
Cover command-line and configuration enablement, explicit overrides, detached checkout navigation, warning output, and hook context. Assert that navigation preserves the checkout and untracked files. Validation at this commit on Linux aarch64: all seven branch-mismatch integration cases pass (cargo test --locked --features shell-integration-tests --test integration branch_mismatch).
Describe expected-path fallback, configuration enablement, and the per-command override. Document branch lookup priority and the creation, placement, and PR/MR exclusions. Update generated guides and examples. Validation at this commit on Linux aarch64: documentation sync, user config example sync, and all 56 help tests pass, using cargo test --locked --features shell-integration-tests --test integration with the test_docs_are_in_sync, test_config_source_generates_example_toml, and test_help filters.
Reload the worktree listing after opt-in selection so pre-switch hooks can create a destination without leaving planning on a stale inventory. Preserve the destination commit identity for detached fallback hooks. Cover hook mutations and failures, branch priority, strict creation and PR selection, unusable registrations, ordinary directories, default-branch navigation, and the parent-shell directory change. Validation on Linux aarch64: 5,342 tests pass, with one skipped. Formatting, all-target/all-feature Clippy, doctests, documentation sync, and the Rust documentation build pass.
worktrunk-bot
left a comment
There was a problem hiding this comment.
This adds a CLI flag pair and a switch.allow-branch-mismatch config key. AGENTS.md ("Project Status") calls both product decisions and protected interfaces once released, so @max-sixty should decide whether to ship them. I'm leaving this as a comment rather than an approval.
The capability is mostly available already. wt switch resolves a worktree's path as a selector, including detached and other-branch checkouts (wt switch ../repo.feature), and the picker can select those rows too. The occupied-path error already names that path. If the goal is to shorten the recovery from that error, a smaller change would be to add wt switch <path> to the WorktreePathOccupied hint, which today suggests only cd <path> && git switch <branch>. This PR instead adds about 150 lines to the switch pipeline: a second target resolution before hooks, a rebuilt Repository for planning, and a post-hook identity check. That is a lot of new state for a case one hint line could cover.
The config default also changes what wt switch feature -x '<cmd>' means. With it on, the command runs on whatever branch occupies feature's template path, and only a warning on stderr signals the difference. Scripts and agents that pass a branch name usually assume {{ branch }} is that branch. A per-invocation flag keeps that choice explicit. A persistent default makes it apply silently.
One correctness gap: once a mismatch is selected, plan_switch returns SwitchPlan::Existing before it resolves the branch again, and require_unchanged checks only the occupant. If a pre-switch hook creates a worktree for the requested branch elsewhere, the switch still goes to the mismatched checkout. That contradicts the documented "branch lookup still takes priority" behavior. It is narrow, but the comment above planning_repo says hooks "may create a destination", and the mismatch path never looks for one.
Resolve the requested branch against the refreshed worktree listing before using an expected-path fallback. A worktree created for that branch by a pre-switch hook takes priority. Emit the mismatch warning only when the fallback is selected. Cover branch priority both before and during pre-switch hooks, including the selected branch, path, and absence of a fallback warning. Validation on Linux aarch64: 5,343 tests pass, with one skipped, using RUSTFLAGS="-D warnings" cargo nextest run --locked --all-features --no-fail-fast. Formatting, all-target/all-feature Clippy with warnings denied, and doctests pass.
|
Changed it such that if a pre-switch hook creates a worktree somewhere else, that worktree is picked instead |
worktrunk-bot
left a comment
There was a problem hiding this comment.
Moving the fallback after branch resolution closes the hook-created-destination gap, and the during_hook case pins it. The warning now prints only when the fallback is actually used. Whether to ship the flag and config key is still up to @max-sixty, as noted in the earlier review.
|
FYI Reasoning for the feature: I use worktrunk manually to be able to work on or review multiple branches at the same time. For that reason I wanted to add a config flag that will switch me to that worktree and then notify me that the branch is wrong CLI flags are mostly for being able to overwrite config in scripts or be able to test it quickly. The feature is disabled by default so there is no impact on users who do not need such a feature or use worktrunk only through their AI agents. |
|
Thanks, that use case explains the config key. Coming back to a worktree you've moved to another branch is a real gap, because One thing to weigh on "no impact on users who only use worktrunk through AI agents": Whether that tradeoff is worth it is still @max-sixty's call. |
|
worktrunk support branches and paths, so we should use one of those; not a "worktree that resolves under a different branch name based on the default path". lmk if I'm misunderstanding. the API is intentionally closely curated so we're very careful about adding new flags |
What would be your suggestion in solving my issue? Changing a error to a warning seemed the simplest to me and thats the path that I chose, but I'm interested in hearing your suggestion. My proposition would be to change the error to a error with a conformation of something like "do you want to go to that worktree nonetheless? [y/N]" and maybe a option to change if the default is yes or no. Then agents using it with without a interactive tty would just get the error, while humans would get the same message but instead of being asked to type the cd manually, they could just press |
|
One thing that may already cover most of this without a new flag: the error names the branch the worktree is on now, and Whether the error should also offer a |
|
I'd prefer a way that does not require another command. If you think that there is no way to achieve what I want in a upstremable way, then as long as this error is reported in a script friendly way, I could probably have a simple bash wrapper that achieves my desired functionality. |
|
Today the error isn't script-friendly. It exits 1, prints only prose on stderr, and A wrapper doesn't need the error, though. It can compute the expected path itself with wts() {
wt switch "$@" && return
local rc=$? p
p=$(wt step eval "{{ repo_path }}/../{{ repo }}.{{ \"$1\" | sanitize }}") || return "$rc"
[ -d "$p" ] || return "$rc"
wt switch "$p"
}If you've set a custom template, put it in that |
|
@BenJurewicz very reasonable! @worktrunk-bot can we think about to make this scriptable? for example, is there a command to generate the expected path? then it might be easily scriptable, if we can set the path to switch to successfully |
|
Not today. Switching by path already works for a worktree on another branch: Two small ways to close that gap without a new switch flag or config key:
(1) is the smaller one, and it's enough for Ben's wrapper. I can open a PR for it if you want it. |
|
yes let's build (1) |
|
Opened #4502 with this as |
--allow-branch-mismatchto navigate to an existing worktree at the requested branch’s expected path, even when it contains another branch or a detached checkout. Warn without changing the checkout.switch.allow-branch-mismatch, disabled by default, and--no-allow-branch-mismatchto override it per command.--create,--path, and PR/MR selectors.--execute. Refresh the worktree listing after pre-switch hooks and reject a fallback whose branch or registration changed.TLDR: Allow for switching to a worktree with a mismatched branch