rustimal 0.6.0

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
//! Running git. Every call goes through here, so that none of them opens a console window
//! (the release build has none of its own) or stops to ask something on a terminal.

use std::os::windows::process::CommandExt;
use std::path::Path;
use std::process::{Command, Stdio};

const CREATE_NO_WINDOW: u32 = 0x0800_0000;

/// A program that runs quietly in the background.
pub fn quiet(program: &str) -> Command {
    let mut c = Command::new(program);
    c.creation_flags(CREATE_NO_WINDOW);
    c
}

pub fn command(dir: &Path) -> Command {
    let mut c = quiet("git");
    c.arg("-C").arg(dir).args(["-c", "core.quotepath=false"]).env("GIT_TERMINAL_PROMPT", "0").stdin(Stdio::null());
    c
}

/// Run git and return what it printed, or why it failed.
pub fn output(dir: &Path, args: &[&str]) -> Result<String, String> {
    let out = command(dir).args(args).output().map_err(|e| format!("could not run git: {e}"))?;
    if out.status.success() {
        Ok(String::from_utf8_lossy(&out.stdout).into_owned())
    } else {
        Err(format!("git {}: {}", args.first().unwrap_or(&""), String::from_utf8_lossy(&out.stderr).trim()))
    }
}

/// The first line of the output, trimmed.
pub fn line(dir: &Path, args: &[&str]) -> Result<String, String> {
    output(dir, args).map(|s| s.lines().next().unwrap_or("").trim().to_string())
}

/// The commit a name refers to. The name can come from anywhere, so it is never read as an option.
pub fn commit_id(dir: &Path, name: &str) -> Option<String> {
    line(dir, &["rev-parse", "--verify", "--quiet", "--end-of-options", &format!("{name}^{{commit}}")])
        .ok()
        .filter(|id| !id.is_empty())
}

#[cfg(test)]
pub mod testing {
    use std::path::Path;

    pub fn run(dir: &Path, args: &[&str]) {
        super::output(dir, args).unwrap_or_else(|e| panic!("{e}"));
    }

    pub fn init(dir: &Path) {
        run(dir, &["init", "--quiet", "--initial-branch=main"]);
        run(dir, &["config", "user.name", "Test"]);
        run(dir, &["config", "user.email", "test@example.invalid"]);
        run(dir, &["config", "core.autocrlf", "false"]);
        run(dir, &["config", "commit.gpgsign", "false"]);
    }

    pub fn commit(dir: &Path, message: &str) {
        run(dir, &["add", "-A"]);
        run(dir, &["commit", "--quiet", "--no-verify", "-m", message]);
    }
}