From d09f88f7fa959b8ab195edd993f1306716bd1221 Mon Sep 17 00:00:00 2001 From: blek Date: Sun, 29 Oct 2023 19:11:38 +1000 Subject: [PATCH 1/6] 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() } ) } -- 2.40.1 From b9f0d80dc30936a75f6627e9b3299186a02820b1 Mon Sep 17 00:00:00 2001 From: blek Date: Sun, 29 Oct 2023 19:13:27 +1000 Subject: [PATCH 2/6] move out code to a function --- filed/build.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/filed/build.rs b/filed/build.rs index 13b1c0c..fde6fbd 100644 --- a/filed/build.rs +++ b/filed/build.rs @@ -17,6 +17,14 @@ fn extfilter(valid: String, x: Option<&OsStr>) -> bool { ext == valid } +fn system(cmd: &str, args: &[&str]) -> String { + let out = Command::new(cmd) + .args(args) + .output() + .unwrap(); + String::from_utf8(out.stdout).unwrap() +} + fn main() { println!("cargo:rerun-if-changed=static/assets"); @@ -60,11 +68,7 @@ fn main() { .unwrap(); }); - let commit = Command::new("git") - .args(&["rev-parse", "HEAD"]) - .output() - .unwrap(); + let commit = system("git", &["rev-parse", "HEAD"]); - let commit = String::from_utf8(commit.stdout).unwrap(); println!("cargo:rustc-env=COMMIT_HASH={commit}"); } \ No newline at end of file -- 2.40.1 From 8d5d7395689af3ffdd6d699f5bd2d40bd9ec05c8 Mon Sep 17 00:00:00 2001 From: blek Date: Sun, 29 Oct 2023 19:14:58 +1000 Subject: [PATCH 3/6] include branch data --- filed/build.rs | 4 +++- filed/src/env.rs | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/filed/build.rs b/filed/build.rs index fde6fbd..dddb33f 100644 --- a/filed/build.rs +++ b/filed/build.rs @@ -69,6 +69,8 @@ fn main() { }); let commit = system("git", &["rev-parse", "HEAD"]); - + let branch = system("git", &["rev-parse", "--abbrev-rev", "HEAD"]); + println!("cargo:rustc-env=COMMIT_HASH={commit}"); + println!("cargo:rustc-env=COMMIT_BRANCH={branch}"); } \ No newline at end of file diff --git a/filed/src/env.rs b/filed/src/env.rs index ecd295f..4f43568 100644 --- a/filed/src/env.rs +++ b/filed/src/env.rs @@ -17,12 +17,15 @@ pub struct Redis { #[derive(Debug, Clone)] pub struct VersionData { - pub commit: String + pub commit: String, + pub branch: String } + impl Default for VersionData { fn default() -> Self { VersionData { - commit: env!("COMMIT_HASH").to_string() + commit: env!("COMMIT_HASH").to_string(), + branch: env!("COMMIT_BRANCH").to_string() } } } -- 2.40.1 From 32375127a96627b111708f48c13288159f29023c Mon Sep 17 00:00:00 2001 From: blek Date: Sun, 29 Oct 2023 19:25:33 +1000 Subject: [PATCH 4/6] display errors --- filed/build.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/filed/build.rs b/filed/build.rs index dddb33f..f067b4f 100644 --- a/filed/build.rs +++ b/filed/build.rs @@ -22,6 +22,11 @@ fn system(cmd: &str, args: &[&str]) -> String { .args(args) .output() .unwrap(); + + if out.stderr.len() != 0 { + panic!("Got this while running {cmd} with \"{}\": {}", args.join(" "), String::from_utf8(out.stderr).unwrap()) + } + String::from_utf8(out.stdout).unwrap() } -- 2.40.1 From 99807b9722501c9c171e4436f13247da95a45305 Mon Sep 17 00:00:00 2001 From: blek Date: Sun, 29 Oct 2023 19:25:53 +1000 Subject: [PATCH 5/6] fix few obscure errors --- docker-compose.dev.yml | 1 + filed/build.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 84557c8..774aea5 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -7,6 +7,7 @@ services: networks: bfile: volumes: + - './.git:/opt/code/.git' - './filed:/opt/code' - './filed/config:/etc/filed' - '/opt/code/target' diff --git a/filed/build.rs b/filed/build.rs index f067b4f..04b9494 100644 --- a/filed/build.rs +++ b/filed/build.rs @@ -74,7 +74,7 @@ fn main() { }); let commit = system("git", &["rev-parse", "HEAD"]); - let branch = system("git", &["rev-parse", "--abbrev-rev", "HEAD"]); + let branch = system("git", &["rev-parse", "--abbrev-ref", "HEAD"]); println!("cargo:rustc-env=COMMIT_HASH={commit}"); println!("cargo:rustc-env=COMMIT_BRANCH={branch}"); -- 2.40.1 From 12614078ad78df0646fb6a3913db2d5b16edca32 Mon Sep 17 00:00:00 2001 From: blek Date: Sun, 29 Oct 2023 19:44:31 +1000 Subject: [PATCH 6/6] print version at the footer --- filed/src/env.rs | 2 ++ filed/templates/base.html | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/filed/src/env.rs b/filed/src/env.rs index 4f43568..46aac2e 100644 --- a/filed/src/env.rs +++ b/filed/src/env.rs @@ -18,6 +18,7 @@ pub struct Redis { #[derive(Debug, Clone)] pub struct VersionData { pub commit: String, + pub short_commit: String, pub branch: String } @@ -25,6 +26,7 @@ impl Default for VersionData { fn default() -> Self { VersionData { commit: env!("COMMIT_HASH").to_string(), + short_commit: env!("COMMIT_HASH").to_string().chars().take(6).collect(), branch: env!("COMMIT_BRANCH").to_string() } } diff --git a/filed/templates/base.html b/filed/templates/base.html index 761bbb5..4e23bf8 100644 --- a/filed/templates/base.html +++ b/filed/templates/base.html @@ -66,6 +66,13 @@ Made with Rust and <3 + + + Version + + {{ env!("CARGO_PKG_VERSION") }} ({{ env.version.branch -}}/{{- env.version.short_commit }}) + +
  • -- 2.40.1