netsky-sh 0.1.7

Shell utilities for netsky: tmux, git, process wrapping
Documentation
//! Git command abstractions (sync).

use std::path::Path;
use std::process::Command;

use crate::{Error, require};

const GIT: &str = "git";

/// Run a git command in a directory and return stdout.
pub fn cmd(dir: &Path, args: &[&str]) -> Result<String, Error> {
    require(GIT)?;
    let output = Command::new(GIT).args(args).current_dir(dir).output()?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
        return Err(Error::CommandFailed {
            cmd: format!("git {}", args.first().unwrap_or(&"")),
            detail: stderr,
        });
    }
    Ok(String::from_utf8_lossy(&output.stdout).into_owned())
}

/// Current branch in `dir`.
pub fn current_branch(dir: &Path) -> Result<String, Error> {
    Ok(cmd(dir, &["symbolic-ref", "--short", "HEAD"])?
        .trim()
        .to_string())
}

/// True iff the working tree has no staged or unstaged changes.
pub fn is_clean(dir: &Path) -> Result<bool, Error> {
    let out = cmd(dir, &["status", "--porcelain"])?;
    Ok(out.trim().is_empty())
}