aboutsummaryrefslogtreecommitdiffstats
path: root/build.rs
diff options
context:
space:
mode:
authorJonas Maier <>2026-05-23 21:38:05 +0200
committerJonas Maier <>2026-05-23 21:38:05 +0200
commitcb63ecee3d07fca77513c6d74e64aed79ad263db (patch)
tree6c9fb0b08f8744eea17b5ed36db3a8a390b754db /build.rs
parent8316ec37e248abbd25342750c1a84b059896df90 (diff)
downloadpish-cb63ecee3d07fca77513c6d74e64aed79ad263db.tar.gz
can now build without git command or .git directory
Diffstat (limited to 'build.rs')
-rw-r--r--build.rs33
1 files changed, 17 insertions, 16 deletions
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<String> {
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");
}
}
+