use std::{
ffi::OsString,
path::{Path, PathBuf},
};
use std::sync::LazyLock;
pub(super) struct WindowsExecutable {
pub program: OsString,
pub is_shim: bool,
}
const MSYS_USR_VARIANTS: &[&str] = &["clangarm64", "ucrt64", "mingw64", "mingw32", "clang64", "clang32"];
fn git_for_windows_root() -> Option<&'static Path> {
static GIT_ROOT: LazyLock<Option<PathBuf>> = LazyLock::new(|| {
super::super::core_dir()
.filter(|core| {
core.is_absolute() && core.ends_with("libexec/git-core")
})
.and_then(|core| core.ancestors().nth(2))
.filter(|prefix| {
MSYS_USR_VARIANTS.iter().any(|name| prefix.ends_with(name))
})
.and_then(|prefix| prefix.parent())
.map(Into::into)
});
GIT_ROOT.as_deref()
}
const BIN_DIR_FRAGMENTS: &[&str] = &["bin", "usr/bin"];
pub(super) fn find_git_associated_windows_executable(stem: &str) -> Option<WindowsExecutable> {
if let Some(bin) = super::msys_bin() {
let program = bin.join(format!("{stem}.exe"));
if !program.is_file() {
return None;
}
let program = crate::from_bstr(crate::to_unix_separators(crate::into_bstr(program).ok()?))
.ok()?
.into_owned();
return Some(WindowsExecutable {
program: program.into(),
is_shim: false,
});
}
let git_root = git_for_windows_root()?;
BIN_DIR_FRAGMENTS
.iter()
.map(|bin_dir_fragment| {
let mut raw_path = OsString::from(git_root);
raw_path.push("/");
raw_path.push(bin_dir_fragment);
raw_path.push("/");
raw_path.push(stem);
raw_path.push(".exe");
WindowsExecutable {
program: raw_path,
is_shim: *bin_dir_fragment == "bin",
}
})
.find(|executable| Path::new(&executable.program).is_file())
}
pub(super) fn find_git_associated_windows_executable_with_fallback(stem: &str) -> WindowsExecutable {
find_git_associated_windows_executable(stem).unwrap_or_else(|| {
let mut raw_path = OsString::from(stem);
raw_path.push(".exe");
WindowsExecutable {
program: raw_path,
is_shim: false,
}
})
}
#[cfg(test)]
mod tests {
use std::path::Path;
const SHOULD_FIND: &[&str] = &[
"sh",
"bash",
"dash",
"diff",
"tar",
"less",
"sed",
"awk",
"perl",
"cygpath",
"gpg",
"ssh-keygen",
];
const SHOULD_NOT_FIND: &[&str] = &[
"nonexistent-command",
"cmd",
"powershell",
"explorer",
"git-credential-manager",
"git-daemon",
];
#[test]
fn find_git_associated_windows_executable() {
for stem in SHOULD_FIND {
let path = super::find_git_associated_windows_executable(stem);
assert!(path.is_some(), "should find {stem:?}");
}
}
#[test]
fn find_git_associated_windows_executable_no_extra() {
for stem in SHOULD_NOT_FIND {
let path = super::find_git_associated_windows_executable(stem);
assert!(path.is_none(), "should not find {stem:?}");
}
}
#[test]
fn find_git_associated_windows_executable_with_fallback() {
for stem in SHOULD_FIND {
let executable = super::find_git_associated_windows_executable_with_fallback(stem);
assert!(Path::new(&executable.program).is_absolute(), "should find {stem:?}");
}
}
#[test]
fn find_git_associated_windows_executable_with_fallback_falls_back() {
for stem in SHOULD_NOT_FIND {
let path = super::find_git_associated_windows_executable_with_fallback(stem)
.program
.to_str()
.expect("valid Unicode")
.to_owned();
assert_eq!(path, format!("{stem}.exe"), "should fall back for {stem:?}");
}
}
}