git2_ext/
utils.rs

1/// Path to a shell suitable for running hooks.
2pub fn git_sh() -> Option<std::path::PathBuf> {
3    let exe_name = if cfg!(target_os = "windows") {
4        "bash.exe"
5    } else {
6        "sh"
7    };
8
9    if cfg!(target_os = "windows") {
10        // Prefer git-bash since that is how git will normally be running the hooks
11        if let Some(path) = find_git_bash() {
12            return Some(path);
13        }
14    }
15
16    which::which(exe_name).ok()
17}
18
19fn find_git_bash() -> Option<std::path::PathBuf> {
20    // Git is typically installed at C:\Program Files\Git\cmd\git.exe with the cmd\ directory
21    // in the path, however git-bash is usually not in PATH and is in bin\ directory:
22    let git_path = which::which("git.exe").ok()?;
23    let git_dir = git_path.parent()?.parent()?;
24    let git_bash = git_dir.join("bin").join("bash.exe");
25    git_bash.is_file().then_some(git_bash)
26}