add build.rs to export GIT_HASH for us

This commit is contained in:
2023-10-24 00:35:28 +02:00
parent 9988028bf0
commit 7f4d5f8bcc
2 changed files with 25 additions and 1 deletions
+24
View File
@@ -0,0 +1,24 @@
use std::{env, process::Command};
fn main() {
// Get the current directory of the project
let project_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let output = Command::new("git")
.arg("rev-parse")
.arg("--short")
.arg("HEAD")
.current_dir(&project_dir)
.output()
.expect("Failed to execute git command");
if output.status.success() {
let commit_hash = String::from_utf8(output.stdout).expect("Git output is not valid UTF-8");
// Export the commit hash as a build-time constant
println!("cargo:rustc-env=GIT_HASH={}", commit_hash);
} else {
// Handle the case where the git command failed
eprintln!("Failed to get Git commit hash");
}
}