Skip to content

Commit

Permalink
version: Add versioning for github tarballs
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
cfergeau committed Nov 23, 2023
1 parent 2ad4228 commit 1e1e8e9
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/cmdline/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1e1e8e9

Please sign in to comment.