use crate::prelude::*;
use std::{path::Path, process::Command};
#[repr(transparent)]
pub struct Git();
impl Git {
const GIT: &str = "git";
pub fn installed() -> Result<bool, std::io::Error> {
Command::new(Self::GIT)
.arg("--version")
.output()
.map(|output| output.status.success())
}
pub fn is_file_modified<P>(f: P) -> Result<bool, Error>
where
P: AsRef<Path>,
{
let output = Command::new(Self::GIT)
.args(["status", "--porcelain"])
.arg(f.as_ref())
.output()?;
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
Ok(!stdout.trim().is_empty())
} else {
Err(anyhow!("Not in a git repository.\n{:?}", output))
}
}
pub fn run_allow_dirty_checks<P>(args: &Opt, f: P) -> Result<(), Error>
where
P: AsRef<Path>,
{
if !args.allow_dirty && Git::installed()? && Git::is_file_modified(f)? {
return Err(anyhow!(
"The file has uncommited changes and the `--allow-dirty` flag isn't set.",
));
}
Ok(())
}
}