Skip to content

feat(hooks): post-remove reads the removed worktree's config (snapshot before removal) - #2736

Merged
max-sixty merged 1 commit into
mainfrom
config-worktree-source
May 12, 2026
Merged

max-sixty merged 1 commit into
mainfrom
config-worktree-source

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

pre-remove already reads the worktree-being-removed's .config/wt.toml, but post-remove read the post-removal working directory's config — the primary worktree for wt remove / wt step prune, the merge destination for wt merge. So a post-remove defined in a feature branch's branch-local config never fired, and a [pre-remove]/[post-remove] pair in the same .config/wt.toml didn't read the same file. The only reason for the asymmetry: the worktree ceases to exist between the two hooks.

This snapshots the removed worktree's ProjectConfig before deletion (stashed on RemoveResult::RemovedWorktree::removed_project_config) and threads it to the post-remove executor. The rule the commands::hooks spec docstring now states up front: every hook is anchored to one worktree — the one it's about — and reads that worktree's .config/wt.toml; when the worktree isn't on disk at read time, read a snapshot. Two hooks need that: post-remove (worktree just deleted) and wt switch --create's post-switch hooks at approval time (worktree not created yet — reads the base ref via git show). Same role, two physical sources; the spec's new "Snapshot paths" section frames them together, and pre-remove/post-remove collapse into one table row.

What changed

  • RemoveResult::RemovedWorktree gains removed_project_config: Option<Box<ProjectConfig>> (boxed — ProjectConfig is ~600 bytes and would otherwise dominate the variant; clippy large_enum_variant). prepare_worktree_removal and finish_after_merge populate it before any deletion. A parse failure there is not propagated: the executor's own load_project_config() re-reads from disk and surfaces the parse error with full chain, and pre-remove aborts before post-remove fires, so the None is invisible. (Routing the parse error through prepare_worktree_removal would land it in main.rs's record_error, which renders only the top message — a pre-existing display gap orthogonal to this work.)
  • output::handlers::spawn_hooks_after_remove registers post-remove via the new HookAnnouncer::register_with_project_config using that snapshot. post-switch after a removal keeps loading from ctx.repo (the post-removal cwd — the worktree it's about).
  • commands::picker::do_removal (the TUI in-place removal path) was the last hook-execution site reading the primary's config — pre-remove was rooted at main_path, post-remove likewise. Now pre-remove is rooted at the worktree being removed and post-remove uses the snapshot, matching wt remove.
  • collect_remove_hook_commands (the shared approval helper for wt remove / wt merge / wt step prune) collects post-remove per-worktree (same loop as pre-remove); post-switch keeps coming from primary_repo.
  • prepare_background_pipelines absorbs its single-use loading shim: it now takes a required Option<&ProjectConfig>, and HookAnnouncer::register (loads from ctx.repo) / register_with_project_config (explicit snapshot) are the two ergonomic wrappers — mirroring the approval side's approve_or_skip / approve_or_skip_with_config.
  • Spec: commands::hooks module docstring rewritten — "anchor worktree" framing, the pre-/post-remove row merge, the "Snapshot paths" section.

Tests

test_post_remove_hook_reads_removed_worktree_config (remove.rs) — the mirror of the existing test_pre_remove_hook_reads_removed_worktree_config: a post-remove in the removed worktree's branch-local .config/wt.toml fires correctly after the worktree is gone. Verified to fail without the snapshot wiring. Existing pre-remove / merge-teardown / prune / approval-prompt / picker suites all pass; wt hook pre-merge --yes (full suite + lints + doctests) is green.

Follow-up (not in this PR)

commands::picker::do_removal is still a parallel reimplementation of the wt remove pipeline (pre-remove → git removal → post-remove), ~80 lines that shadow handle_remove_output's flow plus its own PostRemoveContext construction. This PR makes it follow the same config rule; routing it through handle_remove_output (which has a quiet/background mode) would delete the duplication outright — a separate, larger refactor with its own picker-test burden.

…hot-before-removal); collapse the prep-pipeline shim

`pre-remove` already reads the worktree-being-removed's `.config/wt.toml`,
but `post-remove` read the *post-removal* working directory's config (the
primary worktree, or the merge destination) — so a `post-remove` defined
in a feature branch's branch-local config never fired, and the
`pre-remove`/`post-remove` pair didn't read the same file. The only
reason for the asymmetry was that the worktree ceases to exist between
the two hooks.

