use crate::cli::UI;
use anyhow::Result;
use std::path::Path;
pub fn execute(path: &Path, bare: bool, initial_branch: Option<&str>, ui: &UI) -> Result<()> {
let repo = if bare {
git2::Repository::init_bare(path)?
} else {
git2::Repository::init(path)?
};
if let Some(branch) = initial_branch {
let refname = format!("refs/heads/{}", branch);
repo.reference_symbolic("HEAD", &refname, true, "securegit init: set initial branch")?;
}
let mut config = repo.config()?;
let _ = config.set_bool("core.fsmonitor", false);
let _ = config.set_str("core.hooksPath", ".git/hooks");
ui.success(format!(
"Initialized empty SecureGit repository in {}",
if bare {
path.display().to_string()
} else {
path.join(".git").display().to_string()
}
));
if !bare {
let hooks_dir = path.join(".git/hooks");
std::fs::create_dir_all(&hooks_dir)?;
let pre_commit = hooks_dir.join("pre-commit");
std::fs::write(
&pre_commit,
"#!/bin/sh\n# SecureGit pre-commit hook\nif command -v securegit > /dev/null 2>&1; then\n securegit pre-commit --fail-on high\n exit $?\nfi\n",
)?;
let pre_push = hooks_dir.join("pre-push");
std::fs::write(
&pre_push,
"#!/bin/sh\n# SecureGit pre-push hook\nif command -v securegit > /dev/null 2>&1; then\n securegit pre-push --fail-on high\n exit $?\nfi\n",
)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&pre_commit, std::fs::Permissions::from_mode(0o755))?;
std::fs::set_permissions(&pre_push, std::fs::Permissions::from_mode(0o755))?;
}
ui.info("SecureGit hooks installed");
}
Ok(())
}