synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / crates / synapse-bench / src / gitcmd.rs
| 1 | //! Thin, failure-reporting wrapper around the `git` command-line tool. |
| 2 | |
| 3 | use std::ffi::OsStr; |
| 4 | use std::io::Write; |
| 5 | use std::path::Path; |
| 6 | use std::process::{Command, Stdio}; |
| 7 | |
| 8 | /// Captured outcome of one `git` invocation. |
| 9 | #[derive(Debug)] |
| 10 | pub struct GitRun { |
| 11 | /// Exit code (`None` if terminated by a signal). |
| 12 | pub code: Option<i32>, |
| 13 | pub stdout: Vec<u8>, |
| 14 | pub stderr: Vec<u8>, |
| 15 | } |
| 16 | |
| 17 | impl GitRun { |
| 18 | pub fn stdout_trimmed(&self) -> String { |
| 19 | String::from_utf8_lossy(&self.stdout).trim().to_string() |
| 20 | } |
| 21 | |
| 22 | pub fn stderr_tail(&self) -> String { |
| 23 | let text = String::from_utf8_lossy(&self.stderr); |
| 24 | let trimmed = text.trim(); |
| 25 | let start = trimmed.len().saturating_sub(400); |
| 26 | let mut idx = start; |
| 27 | while !trimmed.is_char_boundary(idx) { |
| 28 | idx += 1; |
| 29 | } |
| 30 | trimmed[idx..].to_string() |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /// Run `git -C <dir> [-c k=v]... <args>` with optional extra environment and stdin bytes. |
| 35 | /// Stdin is written from a helper thread so large outputs cannot deadlock the pipe. |
| 36 | pub fn run_git( |
| 37 | dir: &Path, |
| 38 | config: &[(&str, &str)], |
| 39 | env: &[(&str, &OsStr)], |
| 40 | args: &[&str], |
| 41 | stdin: Option<Vec<u8>>, |
| 42 | ) -> Result<GitRun, String> { |
| 43 | let mut cmd = Command::new("git"); |
| 44 | cmd.arg("-C").arg(dir); |
| 45 | for (k, v) in config { |
| 46 | cmd.arg("-c").arg(format!("{k}={v}")); |
| 47 | } |
| 48 | cmd.args(args); |
| 49 | for (k, v) in env { |
| 50 | cmd.env(k, v); |
| 51 | } |
| 52 | cmd.stdin(if stdin.is_some() { |
| 53 | Stdio::piped() |
| 54 | } else { |
| 55 | Stdio::null() |
| 56 | }) |
| 57 | .stdout(Stdio::piped()) |
| 58 | .stderr(Stdio::piped()); |
| 59 | |
| 60 | let mut child = cmd |
| 61 | .spawn() |
| 62 | .map_err(|e| format!("spawn git {}: {e}", args.join(" ")))?; |
| 63 | let writer = match stdin { |
| 64 | Some(data) => { |
| 65 | let mut pipe = child |
| 66 | .stdin |
| 67 | .take() |
| 68 | .ok_or_else(|| format!("git {}: stdin pipe unavailable", args.join(" ")))?; |
| 69 | Some(std::thread::spawn(move || pipe.write_all(&data))) |
| 70 | } |
| 71 | None => None, |
| 72 | }; |
| 73 | let out = child |
| 74 | .wait_with_output() |
| 75 | .map_err(|e| format!("wait git {}: {e}", args.join(" ")))?; |
| 76 | let write_result = match writer { |
| 77 | Some(handle) => handle |
| 78 | .join() |
| 79 | .map_err(|_| format!("git {}: stdin writer thread panicked", args.join(" ")))?, |
| 80 | None => Ok(()), |
| 81 | }; |
| 82 | let run = GitRun { |
| 83 | code: out.status.code(), |
| 84 | stdout: out.stdout, |
| 85 | stderr: out.stderr, |
| 86 | }; |
| 87 | if let Err(e) = write_result { |
| 88 | if run.code != Some(0) { |
| 89 | return Err(format!( |
| 90 | "git {} failed (exit {:?}, stdin write error {e}): {}", |
| 91 | args.join(" "), |
| 92 | run.code, |
| 93 | run.stderr_tail() |
| 94 | )); |
| 95 | } |
| 96 | } |
| 97 | Ok(run) |
| 98 | } |
| 99 | |
| 100 | /// Run git and require exit status 0, returning stdout. |
| 101 | pub fn git_ok( |
| 102 | dir: &Path, |
| 103 | config: &[(&str, &str)], |
| 104 | env: &[(&str, &OsStr)], |
| 105 | args: &[&str], |
| 106 | stdin: Option<Vec<u8>>, |
| 107 | ) -> Result<Vec<u8>, String> { |
| 108 | let run = run_git(dir, config, env, args, stdin)?; |
| 109 | if run.code != Some(0) { |
| 110 | return Err(format!( |
| 111 | "git {} failed (exit {:?}): {}", |
| 112 | args.join(" "), |
| 113 | run.code, |
| 114 | run.stderr_tail() |
| 115 | )); |
| 116 | } |
| 117 | Ok(run.stdout) |
| 118 | } |
| 119 | |
| 120 | /// Run git without extra config, environment, or stdin and return trimmed stdout. |
| 121 | pub fn git_line(dir: &Path, args: &[&str]) -> Result<String, String> { |
| 122 | let out = git_ok(dir, &[], &[], args, None)?; |
| 123 | Ok(String::from_utf8_lossy(&out).trim().to_string()) |
| 124 | } |