use std::path::Path;
use std::process::Output;
use crate::error::Error;
fn command_str(args: &[&str]) -> String {
args.join(" ")
}
fn map_output(args: &[&str], out: Output) -> Result<Vec<u8>, Error> {
if out.status.success() {
Ok(out.stdout)
} else {
Err(Error::Git {
command: command_str(args),
status: out.status.to_string(),
stderr: String::from_utf8_lossy(&out.stderr).trim().to_string(),
})
}
}
pub(crate) fn run_git_sync(root: &Path, args: &[&str]) -> Result<Vec<u8>, Error> {
let out = std::process::Command::new("git")
.arg("-C")
.arg(root)
.args(args)
.output()
.map_err(Error::Spawn)?;
map_output(args, out)
}
#[cfg(feature = "tokio")]
pub(crate) async fn run_git_async(root: &Path, args: &[&str]) -> Result<Vec<u8>, Error> {
let out = tokio::process::Command::new("git")
.arg("-C")
.arg(root)
.args(args)
.kill_on_drop(true)
.output()
.await
.map_err(Error::Spawn)?;
map_output(args, out)
}