From 1e1e8e949d4fce61b849dad6dcdee53c29b2e99e Mon Sep 17 00:00:00 2001 From: Christophe Fergeau Date: Thu, 23 Nov 2023 15:16:06 +0100 Subject: [PATCH] version: Add versioning for github tarballs When building from github tarballs, getting the version from `git describe` won't work, or worse, will be irrelevant [1] However, we can make use of $Format$ and .gitattributes as described in [2] to automatically substitute the correct version in a go variable when `git archive` is used. In particular, the correct version number will automatically be substituted when GitHub creates release tarballs. [1] rpms/gvisor-tap-vsock.spec uses `%autosetup -Sgit -n %{name}-%{version}` which unpacks the release tarball in a newly created git repository. [2] https://icinga.com/blog/2022/05/25/embedding-git-commit-information-in-go-binaries/ Signed-off-by: Christophe Fergeau --- pkg/cmdline/version.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/cmdline/version.go b/pkg/cmdline/version.go index 80035d02..d2801966 100644 --- a/pkg/cmdline/version.go +++ b/pkg/cmdline/version.go @@ -2,13 +2,23 @@ package cmdline import ( "runtime/debug" + "strings" ) -// set using the '-X github.com/crc-org/vfkit/pkg/cmdline.gitVersion' linker flag -var gitVersion = "unknown" +var ( + // set using the '-X github.com/crc-org/vfkit/pkg/cmdline.gitVersion' linker flag + gitVersion = "unknown" + + // set through .gitattributes when `git archive` is used + // see https://icinga.com/blog/2022/05/25/embedding-git-commit-information-in-go-binaries/ + gitArchiveVersion = "$Format:%(describe)$" +) func Version() string { switch { + // This will be substituted when building from a GitHub tarball + case !strings.HasPrefix(gitArchiveVersion, "$Format:"): + return gitArchiveVersion // This will be set when building from git using make case gitVersion != "": return gitVersion