Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Refactor shell function names to avoid conflicts

Maximilian Roos7146ab0d3d35
diff --git a/default_16038749127235320115_0_42549.profraw b/default_16038749127235320115_0_42549.profraw
new file mode 100644
index 000000000..b4f78252e
Binary files /dev/null and b/default_16038749127235320115_0_42549.profraw differ
diff --git a/default_16038749127235320115_0_88478.profraw b/default_16038749127235320115_0_88478.profraw
new file mode 100644
index 000000000..e4fd948e3
Binary files /dev/null and b/default_16038749127235320115_0_88478.profraw differ
diff --git a/default_16038749127235320115_0_98828.profraw b/default_16038749127235320115_0_98828.profraw
new file mode 100644
index 000000000..5108f5789
Binary files /dev/null and b/default_16038749127235320115_0_98828.profraw differ
diff --git a/src/shell.rs b/src/shell.rs
index cb0e0bb3b..2e0d23091 100644
--- a/src/shell.rs
+++ b/src/shell.rs
@@ -348,6 +348,15 @@ 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")]
@@ -699,6 +708,24 @@ 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.sh
index d1f802952..1429203b2 100644
--- a/templates/bash.sh
+++ b/templates/bash.sh
@@ -51,7 +51,7 @@ if command -v {{ cmd }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
return
fi
- wt_exec "${args[@]}"
+ _{{ cmd|safe_fn }}_exec "${args[@]}"
}
# Lazy completions - generate on first TAB, then delegate to clap's completer
diff --git a/templates/fish.fish b/templates/fish.fish
index 1ad8f0d99..a65d0c07e 100644
--- a/templates/fish.fish
+++ b/templates/fish.fish
@@ -2,15 +2,16 @@
# Only initialize if {{ cmd }} is available (in PATH or via WORKTRUNK_BIN)
if type -q {{ cmd }}; or test -n "$WORKTRUNK_BIN"
- # Execute wt command with file-based directive passing.
+ # Execute {{ 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 wt_exec
+ function _{{ cmd|safe_fn }}_exec
test -n "$WORKTRUNK_BIN"; or set -l WORKTRUNK_BIN (type -P {{ cmd }})
set -l directive_file (mktemp)
@@ -66,7 +67,7 @@ if type -q {{ cmd }}; or test -n "$WORKTRUNK_BIN"
return $status
end
- wt_exec $args
+ _{{ cmd|safe_fn }}_exec $args
end
# 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.sh
index 190cb4ba6..e6bd6365c 100644
--- a/templates/posix_directives.sh
+++ b/templates/posix_directives.sh
@@ -1,7 +1,8 @@
-# Execute wt command with file-based directive passing.
+# 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).
-wt_exec() {
+# 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)"
diff --git a/templates/zsh.zsh b/templates/zsh.zsh
index 807a50bdd..ffdd9b36e 100644
--- a/templates/zsh.zsh
+++ b/templates/zsh.zsh
@@ -54,7 +54,7 @@ if command -v {{ cmd }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
return
fi
- wt_exec "${args[@]}"
+ _{{ cmd|safe_fn }}_exec "${args[@]}"
}
# Lazy completions - generate on first TAB, then delegate to clap's completer
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snap
index 6bbed2c25..6a1b179db 100644
--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap
@@ -38,7 +38,8 @@ 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).
-wt_exec() {
+# Function name includes cmd to avoid conflicts when multiple commands are loaded.
+_wt_exec() {
local directive_file exit_code=0
directive_file="$(mktemp)"
@@ -101,7 +102,7 @@ wt_exec() {
return
fi
- wt_exec "${args[@]}"
+ _wt_exec "${args[@]}"
}
# Lazy completions - generate on first TAB, then delegate to clap's completer
diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snap
index 0fefb0086..b25c94774 100644
--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap
@@ -37,12 +37,13 @@ if type -q wt; or test -n "$WORKTRUNK_BIN"
# Execute 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
+ function _wt_exec
test -n "$WORKTRUNK_BIN"; or set -l WORKTRUNK_BIN (type -P wt)
set -l directive_file (mktemp)
@@ -98,7 +99,7 @@ if type -q wt; or test -n "$WORKTRUNK_BIN"
return $status
end
- wt_exec $args
+ _wt_exec $args
end
# 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.snap
index b0519935d..cfef01f96 100644
--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
@@ -41,7 +41,8 @@ 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).
-wt_exec() {
+# Function name includes cmd to avoid conflicts when multiple commands are loaded.
+_wt_exec() {
local directive_file exit_code=0
directive_file="$(mktemp)"
@@ -104,7 +105,7 @@ wt_exec() {
return
fi
- wt_exec "${args[@]}"
+ _wt_exec "${args[@]}"
}
# Lazy completions - generate on first TAB, then delegate to clap's completer