Skip to content

refactor(hooks): unify dispatch and replace CommandOrigin with closures - #2472

Merged
max-sixty merged 5 commits into
mainfrom
hook-dispatch-unify
Apr 29, 2026
Merged

max-sixty merged 5 commits into
mainfrom
hook-dispatch-unify

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Summary

  • Replaces the CommandOrigin enum in command_executor.rs with explicit fields on ForegroundStep (AnnouncePolicy, pipe_stdin: bool, error_wrapper: ErrorWrapper) and per-command label / log_label set at prep time. handle_command_error collapses from a 4-variant case-split to a single branch (FailFast calls the closure, Warn prints). Resolves the pre-existing TODO at command_executor.rs:308-316.
  • Tightens hooks.rs around two pub entries (run_hooks_foreground, run_hooks_background) plus pub(crate) shortcuts (execute_hook, spawn_background_hooks) that absorb the auto-config-lookup boilerplate. execute_hook is the canonical operation-driven path and applies add_hook_skip_hint centrally; wt hook <type> calls run_hooks_foreground directly so failures don't carry the misleading --no-hooks reminder.
  • Adds HookType::is_pre() and FailureStrategy::default_for(hook_type) so run_hook's pre/post dispatch is one if. Adds approve_or_skip for the "approve hooks → fall through if declined" pattern (used at 4 sites).
  • Collapses run_post_hook's filter/no-filter dispatch to a single is_empty() branch. Eliminates prepare_background_hooks duplication via into_source_groups over the flat prepare_sourced_steps result. Tightens spawn_hook_pipeline_quiet's base-context extraction (caller invariants guarantee non-empty steps).
  • Integrates with HookAnnouncer (hooks: combine background hook announces per command #2457): register calls prepare_background_pipelines, flush calls run_hooks_background.

Net: 500 insertions, 593 deletions across 16 files. 3391 tests pass; lints and snapshots clean. New regression test test_standalone_hook_failure_omits_skip_hint locks in the wt hook <type> skip-hint behaviour.

Test plan

  • `cargo run -- hook pre-merge --yes` (3391 tests, lints, snapshots clean)
  • `cargo insta test --accept -- --test integration "test_help"` (no snapshot drift)
  • Regression test verified by re-introducing the skip-hint over-application — test fails; reverting fixes it.

🤖 Generated with Claude Code

max-sixty and others added 4 commits April 29, 2026 10:56
…osures

Collapse `hooks.rs` to two public entry points — `run_hooks_foreground` and
`run_hooks_background` — replacing `execute_hook`, `run_hook_with_filter`,
`spawn_background_hooks`, `spawn_hook_pipeline`, and
`announce_and_spawn_background_hooks`. `prepare_background_hooks` is gone too;
its source-grouping is now `into_source_groups` over the flat
`prepare_sourced_steps` result.

Replace `CommandOrigin` in `command_executor.rs` with explicit per-step fields
on `ForegroundStep` (`AnnouncePolicy`, `pipe_stdin`, `error_wrapper` closure)
and per-command labels (`label`, `log_label`) on `PreparedCommand` set at
prep time. `handle_command_error` becomes a single branch instead of a
4-variant case-split, resolving the TODO at command_executor.rs:308-316.

Add `HookType::is_pre()` plus `FailureStrategy::default_for(hook_type)` so
`run_hook`'s pre/post dispatch is one `if`. Extract `approve_or_skip` for the
"approve hooks → fall through if declined" pattern repeated across
`step_commands.rs`, `main.rs`, and `handle_switch.rs`. Tighten
`spawn_hook_pipeline_quiet`'s base-context extraction (caller invariants
guarantee non-empty steps).

`add_hook_skip_hint` placement is preserved: operation-driven callers
(merge, switch, commit, …) tag failures with the `--no-hooks` reminder via
`run_hooks_foreground_for_type` or explicit `.map_err`; `wt hook <type>`
leaves errors unwrapped because the user already asked for the hooks.
`test_standalone_hook_failure_omits_skip_hint` locks the invariant.

3377 tests pass (+2 vs main: warn-unnamed coverage, skip-hint regression test).
Net -125 LOC across 14 source files.

