kaspa_utils/
git.rs

1use crate::hex::FromHex;
2use std::fmt::Display;
3
4const VERSION: &str = env!("CARGO_PKG_VERSION");
5
6// generated by `build.rs`
7const FULL_HASH: &str = env!("RUSTY_KASPA_GIT_FULL_COMMIT_HASH");
8const SHORT_HASH: &str = env!("RUSTY_KASPA_GIT_SHORT_COMMIT_HASH");
9
10/// Check if the codebase is built under a Git repository
11/// and return the hash of the current commit as `Vec<u8>`.
12pub fn hash() -> Option<Vec<u8>> {
13    FromHex::from_hex(FULL_HASH).ok()
14}
15
16pub fn short_hash() -> Option<Vec<u8>> {
17    FromHex::from_hex(SHORT_HASH).ok()
18}
19
20pub fn hash_str() -> Option<&'static str> {
21    #[allow(clippy::const_is_empty)]
22    (!FULL_HASH.is_empty()).then_some(FULL_HASH)
23}
24
25pub fn short_hash_str() -> Option<&'static str> {
26    #[allow(clippy::const_is_empty)]
27    (!SHORT_HASH.is_empty()).then_some(SHORT_HASH)
28}
29
30pub fn version() -> String {
31    if let Some(short_hash) = short_hash_str() {
32        format!("v{VERSION}-{short_hash}")
33    } else {
34        format!("v{VERSION}")
35    }
36}
37
38pub fn with_short_hash<V>(version: V) -> impl Display
39where
40    V: Display,
41{
42    if let Some(short_hash) = short_hash_str() {
43        format!("{version}-{short_hash}")
44    } else {
45        version.to_string()
46    }
47}
48
49#[test]
50fn test_git_hash() {
51    println!("FULL_HASH: {:?}", hash_str());
52    println!("SHORT_HASH: {:?}", short_hash_str());
53}