Skip to content

refactor(hooks): route post-commit and post-switch through HookAnnouncer - #2482

Merged
max-sixty merged 5 commits into
mainfrom
announcer-migrate
Apr 30, 2026
Merged

max-sixty merged 5 commits into
mainfrom
announcer-migrate

Conversation

@max-sixty

@max-sixty max-sixty commented Apr 29, 2026 •

Copy link
Copy Markdown
Owner

Extends the HookAnnouncer coordinator (introduced in #2457, refined in #2477) to more hot paths so multi-phase background hooks share a single ◎ Running … line.

Migrated sites:

  • wt switch --create — post-switch + post-start (spawn_switch_background_hooks in handle_switch.rs).
  • wt merge — auto-commit (when dirty) + post-remove + post-switch + post-merge.
  • wt merge --squash — post-commit (from squash) + post-remove + post-switch + post-merge.

Plumbing: CommitOptions::commit and handle_squash gained an optional &mut HookAnnouncer<'_> parameter. handle_merge constructs one announcer early and threads it through the commit, squash, and remove paths; flush() runs once at the end. Standalone wt commit / wt step squash pass None and self-announce as before.

No new abstractions — the Some arm calls extend, the None arm calls run_hooks_background, mirroring the existing precedent in output/handlers.rs::spawn_hooks_after_remove. As a follow-on, spawn_background_hooks (whose only caller after migration was picker::do_removal, gated #[cfg(unix)]) is dropped and the picker's call site is inlined to match — fixing a Windows dead-code build error and unifying all four single-shot post-hook sites on the same shape.

Test coverage: new test_merge_squash_combines_post_commit_post_remove_post_switch_post_merge and test_merge_auto_commit_combines_post_commit_post_remove_post_switch_post_merge snapshot the combined four-phase announce line for both squash and non-squash auto-commit paths. Existing test_merge_combines_post_remove_post_switch_post_merge, test_merge_drops_pending_hooks_when_post_merge_fails, and test_switch_combined_post_switch_and_post_start_hooks remain green.

🤖 Generated with Claude Code

Migrates `wt switch --create` (post-switch + post-start) and `wt merge
--squash` (post-commit + post-remove + post-switch + post-merge) to share
one `◎ Running …` announce line instead of emitting one per phase.

`CommitOptions::commit` and `handle_squash` now accept an optional
`&mut HookAnnouncer`; `wt merge` constructs one early and threads it
through both. Standalone `wt commit` / `wt step squash` pass `None` and
self-announce as before.

Co-Authored-By: Claude Opus 4.7 (1M context) <[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.

One nit on docstring placement — the announcer parameter description is attached to the impl block, so rustdoc and IDE hover on commit() won't pick it up. Move the doc onto the method.

Comment thread src/commands/commit.rs Resolved
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

CI follow-up:

  • codecov/patch failed at 91.66% (2 misses on 24 patch lines). Investigating via the code-coverage artifact (cobertura.xml), the uncovered lines look like the Some arm of the new dispatch in src/commands/commit.rs:

    L242 hits=7    if let Some(announcer) = announcer {
    L243 hits=0        announcer.extend(pipelines);   <-- miss
    L244 hits=0    } else {
    L245 hits=7        run_hooks_background(pipelines, false)?;
    

    The new test_merge_squash_combines_post_commit_post_remove_post_switch_post_merge covers handle_squash's Some arm (step_commands.rs:356 shows hits=13), but the parallel auto-commit path through commit.rs::commit(Some(&mut announcer)) from merge.rs:219 isn't exercised by any test that also configures post-commit hooks. test_user_pre_commit_hook_executes reaches that call site but only configures [pre-commit], so prepare_background_pipelines returns an empty Vec and the call site's instrumentation may not register.

    The most direct fix is a sibling to the new test for the non-squash path — wt merge with uncommitted changes and [post-commit] + [post-merge] configured. Happy to push it as a separate commit if you'd like.

  • test (windows) failed in the Install cargo-insta step (baptiste0928/cargo-install@v3 25s job). Transient infrastructure flake, unrelated to the PR — a rerun should clear it.

Adds `test_merge_auto_commit_combines_post_commit_post_remove_post_switch_post_merge`
exercising `wt merge --no-squash` with uncommitted changes — the path that
threads the announcer through `CommitOptions::commit`'s `Some` arm
(`commit.rs:243`), which the squash sibling test didn't reach.

Also moves the `commit()` doc comment from the impl block onto the method
so rustdoc and IDE hover surface the `announcer` parameter description.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
worktrunk-bot
worktrunk-bot previously approved these changes Apr 30, 2026
# Conflicts:
#	src/commands/commit.rs
#	src/commands/step_commands.rs

@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.

CI follow-up — correcting my earlier diagnosis.

The Windows failure on commit 2766001 was not a cargo-install flake; I misread the log. The actual failure was a real compile error during the test build, and it has reproduced on the merge commit (job 73688190160):

error: function `spawn_background_hooks` is never used
   --> src\commands\hooks.rs:779:15

Root cause: this PR replaces the commit.rs and step_commands.rs callers of spawn_background_hooks with the prepare_background_pipelines + announcer-dispatch pattern. The only remaining caller is src/commands/picker/mod.rs:234, and picker is gated on #[cfg(unix)] in src/commands/mod.rs:18. On Windows the function ends up with zero callers, and -D dead-code (implied by -D warnings) trips the build.

Suggested fix — match the function's reachability to its sole caller by cfg-gating it the same way:

// src/commands/hooks.rs
/// Spawn background hooks for a single hook type (post-commit, post-merge, …).
/// […]
#[cfg(unix)]
pub(crate) fn spawn_background_hooks(
    ctx: &CommandContext,
    hook_type: HookType,
    extra_vars: &[(&str, &str)],
    display_path: Option<&Path>,
) -> anyhow::Result<()> {
    …
}

That preserves the "one-shot single-phase convenience" docstring intent while making the cfg footprint match the actual caller. Happy to push this as a follow-up commit if you'd like — it's a one-line change and shouldn't affect Linux/macOS at all (and Path stays imported because other Path-using items in hooks.rs are unconditional).

Dismissing my previous approval since the failure is PR-related rather than infrastructure.

@worktrunk-bot
worktrunk-bot dismissed their stale review April 30, 2026 00:11

Windows CI fails to compile — function spawn_background_hooks becomes dead code on Windows after this PR removes its commit.rs/step_commands.rs callers (sole remaining caller is picker, which is cfg(unix)). See follow-up review for diagnosis and suggested fix.

max-sixty and others added 2 commits April 29, 2026 17:13
After this PR migrated commit.rs and step_commands.rs to the
prepare_background_pipelines + extend/run_hooks_background pattern, the
sole remaining caller of spawn_background_hooks was picker::do_removal,
which is gated #[cfg(unix)]. Under -D warnings (-> -D dead-code) the
function fails to compile on Windows.

Drop the wrapper and inline the two-line pattern in picker, matching
the call-site shape used elsewhere (commit.rs, step_commands.rs,
output/handlers.rs::spawn_hooks_after_remove).

Reported by worktrunk-bot in the #2482 review.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@max-sixty

Copy link
Copy Markdown
Owner Author

Thanks @worktrunk-bot — fixed in b35d639.

I went one step further than the suggested #[cfg(unix)] and dropped spawn_background_hooks entirely, inlining the two-line prepare_background_pipelines + run_hooks_background pattern at the picker's only call site. That matches the shape used in commit.rs, step_commands.rs, and output/handlers.rs::spawn_hooks_after_remove, so all four single-shot post-hook sites now look the same.

This was written by Claude Code on behalf of @max-sixty

@max-sixty
max-sixty merged commit f499ec0 into main Apr 30, 2026
27 checks passed
@max-sixty
max-sixty deleted the announcer-migrate branch April 30, 2026 00:33
max-sixty added a commit that referenced this pull request Apr 30, 2026
Builds on #2477 and #2482. Drops the `Option<&mut HookAnnouncer<'_>>`
plumbing through `commit.rs::commit`, `step_commands::handle_squash`,
`output/handlers.rs::handle_remove_output` (and its four sub-handlers),
and the multi-target prune loop in `step_commands::step_prune`. Each
function now takes `&mut HookAnnouncer<'_>` unconditionally — the
if/else around \"share with caller's announcer or self-announce\" is
gone. Standalone callers (`wt commit`, `wt step squash`, `wt remove`,
`wt step prune`, `wt switch` picker post-remove) construct a local
announcer and flush right after; multi-phase callers (`wt merge`) keep
their existing single-announcer-per-command pattern.

`run_hooks_background` is now a module-private helper of
`HookAnnouncer::flush` (was `pub(crate)`).
`hook_commands::run_post_hook` also routes through `HookAnnouncer`, so
`flush` is the only path that reaches the announce/spawn primitive.
`RemovedWorktreeOutputContext` loses its now-redundant
`show_branch_in_hooks` field — callers configure the announcer directly
when constructing it.

No behavior change: combined announce lines, log paths, `show_branch`
semantics for batch contexts (prune, multi-target remove), and exit
codes are all unchanged. Existing snapshots and the full integration
suite (3399 tests) pass; `cargo run -- hook pre-merge --yes` is green.

> _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