Skip to main content

wip_git/
git.rs

1use std::process::Command;
2
3/// Captured stdout/stderr from a git subprocess invocation.
4pub struct GitOutput {
5    pub stdout: String,
6    pub stderr: String,
7}
8
9pub fn git(args: &[&str]) -> Result<GitOutput, String> {
10    let output = Command::new("git")
11        .args(args)
12        .output()
13        .map_err(|e| format!("failed to run git: {e}"))?;
14
15    let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
16    let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
17
18    if output.status.success() {
19        Ok(GitOutput { stdout, stderr })
20    } else {
21        Err(format!("git {} failed: {}", args.join(" "), stderr))
22    }
23}
24
25/// Run git command, return stdout trimmed
26pub fn git_stdout(args: &[&str]) -> Result<String, String> {
27    Ok(git(args)?.stdout)
28}
29
30/// Run git command, allow non-zero exit (returns Ok with output either way)
31pub fn git_allow_fail(args: &[&str]) -> Result<GitOutput, String> {
32    let output = Command::new("git")
33        .args(args)
34        .output()
35        .map_err(|e| format!("failed to run git: {e}"))?;
36
37    let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
38    let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
39
40    Ok(GitOutput { stdout, stderr })
41}