From c63da05b72407224fa2bd1fa960cfe04d06d3ccc Mon Sep 17 00:00:00 2001 From: GitHub <> Date: Wed, 18 Jan 2023 17:31:33 +0800 Subject: [PATCH] Add git hash info --- CHANGELOG.md | 4 ++++ build.rs | 12 ++++++++++++ src/controller/mod.rs | 4 +++- src/main.rs | 2 ++ templates/layout.html | 3 ++- 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c3c473..564f7dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add git hash info + ## [0.2.8] - 2023-01-17 ### Added diff --git a/build.rs b/build.rs index 17f4ee0..1cf205d 100644 --- a/build.rs +++ b/build.rs @@ -1,6 +1,18 @@ +use std::process::Command; + fn main() { match std::env::var("PROFILE") { Ok(key) if key.as_str() == "debug" => println!("cargo:rustc-env=PROFILE=debug"), _ => {} } + + let mut git_hash = match Command::new("git").args(["rev-parse", "HEAD"]).output() { + Ok(output) if !output.stdout.is_empty() => { + String::from_utf8_lossy(&output.stdout).to_string() + } + _ => "not found".into(), + }; + + git_hash.truncate(7); + println!("cargo:rustc-env=GIT_HASH={git_hash}"); } diff --git a/src/controller/mod.rs b/src/controller/mod.rs index 264661d..4198cde 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -296,7 +296,7 @@ struct Claim { session_id: String, } -use crate::{config::CONFIG, error::AppError, VERSION}; +use crate::{config::CONFIG, error::AppError, GIT_HASH, VERSION}; use askama::Template; use axum::{ async_trait, @@ -929,6 +929,7 @@ struct PageData<'a> { has_unread: bool, sha256: &'a str, version: &'a str, + git_commit: &'a str, footer_links: Vec<(&'a str, &'a str)>, } @@ -954,6 +955,7 @@ impl<'a> PageData<'a> { has_unread, sha256: &CURRENT_SHA256, version: VERSION, + git_commit: GIT_HASH, footer_links, } } diff --git a/src/main.rs b/src/main.rs index 5a755dd..0c1a163 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,7 @@ use tracing::{error, info}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; const VERSION: &str = env!("CARGO_PKG_VERSION"); +const GIT_HASH: &str = env!("GIT_HASH"); static IS_DEBUG: Lazy = Lazy::new(|| matches!(std::env::var("PROFILE"), Ok(key) if key.as_str() == "debug")); @@ -34,6 +35,7 @@ async fn main() -> Result<(), AppError> { info!("sha256: {}", *CURRENT_SHA256); info!(VERSION); + info!(GIT_HASH); let db_url = &CONFIG.db; let config = sled::Config::default().path(db_url).use_compression(true); diff --git a/templates/layout.html b/templates/layout.html index da75477..5c8fa85 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -130,8 +130,9 @@ {% for footer_link in page_data.footer_links %}

{{footer_link.0|capitalize}}

{% endfor %} -

version: {{page_data.version}}

+

version: v{{page_data.version}}

sha256: {{page_data.sha256|truncate(10)}}

+

commit: {{page_data.git_commit}}