From cb63ecee3d07fca77513c6d74e64aed79ad263db Mon Sep 17 00:00:00 2001 From: Jonas Maier <> Date: Sat, 23 May 2026 21:38:05 +0200 Subject: can now build without git command or .git directory --- build.rs | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'build.rs') diff --git a/build.rs b/build.rs index 63677f7..78068b8 100644 --- a/build.rs +++ b/build.rs @@ -1,19 +1,19 @@ use std::process::Command; -fn git(args: &[&str]) -> String { +fn git(args: &[&str]) -> Option { let output = Command::new("git") .args(args) .output() - .expect("failed to execute git"); + .ok()?; if !output.status.success() { - panic!("git command failed: git {}", args.join(" ")); + return None; } - String::from_utf8(output.stdout) + Some(String::from_utf8(output.stdout) .expect("git output not utf8") .trim() - .to_string() + .to_string()) } fn main() { @@ -21,21 +21,22 @@ fn main() { println!("cargo:rerun-if-changed=.git/HEAD"); println!("cargo:rerun-if-changed=.git/refs"); - let git_hash = git(&["rev-parse", "HEAD"]); - let git_hash_short = git(&["rev-parse", "--short", "HEAD"]); + if let Some(git_hash) = git(&["rev-parse", "HEAD"]) { + println!("cargo:rustc-env=GIT_HASH={git_hash}"); + } - let git_dirty = { - let status = Command::new("git") - .args(["diff", "--quiet"]) - .status() - .expect("failed to check git dirty state"); + if let Some(git_hash_short) = git(&["rev-parse", "--short", "HEAD"]) { + println!("cargo:rustc-env=GIT_HASH_SHORT={git_hash_short}"); + } - !status.success() - }; + let git_dirty = Command::new("git") + .args(["diff-index", "--quiet", "HEAD", "--"]) + .status() + .map(|s| !s.success()) + .unwrap_or(false); - println!("cargo:rustc-env=GIT_HASH={git_hash}"); - println!("cargo:rustc-env=GIT_HASH_SHORT={git_hash_short}"); if git_dirty { println!("cargo:rustc-env=GIT_DIRTY=1"); } } + -- cgit v1.2.3