[][src]Crate git_sha1

Provide the current Git commit SHA1 during build.

When building crates from a Git repository it is often desirable to extract the current version number in form of the Git SHA1 to be displayed. This crate extracts the current Git SHA1 during build time and makes it accessible as an environment variable.

If the crate is currently built without access to the Git SHA1 (i. e. it was extracted from a tar-archive, or Git is not installed), instead of failing, it falls back on a default value. This value defaults to "", but can be changed with the use_default() method.


In build.rs:

use git_sha1::GitSHA1;

fn main() {

In main.rs:

use git_sha1::GitSHA1;

// either as static &str:
static SHA1: &str = env!("GIT_SHA1");

// or during runtime:
fn main() {
    let sha1 = GitSHA1::from_env("GIT_SHA1");

    let long = sha1.long();
    assert_eq!(SHA1, long);

    let short = sha1.short(10);
    // `short` may be shorter if SHA1 does not exist
    assert_eq!(short.len(), usize::min(10, short.len()));