Skip to main content

git_sprout/
delegate.rs

1// ABOUTME: Runs the real git and passes its exit status through unchanged.
2// ABOUTME: Every path the tool declines to accelerate ends here.
3
4use std::ffi::OsString;
5use std::process::{Command, ExitCode};
6
7/// Replaces this process with `git <args>` where the platform allows it, so stdout,
8/// stderr, the exit code and signal disposition are git's own.
9pub fn exec_git(args: &[OsString]) -> ExitCode {
10    let mut command = Command::new("git");
11    command.args(args);
12
13    #[cfg(unix)]
14    {
15        use std::os::unix::process::CommandExt;
16        let error = command.exec();
17        eprintln!("git-sprout: could not run git: {error}");
18        ExitCode::from(1)
19    }
20
21    #[cfg(not(unix))]
22    status_of(&mut command)
23}
24
25/// Runs `git <args>` to completion and returns its exit code.
26pub fn run_git(args: &[OsString]) -> ExitCode {
27    status_of(Command::new("git").args(args))
28}
29
30#[allow(dead_code)]
31fn status_of(command: &mut Command) -> ExitCode {
32    match command.status() {
33        Ok(status) => ExitCode::from(u8::try_from(status.code().unwrap_or(1)).unwrap_or(1)),
34        Err(error) => {
35            eprintln!("git-sprout: could not run git: {error}");
36            ExitCode::from(1)
37        }
38    }
39}