use std::path::Path;
use std::process::Command;
use crate::error::Result;
pub fn run_event(event: &str, cwd: &Path) -> Result<()> {
let config_root =
crate::git::get_main_repo_root(Some(cwd)).unwrap_or_else(|_| cwd.to_path_buf());
let cfg = crate::config::load_effective_config(&config_root)?;
let cmd = match event {
"post_new" => cfg.hooks.post_new,
"pre_rm" => cfg.hooks.pre_rm,
_ => return Ok(()),
};
let Some(cmd) = cmd else {
return Ok(());
};
#[cfg(not(unix))]
return Err(crate::error::CwError::Other(
"hooks require 'sh' shell which is not available on Windows".into(),
));
#[cfg(unix)]
{
let status = Command::new("sh")
.arg("-c")
.arg(&cmd)
.current_dir(cwd)
.status()?;
if !status.success() {
return Err(crate::error::CwError::Other(format!(
"hook '{}' (`{}`) exited with {}",
event,
cmd.chars().take(60).collect::<String>(),
status.code().unwrap_or(-1)
)));
}
Ok(())
}
}