diff --git a/build.rs b/build.rs index 3f0cbcc69..8a3834f4d 100644 --- a/build.rs +++ b/build.rs @@ -1,15 +1,10 @@ fn main() { - let commit_id = std::process::Command::new("git") - .args([ - "describe", - "--always", - "--match", - "__EXCLUDE__", - "--abbrev=7", - ]) + if let Some(commit_id) = std::process::Command::new("git") + .args(["describe", "--always", "--dirty", "--exclude", "*"]) .output() - .map(|r| String::from_utf8(r.stdout).unwrap()) - .unwrap(); - - println!("cargo:rustc-env=AXON_COMMIT_ID={}", commit_id); + .ok() + .and_then(|r| String::from_utf8(r.stdout).ok()) + { + println!("cargo:rustc-env=AXON_COMMIT_ID={}", commit_id); + } } diff --git a/common/version/src/lib.rs b/common/version/src/lib.rs index 0d122e671..6272cf7af 100644 --- a/common/version/src/lib.rs +++ b/common/version/src/lib.rs @@ -1,6 +1,8 @@ use std::fmt::{Display, Formatter}; use std::str::FromStr; +pub const DEFAULT_COMMIT_ID: &str = "unknown"; + #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct Version { inner: semver::Version, @@ -22,7 +24,9 @@ impl Display for Version { impl Version { pub fn new(ver: &str) -> Self { - Version::from_str(ver).unwrap_or_else(|e| panic!("Parse version error {:?}", e)) + Version::from_str(ver) + .unwrap_or_else(|e| panic!("Parse version error {:?}", e)) + .set_commit_id(DEFAULT_COMMIT_ID) } pub fn new_with_commit_id(ver: &str, commit_id: &str) -> Self { @@ -40,3 +44,17 @@ impl Version { self } } + +#[cfg(test)] +mod tests { + use semver::BuildMetadata; + use std::str::FromStr; + + use crate::DEFAULT_COMMIT_ID; + + #[test] + fn test_parse_default_commit_id() { + let build = BuildMetadata::from_str(DEFAULT_COMMIT_ID); + assert_eq!(build.unwrap().as_str(), DEFAULT_COMMIT_ID); + } +} diff --git a/core/cli/src/lib.rs b/core/cli/src/lib.rs index 340d055c0..83f673b3b 100644 --- a/core/cli/src/lib.rs +++ b/core/cli/src/lib.rs @@ -47,14 +47,15 @@ pub struct CheckingVersionError { pub type Result = std::result::Result; pub struct AxonCli { - version: Version, - matches: ArgMatches, + kernel_version: Version, + application_version: Version, + matches: ArgMatches, } impl AxonCli { - pub fn init(axon_version: Version) -> Self { + pub fn init(kernel_version: Version, application_version: Version) -> Self { let matches = Command::new("axon") - .version(axon_version.to_string()) + .version(kernel_version.to_string()) .subcommand_required(true) .subcommand( Command::new("run") @@ -78,7 +79,8 @@ impl AxonCli { ); AxonCli { - version: axon_version, + kernel_version, + application_version, matches: matches.get_matches(), } } @@ -112,7 +114,7 @@ impl AxonCli { register_log(&config); - Axon::new(self.version.to_string(), config, genesis) + Axon::new(self.application_version.to_string(), config, genesis) .run(key_provider) .map_err(Error::Running) } @@ -128,7 +130,7 @@ impl AxonCli { // Won't panic because parent of data_path_for_version() is data_path. check_version( &config.data_path_for_version(), - &self.version, + &self.kernel_version, latest_compatible_version(), ) } diff --git a/src/lib.rs b/src/lib.rs index 8498c4d6b..31cc88b42 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,8 +13,15 @@ use core_executor::FEE_ALLOCATOR; pub fn run( fee_allocator: impl FeeAllocate + 'static, key_provider: impl KeyProvider, - version: &'static str, + app_version: &'static str, ) -> Result<()> { FEE_ALLOCATOR.swap(Arc::new(Box::new(fee_allocator))); - AxonCli::init(version.parse().unwrap()).start_with_custom_key_provider(Some(key_provider)) + + AxonCli::init( + clap::crate_version!() + .parse() + .expect("Parse kernel version"), + app_version.parse().expect("Parse application version"), + ) + .start_with_custom_key_provider(Some(key_provider)) } diff --git a/src/main.rs b/src/main.rs index 55cb63b67..d71b32246 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,9 +4,12 @@ use common_version::Version; use core_cli::AxonCli; fn main() { - let version = Version::new_with_commit_id(crate_version!(), env!("AXON_COMMIT_ID")); + let crate_version = crate_version!(); + let kernel_version = option_env!("AXON_COMMIT_ID") + .map(|commit_id| Version::new_with_commit_id(crate_version, commit_id)) + .unwrap_or_else(|| Version::new(crate_version)); - if let Err(e) = AxonCli::init(version).start() { + if let Err(e) = AxonCli::init(kernel_version.clone(), kernel_version).start() { eprintln!("Error {e}"); std::process::exit(1); }