Include commits since tag in langserver version number

This commit is contained in:
Tad Hardesty 2021-05-22 16:40:32 -07:00
parent 838ddf5974
commit 64e7ffd995

View file

@ -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<String, git2::Error> {
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())
}