1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::io::{self, ErrorKind};
use std::process::Command;

fn exclude_from_diff(path: &str) -> String {
    format!(":(exclude){}", path)
}

pub fn run_git_diff() -> Result<String, io::Error> {
    let files_to_exclude = vec![
        "Cargo.lock",
        "pakcage-lock.json",
        "pnpm-lock.json",
        "*.lock",
    ];

    let exclude_path: Vec<String> = files_to_exclude
        .iter()
        .map(|path| exclude_from_diff(path))
        .collect();

    let mut command = Command::new("git");
    command.args(&[
        "diff",
        "--staged",
        "--ignore-all-space",
        "--diff-algorithm=minimal",
    ]);

    for path in exclude_path {
        command.arg(path);
    }

    let output = command.output();

    match output {
        Ok(output) if output.status.success() => match String::from_utf8(output.stdout) {
            Ok(output_str) => Ok(output_str),
            Err(e) => Err(io::Error::new(
                ErrorKind::InvalidData,
                format!("Output is not valid UTF-8: {}", e),
            )),
        },
        Ok(output) => {
            let stderr = String::from_utf8_lossy(&output.stderr);
            Err(io::Error::new(
                ErrorKind::Other,
                format!("Error: {}", stderr),
            ))
        }
        Err(e) => Err(e),
    }
}