#git-repository

build git-sha1

Expose the Git-SHA1 to the crate during build-time

2 stable releases

1.1.0 Dec 17, 2020
1.0.1 Dec 17, 2020

#752 in Build Utils

MIT/Apache

8KB
61 lines

Rust library to expose the current Git-SHA1 of the crate during the build process.

Refer to the GitLab-Pages https://merkosh.gitlab.io/git-sha1 for the Rust documentation with examples.


lib.rs:

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.

Example

In build.rs:

use git_sha1::GitSHA1;

fn main() {
    GitSHA1::read().set("GIT_SHA1");
}

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()));
}

No runtime deps