From 2e16a556e277035225d02d9a62c2da699235de36 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sat, 3 Jan 2026 21:05:18 +0800 Subject: Add Git branch and commit info to compile info - Include branch and commit hash in compile info display - Add build script functions to extract git metadata - Update export scripts to force rebuilds when needed - Extend share command with placeholder implementations --- build.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'build.rs') diff --git a/build.rs b/build.rs index e215418..45f04af 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()); // Only generate installer script on Windows @@ -87,13 +90,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