Skip to content

Commit 2d90eb5

Browse files
committed
use /bin/sh for git-hooks
1 parent 9b3bc7b commit 2d90eb5

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

git2-hooks/src/hookspath.rs

+15-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use git2::Repository;
33
use crate::{error::Result, HookResult, HooksError};
44

55
use std::{
6-
env,
76
path::{Path, PathBuf},
87
process::Command,
98
str::FromStr,
@@ -111,16 +110,13 @@ impl HookPaths {
111110

112111
let arg_str = format!("{:?} {}", hook, args.join(" "));
113112
// Use -l to avoid "command not found" on Windows.
114-
let bash_args =
113+
let shell_args =
115114
vec!["-l".to_string(), "-c".to_string(), arg_str];
116115

117116
log::trace!("run hook '{:?}' in '{:?}'", hook, self.pwd);
118117

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)
124120
.with_no_window()
125121
.current_dir(&self.pwd)
126122
// This call forces Command to handle the Path environment correctly on windows,
@@ -168,15 +164,20 @@ fn is_executable(path: &Path) -> bool {
168164
}
169165

170166
#[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
172168
/// to be executable (which is not far from the truth for windows platform.)
173169
const fn is_executable(_: &Path) -> bool {
174170
true
175171
}
176172

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> {
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+
pub fn shell() -> PathBuf {
180181
if cfg!(windows) {
181182
Command::new("where.exe")
182183
.arg("git")
@@ -190,18 +191,14 @@ fn find_bash_executable() -> Option<PathBuf> {
190191
.as_deref()
191192
.and_then(Path::parent)
192193
.and_then(Path::parent)
193-
.map(|p| p.join("usr/bin/bash.exe"))
194+
.map(|p| p.join("usr/bin/sh.exe"))
194195
.filter(|p| p.exists())
196+
.unwrap_or_else(|| "sh".into())
195197
} else {
196-
None
198+
"/bin/sh".into()
197199
}
198200
}
199201

200-
// Find default shell on Unix-like OS.
201-
fn find_default_unix_shell() -> Option<PathBuf> {
202-
env::var_os("SHELL").map(PathBuf::from)
203-
}
204-
205202
trait CommandExt {
206203
/// The process is a console application that is being run without a
207204
/// console window. Therefore, the console handle for the application is

0 commit comments

Comments
 (0)