squash! fixup! Show commit hash in version when built from source

If the release is tagged, we don't want to show the commit hash when
running `zoxide -V`.
This commit is contained in:
Cole Helbling 2020-03-31 10:48:09 -07:00
parent 91263789ef
commit f4d237d07a
No known key found for this signature in database
GPG Key ID: B37E0F2371016A4C
1 changed files with 30 additions and 9 deletions

View File

@ -1,14 +1,35 @@
use std::process::Command;
fn main() { fn main() {
let mut hash = std::process::Command::new("git") let status_code = Command::new("git")
.args(&["rev-parse", "--short", "HEAD"]) .args(&["describe", "--tags", "--exact-match", "--dirty"])
.output() .status()
.ok() .ok()
.and_then(|proc| String::from_utf8(proc.stdout).ok()) .and_then(|status| status.code());
.unwrap_or_default();
if !hash.is_empty() { let is_tagged = match status_code {
hash = format!("-{}", hash); Some(code) => code == 0,
} None => false,
};
println!("cargo:rustc-env=GIT_HASH={}", hash); // If this is a tagged commit (a release), we don't want to include the
// commit hash in the version output.
let revision = if is_tagged {
String::new()
} else {
let mut hash = Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.output()
.ok()
.and_then(|proc| String::from_utf8(proc.stdout).ok())
.unwrap_or_default();
if !hash.is_empty() {
hash = format!("-{}", hash);
}
hash
};
println!("cargo:rustc-env=GIT_HASH={}", revision);
} }