git_iris/git/
utils.rs

1use anyhow::{Context, Result};
2use std::process::{Command, Stdio};
3
4/// Checks if the current directory is inside a Git work tree.
5///
6/// # Returns
7///
8/// A Result containing a boolean indicating if inside a work tree or an error.
9pub 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
22/// Determines if the given diff represents a binary file.
23pub 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
29/// Executes a git command and returns the output as a string
30///
31/// # Arguments
32///
33/// * `args` - The arguments to pass to git
34///
35/// # Returns
36///
37/// A Result containing the output as a String or an error.
38pub 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}