build.rs (738B)
1 use std::process::Command; 2 3 fn fallback() { 4 if let Some(dirname) = std::env::current_dir() 5 .as_ref() 6 .ok() 7 .and_then(|cwd| cwd.file_name().and_then(|fname| fname.to_str())) 8 { 9 println!("cargo:rustc-env=GIT_COMMIT_HASH={}", dirname); 10 } else { 11 println!("cargo:rustc-env=GIT_COMMIT_HASH=unknown"); 12 } 13 } 14 15 fn main() { 16 let output = if let Ok(output) = Command::new("git").args(["rev-parse", "HEAD"]).output() { 17 output 18 } else { 19 fallback(); 20 return; 21 }; 22 23 if output.status.success() { 24 let hash = String::from_utf8_lossy(&output.stdout); 25 println!("cargo:rustc-env=GIT_COMMIT_HASH={}", hash.trim()); 26 } else { 27 fallback(); 28 } 29 }