load commit hash at compile time

This commit is contained in:
blek 2023-10-29 19:11:38 +10:00
parent ce7f70ad7b
commit 98c22d2408
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 25 additions and 3 deletions

View File

@ -58,5 +58,13 @@ fn main() {
.arg(asset_path(asset)) .arg(asset_path(asset))
.spawn() .spawn()
.unwrap(); .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}");
} }

View File

@ -15,6 +15,18 @@ pub struct Redis {
pub prefix: String 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)] #[derive(Debug, Clone)]
pub struct Env { pub struct Env {
pub logging: bool, pub logging: bool,
@ -24,7 +36,8 @@ pub struct Env {
pub filedir: String, pub filedir: String,
pub instanceurl: String, pub instanceurl: String,
pub uploadspath: String, pub uploadspath: String,
pub confpath: String pub confpath: String,
pub version: VersionData
} }
fn get_var<T: Into<String>, O: From<String>>(name: T) -> Result<O, String> { fn get_var<T: Into<String>, O: From<String>>(name: T) -> Result<O, String> {
@ -140,7 +153,8 @@ pub fn loadenv() -> Result<Env, Box<dyn std::error::Error>> {
return Err(format!("CONF_FILE is {}, which is not a file!", spath).into()) return Err(format!("CONF_FILE is {}, which is not a file!", spath).into())
} }
spath spath
} },
version: VersionData::default()
} }
) )
} }