From 28a8a15b6b24879c133d9ef8f727b846e629237b Mon Sep 17 00:00:00 2001 From: Xevion Date: Sat, 13 Sep 2025 18:51:48 -0500 Subject: [PATCH] feat: embed git commit into binary, provide link on frontend --- build.rs | 32 ++++++++++++++++++++++++++++++++ src/web/routes.rs | 4 ++++ web/src/lib/api.ts | 4 ++++ web/src/routes/index.tsx | 23 +++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 build.rs diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..5e521b8 --- /dev/null +++ b/build.rs @@ -0,0 +1,32 @@ +use std::process::Command; + +fn main() { + // Get the current Git commit hash + let output = Command::new("git").args(["rev-parse", "HEAD"]).output(); + + let git_hash = match output { + Ok(output) => { + if output.status.success() { + String::from_utf8_lossy(&output.stdout).trim().to_string() + } else { + "unknown".to_string() + } + } + Err(_) => "unknown".to_string(), + }; + + // Get the short hash (first 7 characters) + let short_hash = if git_hash != "unknown" && git_hash.len() >= 7 { + git_hash[..7].to_string() + } else { + git_hash.clone() + }; + + // Set the environment variables that will be available at compile time + println!("cargo:rustc-env=GIT_COMMIT_HASH={}", git_hash); + println!("cargo:rustc-env=GIT_COMMIT_SHORT={}", short_hash); + + // Rebuild if the Git commit changes + println!("cargo:rerun-if-changed=.git/HEAD"); + println!("cargo:rerun-if-changed=.git/refs/heads"); +} diff --git a/src/web/routes.rs b/src/web/routes.rs index d0e6b43..c5b01ef 100644 --- a/src/web/routes.rs +++ b/src/web/routes.rs @@ -239,6 +239,10 @@ async fn status(State(_state): State) -> Json { "banner_api": { "status": "connected" }, + "git": { + "commit": env!("GIT_COMMIT_HASH"), + "short": env!("GIT_COMMIT_SHORT") + }, "timestamp": chrono::Utc::now().to_rfc3339() })) } diff --git a/web/src/lib/api.ts b/web/src/lib/api.ts index bbf8d57..4e66039 100644 --- a/web/src/lib/api.ts +++ b/web/src/lib/api.ts @@ -20,6 +20,10 @@ export interface StatusResponse { banner_api: { status: string; }; + git: { + commit: string; + short: string; + }; timestamp: string; } diff --git a/web/src/routes/index.tsx b/web/src/routes/index.tsx index 9052188..96f3f3f 100644 --- a/web/src/routes/index.tsx +++ b/web/src/routes/index.tsx @@ -234,6 +234,29 @@ function App() { )} + {status?.git?.commit && ( + + + + GitHub + + + + )} );