build-script-utils: allow reading the git commit hash from env var (#10845)

* build-script-utils: allow reading the git commit hash from env var

* build-script-utils: make the env var name substrate specific
This commit is contained in:
André Silva
2022-02-11 19:38:39 +00:00
committed by GitHub
parent 89dfae9530
commit 7393076df2
@@ -20,9 +20,10 @@ use std::{borrow::Cow, process::Command};
/// Generate the `cargo:` key output /// Generate the `cargo:` key output
pub fn generate_cargo_keys() { pub fn generate_cargo_keys() {
let output = Command::new("git").args(&["rev-parse", "--short", "HEAD"]).output(); let commit = if let Ok(hash) = std::env::var("SUBSTRATE_CLI_GIT_COMMIT_HASH") {
Cow::from(hash.trim().to_owned())
let commit = match output { } else {
match Command::new("git").args(&["rev-parse", "--short", "HEAD"]).output() {
Ok(o) if o.status.success() => { Ok(o) if o.status.success() => {
let sha = String::from_utf8_lossy(&o.stdout).trim().to_owned(); let sha = String::from_utf8_lossy(&o.stdout).trim().to_owned();
Cow::from(sha) Cow::from(sha)
@@ -35,6 +36,7 @@ pub fn generate_cargo_keys() {
println!("cargo:warning=Failed to execute git command: {}", err); println!("cargo:warning=Failed to execute git command: {}", err);
Cow::from("unknown") Cow::from("unknown")
}, },
}
}; };
println!("cargo:rustc-env=SUBSTRATE_CLI_IMPL_VERSION={}", get_version(&commit)) println!("cargo:rustc-env=SUBSTRATE_CLI_IMPL_VERSION={}", get_version(&commit))