use std::path::Path; use gitbaby::command::cmd::{Cmd, CmdOutput}; use gitbaby::command::env::Env; use crate::share::errors::GitError; fn build_cmd( label: &str, dir: &Path, args: Vec, env: &[(String, String)], timeout_secs: u64, ) -> Cmd { let mut cmd = Cmd::new( label.to_string(), "git", args, Vec::new(), dir.to_path_buf(), Env::new(), Some(timeout_secs), ); cmd.env.set("GIT_TERMINAL_PROMPT", "0"); for (k, v) in env { cmd.env.set(k.clone(), v.clone()); } cmd } pub async fn run_git( label: impl Into, dir: &Path, args: Vec, env: &[(String, String)], timeout_secs: u64, ) -> Result { let label = label.into(); let mut cmd = build_cmd(&label, dir, args, env, timeout_secs); cmd.run() .await .map_err(|e| GitError::Internal(format!("{label} failed: {e}"))) } pub async fn run_git_soft( label: impl Into, dir: &Path, args: Vec, env: &[(String, String)], timeout_secs: u64, ) -> Result { let label = label.into(); let mut cmd = build_cmd(&label, dir, args, env, timeout_secs); cmd.run_soft() .await .map_err(|e| GitError::Internal(format!("{label} failed: {e}"))) } pub async fn run_git_capped( label: impl Into, dir: &Path, args: Vec, env: &[(String, String)], timeout_secs: u64, output_cap: usize, ) -> Result { let label = label.into(); let mut cmd = build_cmd(&label, dir, args, env, timeout_secs); cmd.output_cap = output_cap; cmd.run() .await .map_err(|e| GitError::Internal(format!("{label} failed: {e}"))) } pub async fn run_exec_cmd( label: impl Into, prog: &str, dir: &Path, args: &[String], timeout_secs: u64, output_cap: usize, ) -> Result { let label = label.into(); let mut cmd = Cmd::new( label.clone(), prog.to_string(), args.to_vec(), Vec::new(), dir.to_path_buf(), Env::new(), Some(timeout_secs), ); cmd.output_cap = output_cap; cmd.run() .await .map_err(|e| GitError::Internal(format!("{label} failed: {e}"))) } pub async fn run_git_with_stdin( label: impl Into, dir: &Path, args: Vec, env: &[(String, String)], stdin_data: &[u8], timeout_secs: u64, ) -> Result { let label = label.into(); let mut cmd = build_cmd(&label, dir, args, env, timeout_secs); cmd.spawn() .await .map_err(|e| GitError::Internal(format!("{label} spawn failed: {e}")))?; cmd.feed(stdin_data) .await .map_err(|e| GitError::Internal(format!("{label} stdin failed: {e}")))?; cmd.run() .await .map_err(|e| GitError::Internal(format!("{label} failed: {e}"))) }