feat(nbd): add --version flag with X.Y.Z+GitSha format [c24ee8]
build.rs captures the short git SHA at compile time via
`git rev-parse --short HEAD` and emits it as GIT_SHORT_SHA. Falls back
to "unknown" when git is unavailable (e.g. Nix sandboxed builds).
main.rs adds a VERSION const (`env!("CARGO_PKG_VERSION") + "+" + SHA`)
and passes it to clap's `version =` attribute, enabling both -V and
--version flags.
Example output: `nbd 0.1.0+8f4d25b`
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
quotesdb
parent
8f4d25b141
commit
16635a908d
@ -0,0 +1,31 @@
|
||||
//! Build script for `nbd`.
|
||||
//!
|
||||
//! Captures the short git SHA at compile time and emits it as the
|
||||
//! `GIT_SHORT_SHA` environment variable, making it available via
|
||||
//! `env!("GIT_SHORT_SHA")` in the crate source.
|
||||
//!
|
||||
//! Falls back to `"unknown"` when git is unavailable (e.g. a clean Nix
|
||||
//! sandbox build where `.git/` is not present).
|
||||
|
||||
fn main() {
|
||||
// Capture the short git SHA at build time.
|
||||
let sha = std::process::Command::new("git")
|
||||
.args(["rev-parse", "--short", "HEAD"])
|
||||
.output()
|
||||
.ok()
|
||||
.and_then(|o| {
|
||||
if o.status.success() {
|
||||
Some(o.stdout)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.and_then(|b| String::from_utf8(b).ok())
|
||||
.map(|s| s.trim().to_string())
|
||||
.unwrap_or_else(|| "unknown".to_string());
|
||||
|
||||
println!("cargo:rustc-env=GIT_SHORT_SHA={sha}");
|
||||
// Re-run whenever HEAD or any ref changes (new commits, branch switches).
|
||||
println!("cargo:rerun-if-changed=.git/HEAD");
|
||||
println!("cargo:rerun-if-changed=.git/refs");
|
||||
}
|
||||
Loading…
Reference in New Issue