use std::path::Path;
use anyhow::Result;
pub fn init(project_dir: &Path, branch: Option<&str>, force: bool) -> Result<()> {
match (gix::discover(project_dir).ok(), force) {
(Some(_), false) => Ok(()),
(Some(_), true) if gix::open(project_dir).is_ok() => Ok(()),
_ => just_init(project_dir, branch),
}
}
fn just_init(project_dir: &Path, branch: Option<&str>) -> Result<()> {
let repo = gix::init(project_dir)?;
if let Some(branch) = branch {
std::fs::write(
repo.git_dir().join("HEAD"),
format!("ref: refs/heads/{branch}\n"),
)?;
}
Ok(())
}