Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add features to control git-version #21

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions rust-git-version
Submodule rust-git-version added at 7a919e
7 changes: 6 additions & 1 deletion version-info/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
83 changes: 57 additions & 26 deletions version-info/src/git_utils.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions version-info/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 14 additions & 0 deletions version-info/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down