Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Refactor shell integration to use `wt` directly
Maximilian Roos8e41268f4b6e
diff --git a/DEMO.md b/DEMO.mdindex 1aed60d27..39517e17e 100644--- a/DEMO.md+++ b/DEMO.md@@ -50,15 +50,23 @@ _wt_exec() {return $exit_code}-# Main commands that support directory changes-wt-switch() {- _wt_exec switch "$@"+# Override wt command to add --internal flag for switch and finish+wt() {+ local subcommand="$1"++ case "$subcommand" in+ switch|finish)+ # Commands that need --internal for directory change support+ shift+ _wt_exec "$subcommand" --internal "$@"+ ;;+ *)+ # All other commands pass through directly+ command wt "$@"+ ;;+ esac}--wt-finish() {- _wt_exec finish "$@"-}-# ... (more functions)+# ... (more code)```### Step 2: Understanding the Directive Protocol@@ -83,7 +91,7 @@ The shell wrapper:$ eval "$(wt init bash)"# Now you can use wt commands:-$ wt-switch feature-x+$ wt switch feature-x__WORKTRUNK_CD__/tmp/example-worktreeSwitched to worktree: feature-x@@ -100,10 +108,10 @@ $ pwd# Use a custom prefix instead of default 'wt'$ eval "$(wt init bash --cmd myprefix)"-# Now use myprefix- commands:-$ myprefix-switch feature-$ myprefix-finish-$ myprefix-list+# Now use myprefix commands:+$ myprefix switch feature+$ myprefix finish+$ myprefix list```### Enable Prompt Hook@@ -136,8 +144,7 @@ $ eval "$(wt init fish --cmd myprefix --hook prompt)"┌─────────────────┐│ Shell Wrapper │ Generated by templates/bash.sh│ Functions: │-│ - wt-switch │ Calls: wt switch --internal-│ - wt-finish │ Calls: wt finish --internal+│ - wt() │ Intercepts: wt switch, wt finish│ - _wt_exec │ Parses: __WORKTRUNK_CD__ directives└────────┬────────┘│diff --git a/README.md b/README.mdindex fd93e302d..7667a1aa6 100644--- a/README.md+++ b/README.md@@ -51,16 +51,16 @@ eval "$(wt init zsh)"wt list# Switch to a worktree (creates if doesn't exist)-wt-switch feature-branch+wt switch feature-branch# Finish current worktree and return to primary-wt-finish+wt finish# Push changes between worktreeswt push target-worktree# Merge and cleanup-wt merge main --squash+wt merge main```### Customization@@ -70,7 +70,7 @@ wt merge main --squash# Use a custom prefix instead of 'wt'eval "$(wt init bash --cmd myprefix)"-# Now use: myprefix-switch, myprefix-finish, etc.+# Now use: myprefix switch, myprefix finish, etc.```**Enable prompt hook:**@@ -113,14 +113,14 @@ See [TODO.md](TODO.md) for detailed roadmap.wt (Rust binary)├── Core commands (work standalone)│ ├── wt list-│ ├── wt remove-│ └── wt status+│ ├── wt push+│ └── wt merge├── Internal commands (for shell wrapper)│ ├── wt switch --internal → outputs __WORKTRUNK_CD__ directives│ ├── wt finish --internal → outputs __WORKTRUNK_CD__ directives│ └── wt hook prompt → for prompt integration└── Shell integration- └── wt init <shell> → outputs shell wrapper functions+ └── wt init <shell> → outputs shell wrapper function```## Design Principlesdiff --git a/src/main.rs b/src/main.rsindex 2820b66f8..00467f1e2 100644--- a/src/main.rs+++ b/src/main.rs@@ -263,7 +263,7 @@ fn handle_switch(} else {println!("{}", success_msg);println!("Path: {}", worktree_path.display());- println!("Note: Use 'wt-switch' (with shell integration) for automatic cd");+ println!("Note: Use 'wt switch' (with shell integration) for automatic cd");}Ok(())@@ -323,7 +323,7 @@ fn handle_finish(internal: bool) -> Result<(), GitError> {if !internal {println!("Moved to primary worktree and removed worktree");println!("Path: {}", primary_worktree_dir.display());- println!("Note: Use 'wt-finish' (with shell integration) for automatic cd");+ println!("Note: Use 'wt finish' (with shell integration) for automatic cd");}} else {// In main repo but not on default branch: switch to defaultdiff --git a/templates/bash.sh b/templates/bash.shindex 3203372a2..6e55223e4 100644--- a/templates/bash.sh+++ b/templates/bash.sh@@ -3,7 +3,7 @@# Helper function to parse wt output and handle directives_wt_exec() {local output line exit_code- output="$("wt" "$@" 2>&1)"+ output="$(command wt "$@" 2>&1)"exit_code=$?# Parse output line by line@@ -20,13 +20,21 @@ _wt_exec() {return $exit_code}-# Main commands that support directory changes-{{ cmd_prefix }}-switch() {- _wt_exec switch --internal "$@"-}+# Override {{ cmd_prefix }} command to add --internal flag for switch and finish+{{ cmd_prefix }}() {+ local subcommand="$1"-{{ cmd_prefix }}-finish() {- _wt_exec finish --internal "$@"+ case "$subcommand" in+ switch|finish)+ # Commands that need --internal for directory change support+ shift+ _wt_exec "$subcommand" --internal "$@"+ ;;+ *)+ # All other commands pass through directly+ command wt "$@"+ ;;+ esac}{% if hook.to_string() == "prompt" %}diff --git a/templates/fish.fish b/templates/fish.fishindex 739ebbec7..b7470bac3 100644--- a/templates/fish.fish+++ b/templates/fish.fish@@ -2,7 +2,7 @@# Helper function to parse wt output and handle directivesfunction _wt_exec- set -l output (wt $argv 2>&1)+ set -l output (command wt $argv 2>&1)set -l exit_code $status# Parse output line by line@@ -19,13 +19,18 @@ function _wt_execreturn $exit_codeend-# Main commands that support directory changes-function {{ cmd_prefix }}-switch- _wt_exec switch --internal $argv-end+# Override {{ cmd_prefix }} command to add --internal flag for switch and finish+function {{ cmd_prefix }}+ set -l subcommand $argv[1]-function {{ cmd_prefix }}-finish- _wt_exec finish --internal $argv+ switch $subcommand+ case switch finish+ # Commands that need --internal for directory change support+ _wt_exec $subcommand --internal $argv[2..-1]+ case '*'+ # All other commands pass through directly+ command wt $argv+ endend{% if hook.to_string() == "prompt" %}diff --git a/tests/integration_tests/e2e_shell.rs b/tests/integration_tests/e2e_shell.rsindex 766ed0332..4d4278caf 100644--- a/tests/integration_tests/e2e_shell.rs+++ b/tests/integration_tests/e2e_shell.rs@@ -79,13 +79,13 @@ fn test_bash_e2e_switch_changes_directory() {// Create a script that:// 1. Sources the init code- // 2. Runs wt-switch to create and switch to a new branch+ // 2. Runs wt switch to create and switch to a new branch// 3. Prints the current directorylet script = format!(r#"export PATH="{}:$PATH"{}- wt-switch --create my-feature+ wt switch --create my-featurepwd"#,get_cargo_bin("wt").parent().unwrap().to_string_lossy(),@@ -125,8 +125,8 @@ fn test_bash_e2e_finish_returns_to_main() {r#"export PATH="{}:$PATH"{}- wt-switch --create my-feature- wt-finish+ wt switch --create my-feature+ wt finishpwd"#,get_cargo_bin("wt").parent().unwrap().to_string_lossy(),@@ -160,7 +160,7 @@ fn test_bash_e2e_switch_preserves_output() {r#"export PATH="{}:$PATH"{}- wt-switch --create test-branch 2>&1+ wt switch --create test-branch 2>&1"#,get_cargo_bin("wt").parent().unwrap().to_string_lossy(),init_code@@ -199,7 +199,7 @@ fn test_fish_e2e_switch_changes_directory() {r#"set -x PATH {} $PATH{}- wt-switch --create my-feature+ wt switch --create my-featurepwd"#,get_cargo_bin("wt").parent().unwrap().to_string_lossy(),@@ -234,8 +234,8 @@ fn test_fish_e2e_finish_returns_to_main() {r#"set -x PATH {} $PATH{}- wt-switch --create my-feature- wt-finish+ wt switch --create my-feature+ wt finishpwd"#,get_cargo_bin("wt").parent().unwrap().to_string_lossy(),@@ -269,7 +269,7 @@ fn test_zsh_e2e_switch_changes_directory() {r#"export PATH="{}:$PATH"{}- wt-switch --create my-feature+ wt switch --create my-featurepwd"#,get_cargo_bin("wt").parent().unwrap().to_string_lossy(),@@ -304,8 +304,8 @@ fn test_zsh_e2e_finish_returns_to_main() {r#"export PATH="{}:$PATH"{}- wt-switch --create my-feature- wt-finish+ wt switch --create my-feature+ wt finishpwd"#,get_cargo_bin("wt").parent().unwrap().to_string_lossy(),@@ -344,12 +344,12 @@ fn test_bash_e2e_custom_prefix() {let init_code = String::from_utf8(output.stdout).expect("Invalid UTF-8 in init code");- // Test that custom-switch works+ // Test that custom switch workslet script = format!(r#"export PATH="{}:$PATH"{}- custom-switch --create my-feature+ custom switch --create my-featurepwd"#,get_cargo_bin("wt").parent().unwrap().to_string_lossy(),@@ -383,8 +383,8 @@ fn test_bash_e2e_error_handling() {r#"export PATH="{}:$PATH"{}- wt-switch --create test-feature- wt-switch --create test-feature 2>&1 || echo "ERROR_CAUGHT"+ wt switch --create test-feature+ wt switch --create test-feature 2>&1 || echo "ERROR_CAUGHT""#,get_cargo_bin("wt").parent().unwrap().to_string_lossy(),init_code@@ -521,10 +521,10 @@ fn test_bash_e2e_switch_to_existing_worktree() {r#"export PATH="{}:$PATH"{}- wt-switch --create existing-branch+ wt switch --create existing-branchpwdcd /tmp- wt-switch existing-branch+ wt switch existing-branchpwd"#,get_cargo_bin("wt").parent().unwrap().to_string_lossy(),@@ -541,3 +541,47 @@ fn test_bash_e2e_switch_to_existing_worktree() {output);}++#[test]+fn test_bash_e2e_multiple_switches() {+ if !is_shell_available("bash") {+ eprintln!("Skipping test: bash not available");+ return;+ }++ let mut repo = TestRepo::new();+ repo.commit("Initial commit");+ repo.setup_remote("main");++ let init_code = generate_init_code(&repo, "bash");++ // Test that multiple switches work+ let script = format!(+ r#"+ export PATH="{}:$PATH"+ {}+ wt switch --create test-branch+ pwd+ wt finish+ pwd+ "#,+ get_cargo_bin("wt").parent().unwrap().to_string_lossy(),+ init_code+ );++ let output = execute_shell_script(&repo, "bash", &script);++ // Should have switched to test-branch+ assert!(+ output.contains("test-branch"),+ "Expected wt switch to work, got: {}",+ output+ );++ // Should have returned to main (wt finish should work)+ assert!(+ output.contains("main"),+ "Expected wt finish to work, got: {}",+ output+ );+}diff --git a/tests/snapshots/integration__integration_tests__finish__finish_from_worktree.snap b/tests/snapshots/integration__integration_tests__finish__finish_from_worktree.snapindex 689af5ab0..d3ebf652b 100644--- a/tests/snapshots/integration__integration_tests__finish__finish_from_worktree.snap+++ b/tests/snapshots/integration__integration_tests__finish__finish_from_worktree.snap@@ -19,6 +19,6 @@ exit_code: 0----- stdout -----Moved to primary worktree and removed worktreePath: [REPO]-Note: Use 'wt-finish' (with shell integration) for automatic cd+Note: Use 'wt finish' (with shell integration) for automatic cd----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snapindex 7ee9cae37..22e118632 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap@@ -23,7 +23,7 @@ exit_code: 0# Helper function to parse wt output and handle directives_wt_exec() {local output line exit_code- output="$("wt" "$@" 2>&1)"+ output="$(command wt "$@" 2>&1)"exit_code=$?# Parse output line by line@@ -40,13 +40,21 @@ _wt_exec() {return $exit_code}-# Main commands that support directory changes-wt-switch() {- _wt_exec switch --internal "$@"-}--wt-finish() {- _wt_exec finish --internal "$@"+# Override wt command to add --internal flag for switch and finish+wt() {+ local subcommand="$1"++ case "$subcommand" in+ switch|finish)+ # Commands that need --internal for directory change support+ shift+ _wt_exec "$subcommand" --internal "$@"+ ;;+ *)+ # All other commands pass through directly+ command wt "$@"+ ;;+ esac}diff --git a/tests/snapshots/integration__integration_tests__init__init_bash_all_options.snap b/tests/snapshots/integration__integration_tests__init__init_bash_all_options.snapindex b814469e6..a7fa4d0c5 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash_all_options.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash_all_options.snap@@ -27,7 +27,7 @@ exit_code: 0# Helper function to parse wt output and handle directives_wt_exec() {local output line exit_code- output="$("wt" "$@" 2>&1)"+ output="$(command wt "$@" 2>&1)"exit_code=$?# Parse output line by line@@ -44,13 +44,21 @@ _wt_exec() {return $exit_code}-# Main commands that support directory changes-wt-switch() {- _wt_exec switch --internal "$@"-}+# Override wt command to add --internal flag for switch and finish+wt() {+ local subcommand="$1"-wt-finish() {- _wt_exec finish --internal "$@"+ case "$subcommand" in+ switch|finish)+ # Commands that need --internal for directory change support+ shift+ _wt_exec "$subcommand" --internal "$@"+ ;;+ *)+ # All other commands pass through directly+ command wt "$@"+ ;;+ esac}diff --git a/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap b/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snapindex 90d396383..060601fd7 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap@@ -25,7 +25,7 @@ exit_code: 0# Helper function to parse wt output and handle directives_wt_exec() {local output line exit_code- output="$("wt" "$@" 2>&1)"+ output="$(command wt "$@" 2>&1)"exit_code=$?# Parse output line by line@@ -42,13 +42,21 @@ _wt_exec() {return $exit_code}-# Main commands that support directory changes-wt-switch() {- _wt_exec switch --internal "$@"-}+# Override wt command to add --internal flag for switch and finish+wt() {+ local subcommand="$1"-wt-finish() {- _wt_exec finish --internal "$@"+ case "$subcommand" in+ switch|finish)+ # Commands that need --internal for directory change support+ shift+ _wt_exec "$subcommand" --internal "$@"+ ;;+ *)+ # All other commands pass through directly+ command wt "$@"+ ;;+ esac}diff --git a/tests/snapshots/integration__integration_tests__init__init_bash_prompt_hook.snap b/tests/snapshots/integration__integration_tests__init__init_bash_prompt_hook.snapindex 6ed4a7273..1ca7f4e1c 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash_prompt_hook.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash_prompt_hook.snap@@ -25,7 +25,7 @@ exit_code: 0# Helper function to parse wt output and handle directives_wt_exec() {local output line exit_code- output="$("wt" "$@" 2>&1)"+ output="$(command wt "$@" 2>&1)"exit_code=$?# Parse output line by line@@ -42,13 +42,21 @@ _wt_exec() {return $exit_code}-# Main commands that support directory changes-wt-switch() {- _wt_exec switch --internal "$@"-}+# Override wt command to add --internal flag for switch and finish+wt() {+ local subcommand="$1"-wt-finish() {- _wt_exec finish --internal "$@"+ case "$subcommand" in+ switch|finish)+ # Commands that need --internal for directory change support+ shift+ _wt_exec "$subcommand" --internal "$@"+ ;;+ *)+ # All other commands pass through directly+ command wt "$@"+ ;;+ esac}diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snapindex 0ab974cd5..6ac8f0689 100644--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap@@ -22,7 +22,7 @@ exit_code: 0# Helper function to parse wt output and handle directivesfunction _wt_exec- set -l output (wt $argv 2>&1)+ set -l output (command wt $argv 2>&1)set -l exit_code $status# Parse output line by line@@ -39,13 +39,18 @@ function _wt_execreturn $exit_codeend-# Main commands that support directory changes-function wt-switch- _wt_exec switch --internal $argv-end--function wt-finish- _wt_exec finish --internal $argv+# Override wt command to add --internal flag for switch and finish+function wt+ set -l subcommand $argv[1]++ switch $subcommand+ case switch finish+ # Commands that need --internal for directory change support+ _wt_exec $subcommand --internal $argv[2..-1]+ case '*'+ # All other commands pass through directly+ command wt $argv+ endenddiff --git a/tests/snapshots/integration__integration_tests__init__init_fish_prompt_hook.snap b/tests/snapshots/integration__integration_tests__init__init_fish_prompt_hook.snapindex 39e92af75..0e11af37f 100644--- a/tests/snapshots/integration__integration_tests__init__init_fish_prompt_hook.snap+++ b/tests/snapshots/integration__integration_tests__init__init_fish_prompt_hook.snap@@ -24,7 +24,7 @@ exit_code: 0# Helper function to parse wt output and handle directivesfunction _wt_exec- set -l output (wt $argv 2>&1)+ set -l output (command wt $argv 2>&1)set -l exit_code $status# Parse output line by line@@ -41,13 +41,18 @@ function _wt_execreturn $exit_codeend-# Main commands that support directory changes-function wt-switch- _wt_exec switch --internal $argv-end+# Override wt command to add --internal flag for switch and finish+function wt+ set -l subcommand $argv[1]-function wt-finish- _wt_exec finish --internal $argv+ switch $subcommand+ case switch finish+ # Commands that need --internal for directory change support+ _wt_exec $subcommand --internal $argv[2..-1]+ case '*'+ # All other commands pass through directly+ command wt $argv+ endenddiff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snapindex d3466389e..197794686 100644--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap@@ -23,7 +23,7 @@ exit_code: 0# Helper function to parse wt output and handle directives_wt_exec() {local output line exit_code- output="$("wt" "$@" 2>&1)"+ output="$(command wt "$@" 2>&1)"exit_code=$?# Parse output line by line@@ -40,13 +40,21 @@ _wt_exec() {return $exit_code}-# Main commands that support directory changes-wt-switch() {- _wt_exec switch --internal "$@"-}--wt-finish() {- _wt_exec finish --internal "$@"+# Override wt command to add --internal flag for switch and finish+wt() {+ local subcommand="$1"++ case "$subcommand" in+ switch|finish)+ # Commands that need --internal for directory change support+ shift+ _wt_exec "$subcommand" --internal "$@"+ ;;+ *)+ # All other commands pass through directly+ command wt "$@"+ ;;+ esac}diff --git a/tests/snapshots/integration__integration_tests__merge__merge_fast_forward.snap b/tests/snapshots/integration__integration_tests__merge__merge_fast_forward.snapindex a995b4e76..8a0b2b891 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_fast_forward.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_fast_forward.snap@@ -25,6 +25,6 @@ Successfully pushed to 'main'Cleaning up worktree...Moved to primary worktree and removed worktreePath: [REPO]-Note: Use 'wt-finish' (with shell integration) for automatic cd+Note: Use 'wt finish' (with shell integration) for automatic cd----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__merge__merge_not_fast_forward.snap b/tests/snapshots/integration__integration_tests__merge__merge_not_fast_forward.snapindex a995b4e76..8a0b2b891 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_not_fast_forward.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_not_fast_forward.snap@@ -25,6 +25,6 @@ Successfully pushed to 'main'Cleaning up worktree...Moved to primary worktree and removed worktreePath: [REPO]-Note: Use 'wt-finish' (with shell integration) for automatic cd+Note: Use 'wt finish' (with shell integration) for automatic cd----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__merge__merge_to_default.snap b/tests/snapshots/integration__integration_tests__merge__merge_to_default.snapindex 2ad51e6e4..a51e934b2 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_to_default.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_to_default.snap@@ -24,6 +24,6 @@ Successfully pushed to 'main'Cleaning up worktree...Moved to primary worktree and removed worktreePath: [REPO]-Note: Use 'wt-finish' (with shell integration) for automatic cd+Note: Use 'wt finish' (with shell integration) for automatic cd----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__switch__switch_create_new.snap b/tests/snapshots/integration__integration_tests__switch__switch_create_new.snapindex 2f1148e2a..a9c01090e 100644--- a/tests/snapshots/integration__integration_tests__switch__switch_create_new.snap+++ b/tests/snapshots/integration__integration_tests__switch__switch_create_new.snap@@ -21,6 +21,6 @@ exit_code: 0----- stdout -----Created new branch and worktree for 'feature-x'Path: [REPO].feature-x-Note: Use 'wt-switch' (with shell integration) for automatic cd+Note: Use 'wt switch' (with shell integration) for automatic cd----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__switch__switch_with_base.snap b/tests/snapshots/integration__integration_tests__switch__switch_with_base.snapindex 8f302d691..5d2e5a9d0 100644--- a/tests/snapshots/integration__integration_tests__switch__switch_with_base.snap+++ b/tests/snapshots/integration__integration_tests__switch__switch_with_base.snap@@ -23,6 +23,6 @@ exit_code: 0----- stdout -----Created new branch and worktree for 'feature-with-base'Path: [REPO].feature-with-base-Note: Use 'wt-switch' (with shell integration) for automatic cd+Note: Use 'wt switch' (with shell integration) for automatic cd----- stderr -----