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

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");
}