Skip to content

Commit

Permalink
feat: add features to control git-version (#21)
Browse files Browse the repository at this point in the history
Added git dependencies:
fusion-engineering/rust-git-version#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 <[email protected]>
  • Loading branch information
tiagolobocastro authored Feb 10, 2023
1 parent 41a8ecf commit 7562791
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 27 deletions.
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

0 comments on commit 7562791

Please sign in to comment.