1use anyhow::Result;
2use std::process::Command;
3
4use crate::opener::augmented_path;
5
6pub struct HookContext {
7 pub owner: String,
8 pub repo: String,
9 pub issue: String,
10 pub branch: String,
11 pub worktree_path: String,
12}
13
14impl HookContext {
15 pub fn render(&self, template: &str) -> String {
16 template
17 .replace("{{owner}}", &self.owner)
18 .replace("{{repo}}", &self.repo)
19 .replace("{{issue}}", &self.issue)
20 .replace("{{branch}}", &self.branch)
21 .replace("{{worktree_path}}", &self.worktree_path)
22 }
23}
24
25pub fn run_hook(script: &str, ctx: &HookContext) -> Result<()> {
29 let rendered = ctx.render(script);
30
31 let tmp_path = std::env::temp_dir().join(format!("worktree-hook-{}.sh", std::process::id()));
32 std::fs::write(&tmp_path, rendered.as_bytes())?;
33
34 #[cfg(unix)]
35 {
36 use std::os::unix::fs::PermissionsExt;
37 std::fs::set_permissions(&tmp_path, std::fs::Permissions::from_mode(0o755))?;
38 }
39
40 let result = Command::new("sh")
41 .arg(&tmp_path)
42 .env("PATH", augmented_path())
43 .status();
44 let _ = std::fs::remove_file(&tmp_path);
45
46 match result {
47 Ok(status) if !status.success() => {
48 eprintln!("Warning: hook exited with status {:?}", status.code());
49 }
50 Err(e) => {
51 eprintln!("Warning: failed to run hook: {e}");
52 }
53 _ => {}
54 }
55
56 Ok(())
57}