Add build script to make git version available in code

Signed-off-by: pep <pep@bouah.net>
This commit is contained in:
pep 2026-01-08 23:16:01 +01:00
commit 457c40bffe
3 changed files with 57 additions and 0 deletions

View file

@ -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" }

41
build.rs Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
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");
}

View file

@ -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 {