diff --git a/src/langserver/build.rs b/src/langserver/build.rs index fbc67b76..91478f6e 100644 --- a/src/langserver/build.rs +++ b/src/langserver/build.rs @@ -11,8 +11,9 @@ fn main() { let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); let mut f = File::create(&out_dir.join("build-info.txt")).unwrap(); - if let Ok(commit) = read_commit() { - writeln!(f, "commit: {}", commit).unwrap(); + match read_commit() { + Ok(commit) => writeln!(f, "commit: {}", commit).unwrap(), + Err(err) => println!("cargo:warning=Failed to fetch commit info: {}", err) } writeln!(f, "build date: {}", chrono::Utc::today()).unwrap(); @@ -31,6 +32,31 @@ fn main() { fn read_commit() -> Result { let repo = git2::Repository::discover(".")?; - let hash = repo.head()?.peel_to_commit()?.id().to_string(); - Ok(hash) + let head = repo.head()?.peel_to_commit()?.id(); + + let mut all_tags = Vec::new(); + repo.tag_foreach(|oid, _| { all_tags.push(oid); true })?; + + let mut best = None; + for tag_id in all_tags { + let tag_commit = repo.find_tag(tag_id)?.as_object().peel_to_commit()?.id(); + let (ahead, behind) = repo.graph_ahead_behind(head, tag_commit)?; + if behind == 0 { + match best { + None => best = Some(ahead), + Some(prev) if ahead < prev => best = Some(ahead), + _ => {} + } + } + if ahead == 0 { + break; + } + } + + match best { + None | Some(0) => {} + Some(ahead) => println!("cargo:rustc-env=CARGO_PKG_VERSION={}+{}", std::env::var("CARGO_PKG_VERSION").unwrap(), ahead), + } + + Ok(head.to_string()) }