include git commit SHA in --version output

This commit is contained in:
Lynne Megido 2021-09-23 02:05:50 +10:00
parent a800be63f7
commit 043e0972c5
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
3 changed files with 30 additions and 3 deletions

View file

@ -7,8 +7,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## Unreleased
### Added
- AIFF (Audio Interchange File Format, a PCM audio format like WAV) detection to [`infer`]
- `--version` output now includes the (short) hash of the git commit fif was built from
### Other
- Refactoring - split fif into main.rs and lib.rs, moved file-related functionality (directory scanning, etc.) into
- Refactoring - split fif into `main.rs` and `lib.rs`, moved file-related functionality (directory scanning, etc.) into
files module, removed string module, etc.
- Changelog "refactoring":
- Based on the somewhat popular [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) guide, splitting the previous

View file

@ -1,3 +1,5 @@
use std::process::Command;
#[allow(unreachable_code, clippy::pedantic)]
fn main() -> Result<(), String> {
#[cfg(all(feature = "infer-backend", feature = "xdg-mime-backend"))]
@ -6,5 +8,21 @@ fn main() -> Result<(), String> {
"fif cannot be compiled with multiple backends set - please enable only one, or use the default.",
));
// a more robust way of doing this would be to use vergen (https://github.com/rustyhorde/vergen), but it pulls in a
// whole bunch of extra dependencies (including chrono and git2), and also blocks compilation on the current MSRV.
// this method is less clever and robust, but it works!
let git = Command::new("git").args(&["rev-parse", "--short", "HEAD"]).output();
let hash = match git {
Ok(output) => String::from_utf8_lossy(&*output.stdout).into(),
Err(_) => {
// git not being present (or failing) shouldn't block compilation
println!("cargo:warning=Failed to retrieve git commit hash");
String::from("???")
}
};
println!("cargo:rustc-env=GIT_SHA={}", hash);
println!("cargo:rustc-rerun-if-changed=.git/HEAD");
Ok(())
}

View file

@ -30,7 +30,15 @@ pub fn clap_version() -> &'static str { CLAP_VERSION.get_or_init(|| String::from
/// Sets [`CLAP_LONG_VERSION`] to be similar to [`CLAP_VERSION`], followed by the chosen backend in parentheses (e.g.
/// "v0.3.1 (XDG-Mime backend)"), then returns it as a String.
pub fn clap_long_version() -> &'static str {
CLAP_LONG_VERSION.get_or_init(|| format!("v{} ({} backend)", VERSION.unwrap_or("???"), BACKEND).into())
CLAP_LONG_VERSION.get_or_init(|| {
format!(
"v{} ({} backend, commit #{})",
VERSION.unwrap_or("???"),
BACKEND,
option_env!("GIT_SHA").unwrap_or("???")
)
.into()
})
}
/// Returns the name of the target operating system with proper casing, like "Windows" or "macOS".