use axoprocess::Cmd;
use camino::{Utf8Path, Utf8PathBuf};
use crate::errors::Result;
#[derive(Clone, Debug)]
pub struct LocalRepo {
pub path: Utf8PathBuf,
pub head: Option<String>,
}
impl LocalRepo {
pub fn new(git: &str, working_dir: &Utf8Path) -> Result<Self> {
let path = get_root(git, working_dir)?;
let head = get_head_commit(git, working_dir).ok();
Ok(Self { path, head })
}
}
fn get_root(git: &str, working_dir: &Utf8Path) -> Result<Utf8PathBuf> {
let mut cmd = Cmd::new(git, "detect a git repo");
cmd.arg("rev-parse")
.arg("--show-toplevel")
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.check(false)
.current_dir(working_dir);
let result = cmd.output()?;
let raw = Utf8PathBuf::from(String::from_utf8(result.stdout)?.trim_end());
let root = Utf8PathBuf::from_iter(raw.components());
Ok(root)
}
fn get_head_commit(git: &str, working_dir: &Utf8Path) -> Result<String> {
let mut cmd = Cmd::new(git, "check for HEAD commit");
cmd.arg("rev-parse")
.arg("HEAD")
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.check(false)
.current_dir(working_dir);
let result = cmd.output()?;
let commit = String::from_utf8(result.stdout)?;
Ok(commit.trim_end().to_owned())
}