build.rs: Use Cargo_PKG_VERSION if no version could be found

`cargo build --git` clones a git repo without any tags, so you get a
version like

```
fish, version f3fc743fc
```

which is *just* the commit hash and missing the "3.7.1-NUM-g" part.

So, if we hit that case (detected because it has no ".", under the
assumption that we'll never make a version that's just "4" instead of
"4.0"), we prepend the version from Cargo.toml.
This commit is contained in:
Fabian Boehm 2024-06-10 19:27:53 +02:00
parent de13e6f9af
commit 0dfc490721

View file

@ -268,9 +268,22 @@ fn get_version(src_dir: &Path) -> String {
if let Ok(output) = Command::new("git").args(args).output() {
let rev = String::from_utf8_lossy(&output.stdout).trim().to_string();
if !rev.is_empty() {
return rev;
// If it contains a ".", we have a proper version like "3.7",
// or "23.2.1-1234-gfab1234"
if rev.contains(".") {
return rev;
}
// If it doesn't, we probably got *just* the commit SHA,
// like "f1242abcdef".
// So we prepend the crate version so it at least looks like
// "3.8-gf1242abcdef"
// This lacks the commit *distance*, but that can't be helped without
// tags.
let version = env!("CARGO_PKG_VERSION").to_owned();
return version + "-g" + &rev;
}
}
// TODO: Do we just use the cargo version here?
"unknown".to_string()
}