Skip to content

Fix hook environment and auto-cd functionality#13

Merged
satococoa merged 3 commits intomainfrom
fix-hook
Aug 1, 2025
Merged

Fix hook environment and auto-cd functionality#13
satococoa merged 3 commits intomainfrom
fix-hook

Conversation

@satococoa
Copy link
Owner

@satococoa satococoa commented Aug 1, 2025

Summary

  • Fix environment variable leakage from shell integration to hooks
  • Improve auto-cd functionality after wtp add commands
  • Rename variables for better code clarity

Changes

1. Environment Variable Filtering in Hooks

  • Filter out WTP_SHELL_INTEGRATION from hook execution environment
  • Prevents shell integration state from affecting hook commands
  • Ensures e2e tests run in clean environment when executed through hooks

2. Auto-CD Functionality Improvements

  • Fix branch name extraction logic for -b/--branch options
  • Properly handle worktree name detection for auto-cd after wtp add
  • Support all shell types (bash, zsh, fish) consistently

3. Code Clarity Improvements

  • Rename branch_name variables to worktree_name for accuracy
  • Update comments to reflect actual functionality

Test plan

  • e2e tests pass when run directly
  • Shell integration works correctly with -b option
  • Hook execution no longer pollutes environment variables
  • Auto-cd functionality works for various wtp add scenarios

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Improvements
    • Enhanced the reliability of shell integration for the wtp add command across bash, zsh, and fish by improving how the target worktree name is determined from command-line arguments.
    • Updated environment handling to prevent certain internal environment variables from being passed to hooks, ensuring cleaner and more predictable execution.

satococoa and others added 2 commits August 1, 2025 22:36
This prevents the shell integration environment variable from
being passed to hook commands, ensuring e2e tests run in a
clean environment when executed through post-create hooks.

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

Co-Authored-By: Claude <noreply@anthropic.com>
- Fix branch name extraction for -b/--branch options in shell integration
- Properly handle worktree name detection for auto-cd functionality
- Rename branch_name variables to worktree_name for clarity
- Support all three shells (bash, zsh, fish) consistently

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

Co-Authored-By: Claude <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings August 1, 2025 14:14
@coderabbitai
Copy link

coderabbitai bot commented Aug 1, 2025

Walkthrough

The changes update shell integration logic for the wtp add command to consistently prioritize extracting the worktree name from -b or --branch options before falling back to positional arguments, across bash, zsh, and fish shell scripts. Additionally, the hook executor now filters out environment variables prefixed with WTP_SHELL_INTEGRATION= before executing hooks.

Changes

Cohort / File(s) Change Summary
Shell Integration Logic (wtp add)
cmd/wtp/completion.go
Refactored shell integration functions for bash, zsh, and fish to consistently extract the worktree name from -b/--branch options first, then from positional arguments. The logic is now unified and robust across all supported shells.
Hook Environment Filtering
internal/hooks/executor.go
Modified hook execution to filter out environment variables starting with WTP_SHELL_INTEGRATION= before passing them to the executed command. The rest of the environment setup remains unchanged.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ShellFunction
    participant wtp

    User->>ShellFunction: wtp add [args] [-b branch]
    ShellFunction->>ShellFunction: Extract worktree name from -b/--branch or positional arg
    ShellFunction->>wtp: wtp cd <worktree_name>
    wtp-->>ShellFunction: Returns directory path
    ShellFunction->>ShellFunction: cd into returned directory
Loading
sequenceDiagram
    participant HookExecutor
    participant OS

    HookExecutor->>OS: Get os.Environ()
    HookExecutor->>HookExecutor: Filter out WTP_SHELL_INTEGRATION=*
    HookExecutor->>OS: Execute hook command with filtered env
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

A bunny hopped through shell and hook,
Tidying logic wherever it looked.
From bash to fish, the names align,
No stray env vars left behind.
With paws so neat and code so bright,
The wtp tools now work just right! 🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0651e76 and d740ad5.

📒 Files selected for processing (1)
  • cmd/wtp/completion.go (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • cmd/wtp/completion.go
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-hook

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@satococoa satococoa self-assigned this Aug 1, 2025

This comment was marked as outdated.

Fixes issue where second argument parsing loop could not access original
arguments after they were consumed by the first loop. This ensures
auto-cd works correctly for all wtp add command variations.

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

Co-Authored-By: Claude <noreply@anthropic.com>
@satococoa satococoa requested a review from Copilot August 1, 2025 14:31
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses environment variable leakage and improves shell integration functionality for the wtp add command. The changes ensure hooks execute in a clean environment and enhance auto-cd behavior across different shell types.

  • Filter out WTP_SHELL_INTEGRATION environment variable from hook execution to prevent state leakage
  • Improve worktree name detection logic to properly handle -b/--branch options for auto-cd functionality
  • Rename variables from branch_name to worktree_name for better code clarity

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
internal/hooks/executor.go Adds environment variable filtering to remove WTP_SHELL_INTEGRATION from hook execution
cmd/wtp/completion.go Enhances shell integration with improved worktree name extraction logic for bash, zsh, and fish
Comments suppressed due to low confidence (1)

internal/hooks/executor.go:130

  • The variable name 'e' is ambiguous and conflicts with the receiver variable 'e *Executor'. Consider renaming to 'envVar' or 'envEntry' for clarity.
	for _, e := range env {

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