Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
build: Use `git describe` to generate a version
Merged lorenz opened 6 months ago

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.

1 file changed +37 -3 384c5064 532e5a0d
modified build.rs
@@ -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 {
+
                    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 {
-
        "pre-release".to_owned()
+
        version
    };

    // Set a build-time `SOURCE_DATE_EPOCH` env var which includes the commit time.