Skip to content
Open
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
40 changes: 40 additions & 0 deletions dash-spv/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::process::Command;

fn git(args: &[&str]) -> Option<String> {
let output = Command::new("git").args(args).output().ok()?;
if !output.status.success() {
return None;
}
let text = String::from_utf8(output.stdout).ok()?.trim().to_string();
if text.is_empty() {
None
} else {
Some(text)
}
}

fn main() {
let hash = git(&["rev-parse", "--short=12", "HEAD"]).unwrap_or_default();
let dirty = git(&["status", "--porcelain", "--untracked-files=no"])
.map(|s| !s.is_empty())
.unwrap_or(false);
let tagged = git(&["describe", "--exact-match", "--tags", "--match", "v*", "HEAD"]).is_some();

println!("cargo:rustc-env=DASH_SPV_GIT_HASH={hash}");
println!("cargo:rustc-env=DASH_SPV_GIT_DIRTY={dirty}");
println!("cargo:rustc-env=DASH_SPV_GIT_TAGGED={tagged}");

println!("cargo:rerun-if-changed=build.rs");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are not checking if src/ changes, so, ifIa build without changes, and then I change the src/, when I build again DASH_SPV_GIT_DIRTY is still defined as false instead of true right??

Also, checking a different branch would change src/, making build.rs run again without needing to track git files right?? or I am missing any case??

// `.git/HEAD` only changes when switching branches, not when committing on
// the current one, so also watch the symbolic target's ref file.
if let Some(head_ref) = git(&["symbolic-ref", "--quiet", "HEAD"]) {
if let Some(p) = git(&["rev-parse", "--git-path", &head_ref]) {
println!("cargo:rerun-if-changed={p}");
}
}
for path in ["HEAD", "index", "packed-refs"] {
if let Some(p) = git(&["rev-parse", "--git-path", path]) {
println!("cargo:rerun-if-changed={p}");
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
}
2 changes: 2 additions & 0 deletions dash-spv/src/client/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ impl<W: WalletInterface, N: NetworkManager, S: StorageManager> DashSpvClient<W,
wallet: Arc<RwLock<W>>,
event_handlers: Vec<Arc<dyn EventHandler>>,
) -> Result<Self> {
tracing::info!("{}", crate::version_info());

// Validate configuration
config.validate().map_err(SpvError::Config)?;

Expand Down
61 changes: 61 additions & 0 deletions dash-spv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,64 @@ pub use dashcore::sml::llmq_type::LLMQType;

/// Current version of the dash-spv library.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

/// Short git commit the library was built from, empty if it could not be determined.
pub const GIT_HASH: &str = env!("DASH_SPV_GIT_HASH");

/// Whether the working tree had uncommitted tracked changes at build time.
pub const GIT_DIRTY: bool = const_str_eq(env!("DASH_SPV_GIT_DIRTY"), "true");

/// Whether the build was made from a commit pointed at by a `v*` release tag.
pub const GIT_TAGGED: bool = const_str_eq(env!("DASH_SPV_GIT_TAGGED"), "true");

const fn const_str_eq(a: &str, b: &str) -> bool {
let (a, b) = (a.as_bytes(), b.as_bytes());
if a.len() != b.len() {
return false;
}
let mut i = 0;
while i < a.len() {
if a[i] != b[i] {
return false;
}
i += 1;
}
true
}

/// Human readable version.
///
/// A release build (the commit is pointed at by a `v*` tag and the tree is clean)
/// renders just `dash-spv 0.42.0`. Any development build surfaces the commit so it
/// is recognizable as non-release: `dash-spv 0.42.0 (a1b2c3d4e5f6)`, or
/// `dash-spv 0.42.0 (a1b2c3d4e5f6-dirty)` with uncommitted changes. Builds with no
/// git context (e.g. a packaged source tarball) render just `dash-spv 0.42.0`.
pub fn version_info() -> String {
if GIT_HASH.is_empty() || (GIT_TAGGED && !GIT_DIRTY) {
format!("dash-spv {VERSION}")
} else if GIT_DIRTY {
format!("dash-spv {VERSION} ({GIT_HASH}-dirty)")
} else {
format!("dash-spv {VERSION} ({GIT_HASH})")
}
}

#[cfg(test)]
mod tests {
use super::{version_info, GIT_DIRTY, GIT_HASH, GIT_TAGGED, VERSION};

#[test]
fn version_info_format() {
let info = version_info();
assert!(info.starts_with("dash-spv "));
assert!(info.contains(VERSION));

let is_release = GIT_HASH.is_empty() || (GIT_TAGGED && !GIT_DIRTY);
if is_release {
assert_eq!(info, format!("dash-spv {VERSION}"));
} else {
assert!(info.contains(GIT_HASH));
assert_eq!(info.ends_with("-dirty)"), GIT_DIRTY);
}
}
}
Loading