From 7562791e76537e4af1f0ab31ef5406d5e6b879de Mon Sep 17 00:00:00 2001 From: Tiago Castro Date: Fri, 10 Feb 2023 10:29:41 +0000 Subject: [PATCH] feat: add features to control git-version (#21) Added git dependencies: https://github.com/fusion-engineering/rust-git-version/pull/19 This allows us to avoid rebuilds when staging changes. Also added another feature to avoid rebuilds altogether. Eg: if git versions are specified via env variable then we don't need to use git from within the rust-build at all. This is fine for most dev setups if we don't care about versions locally testing. Signed-off-by: Tiago Castro --- .gitmodules | 4 ++ rust-git-version | 1 + version-info/Cargo.toml | 7 ++- version-info/src/git_utils.rs | 83 ++++++++++++++++++++++++----------- version-info/src/lib.rs | 1 + version-info/src/macros.rs | 14 ++++++ 6 files changed, 83 insertions(+), 27 deletions(-) create mode 160000 rust-git-version diff --git a/.gitmodules b/.gitmodules index 342f40b..f010046 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,7 @@ [submodule "h2"] path = h2 url = https://github.com/openebs/h2.git +[submodule "rust-git-version"] + path = rust-git-version + url = https://github.com/openebs/rust-git-version.git + branch = mayastor diff --git a/rust-git-version b/rust-git-version new file mode 160000 index 0000000..7a919e6 --- /dev/null +++ b/rust-git-version @@ -0,0 +1 @@ +Subproject commit 7a919e6b1e032587cbe9b6fa1ea5e7c970dd6688 diff --git a/version-info/Cargo.toml b/version-info/Cargo.toml index b2560a0..fdafbbc 100644 --- a/version-info/Cargo.toml +++ b/version-info/Cargo.toml @@ -7,5 +7,10 @@ authors = [ ] [dependencies] -git-version = "0.3.5" +git-version-macro = { path = "../rust-git-version/git-version-macro" } regex = "1" + +[features] +default = [ "default-git-versions" ] +default-git-versions = [ ] +git-version-stale = [ "default-git-versions" ] \ No newline at end of file diff --git a/version-info/src/git_utils.rs b/version-info/src/git_utils.rs index eaf0ede..257e759 100644 --- a/version-info/src/git_utils.rs +++ b/version-info/src/git_utils.rs @@ -1,37 +1,68 @@ use regex::Regex; -/// Returns the git version as &'static str, in the long format: -/// either tag, number of additional commits and the abbreviated commit name, -/// or just commit hash in the case no tags found. -/// See `git describe` manual for details. -pub fn long_raw_version_str() -> &'static str { - // to keep clippy happy - #[allow(dead_code)] - fn fallback() -> &'static str { - option_env!("GIT_VERSION_LONG").expect("git version fallback") +#[cfg(feature = "default-git-versions")] +mod default_version { + /// Returns the git version as &'static str, in the long format: + /// either tag, number of additional commits and the abbreviated commit name, + /// or just commit hash in the case no tags found. + /// See `git describe` manual for details. + pub fn long_raw_version_str() -> &'static str { + // to keep clippy happy + #[allow(dead_code)] + fn fallback() -> &'static str { + option_env!("GIT_VERSION_LONG").expect("git version fallback") + } + + #[cfg(not(feature = "git-version-stale"))] + let version = git_version_macro::git_version!( + args = ["--abbrev=12", "--always", "--long"], + fallback = fallback() + ); + + #[cfg(feature = "git-version-stale")] + let version = git_version_macro::git_version!( + args = ["--abbrev=12", "--always", "--long"], + fallback = fallback(), + git_deps = ["logs/HEAD"] + ); + + version } - git_version::git_version!( - args = ["--abbrev=12", "--always", "--long"], - fallback = fallback() - ) -} -/// Returns the git version as &'static str. -/// See `git describe` manual for details. -pub fn raw_version_str() -> &'static str { - // to keep clippy happy - #[allow(dead_code)] - fn fallback() -> &'static str { - option_env!("GIT_VERSION").expect("git version fallback") + /// Returns the git version as &'static str. + /// See `git describe` manual for details. + pub fn raw_version_str() -> &'static str { + // to keep clippy happy + #[allow(dead_code)] + fn fallback() -> &'static str { + option_env!("GIT_VERSION").expect("git version fallback") + } + + #[cfg(not(feature = "git-version-stale"))] + let version = git_version_macro::git_version!( + args = ["--abbrev=12", "--always"], + fallback = fallback() + ); + + #[cfg(feature = "git-version-stale")] + let version = git_version_macro::git_version!( + args = ["--abbrev=12", "--always"], + fallback = fallback(), + git_deps = ["logs/HEAD"] + ); + + version } - git_version::git_version!(args = ["--abbrev=12", "--always"], fallback = fallback()) -} -/// Returns the git version as String. -pub fn raw_version_string() -> String { - String::from(raw_version_str()) + /// Returns the git version as String. + pub fn raw_version_string() -> String { + String::from(raw_version_str()) + } } +#[cfg(feature = "default-git-versions")] +pub use default_version::{long_raw_version_str, raw_version_str, raw_version_string}; + /// Git version data. pub(super) struct GitVersion { /// Abbreviated commit hash. diff --git a/version-info/src/lib.rs b/version-info/src/lib.rs index d791c98..b1f0298 100644 --- a/version-info/src/lib.rs +++ b/version-info/src/lib.rs @@ -1,5 +1,6 @@ mod git_utils; mod version_info; +#[cfg(feature = "default-git-versions")] pub use git_utils::{long_raw_version_str, raw_version_str, raw_version_string}; pub use version_info::VersionInfo; pub mod macros; diff --git a/version-info/src/macros.rs b/version-info/src/macros.rs index b13a40b..7e3d5e5 100644 --- a/version-info/src/macros.rs +++ b/version-info/src/macros.rs @@ -15,6 +15,20 @@ macro_rules! version_info { }, ) }; + ($version:expr) => { + $crate::VersionInfo::new( + String::from($version), + String::from(env!("CARGO_PKG_NAME")), + String::from(env!("CARGO_PKG_DESCRIPTION")), + String::from(env!("CARGO_PKG_VERSION")), + option_env!("CARGO_BIN_NAME").map(|s| s.to_string()), + if cfg!(debug_assertions) { + String::from("debug") + } else { + String::from("") + }, + ) + }; } /// Returns a version info instance.