Skip to content

Commit

Permalink
add better version information
Browse files Browse the repository at this point in the history
  • Loading branch information
lmaotrigine committed Oct 30, 2023
1 parent c307842 commit 6fae3f8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "heartbeat"
version = "0.1.0"
version = "0.1.0-a"
description = "A service to keep a live heartbeat on multiple devices"
edition = "2021"
license = "MPL-2.0"
Expand Down
26 changes: 25 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@
)]

fn main() {
let rev = git_revision_hash().unwrap_or_else(|| "<unknown>".into());
let mut version = env!("CARGO_PKG_VERSION").to_owned();
let rev = git_revision_hash().unwrap_or_else(|| "main".into());
if ["a", "b", "rc"].iter().any(|s| version.ends_with(s)) {
if let Some(count) = git_commit_count() {
version.push('.');
version.push_str(&count);
}
version.push_str(&format!("+g{rev}"));
}
println!("cargo:rustc-env=HB_VERSION={version}");
println!("cargo:rustc-env=HB_GIT_COMMIT={rev}");
}

Expand All @@ -32,3 +41,18 @@ fn git_revision_hash() -> Option<String> {
}
})
}

fn git_commit_count() -> Option<String> {
std::process::Command::new("git")
.args(["rev-list", "--count", "HEAD"])
.output()
.ok()
.and_then(|output| {
let v = String::from_utf8_lossy(&output.stdout).trim().to_string();
if v.is_empty() {
None
} else {
Some(v)
}
})
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub use config::Config;
pub use error::handle_errors;

/// Crate version and git commit hash.
pub const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "-", env!("HB_GIT_COMMIT"));
pub const VERSION: &str = env!("HB_VERSION");

/// Global application state.
#[derive(Debug, Clone, FromRef)]
Expand Down
3 changes: 2 additions & 1 deletion src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
config::Config,
stats::Stats,
util::formats::{format_relative, FormatNum},
VERSION,
};
use chrono::{DateTime, Utc};
use html::{html, Markup, PreEscaped, DOCTYPE};
Expand Down Expand Up @@ -121,7 +122,7 @@ Due to caching, you will have to check the website if the embed generation time
"This website is running on version "
a href=(href) {
code {
(commit)
(VERSION)
}
}
" of "
Expand Down

0 comments on commit 6fae3f8

Please sign in to comment.