use std::env;
use std::process::{Command, ExitCode};
pub fn find_real_git() -> Option<String> {
let self_exe = env::current_exe().ok()?;
let self_path = self_exe.canonicalize().ok()?;
if let Ok(output) = Command::new("which").arg("-a").arg("git").output() {
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines() {
let line = line.trim();
if line.is_empty() {
continue;
}
if let Ok(candidate) = std::fs::canonicalize(line) {
if candidate != self_path {
return Some(line.to_string());
}
} else {
if line != self_path.to_string_lossy() {
return Some(line.to_string());
}
}
}
}
}
for path in &["/usr/bin/git", "/usr/local/bin/git", "/opt/homebrew/bin/git"] {
if std::path::Path::new(path).exists() {
if let Ok(candidate) = std::fs::canonicalize(path) {
if candidate != self_path {
return Some(path.to_string());
}
}
}
}
None
}
pub fn passthrough(args: &[String]) -> ExitCode {
let real_git = match find_real_git() {
Some(g) => g,
None => {
eprintln!("error: Could not find real git binary");
return ExitCode::from(1);
}
};
let status = Command::new(&real_git).args(args).status();
match status {
Ok(s) => ExitCode::from(s.code().unwrap_or(1) as u8),
Err(e) => {
eprintln!("error: Failed to run git: {}", e);
ExitCode::from(1)
}
}
}