Skip to content

fix(switch): interactive picker runs the full switch pipeline - #2845

Merged
max-sixty merged 3 commits into
mainfrom
2837
May 21, 2026
Merged

max-sixty merged 3 commits into
mainfrom
2837

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

With [switch] cd = false or wt switch --no-cd, opening the interactive picker (wt switch with no branch argument) and selecting a worktree printed the branch name and exited — no switch, no hooks, and Alt-c created nothing. #1445 introduced this as an explicit print-only mode, but it surprises anyone who sets cd = false as a standing navigation preference (driving worktree switches through tmux or cmux): the picker silently stops doing anything.

The picker now runs the same plan_switch → execute_switch pipeline as wt switch <branch>, suppressing only the cd directive. Pre- and post-switch hooks fire, Alt-c creates the worktree, and --format=json works in the picker too (requires = "branch" and the --branches/--remotes conflict are dropped from the flag). JSON emission is extracted into a shared emit_switch_json so the argument path and the picker produce identical output.

This is a behavior change: the print-only picker mode is gone. Scripts that captured its bare-branch-name output should switch to wt switch --format=json, which both performs the switch and prints a structured action/branch/path result — now shown as a pick alias example in tips-patterns. The cmux recipe drops its pick alias workaround, since the picker fires pre-switch hooks directly.

Closes #2837. Thanks to @endigma for the discussion that pinned down the intended behavior: the picker should be identical to passing the branch as an argument.

max-sixty and others added 2 commits May 20, 2026 20:31
With `[switch] cd = false` or `wt switch --no-cd`, opening the
interactive picker (`wt switch` with no branch argument) and selecting
a worktree printed the branch name and exited: no switch, no hooks, and
`Alt-c` created nothing.

The picker now runs the same plan_switch/execute_switch pipeline as
`wt switch <branch>`, suppressing only the cd directive. Pre- and
post-switch hooks fire, `Alt-c` creates the worktree, and `--format=json`
is honored in the picker too — `requires = "branch"` and the
`--branches`/`--remotes` conflict are dropped from the flag. JSON
emission is extracted into a shared `emit_switch_json` so the argument
path and the picker produce identical output.

Scripts that relied on the old print-only output should use
`wt switch --format=json`, which both switches and prints a structured
result.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
# Conflicts:
#	docs/content/tips-patterns.md
#	skills/worktrunk/reference/tips-patterns.md
`wt select` (deprecated) routes through `handle_select_command` to
`handle_picker`, but no test exercised it, so `codecov/patch` flagged
the `handle_picker` call as an uncovered diff line. A non-TTY smoke
test runs `wt select` and snapshots the deprecation warning plus the
interactive-terminal bail.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit ff4e481 into main May 21, 2026
33 checks passed
@max-sixty
max-sixty deleted the 2837 branch May 21, 2026 04:05
max-sixty added a commit that referenced this pull request May 21, 2026
…#2858)

## Summary

`wt switch <branch>` and the interactive picker (`wt switch` with no
argument) each ran the same switch sequence as separate, parallel code.
[#2845](#2845) made the two
paths *behave* identically; this makes the *code* identical too — a
single `SwitchPipeline` that both entry points build and `.run()`.

`SwitchPipeline::run` (in `src/commands/worktree/switch.rs`) owns the
whole sequence: the bare-repo worktree-path fix-up, pre-switch hooks,
source-identity capture, `plan_switch` → `approve_switch_hooks` →
`validate_switch_templates` → `execute_switch`, output, background
hooks, and `--execute`. Each caller now only resolves a branch
identifier and loads config. The picker-vs-argument differences
(`--execute`, the shell-integration offer, source-identity capture) are
struct field values, not divergent branches.

## Bug fixed

The duplication hid a real bug: the picker passed `yes = true` to
`run_pre_switch_hooks`, **auto-approving project `pre-switch` hooks
without a prompt** — unapproved code from a freshly cloned
`.config/wt.toml` running silently. Every other hook the picker runs
(`post-switch`, `pre-create`, `post-create`) already went through the
approval prompt. With one shared `run_pre_switch_hooks` call gated by
the pipeline's single `verify`/`yes` pair, the picker (which has no
`--yes`) now prompts for project `pre-switch` hooks like `wt switch
<branch>` does — and the two paths can't drift on hook approval again.

## Reviewer notes

- One intentional reorder on the argument path:
`offer_bare_repo_worktree_path_fix` now runs before
`run_pre_switch_hooks` (the picker already used this order). The fix
only mutates `worktree-path` config, which pre-switch hooks never read —
behavior-neutral.
- `capture_switch_source` runs inside `run()` after pre-switch hooks —
the same relative position as the old `run_switch`.
- Eight now-internal helpers were narrowed from `pub`/`pub(crate)` to
private; `worktree/mod.rs` re-exports trimmed accordingly.

