From 45dd7d87708eb1405f68cbab7c36f88105950d1a Mon Sep 17 00:00:00 2001 From: nibon7 Date: Wed, 10 Aug 2022 23:31:12 +0800 Subject: [PATCH] Fix panic when building without git (#6289) Signed-off-by: nibon7 Signed-off-by: nibon7 --- crates/nu-command/build.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/crates/nu-command/build.rs b/crates/nu-command/build.rs index 67343c24b8..7ad06cc6f1 100644 --- a/crates/nu-command/build.rs +++ b/crates/nu-command/build.rs @@ -3,16 +3,18 @@ use std::process::Command; fn main() -> shadow_rs::SdResult<()> { // Look up the current Git commit ourselves instead of relying on shadow_rs, // because shadow_rs does it in a really slow-to-compile way (it builds libgit2) - let hash = get_git_hash().expect("failed to get latest git commit hash"); + let hash = get_git_hash().unwrap_or_default(); println!("cargo:rustc-env=NU_COMMIT_HASH={}", hash); shadow_rs::new() } -fn get_git_hash() -> Result { - let out = Command::new("git").args(["rev-parse", "HEAD"]).output()?; - Ok(String::from_utf8(out.stdout) - .expect("could not convert stdout to string") - .trim() - .to_string()) +fn get_git_hash() -> Option { + Command::new("git") + .args(["rev-parse", "HEAD"]) + .output() + .ok() + .filter(|output| output.status.success()) + .and_then(|output| String::from_utf8(output.stdout).ok()) + .map(|hash| hash.trim().to_string()) }