1use anyhow::{Context, Result};
2use std::path::PathBuf;
3use std::process::Command;
4
5pub fn git_dir() -> Result<PathBuf> {
7 let output = Command::new("git")
8 .args(["rev-parse", "--git-dir"])
9 .output()
10 .context("Failed to run git rev-parse --git-dir")?;
11
12 if !output.status.success() {
13 anyhow::bail!(
14 "Not inside a git repository. Run `git init` first.\n{}",
15 String::from_utf8_lossy(&output.stderr).trim()
16 );
17 }
18
19 let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
20 Ok(PathBuf::from(path))
21}
22
23pub fn hooks_dir() -> Result<PathBuf> {
25 let output = Command::new("git")
26 .args(["config", "core.hooksPath"])
27 .output()
28 .context("Failed to query git config")?;
29
30 if output.status.success() {
31 let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
32 if !path.is_empty() {
33 return Ok(PathBuf::from(path));
34 }
35 }
36
37 Ok(git_dir()?.join("hooks"))
38}