summaryrefslogtreecommitdiff
path: root/build.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-01-03 21:05:57 +0800
committer魏曹先生 <1992414357@qq.com>2026-01-03 21:05:57 +0800
commita6a3f42c7be2560d78c832161493574a2a2b4d16 (patch)
tree384f9440eccb49b57aacdc01b0f1884168fc45c6 /build.rs
parent00764a6dcc6d99b73c47f92e9c260be414beaa63 (diff)
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.
Diffstat (limited to 'build.rs')
-rw-r--r--build.rs48
1 files changed, 47 insertions, 1 deletions
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<dyn std::error::
let platform = get_platform(&target);
let toolchain = get_toolchain();
let version = get_version();
+ let branch = get_git_branch().unwrap_or_else(|_| "unknown".to_string());
+ let commit = get_git_commit().unwrap_or_else(|_| "unknown".to_string());
let generated_code = template_code
.replace("{date}", &date)
.replace("{target}", &target)
.replace("{platform}", &platform)
.replace("{toolchain}", &toolchain)
- .replace("{version}", &version);
+ .replace("{version}", &version)
+ .replace("{branch}", &branch)
+ .replace("{commit}", &commit);
// Write the generated code
let compile_info_path = repo_root.join(COMPILE_INFO_RS);
@@ -99,3 +106,42 @@ fn get_version() -> String {
"unknown".to_string()
}
+
+/// Get current git branch
+fn get_git_branch() -> Result<String, Box<dyn std::error::Error>> {
+ 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<String, Box<dyn std::error::Error>> {
+ 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())
+}