From 457c40bffe718cadaf03c099706f3304220afbd0 Mon Sep 17 00:00:00 2001 From: pep Date: Thu, 8 Jan 2026 23:16:01 +0100 Subject: [PATCH] Add build script to make git version available in code Signed-off-by: pep --- Cargo.toml | 3 +++ build.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/config.rs | 13 +++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index 288bc61..3073801 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,9 @@ hex = "0.4" camino = { version = "1.2", features = ["serde1"] } chrono = { version = "0.4", default-features = false } +[build-dependencies] +git2 = "0.20" + [patch.crates-io] forgejo-hooks = { path = "forgejo-hooks" } diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..da0edcf --- /dev/null +++ b/build.rs @@ -0,0 +1,41 @@ +// Copyright (C) 2026-2099 The crate authors. +// +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU Affero General Public License as published by the +// Free Software Foundation, either version 3 of the License, or (at your +// option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License +// for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use std::env; +use std::fs; +use std::path::Path; + +use git2::Repository; + +fn main() { + let oid = { + match Repository::open(".") { + Ok(repo) => { + let head = repo.head().unwrap(); + format!("{}", repo.refname_to_id(head.name().unwrap()).unwrap()) + } + Err(e) => { + println!("cargo::warning={}", e); + String::new() + } + } + }; + + let out_dir = env::var_os("OUT_DIR").unwrap(); + let dest_path = Path::new(&out_dir).join("GIT_COMMIT"); + fs::write(&dest_path, oid).unwrap(); + + println!("cargo::rerun-if-changed=build.rs"); +} diff --git a/src/config.rs b/src/config.rs index 487597c..f5b7f5c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -23,6 +23,14 @@ use xmpp::jid::{BareJid, ResourcePart}; use crate::error::Error; +const MAYBE_VERSION: &'static str = include_str!(concat!(env!("OUT_DIR"), "/GIT_COMMIT")); +const fn version() -> Option<&'static str> { + match MAYBE_VERSION { + v if v.is_empty() => None, + v => Some(v), + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Config { /// Accounts trusted for admin tasks @@ -50,6 +58,11 @@ pub struct Config { /// HTTP Webhook listening address and port, e.g., 127.0.0.1:1234 or [::1]:1234 #[serde(default = "Config::default_addr")] pub addr: SocketAddr, + + /// Software version + #[serde(skip)] + #[serde(default = "version")] + pub version: Option<&'static str>, } impl Config {