This snapshots the removed worktree's `ProjectConfig` before deletion
(stashed on `RemoveResult::RemovedWorktree::removed_project_config`) and
threads it to the `post-remove` executor. The rule the spec docstring
now states up front: **every hook is anchored to one worktree — the one
it's *about* — and reads that worktree's `.config/wt.toml`; when the
worktree isn't on disk at read time, read a snapshot.** Two hooks need
that: `post-remove` (worktree just deleted) and `wt switch --create`'s
post-switch hooks at approval time (worktree not created yet — reads the
base ref via `git show`). Same role, two physical sources; the spec's
new "Snapshot paths" section frames them together. `pre-remove` and
`post-remove` collapse into one table row.

### What changed

- **`RemoveResult::RemovedWorktree`** gains `removed_project_config:
Option<Box<ProjectConfig>>` (boxed — `ProjectConfig` is ~600 bytes and
would otherwise dominate the variant; clippy `large_enum_variant`).
`prepare_worktree_removal` and `finish_after_merge` (for `wt merge`)
populate it before any deletion. A parse failure there is *not*
propagated — the executor's own `load_project_config()` re-reads from
disk and surfaces the parse error with full chain, and `pre-remove`
aborts before `post-remove` fires, so the `None` is invisible. (Routing
the parse error through `prepare_worktree_removal` would land it in
`main.rs`'s `record_error`, which renders only the top message — a
pre-existing display gap orthogonal to this work.)
- **`output::handlers::spawn_hooks_after_remove`** registers
`post-remove` via the new `HookAnnouncer::register_with_project_config`
using that snapshot. `post-switch` after a removal keeps loading from
`ctx.repo` (the post-removal cwd — that's the worktree it's *about*).
- **`commands::picker::do_removal`** (the TUI in-place removal path) was
the last hook-execution site reading the primary's config — `pre-remove`
was rooted at `main_path`, `post-remove` likewise. Now `pre-remove` is
rooted at the worktree being removed and `post-remove` uses the
snapshot, matching `wt remove`.
- **`collect_remove_hook_commands`** (the shared approval helper for
`wt remove` / `wt merge` / `wt step prune`) collects `post-remove`
per-worktree (same loop as `pre-remove`); `post-switch` keeps coming
from `primary_repo`.
- **`prepare_background_pipelines`** absorbs its single-use loading shim:
it now takes a required `Option<&ProjectConfig>`, and
`HookAnnouncer::register` (loads from `ctx.repo`) /
`register_with_project_config` (explicit snapshot) are the two
ergonomic wrappers — mirroring the approval side's `approve_or_skip` /
`approve_or_skip_with_config`.
- **Spec**: `commands::hooks` module docstring rewritten — "anchor
worktree" framing, the `pre-`/`post-remove` row merge, the "Snapshot
paths" section.

### Tests

- `test_post_remove_hook_reads_removed_worktree_config` (remove.rs) —
the mirror of the existing `test_pre_remove_hook_reads_removed_worktree_config`:
a `post-remove` in the removed worktree's branch-local `.config/wt.toml`
fires correctly *after* the worktree is gone. Verified to fail without
the snapshot wiring.
- Existing pre-remove / merge-teardown / prune / approval-prompt /
picker suites all pass; `wt hook pre-merge --yes` (full suite + lints +
doctests) is green.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit 23d6552 into main May 12, 2026
33 checks passed
@max-sixty
max-sixty deleted the config-worktree-source branch May 12, 2026 02:02
max-sixty added a commit that referenced this pull request May 12, 2026
…2746)

The picker's `alt-r` removal (`PickerCollector::do_removal`) was a
parallel reimplementation of the `wt remove` teardown pipeline —
`pre-remove` hook → git worktree removal → `post-remove` (and
`post-switch`) hooks — that had to be kept in lockstep with
`output::handle_remove_output` by hand. #2736 had to fix the same
hook-config bug in both. This routes `do_removal` through
`handle_remove_output`, so the duplication is gone, and gates the
picker's hooks on approval (which the old path didn't do).

## What changed

**`handle_remove_output` gains a `silent` flag**
(`src/output/handlers.rs`). When set, a `RemovedWorktree` result is
removed with no progress/success messages, no trash-cleanup spinner, and
no `cd` directive — just `pre-remove`, the synchronous git worktree
removal (`remove_removed_worktree_silently`), and `post-remove` /
`post-switch` hook registration. Required because the picker runs
`do_removal` from a background thread spawned off skim's event loop, so
any `wt`-generated stderr output would corrupt skim's frame. `silent` is
the only new knob; it threads through the five `handle_remove_output`
call sites as `false` everywhere except the picker.

