From a6a3f42c7be2560d78c832161493574a2a2b4d16 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sat, 3 Jan 2026 21:05:57 +0800 Subject: Add git branch and commit to compile info Include build_branch and build_commit fields in CoreCompileInfo struct. The build script now extracts current git branch and commit hash, falling back to "unknown" if git commands fail. --- build.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'build.rs') diff --git a/build.rs b/build.rs index 04fcd71..6874429 100644 --- a/build.rs +++ b/build.rs @@ -1,10 +1,13 @@ use std::env; use std::path::PathBuf; +use std::process::Command; const COMPILE_INFO_RS: &str = "./src/data/compile_info.rs"; const COMPILE_INFO_RS_TEMPLATE: &str = "./src/data/compile_info.rs.template"; fn main() { + println!("cargo:rerun-if-env-changed=FORCE_BUILD"); + let repo_root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); if let Err(e) = generate_compile_info(&repo_root) { @@ -23,13 +26,17 @@ fn generate_compile_info(repo_root: &PathBuf) -> Result<(), Box String { "unknown".to_string() } + +/// Get current git branch +fn get_git_branch() -> Result> { + let output = Command::new("git") + .args(["branch", "--show-current"]) + .output()?; + + if output.status.success() { + let branch = String::from_utf8(output.stdout)?.trim().to_string(); + + if branch.is_empty() { + // Try to get HEAD reference if no branch (detached HEAD) + let output = Command::new("git") + .args(["rev-parse", "--abbrev-ref", "HEAD"]) + .output()?; + + if output.status.success() { + let head_ref = String::from_utf8(output.stdout)?.trim().to_string(); + return Ok(head_ref); + } + } else { + return Ok(branch); + } + } + + Err("Failed to get git branch".into()) +} + +/// Get current git commit hash +fn get_git_commit() -> Result> { + let output = Command::new("git").args(["rev-parse", "HEAD"]).output()?; + + if output.status.success() { + let commit = String::from_utf8(output.stdout)?.trim().to_string(); + return Ok(commit); + } + + Err("Failed to get git commit".into()) +} -- cgit