Run a git command and return stdout as a string
(repo: &Path, args: &[&str])
| 22 | |
| 23 | /// Run a git command and return stdout as a string |
| 24 | pub fn run(repo: &Path, args: &[&str]) -> Result<String, GitError> { |
| 25 | let repo_str = repo |
| 26 | .to_str() |
| 27 | .ok_or_else(|| GitError::InvalidPath(repo.display().to_string()))?; |
| 28 | |
| 29 | let output = Command::new("git") |
| 30 | .args(["-C", repo_str]) |
| 31 | .args(args) |
| 32 | .output() |
| 33 | .map_err(|e| { |
| 34 | if e.kind() == std::io::ErrorKind::NotFound { |
| 35 | GitError::GitNotFound |
| 36 | } else { |
| 37 | GitError::CommandFailed(e.to_string()) |
| 38 | } |
| 39 | })?; |
| 40 | |
| 41 | if !output.status.success() { |
| 42 | let stderr = String::from_utf8_lossy(&output.stderr); |
| 43 | if stderr.contains("not a git repository") { |
| 44 | return Err(GitError::NotARepo(repo.display().to_string())); |
| 45 | } |
| 46 | return Err(GitError::CommandFailed(stderr.into_owned())); |
| 47 | } |
| 48 | |
| 49 | String::from_utf8(output.stdout).map_err(|_| GitError::InvalidUtf8) |
| 50 | } |
no test coverage detected