1use anyhow::{Context, Result};
2use std::process::{Command, Stdio};
3
4pub fn is_inside_work_tree() -> Result<bool> {
10 let status = Command::new("git")
11 .args(["rev-parse", "--is-inside-work-tree"])
12 .stdout(Stdio::null())
13 .stderr(Stdio::null())
14 .status();
15
16 match status {
17 Ok(exit) => Ok(exit.success()),
18 Err(_) => Ok(false),
19 }
20}
21
22pub fn is_binary_diff(diff: &str) -> bool {
24 diff.contains("Binary files")
25 || diff.contains("GIT binary patch")
26 || diff.contains("[Binary file changed]")
27}
28
29pub fn run_git_command(args: &[&str]) -> Result<String> {
39 let output = Command::new("git")
40 .args(args)
41 .output()
42 .context("Failed to execute git command")?;
43
44 if !output.status.success() {
45 return Err(anyhow::anyhow!(
46 "Git command failed: {}",
47 String::from_utf8_lossy(&output.stderr)
48 ));
49 }
50
51 let stdout =
52 String::from_utf8(output.stdout).context("Invalid UTF-8 output from git command")?;
53
54 Ok(stdout.trim().to_string())
55}