From 98c22d24082db892585abb94e8660092989c2c35 Mon Sep 17 00:00:00 2001 From: blek Date: Sun, 29 Oct 2023 19:11:38 +1000 Subject: [PATCH] load commit hash at compile time --- filed/build.rs | 10 +++++++++- filed/src/env.rs | 18 ++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/filed/build.rs b/filed/build.rs index 6d6c080..13b1c0c 100644 --- a/filed/build.rs +++ b/filed/build.rs @@ -58,5 +58,13 @@ fn main() { .arg(asset_path(asset)) .spawn() .unwrap(); - }) + }); + + let commit = Command::new("git") + .args(&["rev-parse", "HEAD"]) + .output() + .unwrap(); + + let commit = String::from_utf8(commit.stdout).unwrap(); + println!("cargo:rustc-env=COMMIT_HASH={commit}"); } \ No newline at end of file diff --git a/filed/src/env.rs b/filed/src/env.rs index 545dac4..ecd295f 100644 --- a/filed/src/env.rs +++ b/filed/src/env.rs @@ -15,6 +15,18 @@ pub struct Redis { pub prefix: String } +#[derive(Debug, Clone)] +pub struct VersionData { + pub commit: String +} +impl Default for VersionData { + fn default() -> Self { + VersionData { + commit: env!("COMMIT_HASH").to_string() + } + } +} + #[derive(Debug, Clone)] pub struct Env { pub logging: bool, @@ -24,7 +36,8 @@ pub struct Env { pub filedir: String, pub instanceurl: String, pub uploadspath: String, - pub confpath: String + pub confpath: String, + pub version: VersionData } fn get_var, O: From>(name: T) -> Result { @@ -140,7 +153,8 @@ pub fn loadenv() -> Result> { return Err(format!("CONF_FILE is {}, which is not a file!", spath).into()) } spath - } + }, + version: VersionData::default() } ) }