use std::path::Path;
use std::process::Command;
use crate::{Error, require};
const GIT: &str = "git";
pub fn cmd(dir: &Path, args: &[&str]) -> Result<String, Error> {
require(GIT)?;
let output = Command::new(GIT).args(args).current_dir(dir).output()?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
return Err(Error::CommandFailed {
cmd: format!("git {}", args.first().unwrap_or(&"")),
detail: stderr,
});
}
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
}
pub fn current_branch(dir: &Path) -> Result<String, Error> {
Ok(cmd(dir, &["symbolic-ref", "--short", "HEAD"])?
.trim()
.to_string())
}
pub fn is_clean(dir: &Path) -> Result<bool, Error> {
let out = cmd(dir, &["status", "--porcelain"])?;
Ok(out.trim().is_empty())
}