@@ -3,7 +3,6 @@ use git2::Repository;
3
3
use crate :: { error:: Result , HookResult , HooksError } ;
4
4
5
5
use std:: {
6
- env,
7
6
path:: { Path , PathBuf } ,
8
7
process:: Command ,
9
8
str:: FromStr ,
@@ -111,16 +110,13 @@ impl HookPaths {
111
110
112
111
let arg_str = format ! ( "{:?} {}" , hook, args. join( " " ) ) ;
113
112
// Use -l to avoid "command not found" on Windows.
114
- let bash_args =
113
+ let shell_args =
115
114
vec ! [ "-l" . to_string( ) , "-c" . to_string( ) , arg_str] ;
116
115
117
116
log:: trace!( "run hook '{:?}' in '{:?}'" , hook, self . pwd) ;
118
117
119
- let git_shell = find_bash_executable ( )
120
- . or_else ( find_default_unix_shell)
121
- . unwrap_or_else ( || "bash" . into ( ) ) ;
122
- let output = Command :: new ( git_shell)
123
- . args ( bash_args)
118
+ let output = Command :: new ( shell ( ) )
119
+ . args ( shell_args)
124
120
. with_no_window ( )
125
121
. current_dir ( & self . pwd )
126
122
// This call forces Command to handle the Path environment correctly on windows,
@@ -168,16 +164,22 @@ fn is_executable(path: &Path) -> bool {
168
164
}
169
165
170
166
#[ cfg( windows) ]
171
- /// windows does not consider bash scripts to be executable so we consider everything
167
+ /// windows does not consider shell scripts to be executable so we consider everything
172
168
/// to be executable (which is not far from the truth for windows platform.)
173
169
const fn is_executable ( _: & Path ) -> bool {
174
170
true
175
171
}
176
172
177
- // Find bash.exe, and avoid finding wsl's bash.exe on Windows.
178
- // None for non-Windows.
179
- fn find_bash_executable ( ) -> Option < PathBuf > {
180
- if cfg ! ( windows) {
173
+ /// Return the shell that Git would use, the shell to execute commands from.
174
+ ///
175
+ /// On Windows, this is the full path to `sh.exe` bundled with it, and on
176
+ /// Unix it's `/bin/sh` as posix compatible shell.
177
+ /// If the bundled shell on Windows cannot be found, `sh` is returned as the name of a shell
178
+ /// as it could possibly be found in `PATH`.
179
+ /// Note that the returned path might not be a path on disk.
180
+ /// SEE: https://github.com/GitoxideLabs/gitoxide/commit/51bbb8646287f0a6dfa9ff0fd3852c25e001aaf0
181
+ pub fn shell ( ) -> PathBuf {
182
+ if cfg ! ( windows) {
181
183
Command :: new ( "where.exe" )
182
184
. arg ( "git" )
183
185
. output ( )
@@ -190,16 +192,12 @@ fn find_bash_executable() -> Option<PathBuf> {
190
192
. as_deref ( )
191
193
. and_then ( Path :: parent)
192
194
. and_then ( Path :: parent)
193
- . map ( |p| p. join ( "usr/bin/bash .exe" ) )
195
+ . map ( |p| p. join ( "usr/bin/sh .exe" ) )
194
196
. filter ( |p| p. exists ( ) )
195
- } else {
196
- None
197
- }
198
- }
199
-
200
- // Find default shell on Unix-like OS.
201
- fn find_default_unix_shell ( ) -> Option < PathBuf > {
202
- env:: var_os ( "SHELL" ) . map ( PathBuf :: from)
197
+ . unwrap_or_else ( || "sh" . into ( ) )
198
+ } else {
199
+ "/bin/sh" . into ( )
200
+ }
203
201
}
204
202
205
203
trait CommandExt {
0 commit comments