use std::path::PathBuf;
use camino::Utf8Path;
pub const MARKER: &str = "# release-kit branch reminder";
pub const HOOK_BODY: &str = "#!/bin/sh
# release-kit branch reminder
# Installed by rk setup step branch-reminder; rerunning that step rewrites it.
# After a merge arrives, report the branches and worktrees the forge already
# merged and retired. Prints nothing when there are none, never blocks a pull.
if rk branches prune --help >/dev/null 2>&1; then
rk branches prune --quiet || :
fi
if rk worktree prune --help >/dev/null 2>&1; then
rk worktree prune --quiet || :
fi
exit 0
";
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HookState {
Installed,
Drifted,
Foreign,
Absent,
Unreadable(String),
}
pub fn hook_path(target: &Utf8Path) -> Result<PathBuf, String> {
let answered = std::process::Command::new("git")
.arg("-C")
.arg(target.as_std_path())
.args(["rev-parse", "--git-path", "hooks"])
.output()
.map_err(|source| format!("git did not run: {source}"))?;
if !answered.status.success() {
return Err(format!("{target} is not a git repository"));
}
let hooks = String::from_utf8_lossy(&answered.stdout).trim().to_owned();
if hooks.is_empty() {
return Err("git named no hooks directory".to_owned());
}
let hooks = PathBuf::from(hooks);
let hooks = if hooks.is_absolute() {
hooks
} else {
target.as_std_path().join(hooks)
};
Ok(hooks.join("post-merge"))
}
#[must_use]
pub fn observe_hook(target: &Utf8Path) -> HookState {
let path = match hook_path(target) {
Ok(path) => path,
Err(detail) => return HookState::Unreadable(detail),
};
match std::fs::symlink_metadata(&path) {
Err(source) if source.kind() == std::io::ErrorKind::NotFound => return HookState::Absent,
Err(source) => return HookState::Unreadable(format!("{}: {source}", path.display())),
Ok(meta) if !meta.is_file() => return HookState::Foreign,
Ok(_) => {}
}
let bytes = match std::fs::read(&path) {
Ok(bytes) => bytes,
Err(source) => return HookState::Unreadable(format!("{}: {source}", path.display())),
};
if !String::from_utf8_lossy(&bytes).contains(MARKER) {
return HookState::Foreign;
}
let executable = {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
std::fs::metadata(&path).is_ok_and(|meta| meta.permissions().mode() & 0o111 != 0)
}
#[cfg(not(unix))]
{
true
}
};
if bytes == HOOK_BODY.as_bytes() && executable {
HookState::Installed
} else {
HookState::Drifted
}
}
#[cfg(test)]
mod tests {
#[test]
fn the_hook_body_carries_the_marker_and_never_fails() {
assert!(super::HOOK_BODY.starts_with("#!/bin/sh\n"));
assert!(super::HOOK_BODY.contains(super::MARKER));
for verb in ["branches", "worktree"] {
assert!(
super::HOOK_BODY
.contains(&format!("if rk {verb} prune --help >/dev/null 2>&1; then")),
"the {verb} call is guarded by its own capability probe"
);
assert!(
super::HOOK_BODY.contains(&format!("rk {verb} prune --quiet || :")),
"the {verb} prune runs quiet and never fails the pull"
);
}
assert!(
!super::HOOK_BODY.contains("command -v"),
"a presence check answers the wrong question; the probe is per verb"
);
assert!(super::HOOK_BODY.ends_with("exit 0\n"));
}
}