pub fn get_git_output<I, S>(args: I, dir: &Path) -> Result<String, Error>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 toOsStr.
§Arguments
args— Git command-line arguments (e.g.,["rev-parse", "HEAD"]).dir— Path to the Git repository root or working directory.
§Returns
Ok(String)— Trimmedstdoutoutput from Git.Err(Error)— If Git fails to launch or exits with non-zero status.
§Errors
This function returns an Error if:
- The
gitbinary 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);