From 0dfc4907215880050260b0ca1d3641a957189f09 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Mon, 10 Jun 2024 19:27:53 +0200 Subject: [PATCH] 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. --- build.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 3ee336fc0..e1ef19a5d 100644 --- a/build.rs +++ b/build.rs @@ -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() }