Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 6 additions & 26 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,13 @@ pub enum ConfigCommand {
/// Refresh default branch from remote
RefreshCache,
/// Configure shell integration
#[command(long_about = r#"Configure shell by writing to config files
#[command(long_about = r#"Configure shell integration for directory changes

This command automatically adds the appropriate integration line to your shell's config file.
It supports Bash, Zsh, Fish, Nushell, PowerShell, Elvish, Xonsh, and Oil.
Adds the integration line to your shell config so `wt switch` can cd and `wt remove` can
return to the previous directory. Supports Bash, Zsh, and Fish; other shells are temporarily
disabled while compatibility issues are resolved.

The integration enables 'wt switch' to change directories and 'wt remove' to return to the
previous location.

MANUAL SETUP (if you prefer):

Add one line to your shell config:
Manual setup (single line in your shell config):

Bash (~/.bashrc):
eval "$(wt init bash)"
Expand All @@ -119,23 +115,7 @@ Fish (~/.config/fish/config.fish):
Zsh (~/.zshrc):
eval "$(wt init zsh)"

Nushell (~/.config/nushell/env.nu):
wt init nushell | save -f ~/.cache/wt-init.nu

Then add to ~/.config/nushell/config.nu:
source ~/.cache/wt-init.nu

PowerShell (profile):
wt init powershell | Out-String | Invoke-Expression

Elvish (~/.config/elvish/rc.elv):
eval (wt init elvish | slurp)

Xonsh (~/.xonshrc):
execx($(wt init xonsh))

Oil Shell (~/.config/oil/oshrc):
eval "$(wt init oil)""#)]
Other shells will be documented once compatibility issues are resolved."#)]
Shell {
/// Shell to configure
#[arg(long, value_enum)]
Expand Down
114 changes: 21 additions & 93 deletions src/commands/worktree.rs
Original file line number Diff line number Diff line change
@@ -1,102 +1,30 @@
//! Worktree operations with dual-mode output for shell integration.
//! Worktree operations emit either user-facing messages or directives for shell wrappers.
//!
//! # The Directory Change Problem
//! # Why dual mode?
//! Worktree commands must change the parent shell's directory, which a child process cannot do.
//! Without integration, we print paths and the user `cd`s manually. With integration, we emit
//! directives that the shell wrapper executes on the user's behalf.
//!
//! Worktree commands (`switch`, `remove`, `merge`) need to change the user's working directory,
//! but there's a fundamental Unix limitation: **child processes cannot change their parent's
//! working directory**. This is a security feature and core Unix design principle.
//! ## Without `--internal`
//! - Git operations run normally and messages print to stderr
//! - Shell stays in place; user manually switches directories
//!
//! When a user runs `wt switch my-feature`, the `wt` binary runs as a child process of the shell.
//! The binary can change *its own* working directory, but when it exits, the parent shell remains
//! in the original directory.
//! ## With `--internal` (shell wrapper)
//! - Wrapper calls `wt switch --internal ...`
//! - Stdout carries NUL-delimited directives such as `__WORKTRUNK_CD__/path\0`
//! - Stderr streams status in real time
//! - Wrapper parses directives and performs the `cd`/exec in the parent shell
//!
//! # Solution: Dual-Mode Output
//! Directives support multi-line commands for `-x` and are generated by templates from
//! `wt init <shell>`.
//!
//! We solve this with two output modes:
//! ## Exit codes with `-x`
//! - wt failure returns wt's exit code; the command is not run
//! - wt success with a failing command returns the command's exit code
//!
//! ## Without `--internal` (Direct Binary Call)
//!
//! ```bash
//! $ wt switch my-feature
//! Created new branch and worktree for 'my-feature' at /path/to/worktree
//!
//! To enable automatic cd, run: wt config shell
//!
//! $ pwd
//! /original/directory # ← User is still here!
//! ```
//!
//! The binary performs git operations and prints user-friendly messages, but **cannot** change
//! the parent shell's directory. User must manually `cd` to the worktree.
//!
//! ## With `--internal` (Via Shell Wrapper)
//!
//! ```bash
//! $ wt switch my-feature
//! Created new branch and worktree for 'my-feature' at /path/to/worktree
//!
//! $ pwd
//! /path/to/worktree # ← Automatically changed!
//! ```
//!
//! When shell integration is enabled (`eval "$(wt init bash)"`), the shell function intercepts
//! commands and adds `--internal`:
//!
//! 1. Shell wrapper calls: `wt switch --internal my-feature`
//! 2. Binary outputs special directive protocol:
//! ```text
//! __WORKTRUNK_CD__/path/to/worktree
//! Created new branch and worktree for 'my-feature' at /path/to/worktree
//! ```
//! 3. Shell wrapper parses output line-by-line
//! 4. When it sees `__WORKTRUNK_CD__<path>`, it executes `cd <path>` in the parent shell
//! 5. Other lines are printed normally
//!
//! The binary **never changes directories itself** - it just communicates the desired path back
//! to the shell wrapper via stdout using the `__WORKTRUNK_CD__` directive protocol.
//!
//! # Implementation Details
//!
//! Result types (`SwitchResult`, `RemoveResult`) are pure data structures that only contain
//! operation results. All presentation logic is handled by the `output` module:
//!
//! - `output::handle_switch_output()`: Formats and outputs switch operation results
//! - `output::handle_remove_output()`: Formats and outputs remove operation results
//!
//! These handlers automatically select the appropriate mode (user-friendly or directive protocol)
//! based on the `internal` flag
//!
//! ## Directive Protocol
//!
//! Directives are NUL-terminated to support multi-line commands (e.g., via `-x` flag):
//!
//! ```text
//! __WORKTRUNK_CD__/path/to/worktree\0
//! Message for user\0
//! __WORKTRUNK_EXEC__echo 'line1'\necho 'line2'\0
//! ```
//!
//! Shell wrappers split on `\0` bytes and process each chunk:
//! - `__WORKTRUNK_CD__<path>`: Change directory to `<path>`
//! - `__WORKTRUNK_EXEC__<command>`: Execute `<command>` (may contain newlines)
//! - Other chunks: Print as regular output
//!
//! ## Exit Code Semantics
//!
//! When using `-x` to execute commands after switching:
//!
//! - **If wt operation fails**: Returns wt's exit code (command never executes)
//! - **If wt succeeds but command fails**: Returns the command's exit code
//! - **Rationale**: Enables command chaining with proper error propagation
//!
//! Example:
//! ```bash
//! wt switch feature -x "cargo build" && cargo test
//! # If wt fails (e.g., worktree doesn't exist), cargo build never runs
//! # If cargo build fails, cargo test doesn't run (exit code propagates)
//! ```
//!
//! The shell wrapper is generated by `wt init <shell>` from templates in `templates/`.
//! Result types (`SwitchResult`, `RemoveResult`) stay data-only. Presentation lives in
//! `output::handle_switch_output()` and `output::handle_remove_output()`, which pick the right
//! mode based on the `internal` flag.

use std::path::PathBuf;
use worktrunk::HookType;
Expand Down