Skip to content

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

fix: find Git Bash when git resolves under Git\mingw64\bin (#4411)

Worktrunk Bot53eb9b9ebbbb
diff --git a/plugins/worktrunk/hooks/wt.cmd b/plugins/worktrunk/hooks/wt.cmd
index ddca2db0c..8e286fd1d 100644
--- a/plugins/worktrunk/hooks/wt.cmd
+++ b/plugins/worktrunk/hooks/wt.cmd
@@ -20,10 +20,11 @@ rem hands each hook the session env snapshot, so an inherited BASH would name
rem what every event runs.
set "BASH="
-rem git.exe installs at Git\cmd\git.exe or Git\bin\git.exe, and bash.exe at
-rem Git\bin\bash.exe or Git\usr\bin\bash.exe. The lookup uses `where`'s `$var:`
-rem prefix, which searches the directories named in that variable and nowhere
-rem else: a bare `where git.exe` searches the current directory first, as cmd's
+rem git.exe installs at Git\cmd\git.exe, Git\bin\git.exe, or
+rem Git\mingw64\bin\git.exe, and bash.exe at Git\bin\bash.exe or
+rem Git\usr\bin\bash.exe. The lookup uses `where`'s `$var:` prefix, which
+rem searches the directories named in that variable and nowhere else: a bare
+rem `where git.exe` searches the current directory first, as cmd's
rem own bare-name lookup does, and a hook's current directory is the user's
rem project -- so a `git.exe` committed to a repo would otherwise choose the
rem bash every event runs. `where` itself is spelled absolutely for the same
@@ -41,13 +42,17 @@ if not defined BASH goto :missing
"%BASH%" "%~dp0wt.sh" %*
exit /b %ERRORLEVEL%
-rem %1 is a Git install's cmd\ or bin\ directory, with the trailing separator
-rem `%~dpI` leaves on. Git\bin\bash.exe first, as find_git_bash does: it is the
-rem wrapper that sets up the MSYS environment for a caller outside Git Bash,
-rem which is what puts `uname` and friends within reach of wt.sh.
+rem %1 is a Git install's cmd\, bin\, or mingw64\bin\ directory, with the
+rem trailing separator `%~dpI` leaves on; the last sits a level deeper, so the
+rem install root is tried one level further up when the first two miss.
+rem Git\bin\bash.exe first, as find_git_bash does: it is the wrapper that
+rem sets up the MSYS environment for a caller outside Git Bash, which is what
+rem puts `uname` and friends within reach of wt.sh.
:derive
call :accept "%~1..\bin\bash.exe"
if not defined BASH call :accept "%~1..\usr\bin\bash.exe"
+if not defined BASH call :accept "%~1..\..\bin\bash.exe"
+if not defined BASH call :accept "%~1..\..\usr\bin\bash.exe"
goto :eof
:accept
diff --git a/src/shell_exec.rs b/src/shell_exec.rs
index 475816e48..c2c72f211 100644
--- a/src/shell_exec.rs
+++ b/src/shell_exec.rs
@@ -525,19 +525,10 @@ fn detect_windows_shell() -> Result<ShellConfig, String> {
#[cfg(windows)]
fn find_git_bash() -> Option<PathBuf> {
// Primary: find git in PATH and derive bash location
- if let Ok(git_path) = which::which("git") {
- // git.exe is typically at Git/cmd/git.exe or Git/bin/git.exe
- // bash.exe is at Git/bin/bash.exe or Git/usr/bin/bash.exe
- if let Some(git_dir) = git_path.parent().and_then(|p| p.parent()) {
- let bash_path = git_dir.join("bin").join("bash.exe");
- if bash_path.exists() {
- return Some(bash_path);
- }
- let bash_path = git_dir.join("usr").join("bin").join("bash.exe");
- if bash_path.exists() {
- return Some(bash_path);
- }
- }
+ if let Ok(git_path) = which::which("git")
+ && let Some(bash_path) = git_bash_beside_git(&git_path)
+ {
+ return Some(bash_path);
}
// Fallback: standard Git for Windows paths (needed on some CI environments
@@ -562,6 +553,22 @@ fn find_git_bash() -> Option<PathBuf> {
None
}
+/// Derive Git Bash's location from a Git for Windows `git.exe` path.
+///
+/// `git.exe` sits one level below the install root (`Git/cmd`, `Git/bin`) or
+/// below `usr` (`Git/usr/bin`), or two levels below the root in
+/// `Git/mingw64/bin` — the copy MSYS puts first on PATH inside a Git Bash
+/// session. bash.exe is at `Git/bin/bash.exe` or `Git/usr/bin/bash.exe`.
+#[cfg(any(windows, test))]
+fn git_bash_beside_git(git_path: &Path) -> Option<PathBuf> {
+ git_path.ancestors().skip(2).take(2).find_map(|root| {
+ [root.join("bin"), root.join("usr").join("bin")]
+ .into_iter()
+ .map(|dir| dir.join("bash.exe"))
+ .find(|bash_path| bash_path.exists())
+ })
+}
+
/// Environment variable naming the directive file for `cd` path changes.
///
/// Shell wrappers set this to a temp file; wt writes a raw absolute path to
@@ -2941,6 +2948,37 @@ print(*counts)
);
}
+ #[test]
+ fn test_git_bash_beside_git_install_layouts() {
+ let dir = tempfile::tempdir().unwrap();
+ let root = dir.path().join("Git");
+ for sub in ["bin", "usr/bin", "cmd", "mingw64/bin"] {
+ std::fs::create_dir_all(root.join(sub)).unwrap();
+ }
+ let bin_bash = root.join("bin").join("bash.exe");
+ std::fs::write(&bin_bash, "").unwrap();
+ std::fs::write(root.join("usr").join("bin").join("bash.exe"), "").unwrap();
+
+ for git in ["cmd/git.exe", "bin/git.exe", "mingw64/bin/git.exe"] {
+ assert_eq!(
+ git_bash_beside_git(&root.join(git)),
+ Some(bin_bash.clone()),
+ "{git}"
+ );
+ }
+ assert_eq!(
+ git_bash_beside_git(&root.join("usr/bin/git.exe")),
+ Some(root.join("usr").join("bin").join("bash.exe"))
+ );
+
+ // A minimal install with only usr/bin/bash.exe, reached from mingw64/bin
+ std::fs::remove_file(&bin_bash).unwrap();
+ assert_eq!(
+ git_bash_beside_git(&root.join("mingw64/bin/git.exe")),
+ Some(root.join("usr").join("bin").join("bash.exe"))
+ );
+ }
+
#[test]
#[cfg(windows)]
fn test_windows_uses_git_bash() {