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 <durumcrustulum@gmail.com>
This commit is contained in:
teor 2021-04-22 08:14:36 +10:00 committed by GitHub
parent 0203d1475a
commit 96b3c94dbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 15 deletions

View File

@ -22,6 +22,12 @@ impl From<&Network> for &'static str {
}
}
impl From<Network> 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())

View File

@ -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) {

View File

@ -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<ZebradApp> {
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()
}
}

View File

@ -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<A: abscissa_core::Application> Component<A> 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> {