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.

However, we can make use of $Format$ and .gitattributes as described in
[1] 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] 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 Jan 23, 2024
1 parent 90e37b1 commit 206bfd4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/pkg/cmdline/version.go export-subst
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 206bfd4

Please sign in to comment.