1 //! Just embed git-hash to `--version`
3 use std::{env, path::PathBuf, process::Command};
9 env::var("RUST_ANALYZER_REV").ok().or_else(rev).unwrap_or_else(|| "???????".to_string());
10 println!("cargo:rustc-env=REV={}", rev)
14 println!("cargo:rerun-if-env-changed=RUST_ANALYZER_REV");
16 let mut manifest_dir = PathBuf::from(
17 env::var("CARGO_MANIFEST_DIR").expect("`CARGO_MANIFEST_DIR` is always set by cargo."),
20 while manifest_dir.parent().is_some() {
21 if manifest_dir.join(".git/HEAD").exists() {
22 let git_dir = manifest_dir.join(".git");
24 println!("cargo:rerun-if-changed={}", git_dir.join("HEAD").display());
27 Command::new("git").args(&["rev-parse", "--symbolic-full-name", "HEAD"]).output()
29 if let Ok(ref_link) = String::from_utf8(output.stdout) {
30 println!("cargo:rerun-if-changed={}", git_dir.join(ref_link).display());
38 println!("cargo:warning=Could not find `.git/HEAD` from manifest dir!");
41 fn rev() -> Option<String> {
42 let output = Command::new("git").args(&["rev-parse", "HEAD"]).output().ok()?;
43 let stdout = String::from_utf8(output.stdout).ok()?;
44 let short_hash = stdout.get(0..7)?;
45 Some(short_hash.to_owned())