Skip to content

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

fix: Windows Git Bash shell integration path handling (#398)

Maximilian Roos81077c3265fb
diff --git a/src/commands/select.rs b/src/commands/select.rs
index 18134bc76..8df9718b5 100644
--- a/src/commands/select.rs
+++ b/src/commands/select.rs
@@ -9,6 +9,7 @@ use std::sync::{Arc, OnceLock};
use std::time::{Duration, Instant};
use worktrunk::config::WorktrunkConfig;
use worktrunk::git::{Repository, parse_numstat_line};
+use worktrunk::shell::extract_filename_from_path;
use super::list::collect;
use super::list::layout::{DiffDisplayConfig, DiffVariant};
@@ -48,13 +49,19 @@ fn get_diff_pager() -> Option<&'static String> {
/// pager command with appropriate flags. This would eliminate the need to
/// maintain a list of pagers that need special handling.
fn pager_needs_paging_disabled(pager_cmd: &str) -> bool {
- // Split on whitespace to get the command name, then check basename
+ // Split on whitespace to get the command name, then extract basename
+ // Uses extract_filename_from_path for consistent handling of Windows paths and .exe
pager_cmd
.split_whitespace()
.next()
- .and_then(|cmd| cmd.rsplit('/').next())
+ .and_then(extract_filename_from_path)
// bat is called "batcat" on Debian/Ubuntu
- .is_some_and(|basename| matches!(basename, "delta" | "bat" | "batcat"))
+ // Case-insensitive for Windows where commands might be Delta.exe, BAT.EXE, etc.
+ .is_some_and(|basename| {
+ basename.eq_ignore_ascii_case("delta")
+ || basename.eq_ignore_ascii_case("bat")
+ || basename.eq_ignore_ascii_case("batcat")
+ })
}
/// Maximum time to wait for pager to complete.
@@ -1043,6 +1050,15 @@ mod tests {
assert!(!pager_needs_paging_disabled("delta-preview"));
assert!(!pager_needs_paging_disabled("/path/to/delta-preview"));
assert!(pager_needs_paging_disabled("batcat")); // Debian's bat package name
+
+ // Case-insensitive matching (Windows command names)
+ assert!(pager_needs_paging_disabled("Delta"));
+ assert!(pager_needs_paging_disabled("DELTA"));
+ assert!(pager_needs_paging_disabled("BAT"));
+ assert!(pager_needs_paging_disabled("Bat"));
+ assert!(pager_needs_paging_disabled("BatCat"));
+ assert!(pager_needs_paging_disabled("delta.exe"));
+ assert!(pager_needs_paging_disabled("Delta.EXE"));
}
#[test]
diff --git a/src/main.rs b/src/main.rs
index 2feb37d7b..9078a23c7 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,6 +6,7 @@ use std::process;
use worktrunk::config::{WorktrunkConfig, set_config_path};
use worktrunk::git::{Repository, exit_code, set_base_path};
use worktrunk::path::format_path_for_display;
+use worktrunk::shell::extract_filename_from_path;
use worktrunk::styling::{
error_message, format_with_gutter, hint_message, info_message, println, success_message,
warning_message,
@@ -935,7 +936,7 @@ fn main() {
// Hint about restarting shell (only if current shell was affected)
let current_shell = std::env::var("SHELL")
.ok()
- .and_then(|s| s.rsplit('/').next().map(String::from));
+ .and_then(|s| extract_filename_from_path(&s).map(String::from));
let current_shell_affected =
current_shell.as_ref().is_some_and(|shell_name| {
diff --git a/src/output/shell_integration.rs b/src/output/shell_integration.rs
index 18b8aa2cb..be956828a 100644
--- a/src/output/shell_integration.rs
+++ b/src/output/shell_integration.rs
@@ -60,7 +60,7 @@ use color_print::cformat;
use worktrunk::config::WorktrunkConfig;
use worktrunk::path::format_path_for_display;
-use worktrunk::shell::Shell;
+use worktrunk::shell::{Shell, extract_filename_from_path};
use worktrunk::styling::hint_message;
/// Shell integration install hint message.
@@ -75,8 +75,9 @@ pub(crate) fn shell_restart_hint() -> &'static str {
/// Shell integration hint for unknown/unsupported shell.
fn shell_integration_unsupported_shell(shell_path: &str) -> String {
- // Extract shell name from path (e.g., "/bin/tcsh" -> "tcsh")
- let shell_name = shell_path.rsplit('/').next().unwrap_or(shell_path);
+ // Extract shell name from path, handling both Unix and Windows paths
+ // e.g., "/bin/tcsh" -> "tcsh", "C:\...\tcsh.exe" -> "tcsh"
+ let shell_name = extract_filename_from_path(shell_path).unwrap_or(shell_path);
format!(
"Shell integration not yet supported for {shell_name} (supports bash, zsh, fish, PowerShell)"
)
@@ -252,7 +253,7 @@ pub fn print_shell_install_result(
if shells_configured_count > 0 {
let current_shell = std::env::var("SHELL")
.ok()
- .and_then(|s| s.rsplit('/').next().map(String::from));
+ .and_then(|s| extract_filename_from_path(&s).map(String::from));
let current_shell_result = current_shell.as_ref().and_then(|shell_name| {
scan_result
diff --git a/src/shell.rs b/src/shell.rs
index 13dd1e064..f219231ff 100644
--- a/src/shell.rs
+++ b/src/shell.rs
@@ -646,15 +646,31 @@ pub fn detect_zsh_compinit() -> Option<bool> {
}
}
-/// Get the current shell from `$SHELL` environment variable.
+/// Extract executable name from a path, stripping `.exe` on Windows.
///
-/// Returns `None` if `$SHELL` is not set or doesn't match a known shell.
-/// Handles versioned/prefixed binaries like `/nix/store/.../zsh-5.9` or `bash5`
-/// by checking if the name starts with a known shell.
-pub fn current_shell() -> Option<Shell> {
- let shell_path = std::env::var("SHELL").ok()?;
- let shell_name = shell_path.rsplit('/').next()?;
+/// Uses `std::path::Path` for platform-native path handling:
+/// - Unix: `/usr/bin/bash` -> "bash"
+/// - Windows: `C:\Program Files\Git\usr\bin\bash.exe` -> "bash"
+///
+/// Only strips `.exe` extension (not other extensions like `.9` in `zsh-5.9`).
+pub fn extract_filename_from_path(path: &str) -> Option<&str> {
+ let filename = std::path::Path::new(path).file_name()?.to_str()?;
+
+ // Strip .exe extension (case-insensitive for Windows)
+ // Don't use file_stem() because it would strip version numbers like ".9" from "zsh-5.9"
+ // Handle all case variants: .exe, .EXE, .Exe, .eXe, etc.
+ if filename.len() > 4 && filename[filename.len() - 4..].eq_ignore_ascii_case(".exe") {
+ Some(&filename[..filename.len() - 4])
+ } else {
+ Some(filename)
+ }
+}
+/// Determine Shell variant from a shell name (without path or extension).
+///
+/// Handles versioned/prefixed binaries like `zsh-5.9` or `bash5`
+/// by checking if the name starts with a known shell.
+fn shell_from_name(shell_name: &str) -> Option<Shell> {
// Try exact match first
if let Ok(shell) = shell_name.parse() {
return Some(shell);
@@ -676,6 +692,21 @@ pub fn current_shell() -> Option<Shell> {
}
}
+/// Get the current shell from `$SHELL` environment variable.
+///
+/// Returns `None` if `$SHELL` is not set or doesn't match a known shell.
+/// Handles versioned/prefixed binaries like `/nix/store/.../zsh-5.9` or `bash5`
+/// by checking if the name starts with a known shell.
+///
+/// Works on both Unix and Windows:
+/// - Unix: `/usr/bin/bash` -> Bash
+/// - Windows Git Bash: `C:\Program Files\Git\usr\bin\bash.exe` -> Bash
+pub fn current_shell() -> Option<Shell> {
+ let shell_path = std::env::var("SHELL").ok()?;
+ let shell_name = extract_filename_from_path(&shell_path)?;
+ shell_from_name(shell_name)
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -706,6 +737,60 @@ mod tests {
assert_eq!(Shell::PowerShell.to_string(), "powershell");
}
+ // ==========================================================================
+ // Path extraction tests (Issue #348)
+ // ==========================================================================
+
+ #[rstest]
+ #[case::just_name("bash", Some("bash"))]
+ #[case::just_name_exe("bash.exe", Some("bash"))]
+ #[case::mixed_case_exe_title("bash.Exe", Some("bash"))]
+ #[case::mixed_case_exe_upper("bash.EXE", Some("bash"))]
+ #[case::mixed_case_exe_camel("bash.eXe", Some("bash"))]
+ #[case::empty("", None)]
+ fn test_extract_filename_from_path_common(#[case] path: &str, #[case] expected: Option<&str>) {
+ assert_eq!(extract_filename_from_path(path), expected);
+ }
+
+ #[cfg(unix)]
+ #[rstest]
+ #[case::unix_bash("/usr/bin/bash", Some("bash"))]
+ #[case::unix_zsh("/bin/zsh", Some("zsh"))]
+ #[case::unix_fish("/usr/local/bin/fish", Some("fish"))]
+ #[case::nix_versioned("/nix/store/abc123/zsh-5.9", Some("zsh-5.9"))]
+ fn test_extract_filename_from_path_unix(#[case] path: &str, #[case] expected: Option<&str>) {
+ assert_eq!(extract_filename_from_path(path), expected);
+ }
+
+ #[cfg(windows)]
+ #[rstest]
+ #[case::windows_git_bash(r"C:\Program Files\Git\usr\bin\bash.exe", Some("bash"))]
+ #[case::windows_powershell(
+ r"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe",
+ Some("powershell")
+ )]
+ #[case::windows_pwsh(r"C:\Program Files\PowerShell\7\pwsh.exe", Some("pwsh"))]
+ #[case::windows_zsh(r"C:\msys64\usr\bin\zsh.exe", Some("zsh"))]
+ #[case::uppercase_exe(r"C:\WINDOWS\SYSTEM32\BASH.EXE", Some("BASH"))]
+ fn test_extract_filename_from_path_windows(#[case] path: &str, #[case] expected: Option<&str>) {
+ assert_eq!(extract_filename_from_path(path), expected);
+ }
+
+ #[rstest]
+ #[case::bash("bash", Some(Shell::Bash))]
+ #[case::bash_versioned("bash5", Some(Shell::Bash))]
+ #[case::zsh("zsh", Some(Shell::Zsh))]
+ #[case::zsh_versioned("zsh-5.9", Some(Shell::Zsh))]
+ #[case::fish("fish", Some(Shell::Fish))]
+ #[case::powershell("powershell", Some(Shell::PowerShell))]
+ #[case::pwsh("pwsh", Some(Shell::PowerShell))]
+ #[case::pwsh_preview("pwsh-preview", Some(Shell::PowerShell))]
+ #[case::unknown("tcsh", None)]
+ #[case::unknown_csh("csh", None)]
+ fn test_shell_from_name(#[case] name: &str, #[case] expected: Option<Shell>) {
+ assert_eq!(shell_from_name(name), expected);
+ }
+
#[test]
fn test_shell_config_line() {
insta::assert_snapshot!("config_line_bash", Shell::Bash.config_line("wt"));