You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
git-view/src/git.rs

82 lines
2.2 KiB
Rust

use mockall::automock;
use std::process::{Command, Output};
use crate::error::AppError;
pub(crate) enum Git<'a> {
IsValidRepository,
LocalBranch,
DefaultRemote,
TrackedRemote(&'a str),
UpstreamBranch(&'a str),
IsValidRemote(&'a str),
}
pub(crate) enum GitOutput {
Ok(String),
Err(String),
}
#[automock]
pub(crate) trait GitCommand {
fn execute(&self) -> Result<GitOutput, AppError>;
}
impl<'a> Git<'a> {
fn command(&self) -> Result<Output, std::io::Error> {
match *self {
Git::IsValidRepository => Command::new("git")
.arg("rev-parse")
.arg("--is-inside-work-tree")
.output(),
Git::LocalBranch => Command::new("git")
.arg("symbolic-ref")
.arg("-q")
.arg("--short")
.arg("HEAD")
.output(),
Git::DefaultRemote => Command::new("git")
.arg("config")
.arg("open.default.remote")
.output(),
Git::TrackedRemote(branch) => Command::new("git")
.arg("config")
.arg(format!("branch.{}.remote", branch))
.output(),
Git::UpstreamBranch(branch) => Command::new("git")
.arg("config")
.arg(format!("branch.{}.merge", branch))
.output(),
Git::IsValidRemote(remote) => Command::new("git")
.arg("ls-remote")
.arg("--get-url")
.arg(remote)
.output(),
}
}
fn trim(&self, bytes: &[u8]) -> Result<String, AppError> {
let mut utf_8_string = String::from(std::str::from_utf8(bytes)?.trim());
if utf_8_string.ends_with('\n') {
utf_8_string.pop();
if utf_8_string.ends_with('\r') {
utf_8_string.pop();
}
}
Ok(utf_8_string)
}
}
impl<'a> GitCommand for Git<'a> {
fn execute(&self) -> Result<GitOutput, AppError> {
let command = self.command()?;
if command.status.success() {
Ok(GitOutput::Ok(self.trim(&command.stdout)?))
} else {
Ok(GitOutput::Err(self.trim(&command.stderr)?))
}
}
}