Skip to content

feat: Add user-level hooks that run for all repositories, add wt hook show - #118

Merged
max-sixty merged 28 commits into
mainfrom
user-hooks
Dec 10, 2025
Merged

max-sixty merged 28 commits into
mainfrom
user-hooks

Conversation

@max-sixty

@max-sixty max-sixty commented Dec 10, 2025 •

Copy link
Copy Markdown
Owner

(PR description by Claude)

ref #101


Implement a new user hooks system that allows developers to define
personal hooks in ~/.config/worktrunk/config.toml that execute for
all repositories. User hooks complement project hooks with these key
differences:

  • Scope: Run for all repositories (vs. single repository)
  • Location: User config file (vs. .config/wt.toml)
  • Approval: Not required (user implicitly approves via config)
  • Execution order: Run before project hooks
  • Skip behavior: Skipped together with project hooks via --no-verify

Implementation Details

Configuration syntax (same as project hooks):

[post-create]
setup = "echo 'Setting up worktree...'"

[pre-merge]
notify = "notify-send 'Merging {{ branch }}'"

Hook types supported: post-create, post-start, pre-commit, pre-merge,
post-merge, pre-remove

Template variables: Support all project hook variables plus remote_url
for repository-specific conditional logic

New data structures:

  • HookSource enum: Distinguishes user vs. project hooks
  • prepare_user_commands(): Expands user hook templates without approval

Updated hook pipeline:

  • HookPipeline::run_sequential(): Accepts HookSource instead of
    phase and label_prefix
  • HookPipeline::spawn_background(): Renamed from spawn_detached() for
    consistency

Documentation Updates

  • Updated help text and documentation to clarify --no-verify skips all
    hooks (both user and project)
  • Added "User hooks" section to config documentation with examples
  • Added comparison table showing key differences from project hooks
  • Added use cases and filtering examples

Tests

Added comprehensive integration test suite (tests/integration_tests/user_hooks.rs)
covering:

  • Basic execution of all hook types
  • Execution order (user before project)
  • No approval required for user hooks
  • --no-verify skips all hooks
  • Failure handling and behavior
  • Template variable expansion
  • Background hook execution
  • Combined user and project hook scenarios

Implement a new user hooks system that allows developers to define
personal hooks in `~/.config/worktrunk/config.toml` that execute for
all repositories. User hooks complement project hooks with these key
differences:

- **Scope**: Run for all repositories (vs. single repository)
- **Location**: User config file (vs. `.config/wt.toml`)
- **Approval**: Not required (user implicitly approves via config)
- **Execution order**: Run before project hooks
- **Skip behavior**: Skipped together with project hooks via `--no-verify`

### Implementation Details

**Configuration syntax** (same as project hooks):
```toml
[post-create]
setup = "echo 'Setting up worktree...'"

[pre-merge]
notify = "notify-send 'Merging {{ branch }}'"
```

**Hook types supported**: post-create, post-start, pre-commit, pre-merge,
post-merge, pre-remove

**Template variables**: Support all project hook variables plus `remote_url`
for repository-specific conditional logic

**New data structures**:
- `HookSource` enum: Distinguishes user vs. project hooks
- `prepare_user_commands()`: Expands user hook templates without approval

**Updated hook pipeline**:
- `HookPipeline::run_sequential()`: Accepts `HookSource` instead of
  `phase` and `label_prefix`
- `HookPipeline::spawn_background()`: Renamed from `spawn_detached()` for
  consistency

### Documentation Updates

- Updated help text and documentation to clarify `--no-verify` skips all
  hooks (both user and project)
- Added "User hooks" section to config documentation with examples
- Added comparison table showing key differences from project hooks
- Added use cases and filtering examples

### Tests

Added comprehensive integration test suite (`tests/integration_tests/user_hooks.rs`)
covering:
- Basic execution of all hook types
- Execution order (user before project)
- No approval required for user hooks
- `--no-verify` skips all hooks
- Failure handling and behavior
- Template variable expansion
- Background hook execution
- Combined user and project hook scenarios
max-sixty and others added 7 commits December 9, 2025 16:10
- Document remote_url template variable in cli.rs (syncs to docs)
- Move check_any_hook_configured into run_hook_with_filter
- Simplify standalone hook match arms

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