Co-Authored-By: Claude <[email protected]>
Integrates `HookAnnouncer` (#2457) with the unified hook entry points:
- `HookAnnouncer::register` now calls `prepare_background_pipelines`
  (this branch's helper) instead of the deleted `prepare_background_hooks`.
- `HookAnnouncer::flush` calls `run_hooks_background` (this branch's
  unified entry) instead of the private `announce_and_spawn_pipelines`
  helper. The empty-filter is folded into `run_hooks_background` itself.
- `output/handlers::spawn_hooks_after_remove` keeps the
  `Option<&mut HookAnnouncer<'_>>` parameter and falls back to
  `run_hooks_background` when no announcer is provided.
- Also pulls in `PushKind` (#2468); no integration changes needed.

Co-Authored-By: Claude <[email protected]>
Brings in `TemplateVars` (#XXXX) helper for building hook template vars.
The conflict in handle_switch.rs was just imports — keep both this branch's
hook helpers (prepare_background_pipelines, run_hooks_background,
run_hooks_foreground_for_type) and main's TemplateVars.

Co-Authored-By: Claude <[email protected]>
The previous refactor collapsed `hooks.rs` to two public entry points
(`run_hooks_foreground`, `run_hooks_background`) and pushed config-lookup
boilerplate onto every operation-driven caller. This iteration walks back
that part — the call sites are what readers actually touch, and they should
be one-liners.

Restore two `pub(crate)` shortcuts:
- `execute_hook(ctx, hook_type, extra_vars, strategy, display_path)` —
  auto-lookup foreground hooks, with the `--no-hooks` skip-hint applied
  centrally in one place. Replaces the previous `run_hooks_foreground_for_type`
  (renamed for familiarity) and absorbs `commit.rs` / `step_commands.rs`'s
  bare-spec + manual `.map_err(add_hook_skip_hint)` calls.
- `spawn_background_hooks(ctx, hook_type, extra_vars, display_path)` —
  symmetric one-liner for single-type background hooks.

`run_hooks_foreground` becomes `pub(crate)`; only `wt hook <type>` now calls
it directly, where the no-hint behaviour is intentional.

Multi-type background callers (`handle_switch::spawn_switch_background_hooks`
for PostSwitch + PostStart, `output::handlers::spawn_hooks_after_remove` for
PostRemove + PostSwitch) keep using `prepare_background_pipelines` +
`run_hooks_background` so all types share one announce line. `merge.rs` keeps
using `HookAnnouncer` for the same reason across phases.

Caller diffs vs main shrink dramatically:
- picker: 29 → 1 line changed
- merge: 17 → 7
- worktree/hooks: 5 → 1
- commit / step_commands: pre-commit hook execution is now a single
  `execute_hook(...)` call instead of building a `HookCommandSpec`.

The skip-hint policy is now centralized in `execute_hook` (operation-driven
path always adds it). The regression test
`test_standalone_hook_failure_omits_skip_hint` continues to lock in that
`wt hook <type>` does not.

3391 tests pass.

Co-Authored-By: Claude <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two minor suggestions inline. Otherwise the refactor reads cleanly: the CommandOrigin → explicit-fields split resolves the per-origin branching genuinely (single error_wrapper callsite, no enum match in handle_command_error), and execute_hook / spawn_background_hooks shave the operation-driven callsites down to one-liners as advertised.

Comment thread src/git/mod.rs Resolved
Comment thread src/output/global.rs Resolved
- HookType::is_pre uses an exhaustive match instead of `matches!` so a
  future variant trips a compile-time nudge.
- post_hook_display_path doc example uses the spawn_background_hooks
  one-liner (the recommended path for single-type background hooks)
  instead of the two-line prepare+run form (which is for multi-type).

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit d2ef2ee into main Apr 29, 2026
25 checks passed
@max-sixty
max-sixty deleted the hook-dispatch-unify branch April 29, 2026 19:45
max-sixty added a commit that referenced this pull request Apr 29, 2026
Follow-up to #2472. Four sites still built hook extra-vars by hand:
`commit.rs`, `step_commands::handle_squash`, `picker::do_removal`, and
`hook_commands::build_manual_hook_extra_vars`. They now use
`TemplateVars` like the rest of the hook call sites — owning the strings
and centralizing the `worktree_path`/`worktree` alias.

`build_manual_hook_extra_vars` is renamed to
`build_manual_hook_template_vars` and returns `TemplateVars` so the two
callers (`run_hook`, `expand_command_template`) can append CLI bindings
or `{{ args }}` before materializing. Drops two redundant
`to_posix_path` calls along the way.

Module docstring on `template_vars.rs` updated to reflect that the
struct now covers all hook call sites, not just switch/merge.

> _This was written by Claude Code on behalf of @max-sixty_

---------

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.

2 participants