Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Inline shell helper function to fix Claude Code compatibility (#292)
Maximilian Roos4b8d3b489961
diff --git a/src/shell.rs b/src/shell.rsindex 2e0d23091..f14fc16a0 100644--- a/src/shell.rs+++ b/src/shell.rs@@ -320,20 +320,14 @@ impl ShellInit {pub fn generate(&self) -> Result<String, askama::Error> {match self.shell {Shell::Bash => {- let posix_shim = PosixDirectivesTemplate { cmd: &self.cmd }.render()?;let template = BashTemplate {shell_name: self.shell.to_string(),cmd: &self.cmd,- posix_shim: &posix_shim,};template.render()}Shell::Zsh => {- let posix_shim = PosixDirectivesTemplate { cmd: &self.cmd }.render()?;- let template = ZshTemplate {- cmd: &self.cmd,- posix_shim: &posix_shim,- };+ let template = ZshTemplate { cmd: &self.cmd };template.render()}Shell::Fish => {@@ -348,29 +342,12 @@ impl ShellInit {}}-/// Custom Askama filters for shell templates-mod filters {- /// Convert command name to a safe shell function identifier (hyphens → underscores)- /// Used in templates as: {{ cmd|safe_fn }}- pub fn safe_fn(cmd: &str, _: &dyn askama::Values) -> askama::Result<String> {- Ok(cmd.replace('-', "_"))- }-}--/// POSIX directive shim template (shared by bash, zsh, oil)-#[derive(Template)]-#[template(path = "posix_directives.sh", escape = "none")]-struct PosixDirectivesTemplate<'a> {- cmd: &'a str,-}-/// Bash shell template#[derive(Template)]#[template(path = "bash.sh", escape = "none")]struct BashTemplate<'a> {shell_name: String,cmd: &'a str,- posix_shim: &'a str,}/// Zsh shell template@@ -378,7 +355,6 @@ struct BashTemplate<'a> {#[template(path = "zsh.zsh", escape = "none")]struct ZshTemplate<'a> {cmd: &'a str,- posix_shim: &'a str,}/// Fish shell template@@ -708,24 +684,6 @@ mod tests {);}- #[test]- fn test_shell_init_unique_function_names() {- // Verify that commands with hyphens get safe function names- // This prevents conflicts when multiple commands are loaded (e.g., wt and git-wt)- let init = ShellInit::with_prefix(Shell::Bash, "git-wt".to_string());- let output = init.generate().unwrap();-- // Should use _git_wt_exec (underscores), not _git-wt_exec (hyphens)- assert!(- output.contains("_git_wt_exec"),- "Function name should use underscores: {output}"- );- assert!(- !output.contains("git-wt_exec"),- "Function name should not contain hyphens"- );- }-/// Verify that `config_line_with_prefix()` generates lines that/// `is_shell_integration_line()` can detect.///diff --git a/templates/bash.sh b/templates/bash.shindex f29f30709..4a37faf60 100644--- a/templates/bash.sh+++ b/templates/bash.sh@@ -3,9 +3,9 @@# Only initialize if {{ cmd }} is available (in PATH or via WORKTRUNK_BIN)if command -v {{ cmd }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then-{{ posix_shim }}-- # Override {{ cmd }} command+ # Override {{ cmd }} command with file-based directive passing.+ # Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, sources it after.+ # WORKTRUNK_BIN can override the binary path (for testing dev builds).{{ cmd }}() {local use_source=falselocal args=()@@ -14,31 +14,33 @@ if command -v {{ cmd }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; thenif [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fidone- # Completion mode: call binary directly, bypassing wt_exec.+ # Completion mode: call binary directly, no directive file needed.# This check MUST be here (not in the binary) because clap's completion# handler runs before argument parsing.- # The binary outputs completion candidates, not directives to source.if [[ -n "${COMPLETE:-}" ]]; thencommand "${WORKTRUNK_BIN:-{{ cmd }}}" "${args[@]}"returnfi+ local directive_file exit_code=0+ directive_file="$(mktemp)"+# --source: use cargo run (builds from source)if [[ "$use_source" == true ]]; then- local directive_file exit_code=0- directive_file="$(mktemp)"WORKTRUNK_DIRECTIVE_FILE="$directive_file" cargo run --bin {{ cmd }} --quiet -- "${args[@]}" || exit_code=$?- if [[ -s "$directive_file" ]]; then- source "$directive_file"- if [[ $exit_code -eq 0 ]]; then- exit_code=$?- fi+ else+ WORKTRUNK_DIRECTIVE_FILE="$directive_file" command "${WORKTRUNK_BIN:-{{ cmd }}}" "${args[@]}" || exit_code=$?+ fi++ if [[ -s "$directive_file" ]]; then+ source "$directive_file"+ if [[ $exit_code -eq 0 ]]; then+ exit_code=$?fi- rm -f "$directive_file"- return "$exit_code"fi- _{{ cmd|safe_fn }}_exec "${args[@]}"+ rm -f "$directive_file"+ return "$exit_code"}# Lazy completions - generate on first TAB, then delegate to clap's completerdiff --git a/templates/fish.fish b/templates/fish.fishindex c5f8d0a1b..61f2edd0e 100644--- a/templates/fish.fish+++ b/templates/fish.fish@@ -2,34 +2,15 @@# Only initialize if {{ cmd }} is available (in PATH or via WORKTRUNK_BIN)if type -q {{ cmd }}; or test -n "$WORKTRUNK_BIN"- # Execute {{ cmd }} command with file-based directive passing.++ # Override {{ cmd }} command with file-based directive passing.# Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, evals it after.# WORKTRUNK_BIN can override the binary path (for testing dev builds).- # Function name includes cmd to avoid conflicts when multiple commands are loaded.## Note: We use `eval (cat ... | string collect)` instead of `source` because:# 1. fish's `source` doesn't propagate `exit` to the parent function# 2. `eval (cat ...)` without `string collect` splits on newlines, breaking multiline directives# With `string collect`, `exit 42` properly exits the function with code 42.- function _{{ cmd|safe_fn }}_exec- test -n "$WORKTRUNK_BIN"; or set -l WORKTRUNK_BIN (type -P {{ cmd }})-- set -l directive_file (mktemp)- WORKTRUNK_DIRECTIVE_FILE=$directive_file command $WORKTRUNK_BIN $argv- set -l exit_code $status-- if test -s "$directive_file"- eval (cat "$directive_file" | string collect)- if test $exit_code -eq 0- set exit_code $status- end- end-- rm -f "$directive_file"- return $exit_code- end-- # Override {{ cmd }} commandfunction {{ cmd }}set -l use_source falseset -l args@@ -38,22 +19,26 @@ if type -q {{ cmd }}; or test -n "$WORKTRUNK_BIN"if test "$arg" = "--source"; set use_source true; else; set -a args $arg; endend+ test -n "$WORKTRUNK_BIN"; or set -l WORKTRUNK_BIN (type -P {{ cmd }})+ set -l directive_file (mktemp)+# --source: use cargo run (builds from source)if test $use_source = true- set -l directive_file (mktemp)WORKTRUNK_DIRECTIVE_FILE=$directive_file cargo run --bin {{ cmd }} --quiet -- $args- set -l exit_code $status- if test -s "$directive_file"- eval (cat "$directive_file" | string collect)- if test $exit_code -eq 0- set exit_code $status- end+ else+ WORKTRUNK_DIRECTIVE_FILE=$directive_file command $WORKTRUNK_BIN $args+ end+ set -l exit_code $status++ if test -s "$directive_file"+ eval (cat "$directive_file" | string collect)+ if test $exit_code -eq 0+ set exit_code $statusend- rm -f "$directive_file"- return $exit_codeend- _{{ cmd|safe_fn }}_exec $args+ rm -f "$directive_file"+ return $exit_codeend# Completions are in ~/.config/fish/completions/wt.fish (installed by `wt config shell install`)diff --git a/templates/posix_directives.sh b/templates/posix_directives.shdeleted file mode 100644index e6bd6365c..000000000--- a/templates/posix_directives.sh+++ /dev/null@@ -1,20 +0,0 @@-# Execute {{ cmd }} command with file-based directive passing.-# Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, sources it after.-# WORKTRUNK_BIN can override the binary path (for testing dev builds).-# Function name includes cmd to avoid conflicts when multiple commands are loaded.-_{{ cmd|safe_fn }}_exec() {- local directive_file exit_code=0- directive_file="$(mktemp)"-- WORKTRUNK_DIRECTIVE_FILE="$directive_file" command "${WORKTRUNK_BIN:-{{ cmd }}}" "$@" || exit_code=$?-- if [[ -s "$directive_file" ]]; then- source "$directive_file"- if [[ $exit_code -eq 0 ]]; then- exit_code=$?- fi- fi-- rm -f "$directive_file"- return "$exit_code"-}diff --git a/templates/zsh.zsh b/templates/zsh.zshindex 16968ef89..ed510b0af 100644--- a/templates/zsh.zsh+++ b/templates/zsh.zsh@@ -6,9 +6,9 @@# Only initialize if {{ cmd }} is available (in PATH or via WORKTRUNK_BIN)if command -v {{ cmd }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then-{{ posix_shim }}-- # Override {{ cmd }} command+ # Override {{ cmd }} command with file-based directive passing.+ # Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, sources it after.+ # WORKTRUNK_BIN can override the binary path (for testing dev builds).{{ cmd }}() {local use_source=falselocal -a args@@ -17,31 +17,33 @@ if command -v {{ cmd }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; thenif [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fidone- # Completion mode: call binary directly, bypassing wt_exec.+ # Completion mode: call binary directly, no directive file needed.# This check MUST be here (not in the binary) because clap's completion# handler runs before argument parsing.- # The binary outputs completion candidates, not directives to source.if [[ -n "${COMPLETE:-}" ]]; thencommand "${WORKTRUNK_BIN:-{{ cmd }}}" "${args[@]}"returnfi+ local directive_file exit_code=0+ directive_file="$(mktemp)"+# --source: use cargo run (builds from source)if [[ "$use_source" == true ]]; then- local directive_file exit_code=0- directive_file="$(mktemp)"WORKTRUNK_DIRECTIVE_FILE="$directive_file" cargo run --bin {{ cmd }} --quiet -- "${args[@]}" || exit_code=$?- if [[ -s "$directive_file" ]]; then- source "$directive_file"- if [[ $exit_code -eq 0 ]]; then- exit_code=$?- fi+ else+ WORKTRUNK_DIRECTIVE_FILE="$directive_file" command "${WORKTRUNK_BIN:-{{ cmd }}}" "${args[@]}" || exit_code=$?+ fi++ if [[ -s "$directive_file" ]]; then+ source "$directive_file"+ if [[ $exit_code -eq 0 ]]; then+ exit_code=$?fi- rm -f "$directive_file"- return "$exit_code"fi- _{{ cmd|safe_fn }}_exec "${args[@]}"+ rm -f "$directive_file"+ return "$exit_code"}# Lazy completions - generate on first TAB, then delegate to clap's completerdiff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rsindex af4f9a108..b1c9f9dc1 100644--- a/tests/integration_tests/shell_wrapper.rs+++ b/tests/integration_tests/shell_wrapper.rs@@ -1666,7 +1666,7 @@ approved-commands = ["echo 'background job'"]/// To detect if they leak, we use `exec_bash_truly_interactive` which runs bash without/// `-c` and writes commands to the PTY, triggering prompts where notifications appear.///- /// The shell wrapper suppresses these via two mechanisms (see posix_directives.sh):+ /// The shell wrapper suppresses these via two mechanisms (see bash.sh/zsh.zsh templates):/// - START notifications (`[1] 12345`): stderr redirection around `&`/// - DONE notifications (`[1]+ Done`): `set +m` before backgrounding#[rstest]diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snapindex bc250d318..5a56c4681 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap@@ -35,28 +35,9 @@ exit_code: 0# Only initialize if wt is available (in PATH or via WORKTRUNK_BIN)if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then-# Execute wt command with file-based directive passing.-# Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, sources it after.-# WORKTRUNK_BIN can override the binary path (for testing dev builds).-# Function name includes cmd to avoid conflicts when multiple commands are loaded.-_wt_exec() {- local directive_file exit_code=0- directive_file="$(mktemp)"-- WORKTRUNK_DIRECTIVE_FILE="$directive_file" command "${WORKTRUNK_BIN:-wt}" "$@" || exit_code=$?-- if [[ -s "$directive_file" ]]; then- source "$directive_file"- if [[ $exit_code -eq 0 ]]; then- exit_code=$?- fi- fi-- rm -f "$directive_file"- return "$exit_code"-}-- # Override wt command+ # Override wt command with file-based directive passing.+ # Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, sources it after.+ # WORKTRUNK_BIN can override the binary path (for testing dev builds).wt() {local use_source=falselocal args=()@@ -65,31 +46,33 @@ _wt_exec() {if [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fidone- # Completion mode: call binary directly, bypassing wt_exec.+ # Completion mode: call binary directly, no directive file needed.# This check MUST be here (not in the binary) because clap's completion# handler runs before argument parsing.- # The binary outputs completion candidates, not directives to source.if [[ -n "${COMPLETE:-}" ]]; thencommand "${WORKTRUNK_BIN:-wt}" "${args[@]}"returnfi+ local directive_file exit_code=0+ directive_file="$(mktemp)"+# --source: use cargo run (builds from source)if [[ "$use_source" == true ]]; then- local directive_file exit_code=0- directive_file="$(mktemp)"WORKTRUNK_DIRECTIVE_FILE="$directive_file" cargo run --bin wt --quiet -- "${args[@]}" || exit_code=$?- if [[ -s "$directive_file" ]]; then- source "$directive_file"- if [[ $exit_code -eq 0 ]]; then- exit_code=$?- fi+ else+ WORKTRUNK_DIRECTIVE_FILE="$directive_file" command "${WORKTRUNK_BIN:-wt}" "${args[@]}" || exit_code=$?+ fi++ if [[ -s "$directive_file" ]]; then+ source "$directive_file"+ if [[ $exit_code -eq 0 ]]; then+ exit_code=$?fi- rm -f "$directive_file"- return "$exit_code"fi- _wt_exec "${args[@]}"+ rm -f "$directive_file"+ return "$exit_code"}# Lazy completions - generate on first TAB, then delegate to clap's completerdiff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snapindex 68e8fa7c5..29af410db 100644--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap@@ -34,34 +34,15 @@ exit_code: 0# Only initialize if wt is available (in PATH or via WORKTRUNK_BIN)if type -q wt; or test -n "$WORKTRUNK_BIN"- # Execute wt command with file-based directive passing.++ # Override wt command with file-based directive passing.# Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, evals it after.# WORKTRUNK_BIN can override the binary path (for testing dev builds).- # Function name includes cmd to avoid conflicts when multiple commands are loaded.## Note: We use `eval (cat ... | string collect)` instead of `source` because:# 1. fish's `source` doesn't propagate `exit` to the parent function# 2. `eval (cat ...)` without `string collect` splits on newlines, breaking multiline directives# With `string collect`, `exit 42` properly exits the function with code 42.- function _wt_exec- test -n "$WORKTRUNK_BIN"; or set -l WORKTRUNK_BIN (type -P wt)-- set -l directive_file (mktemp)- WORKTRUNK_DIRECTIVE_FILE=$directive_file command $WORKTRUNK_BIN $argv- set -l exit_code $status-- if test -s "$directive_file"- eval (cat "$directive_file" | string collect)- if test $exit_code -eq 0- set exit_code $status- end- end-- rm -f "$directive_file"- return $exit_code- end-- # Override wt commandfunction wtset -l use_source falseset -l args@@ -70,22 +51,26 @@ if type -q wt; or test -n "$WORKTRUNK_BIN"if test "$arg" = "--source"; set use_source true; else; set -a args $arg; endend+ test -n "$WORKTRUNK_BIN"; or set -l WORKTRUNK_BIN (type -P wt)+ set -l directive_file (mktemp)+# --source: use cargo run (builds from source)if test $use_source = true- set -l directive_file (mktemp)WORKTRUNK_DIRECTIVE_FILE=$directive_file cargo run --bin wt --quiet -- $args- set -l exit_code $status- if test -s "$directive_file"- eval (cat "$directive_file" | string collect)- if test $exit_code -eq 0- set exit_code $status- end+ else+ WORKTRUNK_DIRECTIVE_FILE=$directive_file command $WORKTRUNK_BIN $args+ end+ set -l exit_code $status++ if test -s "$directive_file"+ eval (cat "$directive_file" | string collect)+ if test $exit_code -eq 0+ set exit_code $statusend- rm -f "$directive_file"- return $exit_codeend- _wt_exec $args+ rm -f "$directive_file"+ return $exit_codeend# Completions are in ~/.config/fish/completions/wt.fish (installed by `wt config shell install`)diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snapindex d04041814..be0a9353f 100644--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap@@ -38,28 +38,9 @@ exit_code: 0# Only initialize if wt is available (in PATH or via WORKTRUNK_BIN)if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then-# Execute wt command with file-based directive passing.-# Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, sources it after.-# WORKTRUNK_BIN can override the binary path (for testing dev builds).-# Function name includes cmd to avoid conflicts when multiple commands are loaded.-_wt_exec() {- local directive_file exit_code=0- directive_file="$(mktemp)"-- WORKTRUNK_DIRECTIVE_FILE="$directive_file" command "${WORKTRUNK_BIN:-wt}" "$@" || exit_code=$?-- if [[ -s "$directive_file" ]]; then- source "$directive_file"- if [[ $exit_code -eq 0 ]]; then- exit_code=$?- fi- fi-- rm -f "$directive_file"- return "$exit_code"-}-- # Override wt command+ # Override wt command with file-based directive passing.+ # Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, sources it after.+ # WORKTRUNK_BIN can override the binary path (for testing dev builds).wt() {local use_source=falselocal -a args@@ -68,31 +49,33 @@ _wt_exec() {if [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fidone- # Completion mode: call binary directly, bypassing wt_exec.+ # Completion mode: call binary directly, no directive file needed.# This check MUST be here (not in the binary) because clap's completion# handler runs before argument parsing.- # The binary outputs completion candidates, not directives to source.if [[ -n "${COMPLETE:-}" ]]; thencommand "${WORKTRUNK_BIN:-wt}" "${args[@]}"returnfi+ local directive_file exit_code=0+ directive_file="$(mktemp)"+# --source: use cargo run (builds from source)if [[ "$use_source" == true ]]; then- local directive_file exit_code=0- directive_file="$(mktemp)"WORKTRUNK_DIRECTIVE_FILE="$directive_file" cargo run --bin wt --quiet -- "${args[@]}" || exit_code=$?- if [[ -s "$directive_file" ]]; then- source "$directive_file"- if [[ $exit_code -eq 0 ]]; then- exit_code=$?- fi+ else+ WORKTRUNK_DIRECTIVE_FILE="$directive_file" command "${WORKTRUNK_BIN:-wt}" "${args[@]}" || exit_code=$?+ fi++ if [[ -s "$directive_file" ]]; then+ source "$directive_file"+ if [[ $exit_code -eq 0 ]]; then+ exit_code=$?fi- rm -f "$directive_file"- return "$exit_code"fi- _wt_exec "${args[@]}"+ rm -f "$directive_file"+ return "$exit_code"}# Lazy completions - generate on first TAB, then delegate to clap's completer