diff options
| author | Jonas Maier <> | 2026-05-23 21:38:05 +0200 |
|---|---|---|
| committer | Jonas Maier <> | 2026-05-23 21:38:05 +0200 |
| commit | cb63ecee3d07fca77513c6d74e64aed79ad263db (patch) | |
| tree | 6c9fb0b08f8744eea17b5ed36db3a8a390b754db | |
| parent | 8316ec37e248abbd25342750c1a84b059896df90 (diff) | |
| download | pish-cb63ecee3d07fca77513c6d74e64aed79ad263db.tar.gz | |
can now build without git command or .git directory
| -rw-r--r-- | build.rs | 33 | ||||
| -rw-r--r-- | src/consts.rs | 5 |
2 files changed, 21 insertions, 17 deletions
@@ -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"); } } + diff --git a/src/consts.rs b/src/consts.rs index 7e5d0fd..2e04183 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -1,3 +1,6 @@ pub const PISH_VERSION: &str = env!("CARGO_PKG_VERSION"); -pub const PISH_COMMIT: &str = env!("GIT_HASH_SHORT"); +pub const PISH_COMMIT: &str = match option_env!("GIT_HASH_SHORT") { + Some(hash) => hash, + None => "unknown", +}; pub const PISH_DIRTY: bool = option_env!("GIT_DIRTY").is_some(); |
