git_override/
upstream.rs

1use crate::scan_path;
2use std::env;
3use std::path::PathBuf;
4
5/// Find the "upstream" `git` path after the `git-override` binary named `git`
6///
7/// `git-override` delegates to this upstream `git`.
8pub fn find_upstream_git() -> anyhow::Result<PathBuf> {
9    let path = std::env::var("PATH")?;
10    let thisgit = env::current_exe()?;
11
12    let mut gits = scan_path("git")?.into_iter();
13
14    let mut upstream = gits
15        .next()
16        .ok_or_else(|| errormsg!("No `git` found at all on PATH: {:?}", path))?;
17
18    if upstream == thisgit {
19        // Skip ourselves:
20        upstream = gits
21            .next()
22            .ok_or_else(|| errormsg!("No upstream `git` found on PATH: {}", path))?;
23    }
24
25    Ok(upstream)
26}