use std::path::Path;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum HookOrder {
#[default]
Before,
After,
Replace,
}
#[derive(Debug, Clone)]
pub struct RepoHookEntry {
pub script: String,
pub order: HookOrder,
}
#[derive(Debug, Clone, Default)]
pub struct RepoHooksConfig {
pub pre_open: Option<RepoHookEntry>,
pub post_open: Option<RepoHookEntry>,
}
#[derive(Debug, Clone, Default)]
pub struct RepoConfig {
pub hooks: RepoHooksConfig,
}
impl RepoConfig {
#[must_use]
pub fn load_from(worktree_path: &Path) -> Option<Self> {
let path = worktree_path.join(".worktree.toml");
let contents = std::fs::read_to_string(&path).ok()?;
match crate::repo_hooks_parse::parse(&contents) {
Ok(cfg) => Some(cfg),
Err(e) => {
eprintln!("warning: ignoring {}: {e}", path.display());
None
}
}
}
}
#[must_use]
pub fn combined_script(global: Option<&str>, repo_entry: Option<&RepoHookEntry>) -> Option<String> {
match (global, repo_entry) {
(None, None) => None,
(Some(g), None) => Some(g.to_owned()),
(None, Some(r)) => Some(r.script.clone()),
(Some(g), Some(r)) => Some(match r.order {
HookOrder::Before => format!("{}\n{}", r.script, g),
HookOrder::After => format!("{}\n{}", g, r.script),
HookOrder::Replace => r.script.clone(),
}),
}
}
#[cfg(test)]
#[path = "repo_hooks_load_tests.rs"]
mod load_tests;
#[cfg(test)]
#[path = "repo_hooks_tests.rs"]
mod tests;