Skip to content

Commit

Permalink
Add the git hash and commit/date to --version (#896)
Browse files Browse the repository at this point in the history
Inspired by similar functionality on `wit-bindgen`
  • Loading branch information
alexcrichton authored Jan 27, 2023
1 parent 21478d4 commit e60c90e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
34 changes: 34 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::{path::Path, process::Command};

fn main() {
println!("cargo:rerun-if-changed=build.rs");

commit_info();
}

fn commit_info() {
if !Path::new(".git").exists() {
return;
}
let output = match Command::new("git")
.arg("log")
.arg("-1")
.arg("--date=short")
.arg("--format=%H %h %cd")
.arg("--abbrev=9")
.output()
{
Ok(output) if output.status.success() => output,
_ => return,
};
let stdout = String::from_utf8(output.stdout).unwrap();
let mut parts = stdout.split_whitespace();
let mut next = || parts.next().unwrap();
println!("cargo:rustc-env=CARGO_GIT_HASH={}", next());
println!(
"cargo:rustc-env=CARGO_VERSION_INFO={} ({} {})",
env!("CARGO_PKG_VERSION"),
next(),
next()
);
}
7 changes: 6 additions & 1 deletion src/bin/wasm-tools/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ macro_rules! subcommands {
)*

#[derive(Parser)]
#[clap(version)]
#[clap(version = version())]
#[allow(non_camel_case_types)]
enum WasmTools {
$(
Expand Down Expand Up @@ -72,6 +72,11 @@ fn main() -> ExitCode {
ExitCode::FAILURE
}

/// If CARGO_VERSION_INFO is set, use it, otherwise use CARGO_PKG_VERSION.
fn version() -> &'static str {
option_env!("CARGO_VERSION_INFO").unwrap_or(env!("CARGO_PKG_VERSION"))
}

#[test]
fn verify_cli() {
use clap::CommandFactory;
Expand Down

0 comments on commit e60c90e

Please sign in to comment.