Fix panic when building without git (#6289)

Signed-off-by: nibon7 <nibon7@163.com>

Signed-off-by: nibon7 <nibon7@163.com>
This commit is contained in:
nibon7 2022-08-10 23:31:12 +08:00 committed by GitHub
parent 0f10d984c3
commit 45dd7d8770
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,16 +3,18 @@ use std::process::Command;
fn main() -> shadow_rs::SdResult<()> { fn main() -> shadow_rs::SdResult<()> {
// Look up the current Git commit ourselves instead of relying on shadow_rs, // 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) // 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); println!("cargo:rustc-env=NU_COMMIT_HASH={}", hash);
shadow_rs::new() shadow_rs::new()
} }
fn get_git_hash() -> Result<String, std::io::Error> { fn get_git_hash() -> Option<String> {
let out = Command::new("git").args(["rev-parse", "HEAD"]).output()?; Command::new("git")
Ok(String::from_utf8(out.stdout) .args(["rev-parse", "HEAD"])
.expect("could not convert stdout to string") .output()
.trim() .ok()
.to_string()) .filter(|output| output.status.success())
.and_then(|output| String::from_utf8(output.stdout).ok())
.map(|hash| hash.trim().to_string())
} }