get_git_output

Function get_git_output 

Source
pub fn get_git_output<I, S>(args: I, dir: &Path) -> Result<String, Error>
where I: IntoIterator<Item = S>, S: AsRef<OsStr>,
Expand description

Run a Git command and return the trimmed stdout output as a String.

This is the central utility for invoking Git. It is used by the mdbook-gitinfo preprocessor to fetch commit information such as:

  • short or long commit hash
  • nearest tag
  • commit date/time

See also: verify_branch, which builds on this function to check if a branch exists locally.

§Type Parameters

  • I: An iterator of arguments (e.g., a string slice array).
  • S: Each argument, convertible to OsStr.

§Arguments

  • args — Git command-line arguments (e.g., ["rev-parse", "HEAD"]).
  • dir — Path to the Git repository root or working directory.

§Returns

  • Ok(String) — Trimmed stdout output from Git.
  • Err(Error) — If Git fails to launch or exits with non-zero status.

§Errors

This function returns an Error if:

  • The git binary is missing or fails to start.
  • The command returns a non-zero exit code.
  • The output cannot be decoded as UTF-8.

§Example

use std::path::Path;
use mdbook_gitinfo::git::get_git_output;

let hash = get_git_output(["rev-parse", "--short", "HEAD"], Path::new("."))
    .expect("failed to get commit hash");
println!("Current short commit hash: {}", hash);