build: Use `git describe` to generate a version

Only if `RADICLE_VERSION` is not set, we ask Git to desribe the commit
we are building. This is better than having all these cases just be
"pre-release", which is not very informative.
This commit is contained in:
Lorenz Leutgeb 2025-10-14 15:49:05 +02:00 committed by Fintan Halpenny
parent 384c506489
commit 532e5a0de0
1 changed files with 37 additions and 3 deletions

View File

@ -21,10 +21,44 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.unwrap_or("unknown".into())
});
let version = if let Ok(version) = env::var("RADICLE_VERSION") {
version
let version = env::var("RADICLE_VERSION").unwrap_or_else(|_| {
// If `RADICLE_VERSION` is not set, we still try our best to
// describe this version by asking git. The result will in many
// cases be a reference to the last released version, and how
// many commits we are ahead, plus a short version of the
// object ID of `HEAD`, e.g. `releases/x.y.z-80-gefe10f95be-dirty`
// which would mean that we built 80 commits ahead of release
// x.y.z, with efe10f95be being a unique prefix of the OID of
// `HEAD`, and the working directory was dirty.
// If this is a build pointing to a commit that has release tag, this
// will just return the tag name itelf, e.g. `releases/x.y.z`.
// If all fails, we just use `hash`, which, in the worst case is
// still "unknown" (see above) but in most cases will just be
// the short OID of `HEAD`.
Command::new("git")
.arg("describe")
.arg("--always")
.arg("--broken")
.arg("--dirty")
.output()
.ok()
.and_then(|output| {
if output.status.success() {
String::from_utf8(output.stdout).ok()
} else {
"pre-release".to_owned()
None
}
})
.unwrap_or(hash.clone())
});
// Since in the previous step we are likely to almost always end up with
// a prefix of `releases/`, as this is the scheme we use in this
// repository, we remove this common prefix, to get nice version numbers.
let version = if let Some(stripped) = version.strip_prefix("releases/") {
stripped.to_owned()
} else {
version
};
// Set a build-time `SOURCE_DATE_EPOCH` env var which includes the commit time.