Stop assuming there will always be a git commit

Enable builds where:
* there is no google cloud git commit env var, and
* there is no `.git` directory.

By making all `vergen` env vars optional, and skipping any env vars that
don't exist.
This commit is contained in:
teor 2021-04-20 16:53:04 +10:00 committed by Deirdre Connolly
parent ccf5d82517
commit 79c0c4ec57
2 changed files with 41 additions and 18 deletions

View File

@ -9,5 +9,11 @@ fn main() {
*config.git_mut().semver_mut() = false; *config.git_mut().semver_mut() = false;
*config.git_mut().sha_kind_mut() = ShaKind::Short; *config.git_mut().sha_kind_mut() = ShaKind::Short;
vergen(config).expect("Unable to generate the cargo keys!"); match vergen(config) {
Ok(_) => {}
Err(e) => eprintln!(
"skipping detailed git and target info due to vergen error: {:?}",
e
),
}
} }

View File

@ -53,11 +53,18 @@ impl ZebradApp {
atty::is(atty::Stream::Stdout) && atty::is(atty::Stream::Stderr) atty::is(atty::Stream::Stdout) && atty::is(atty::Stream::Stderr)
} }
pub fn git_commit() -> &'static str { /// Returns the git commit for this build, if available.
const GIT_COMMIT_VERGEN: &str = env!("VERGEN_GIT_SHA_SHORT"); ///
///
/// # Accuracy
///
/// If the user makes changes, but does not commit them, the git commit will
/// not match the compiled source code.
pub fn git_commit() -> Option<&'static str> {
const GIT_COMMIT_GCLOUD: Option<&str> = option_env!("SHORT_SHA"); const GIT_COMMIT_GCLOUD: Option<&str> = option_env!("SHORT_SHA");
const GIT_COMMIT_VERGEN: Option<&str> = option_env!("VERGEN_GIT_SHA_SHORT");
GIT_COMMIT_GCLOUD.unwrap_or(GIT_COMMIT_VERGEN) GIT_COMMIT_GCLOUD.or(GIT_COMMIT_VERGEN)
} }
} }
@ -154,18 +161,26 @@ impl Application for ZebradApp {
color_eyre::config::Theme::new() color_eyre::config::Theme::new()
}; };
// collect the common metadata for the issue URL and panic report // collect the common metadata for the issue URL and panic report,
let panic_metadata = vec![ // skipping any env vars that aren't present
let panic_metadata: Vec<(&'static str, &'static str)> = [
// cargo or git tag + short commit
("version", Some(env!("CARGO_PKG_VERSION"))),
// git // git
("version", env!("CARGO_PKG_VERSION")), ("branch", option_env!("VERGEN_GIT_BRANCH")),
("branch", env!("VERGEN_GIT_BRANCH")),
("git commit", Self::git_commit()), ("git commit", Self::git_commit()),
("commit timestamp", env!("VERGEN_GIT_COMMIT_TIMESTAMP")), (
"commit timestamp",
option_env!("VERGEN_GIT_COMMIT_TIMESTAMP"),
),
// build // build
("target triple", env!("VERGEN_CARGO_TARGET_TRIPLE")), ("target triple", option_env!("VERGEN_CARGO_TARGET_TRIPLE")),
// config // config
("Zcash network", (&config.network.network).into()), ("Zcash network", Some((&config.network.network).into())),
]; ]
.iter()
.filter_map(|(k, opt_v)| Some((*k, *opt_v.as_ref()?)))
.collect();
let mut builder = color_eyre::config::HookBuilder::default(); let mut builder = color_eyre::config::HookBuilder::default();
let mut metadata_section = "Metadata:".to_string(); let mut metadata_section = "Metadata:".to_string();
@ -218,7 +233,7 @@ impl Application for ZebradApp {
let guard = sentry::init( let guard = sentry::init(
sentry::ClientOptions { sentry::ClientOptions {
debug: true, debug: true,
release: Some(Self::git_commit().into()), release: Self::git_commit().map(Into::into),
..Default::default() ..Default::default()
} }
.add_integration(sentry_tracing::TracingIntegration::default()), .add_integration(sentry_tracing::TracingIntegration::default()),
@ -270,11 +285,13 @@ impl Application for ZebradApp {
// Activate the global span, so it's visible when we load the other // Activate the global span, so it's visible when we load the other
// components. Space is at a premium here, so we use an empty message, // components. Space is at a premium here, so we use an empty message,
// short commit hash, and the unique part of the network name. // short commit hash, and the unique part of the network name.
let global_span = error_span!( let net = &self.config.clone().unwrap().network.network.to_string()[..4];
"", let global_span = if let Some(git_commit) = ZebradApp::git_commit() {
zebrad = ZebradApp::git_commit(), error_span!("", zebrad = git_commit, net)
net = &self.config.clone().unwrap().network.network.to_string()[..4], } else {
); error_span!("", net)
};
let global_guard = global_span.enter(); let global_guard = global_span.enter();
// leak the global span, to make sure it stays active // leak the global span, to make sure it stays active
std::mem::forget(global_guard); std::mem::forget(global_guard);