From 8c1df01ecea5b684dd420bcd5b4e4e25e56c3af8 Mon Sep 17 00:00:00 2001 From: Saya Andy Date: Thu, 30 Jul 2026 12:22:56 +0700 Subject: gen-version: report the version as semver with the commit distance `git describe` produces "v1.3.1-2-gfbb7", which is neither a Semantic Version nor especially readable. Derive the version from the most recent release tag and the number of commits made since it instead, giving "v1.3.1+7", and plain "v1.3.1" when building the tag itself. That is a valid Semantic Version 2.0.0 with the commit distance as build metadata. Note that build metadata is excluded from precedence, so "v1.3.1+7" and "v1.3.1" compare equal; the distance identifies a build rather than ordering it. Tarball builds keep falling back to the version in the Makefile, and release tags are expected to be named vMAJOR.MINOR.PATCH for the result to stay well-formed. Signed-off-by: Saya Andy --- gen-version.sh | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/gen-version.sh b/gen-version.sh index 80cf49a..b493cf5 100755 --- a/gen-version.sh +++ b/gen-version.sh @@ -3,10 +3,24 @@ # Get version-info specified in Makefile V=$1 -# Use `git describe` to get current version if we're inside a git repo +# When building from a git repository, derive the version from the most recent +# release tag instead. The result follows Semantic Versioning 2.0.0, using the +# number of commits made since that tag as build metadata, so seven commits +# after the v1.3.1 tag gives "v1.3.1+7", while a build of the tag itself gives +# plain "v1.3.1". Release tags are expected to be named vMAJOR.MINOR.PATCH. if test "$(git rev-parse --git-dir 2>/dev/null)" = '.git' then - V=$(git describe --abbrev=4 HEAD 2>/dev/null) + tag=$(git describe --abbrev=0 --match 'v[0-9]*' HEAD 2>/dev/null) + if test -n "$tag" + then + ahead=$(git rev-list --count "$tag..HEAD" 2>/dev/null) + if test -n "$ahead" && test "$ahead" -gt 0 + then + V="$tag+$ahead" + else + V="$tag" + fi + fi fi new="CGIT_VERSION = $V" -- cgit v1.3.1+13