## Testing

Pure refactor — the existing `test_switch_*`,
`test_switch_format_json_*`, and `test_switch_picker_*` suites cover
behavior preservation.
`test_switch_picker_pre_switch_hook_requires_approval` is a new
regression test for the bug fix (verified to fail under the bug).

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request May 23, 2026
…#2858)

## Summary

`wt switch <branch>` and the interactive picker (`wt switch` with no
argument) each ran the same switch sequence as separate, parallel code.
[#2845](#2845) made the two
paths *behave* identically; this makes the *code* identical too — a
single `SwitchPipeline` that both entry points build and `.run()`.

`SwitchPipeline::run` (in `src/commands/worktree/switch.rs`) owns the
whole sequence: the bare-repo worktree-path fix-up, pre-switch hooks,
source-identity capture, `plan_switch` → `approve_switch_hooks` →
`validate_switch_templates` → `execute_switch`, output, background
hooks, and `--execute`. Each caller now only resolves a branch
identifier and loads config. The picker-vs-argument differences
(`--execute`, the shell-integration offer, source-identity capture) are
struct field values, not divergent branches.

## Bug fixed

The duplication hid a real bug: the picker passed `yes = true` to
`run_pre_switch_hooks`, **auto-approving project `pre-switch` hooks
without a prompt** — unapproved code from a freshly cloned
`.config/wt.toml` running silently. Every other hook the picker runs
(`post-switch`, `pre-create`, `post-create`) already went through the
approval prompt. With one shared `run_pre_switch_hooks` call gated by
the pipeline's single `verify`/`yes` pair, the picker (which has no
`--yes`) now prompts for project `pre-switch` hooks like `wt switch
<branch>` does — and the two paths can't drift on hook approval again.

## Reviewer notes

- One intentional reorder on the argument path:
`offer_bare_repo_worktree_path_fix` now runs before
`run_pre_switch_hooks` (the picker already used this order). The fix
only mutates `worktree-path` config, which pre-switch hooks never read —
behavior-neutral.
- `capture_switch_source` runs inside `run()` after pre-switch hooks —
the same relative position as the old `run_switch`.
- Eight now-internal helpers were narrowed from `pub`/`pub(crate)` to
private; `worktree/mod.rs` re-exports trimmed accordingly.

## Testing

Pure refactor — the existing `test_switch_*`,
`test_switch_format_json_*`, and `test_switch_picker_*` suites cover
behavior preservation.
`test_switch_picker_pre_switch_hook_requires_approval` is a new
regression test for the bug fix (verified to fail under the bug).

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request May 24, 2026
…le (#2870)

Five data-safety / correctness fixes in the worktree-lifecycle paths,
plus one rebase-integration follow-up. Each closes a real TOCTOU or
scoping gap and carries integration tests; together they form a
dependency chain through `output/handlers.rs` and `step/prune.rs`.

- **`merge` / `remove` — revalidate cleanliness after `pre-remove`
hooks.** `wt merge` / `wt remove` snapshotted the clean check and
branch-integration decision during planning, then ran `pre-remove` hooks
before cleanup. A hook (or concurrent process) could dirty the worktree
or advance the branch in between; the stale decision then trashed the
directory or ran `git branch -D`. Cleanliness and integration are now
recomputed from a fresh ref snapshot after hooks run.
- **`remove` — recompute branch safety in the background path.** The
background removal path also acted on pre-hook planning state, and
synthesized `git worktree remove --force` whenever `.gitmodules`
existed. It now re-runs `ensure_clean`, recomputes integration via
`delete_branch_if_safe`, and fails closed for submodule worktrees on the
fallback path instead of forcing.
- **`prune` — serialize the rename-failure fallback.** When the
fast-path rename failed, prune spawned a detached `git branch -d` that
ran after the lock guard dropped, racing a live integration-check reader
on `.git/config` (#2801's residual gap). It now runs synchronously under
the write guard for non-current worktrees.
- **`prune` — scope hook approval to the removal set.** Approval was
built from every linked worktree (including ones prune would skip) plus
an always-on primary `post-switch`, so a non-interactive prune could
abort on an unrelated unapproved hook. Approval is now built only for
the worktrees prune will actually remove.
- **`prune` — prune stale *detached* worktree metadata.** A detached
worktree whose directory was deleted outside Worktrunk (`branch ==
None`) was silently skipped and lingered in `git worktree list` forever;
it is now pruned via an idempotent `git worktree prune`.

The final commit drops a struct field — removed by the first commit —
from a test constructor that landed in `picker/mod.rs` after this
chain's base (#2845), which the cherry-pick's auto-merge did not catch.

> _This was written by Claude Code on behalf of Maximilian Roos_

---------

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wt switch interactive picker with cd = false skips switch hooks

2 participants