#git #sha1

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

#340 in Build Utils

Download history 2/week @ 2023-05-28 1/week @ 2023-06-11 6/week @ 2023-06-18 4/week @ 2023-06-25 2/week @ 2023-07-02 1/week @ 2023-07-09 2/week @ 2023-07-23 2/week @ 2023-07-30 4/week @ 2023-08-06 83/week @ 2023-08-13 95/week @ 2023-08-20 59/week @ 2023-08-27 134/week @ 2023-09-03

371 downloads per month


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.


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

No runtime deps