From c96b94aa3a00a3c662005c54c8ea7b1f5f9e6354 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Wed, 18 Feb 2026 08:07:14 +0000 Subject: chore(build): Replace vergen with git-based build script --- build.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) (limited to 'build.rs') diff --git a/build.rs b/build.rs index 2b8c3be..349f946 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,51 @@ -fn main() -> Result<(), Box> { - vergen::EmitBuilder::builder().git_sha(true).emit()?; +use std::{fs, path::Path, process::Command}; - Ok(()) +fn git_directory() -> Option { + let directory = Path::new(".git"); + + if directory.is_dir() { + return Some(directory.to_path_buf()); + } + + let content = fs::read_to_string(directory).ok()?; + let path = Path::new(content.trim().strip_prefix("gitdir: ")?); + + if path.is_absolute() { + Some(path.to_path_buf()) + } else { + Some(directory.parent()?.join(path)) + } +} + +fn git_sha() -> Option { + let output = Command::new("git").args(["rev-parse", "HEAD"]).output().ok()?; + + if !output.status.success() { + return None; + } + + let sha = String::from_utf8(output.stdout).ok()?.trim().to_string(); + + if sha.is_empty() { None } else { Some(sha) } +} + +fn main() { + if let Some(git_dir) = git_directory() { + let head_path = git_dir.join("HEAD"); + + println!("cargo:rerun-if-changed={}", head_path.display()); + + if let Ok(head_contents) = fs::read_to_string(&head_path) { + if let Some(reference) = head_contents.trim().strip_prefix("ref: ") { + println!( + "cargo:rerun-if-changed={}", + git_dir.join(reference).display() + ); + } + } + } + + let sha = git_sha().unwrap_or_else(|| "UNKNOWN".to_string()); + + println!("cargo:rustc-env=VERGEN_GIT_SHA={sha}"); } -- cgit v1.2.3