42 lines
1.5 KiB
Rust
42 lines
1.5 KiB
Rust
use std::process::Command;
|
|
|
|
fn main() {
|
|
// Emit git commit hash (short)
|
|
let commit_hash = Command::new("git")
|
|
.args(["rev-parse", "--short", "HEAD"])
|
|
.output()
|
|
.ok()
|
|
.and_then(|o| String::from_utf8(o.stdout).ok())
|
|
.map(|s| s.trim().to_string())
|
|
.unwrap_or_else(|| "unknown".to_string());
|
|
|
|
println!("cargo:rustc-env=GIT_COMMIT_HASH={commit_hash}");
|
|
|
|
// Emit git tag describing the current commit (e.g. "v1.0.0" or "v1.0.0-3-gabcdef1")
|
|
// `git describe --tags --exact-match` gives the exact tag; fall back to `git describe --tags`.
|
|
let git_tag = Command::new("git")
|
|
.args(["describe", "--tags", "--exact-match"])
|
|
.output()
|
|
.ok()
|
|
.filter(|o| o.status.success())
|
|
.and_then(|o| String::from_utf8(o.stdout).ok())
|
|
.map(|s| s.trim().to_string())
|
|
.or_else(|| {
|
|
Command::new("git")
|
|
.args(["describe", "--tags"])
|
|
.output()
|
|
.ok()
|
|
.filter(|o| o.status.success())
|
|
.and_then(|o| String::from_utf8(o.stdout).ok())
|
|
.map(|s| s.trim().to_string())
|
|
});
|
|
|
|
let tag_value = git_tag.unwrap_or_else(|| "untagged".to_string());
|
|
println!("cargo:rustc-env=GIT_TAG={tag_value}");
|
|
|
|
// Re-run if HEAD or tag refs change
|
|
println!("cargo:rerun-if-changed=.git/HEAD");
|
|
println!("cargo:rerun-if-changed=.git/refs/tags");
|
|
println!("cargo:rerun-if-changed=.git/packed-refs");
|
|
}
|