From 96b3c94dbcf54c5b2ad139fc24544483ea63fe6c Mon Sep 17 00:00:00 2001 From: teor Date: Thu, 22 Apr 2021 08:14:36 +1000 Subject: [PATCH] Add the new commit count and git hash to the version (#2038) * Use the git version + new commit count + hash for the app version This helps diagnose bugs in versions of Zebra built from git branches, rather than git version tags. * Fill in assert * Also log semver string * Fix syntax * Handle vergen using the cargo package version or raw git tag * s/Semver/SemVer/ Co-authored-by: Deirdre Connolly --- zebra-chain/src/parameters/network.rs | 6 ++ zebrad/build.rs | 1 - zebrad/src/application.rs | 84 ++++++++++++++++++---- zebrad/src/components/tracing/component.rs | 4 +- 4 files changed, 80 insertions(+), 15 deletions(-) diff --git a/zebra-chain/src/parameters/network.rs b/zebra-chain/src/parameters/network.rs index 860b84697..fcb4be9d6 100644 --- a/zebra-chain/src/parameters/network.rs +++ b/zebra-chain/src/parameters/network.rs @@ -22,6 +22,12 @@ impl From<&Network> for &'static str { } } +impl From for &'static str { + fn from(network: Network) -> &'static str { + (&network).into() + } +} + impl fmt::Display for Network { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(self.into()) diff --git a/zebrad/build.rs b/zebrad/build.rs index 331f47044..7165b63d0 100644 --- a/zebrad/build.rs +++ b/zebrad/build.rs @@ -6,7 +6,6 @@ fn main() { *config.cargo_mut().features_mut() = false; *config.cargo_mut().profile_mut() = false; - *config.git_mut().semver_mut() = false; *config.git_mut().sha_kind_mut() = ShaKind::Short; match vergen(config) { diff --git a/zebrad/src/application.rs b/zebrad/src/application.rs index 5e1274549..4444f4c60 100644 --- a/zebrad/src/application.rs +++ b/zebrad/src/application.rs @@ -7,7 +7,7 @@ use abscissa_core::{ config::Configurable, terminal::component::Terminal, terminal::ColorChoice, - Application, Component, EntryPoint, FrameworkError, Shutdown, StandardPaths, + Application, Component, EntryPoint, FrameworkError, Shutdown, StandardPaths, Version, }; use application::fatal_error; use std::process; @@ -37,6 +37,59 @@ pub fn app_config() -> config::Reader { config::Reader::new(&APPLICATION) } +/// Returns the zebrad version for this build, in SemVer 2.0 format. +/// +/// Includes the git commit and the number of commits since the last version +/// tag, if available. +/// +/// For details, see https://semver.org/ +pub fn app_version() -> Version { + const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION"); + let vergen_git_semver: Option<&str> = option_env!("VERGEN_GIT_SEMVER"); + + match vergen_git_semver { + // change the git semver format to the semver 2.0 format + Some(mut vergen_git_semver) if !vergen_git_semver.is_empty() => { + // strip the leading "v", if present + if &vergen_git_semver[0..1] == "v" { + vergen_git_semver = &vergen_git_semver[1..]; + } + + // split into tag, commit count, hash + let rparts: Vec<_> = vergen_git_semver.rsplitn(3, '-').collect(); + + match rparts.as_slice() { + // assume it's a cargo package version or a git tag with no hash + [_] | [_, _] => vergen_git_semver.parse().unwrap_or_else(|_| { + panic!( + "VERGEN_GIT_SEMVER without a hash {:?} must be valid semver 2.0", + vergen_git_semver + ) + }), + + // it's the "git semver" format, which doesn't quite match SemVer 2.0 + [hash, commit_count, tag] => { + let semver_fix = format!("{}+{}.{}", tag, commit_count, hash); + semver_fix.parse().unwrap_or_else(|_| + panic!("Modified VERGEN_GIT_SEMVER {:?} -> {:?} -> {:?} must be valid. Note: CARGO_PKG_VERSION was {:?}.", + vergen_git_semver, + rparts, + semver_fix, + CARGO_PKG_VERSION)) + } + + _ => unreachable!("split is limited to 3 parts"), + } + } + _ => CARGO_PKG_VERSION.parse().unwrap_or_else(|_| { + panic!( + "CARGO_PKG_VERSION {:?} must be valid semver 2.0", + CARGO_PKG_VERSION + ) + }), + } +} + /// Zebrad Application #[derive(Debug)] pub struct ZebradApp { @@ -163,30 +216,33 @@ impl Application for ZebradApp { // collect the common metadata for the issue URL and panic report, // skipping any env vars that aren't present - let panic_metadata: Vec<(&'static str, &'static str)> = [ + let panic_metadata: Vec<(_, String)> = [ // cargo or git tag + short commit - ("version", Some(env!("CARGO_PKG_VERSION"))), + ("version", Some(app_version().to_string())), // git - ("branch", option_env!("VERGEN_GIT_BRANCH")), - ("git commit", Self::git_commit()), + ("branch", option_env!("VERGEN_GIT_BRANCH").map(Into::into)), + ("git commit", Self::git_commit().map(Into::into)), ( "commit timestamp", - option_env!("VERGEN_GIT_COMMIT_TIMESTAMP"), + option_env!("VERGEN_GIT_COMMIT_TIMESTAMP").map(Into::into), ), // build - ("target triple", option_env!("VERGEN_CARGO_TARGET_TRIPLE")), + ( + "target triple", + option_env!("VERGEN_CARGO_TARGET_TRIPLE").map(Into::into), + ), // config - ("Zcash network", Some((&config.network.network).into())), + ("Zcash network", Some(config.network.network.to_string())), ] .iter() - .filter_map(|(k, opt_v)| Some((*k, *opt_v.as_ref()?))) + .filter_map(|(k, opt_v)| Some((*k, opt_v.as_ref()?.clone()))) .collect(); let mut builder = color_eyre::config::HookBuilder::default(); let mut metadata_section = "Metadata:".to_string(); for (k, v) in panic_metadata { - builder = builder.add_issue_metadata(k, v); - metadata_section.push_str(&format!("\n{}: {}", k, v)); + builder = builder.add_issue_metadata(k, v.clone()); + metadata_section.push_str(&format!("\n{}: {}", k, v.clone())); } builder = builder @@ -233,7 +289,7 @@ impl Application for ZebradApp { let guard = sentry::init( sentry::ClientOptions { debug: true, - release: Self::git_commit().map(Into::into), + release: Some(app_version().to_string().into()), ..Default::default() } .add_integration(sentry_tracing::TracingIntegration::default()), @@ -347,4 +403,8 @@ impl Application for ZebradApp { Shutdown::Crash => process::exit(2), } } + + fn version(&self) -> Version { + app_version() + } } diff --git a/zebrad/src/components/tracing/component.rs b/zebrad/src/components/tracing/component.rs index df45788fb..c0ac420e5 100644 --- a/zebrad/src/components/tracing/component.rs +++ b/zebrad/src/components/tracing/component.rs @@ -5,7 +5,7 @@ use tracing_subscriber::{ FmtSubscriber, }; -use crate::config::TracingSection; +use crate::{application::app_version, config::TracingSection}; use super::flame; @@ -77,7 +77,7 @@ impl Component for Tracing { } fn version(&self) -> abscissa_core::Version { - abscissa_core::Version::parse(env!("CARGO_PKG_VERSION")).unwrap() + app_version() } fn before_shutdown(&self, _kind: Shutdown) -> Result<(), FrameworkError> {