**`do_removal` is now a thin call into `handle_remove_output`**
(`src/commands/picker/mod.rs`). The `RemovedWorktree` arm drops its
hand-rolled `pre-remove` (`CommandContext` + `execute_hook`) / git
removal / `post-remove` (`PostRemoveContext` +
`HookAnnouncer::register_with_project_config`) code — net ~80 lines,
plus the `removed_project_config` snapshot plumbing #2736 added now
lives in one place. The `BranchOnly` arm is unchanged (it's small and
`handle_remove_output`'s `BranchOnly` path prints things the picker
can't have).

**The picker's hooks now run only when already approved.** The old
`do_removal` ran project `pre-remove` / `post-remove` / `post-switch`
hooks *unconditionally* — it was the one removal/switch path that
bypassed the approval gate, so a freshly-cloned repo's `post-switch`
hook would fire on `alt-r`. The picker can't show an approval prompt
mid-render, so it now consults `Approvals` read-only
(`removal_hooks_approved` — the same `collect_remove_hook_commands` set
`wt remove` would prompt for, checked against
`Approvals::is_command_approved`) and passes the result as `verify`;
when it's `false`, `execute_pre_remove_hooks_if_needed` and
`spawn_hooks_after_remove` early-return. `CLAUDE.md` gains a "Project
Commands Run Only After Approval" section stating the general rule
(`src/commands/command_approval.rs` is the gate; never build a path that
runs project commands without it; a context that can't prompt skips the
unapproved ones).

**One behavior delta beyond consolidation:** removing the *current*
worktree via `alt-r` now also registers `post-switch` hooks for the home
worktree (if approved), matching `wt remove` / `wt merge` / `wt step
prune`. The old `do_removal` registered only `post-remove`.

## Testing

`cargo run -- hook pre-merge --yes` is green (3690 tests). The
`test_do_removal_*` unit tests cover the silent removal path (worktree +
branch + detached), and a new
`test_do_removal_skips_unapproved_pre_remove_hook` covers the approval
gating (`removal_hooks_approved` is fully covered). Not covered: the two
lines in `invoke()`'s `alt-r` spawn block that hand the `Approvals`
snapshot to the removal thread — `invoke()` is already documented as not
unit-testable ("Full invoke() tests require interactive skim"), and the
rest of its body is uncovered too; the approval-check logic itself is.
No end-to-end test exercises "hook present *and* approved → runs"
through the picker (would need seeding `Approvals` against the test
repo's path) — the unit test asserts the logic, just not that exact
integration.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
@max-sixty max-sixty mentioned this pull request May 13, 2026
max-sixty added a commit that referenced this pull request May 13, 2026
Release v0.50.0.

Highlights:

- Experimental Azure DevOps support (#1256, thanks @mikeyroush; fixes
#1144 from @dlecan) — `wt switch pr:<N>`, `wt list --full`, and `wt
config show --full` recognize Azure DevOps via the `az` CLI.
- Experimental Gitea CI-status detection (#2702) on top of the Gitea
`pr:` shortcut (#1320, thanks @SjB).
- Hooks now resolve `.config/wt.toml` from the worktree they act on —
the primary-worktree fallback is gone, and `post-remove` reads the
removed worktree's config (snapshotted before removal). Approval prompts
collect hook commands from the same worktree. Breaking change for setups
that relied on the primary-worktree fallback; the changelog entry has
the recovery action. (#2690, #2703, #2714, #2717, #2701, #2708, #2727,
#2736, #2748)
- The `wt switch` picker's `alt-r` removal no longer runs unapproved
project hooks (#2746) — the picker's removal path is now routed through
`handle_remove_output` and consults the existing approval state
read-only.
- `wt config alias show` with no name lists every alias's full
definition (#2684, #2691); `wt --help` switches to a compact aliases
pointer (#2688).
- `wt list --branches` warm-run perf: SHA-keyed cache for `main↕` and
`Remote⇅` ahead/behind counts; shared push-remote URL and local-branch
scan (#2704, #2718, #2673).
- Claude Code plugin ships the `wt-switch-create` skill (#2737, thanks
@onetom for #2631).

See `CHANGELOG.md` for the full list (8 Improved, 5 Fixed, 5 Internal).

semver-checks reports breaking library-API changes (new enum variants
without `#[non_exhaustive]`, removed `Branch::github_push_url`, new
trait method on `RemoteRefProvider`), which mandates at minimum a minor
bump pre-1.0.
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.

2 participants