Skip to content

n1xyz/gitver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gitver

Embed git commit info in your binary.

usage

Cargo.toml:

[dependencies]
gitver = { git = "..." }

[build-dependencies]
gitver = { git = "...", features = ["build"] }

build.rs:

pub fn main() {
    gitver::build::from_env_override()
        .unwrap_or_else(|_| {
            gitver::build::from_git_repo(std::env!("CARGO_MANIFEST_DIR"))
                .inspect_err(|e| println!("cargo::error={e}"))
                .unwrap()
        })
        .print_cargo_directives();
}

src/lib.rs:

    println!("sha: {}", gitver::rev!());
    println!("shortsha: {}", gitver::shortrev!());
    println!("time: {}", gitver::time!());

In a workspace, it's recommended to have a dedicated version-proxy crate that re-exports these constants to limit rebuilds:

pub const COMMIT_REV: &str = buildref::rev!();
pub const COMMIT_SHORTREV: &str = buildref::shortrev!();
pub const COMMIT_TIME: &str = buildref::time!();

The values can be overridden through GITVER_{REV,SHORTREV,TIME}_OVERRIDE We use this when building a rust package from a nix derivation, since git repository is removed prior to building. We can recover the git info through the flake's attributes:

buildRustPackage {
  # ...

  GITVER_REV_OVERRIDE = self.rev or self.dirtyRev;
  GITVER_SHORTREV_OVERRIDE = self.shortRev or self.dirtyShortRev;

  preBuild = ''
    # use `date` to format the time nicely, since nix lacks such functionality.
    export GITVER_TIME_OVERRIDE=$(date -u -d @${builtins.toString self.lastModified} +%Y-%m-%dT%H:%M:%SZ)

  '';

  # ...
};

alternatives

  • git-version: Includes the entire git index using include_bytes!, which causes frequent false positive rebuilds.

  • vergen: Excessively complex for most use cases, and weird behaviour for idempotency.

  • git2version: Closest crate to this one. We made the api a bit more opinionated and added env overrides.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages