diff --git a/build.rs b/build.rs index 3a8149e..2bf0bd2 100644 --- a/build.rs +++ b/build.rs @@ -1,3 +1,16 @@ +use std::process::Command; + fn main() { println!("cargo:rerun-if-changed=migrations"); + + let git_short_hash = Command::new("git") + .args(["rev-parse", "--short", "HEAD"]) + .output() + .ok() + .and_then(|o| String::from_utf8(o.stdout).ok()) + .map(|s| s.trim().to_string()) // Trim newline + .unwrap_or_else(|| "UNKNOWN".to_string()); + + // Tell Cargo to set `GIT_SHORT_HASH` for the main compilation + println!("cargo:rustc-env=GIT_SHORT_HASH={}", git_short_hash); } diff --git a/src/api/v1/stats.rs b/src/api/v1/stats.rs index bf2bec3..30888aa 100644 --- a/src/api/v1/stats.rs +++ b/src/api/v1/stats.rs @@ -12,6 +12,7 @@ use crate::error::Error; use crate::schema::users::dsl::{users, uuid}; const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); +const GIT_SHORT_HASH: &str = env!("GIT_SHORT_HASH"); #[derive(Serialize)] struct Response { @@ -57,7 +58,7 @@ pub async fn res(data: web::Data) -> Result { registration_enabled: data.config.instance.registration, email_verification_required: data.config.instance.require_email_verification, // TODO: Get build number from git hash or remove this from the spec - build_number: String::from("how do i implement this?"), + build_number: String::from(GIT_SHORT_HASH), }; Ok(HttpResponse::Ok().json(response))