Skip to content

Commit 859aa5a

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

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

git2-hooks/src/hookspath.rs

+19-21
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,16 +164,22 @@ 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> {
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) {
181183
Command::new("where.exe")
182184
.arg("git")
183185
.output()
@@ -190,16 +192,12 @@ fn find_bash_executable() -> Option<PathBuf> {
190192
.as_deref()
191193
.and_then(Path::parent)
192194
.and_then(Path::parent)
193-
.map(|p| p.join("usr/bin/bash.exe"))
195+
.map(|p| p.join("usr/bin/sh.exe"))
194196
.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+
}
203201
}
204202

205203
trait CommandExt {

0 commit comments

Comments
 (0)