From 00089639fe21fd5f8c62f2b91f445c302a0edf08 Mon Sep 17 00:00:00 2001 From: dbr Date: Fri, 21 May 2021 16:31:25 +1000 Subject: [PATCH] Include git HEAD rev in version output Closes #241 --- cli/Cargo.toml | 2 ++ cli/build.rs | 34 +++++++++++++++++++++++++++++++ cli/src/invocation/cmd/version.rs | 7 ++++++- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 cli/build.rs diff --git a/cli/Cargo.toml b/cli/Cargo.toml index d66cc63b9..23f68e12e 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -4,6 +4,8 @@ edition = "2018" name = "taskchampion-cli" version = "0.3.0" +build = "build.rs" + [dependencies] dirs-next = "^2.0.0" env_logger = "^0.8.3" diff --git a/cli/build.rs b/cli/build.rs new file mode 100644 index 000000000..3c87576f9 --- /dev/null +++ b/cli/build.rs @@ -0,0 +1,34 @@ +use std::process::Command; + +fn main() { + // Query HEAD revision and expose as $TC_GIT_REV during build + // + // Adapted from https://stackoverflow.com/questions/43753491 + let cmd = Command::new("git") + .args(&["rev-parse", "--short", "HEAD"]) + .spawn() + // Wait for process to exit + .and_then(|cmd| cmd.wait_with_output()) + // Handle error if failed to launch git + .map_err(|_e| println!("cargo:warning=Failed to run 'git' to determine HEAD rev")) + // Remap to Some/None for simpler error handling + .ok() + // Handle command failing + .and_then(|o| { + if o.status.success() { + Some(o) + } else { + println!( + "cargo:warning='git' exited with non-zero exit code while determining HEAD rev" + ); + None + } + }) + // Get output as UTF-8 string + .map(|out| String::from_utf8(out.stdout).expect("Invalid output in stdout")); + + // Only output git rev if successful + if let Some(h) = cmd { + println!("cargo:rustc-env=TC_GIT_REV={}", h); + } +} diff --git a/cli/src/invocation/cmd/version.rs b/cli/src/invocation/cmd/version.rs index baef94161..aebac3f5b 100644 --- a/cli/src/invocation/cmd/version.rs +++ b/cli/src/invocation/cmd/version.rs @@ -3,8 +3,13 @@ use termcolor::{ColorSpec, WriteColor}; pub(crate) fn execute(w: &mut W) -> anyhow::Result<()> { write!(w, "TaskChampion ")?; w.set_color(ColorSpec::new().set_bold(true))?; - writeln!(w, "{}", env!("CARGO_PKG_VERSION"))?; + write!(w, "{}", env!("CARGO_PKG_VERSION"))?; w.reset()?; + + if let Some(h) = option_env!("TC_GIT_REV") { + write!(w, " (git rev: {})", h)?; + } + writeln!(w)?; Ok(()) }