Co-Authored-By: Claude <[email protected]>
Project hooks now show "project" prefix (e.g., "Running project pre-merge")
for consistency with user hook labeling.

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

Co-Authored-By: Claude <[email protected]>
- wt hook intro: Mention both user and project hooks
- --no-verify: Change "skip project hooks" to "skip all hooks"
- User config comment: Clarify verify skips all hooks

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

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

Adds a new `show` subcommand to `wt hook` that displays all configured hooks
from both user and project config files with their approval status. Project
hooks show a ❓ indicator when they require approval.

Features:
- Lists hooks organized by type (post-create, post-start, pre-commit, etc.)
- Shows both user-level hooks (run for all repos) and project-specific hooks
- Displays approval status for project hooks (❓ = needs approval)
- Optional `--expanded` flag to show template variables substituted with current context
- Optional hook type filter to show only specific hook types
- Output is displayed through a pager (like git help) when available

Also reorders hook subcommands to put `show` first, making hook inspection
the primary entry point before hook execution commands.
Personal hooks in ~/.config/worktrunk/config.toml now run automatically
before project hooks for all worktree operations. User hooks don't require
approval and use the same syntax and template variables as project hooks.

Changes:
- Add user hook support for all hook types (post-create, post-start,
  pre-commit, pre-merge, post-merge, pre-remove)
- User hooks run before project hooks in execution order
- Skip all hooks with --no-verify (applies to both user and project)
- Add remote_url template variable for repository-aware filtering
- Update documentation and help text to reflect user hooks
- Refine hook labeling to distinguish "user" vs "project" hooks in output
- Add comprehensive integration tests for user hook functionality
- Add user hooks to shell completion for `wt hook <type> <name>`
- Include PreRemove in batch approval during merge (fixes --force bypass)
- Add source prefix to background log filenames to prevent collisions
- Update documentation for new log format: {branch}-{source}-post-start-{name}.log

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

Co-Authored-By: Claude <[email protected]>
max-sixty and others added 15 commits December 9, 2025 17:43
User hooks enable personal, repository-agnostic automations while maintaining
team standards via project hooks. Key improvements:

- User hooks defined in ~/.config/worktrunk/config.toml run before project hooks
- User hooks don't require approval (user implicitly approves by defining them)
- Post-start logs now include source prefix to avoid collisions (user/project)
- remote_url template variable added for conditional hook logic
- Hook output labels distinguish user vs project hooks for clarity
- All hooks skipped together via --no-verify flag
- --no-verify help text simplified from "Skip project hooks" to "Skip hooks"

Implementation:
- HookSource enum tracks whether hooks are user or project
- prepare_user_commands() handles user hooks (no approval needed)
- Hook pipeline shows source in output: "Running user pre-merge" vs "Running project pre-merge"
- Background operation names include source to prevent log file collisions
Add support for ssh:// URL format in git remote parsing to handle
SSH URLs alongside existing git@ and https:// formats.

Improve branch name escaping for git config keys by using hex encoding
(-XX format) instead of percent-encoding, ensuring all non-alphanumeric
characters (except . and -) are properly escaped. This handles UTF-8
multi-byte sequences and enables reliable round-trip encoding/decoding
of branch names containing /, _, and other special characters.
User hooks enable personal, repository-agnostic automations while maintaining
team standards via project hooks. Key improvements:

- User hooks defined in ~/.config/worktrunk/config.toml run before project hooks
- User hooks don't require approval (user implicitly approves by defining them)
- Post-start logs now include source prefix to avoid collisions (user/project)
- remote_url template variable added for conditional hook logic
- Hook output labels distinguish user vs project hooks for clarity
- All hooks skipped together via --no-verify flag
- --no-verify help text simplified from "Skip project hooks" to "Skip hooks"

Implementation:
- HookSource enum tracks whether hooks are user or project
- prepare_user_commands() handles user hooks (no approval needed)
- Hook pipeline shows source in output: "Running user pre-merge" vs "Running project pre-merge"
- Background operation names include source to prevent log file collisions
The pre-remove hook execution was incorrectly using auto_trust=true,
bypassing approval prompts. This commit introduces a design pattern
where approval happens exactly once at command entry points:

- Add collect_and_approve_hooks() helper for upfront approval
- wt remove now approves pre-remove hooks before any execution
- Add --force flag to skip approval prompts
- Thread auto_trust parameter through handle_remove_output

The pattern ensures approval happens at the "gate" (command entry),
eliminating error-prone threading of auto_trust through execution layers.

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

Co-Authored-By: Claude <[email protected]>
Integrates user-hooks features:
- Hook command counting (run_sequential returns usize)
- HookCommandNotFound error with available names
- Pre-remove approval fix ("Approve at the Gate" pattern)
- collect_and_approve_hooks function

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

Co-Authored-By: Claude <[email protected]>
The .statusline.rs.pending-snap file is no longer needed and should not be committed to version control.
Moves approval prompts to command entry points instead of hook execution.
Commands are now approved once in a batch before any hooks run, with template
expansion so users see actual values (branch names, paths) instead of
placeholders. This eliminates redundant prompts and simplifies the execution
layer by removing the `auto_trust` parameter threading.

Key changes:
- Rename `collect_and_approve_hooks()` to `collect_and_approve_hooks_with_context()`
  and add `expand_commands_for_approval()` for showing expanded templates in prompts
- Remove `auto_trust` parameter from hook execution functions
- Simplify `HookSource::Project` enum variant (no longer carries approval state)
- Add gate approval at `wt switch --create`, `wt remove`, `wt step commit/squash`
- Update merge command collection to expand templates before approval batch
- Remove binary `rust_out` artifact and test snapshot file
Update documentation and help text to better explain that `wt hook`
runs hooks independently of normal worktree operations. Changes emphasize
the use cases (testing, CI, re-running after failure) rather than
describing it as "manually" running hooks.

Also simplify phrasing around user hooks and remove unnecessary markdown
formatting for consistency.
@max-sixty max-sixty changed the title feat: Add user-level hooks that run for all repositories feat: Add user-level hooks that run for all repositories, add wt hook show Dec 10, 2025
max-sixty and others added 4 commits December 9, 2025 19:50
Update function names to be more concise and consistent:
- `collect_and_approve_hooks_with_context()` → `approve_hooks()`
- `handle_standalone_run_hook()` → `run_hook()`
- `handle_standalone_commit()` → `step_commit()`
- `handle_standalone_add_approvals()` → `add_approvals()`
- `handle_standalone_clear_approvals()` → `clear_approvals()`

Also update documentation examples to reference the new names.
…operations

When users decline command approval, hooks are now skipped but the operation
proceeds. This applies consistently across all approval gates: merge, commit,
squash, remove, and hook execution.

Previously, declined approvals would still attempt hook execution. Now, the
verify flag is shadowed based on approval result - if declined, verify becomes
false to gate all subsequent hook execution. For explicit hook runs via
`wt hook`, a declined approval returns early since the entire purpose is hook
execution.

This ensures users have clear control: approving runs hooks, declining skips
them but continues the workflow.
Two fixes for `wt hook` command:

1. Pass name_filter to approval so `wt hook pre-merge --name foo` only
   prompts for approval of the "foo" hook, not all hooks of that type.
   Added `approve_hooks_filtered()` function that accepts optional name
   filter parameter.

2. Use current branch as target for pre-merge/post-merge approval prompts
   to match execution. Previously approval used default_branch while
   execution used current branch, showing misleading command expansions.

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

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

The expand_command_template function now passes hook-specific extra vars:
- PreCommit: target = default branch (for comparison context)
- PreMerge/PostMerge: target = current branch (matches run_hook behavior)

Previously, --expanded would show raw {{ target }} placeholders for these
hook types because extra_vars was always empty.

Also added TODO for pre-remove approval context issue: when removing
another worktree, the approval preview uses current worktree context
instead of the target worktree context.

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

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit b2048db into main Dec 10, 2025
@max-sixty
max-sixty deleted the user-hooks branch December 10, 2025 04:57
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.

1 participant