Use revision etc. from debug.BuildInfo

Fixes #9680
This commit is contained in:
Bjørn Erik Pedersen 2022-03-17 09:27:11 +01:00
parent 004bec2e9a
commit 64afb7ca51
5 changed files with 82 additions and 22 deletions

View file

@ -21,6 +21,7 @@ import (
"runtime/debug" "runtime/debug"
"sort" "sort"
"strings" "strings"
"sync"
"time" "time"
"github.com/gohugoio/hugo/hugofs/files" "github.com/gohugoio/hugo/hugofs/files"
@ -37,13 +38,6 @@ const (
) )
var ( var (
// commitHash contains the current Git revision.
// Use mage to build to make sure this gets set.
commitHash string
// buildDate contains the date of the current build.
buildDate string
// vendorInfo contains vendor notes about the current build. // vendorInfo contains vendor notes about the current build.
vendorInfo string vendorInfo string
) )
@ -90,6 +84,17 @@ func NewInfo(environment string, deps []*Dependency) Info {
if environment == "" { if environment == "" {
environment = EnvironmentProduction environment = EnvironmentProduction
} }
var (
commitHash string
buildDate string
)
bi := getBuildInfo()
if bi != nil {
commitHash = bi.Revision
buildDate = bi.RevisionTime
}
return Info{ return Info{
CommitHash: commitHash, CommitHash: commitHash,
BuildDate: buildDate, BuildDate: buildDate,
@ -125,6 +130,52 @@ func GetExecEnviron(workDir string, cfg config.Provider, fs afero.Fs) []string {
return env return env
} }
type buildInfo struct {
VersionControlSystem string
Revision string
RevisionTime string
Modified bool
GoOS string
GoArch string
*debug.BuildInfo
}
var bInfo *buildInfo
var bInfoInit sync.Once
func getBuildInfo() *buildInfo {
bInfoInit.Do(func() {
bi, ok := debug.ReadBuildInfo()
if !ok {
return
}
bInfo = &buildInfo{BuildInfo: bi}
for _, s := range bInfo.Settings {
switch s.Key {
case "vcs":
bInfo.VersionControlSystem = s.Value
case "vcs.revision":
bInfo.Revision = s.Value
case "vcs.time":
bInfo.RevisionTime = s.Value
case "vcs.modified":
bInfo.Modified = s.Value == "true"
case "GOOS":
bInfo.GoOS = s.Value
case "GOARCH":
bInfo.GoArch = s.Value
}
}
})
return bInfo
}
// GetDependencyList returns a sorted dependency list on the format package="version". // GetDependencyList returns a sorted dependency list on the format package="version".
// It includes both Go dependencies and (a manually maintained) list of C(++) dependencies. // It includes both Go dependencies and (a manually maintained) list of C(++) dependencies.
func GetDependencyList() []string { func GetDependencyList() []string {
@ -143,8 +194,8 @@ func GetDependencyList() []string {
) )
} }
bi, ok := debug.ReadBuildInfo() bi := getBuildInfo()
if !ok { if bi == nil {
return deps return deps
} }

View file

@ -27,8 +27,12 @@ func TestHugoInfo(t *testing.T) {
c.Assert(hugoInfo.Version(), qt.Equals, CurrentVersion.Version()) c.Assert(hugoInfo.Version(), qt.Equals, CurrentVersion.Version())
c.Assert(fmt.Sprintf("%T", VersionString("")), qt.Equals, fmt.Sprintf("%T", hugoInfo.Version())) c.Assert(fmt.Sprintf("%T", VersionString("")), qt.Equals, fmt.Sprintf("%T", hugoInfo.Version()))
c.Assert(hugoInfo.CommitHash, qt.Equals, commitHash)
c.Assert(hugoInfo.BuildDate, qt.Equals, buildDate) bi := getBuildInfo()
if bi != nil {
c.Assert(hugoInfo.CommitHash, qt.Equals, bi.Revision)
c.Assert(hugoInfo.BuildDate, qt.Equals, bi.RevisionTime)
}
c.Assert(hugoInfo.Environment, qt.Equals, "production") c.Assert(hugoInfo.Environment, qt.Equals, "production")
c.Assert(string(hugoInfo.Generator()), qt.Contains, fmt.Sprintf("Hugo %s", hugoInfo.Version())) c.Assert(string(hugoInfo.Generator()), qt.Contains, fmt.Sprintf("Hugo %s", hugoInfo.Version()))
c.Assert(hugoInfo.IsProduction(), qt.Equals, true) c.Assert(hugoInfo.IsProduction(), qt.Equals, true)

View file

@ -131,16 +131,21 @@ func BuildVersionString() string {
program := "hugo" program := "hugo"
version := "v" + CurrentVersion.String() version := "v" + CurrentVersion.String()
if commitHash != "" {
version += "-" + strings.ToUpper(commitHash) bi := getBuildInfo()
if bi == nil {
return version
}
if bi.Revision != "" {
version += "-" + bi.Revision
} }
if IsExtended { if IsExtended {
version += "+extended" version += "+extended"
} }
osArch := runtime.GOOS + "/" + runtime.GOARCH osArch := bi.GoOS + "/" + bi.GoArch
date := buildDate date := bi.RevisionTime
if date == "" { if date == "" {
date = "unknown" date = "unknown"
} }

View file

@ -9,7 +9,7 @@ builds:
- -
binary: hugo binary: hugo
id: hugo id: hugo
ldflags: -s -w -X github.com/gohugoio/hugo/common/hugo.buildDate={{.Date}} -X github.com/gohugoio/hugo/common/hugo.commitHash={{ .ShortCommit }} -X github.com/gohugoio/hugo/common/hugo.vendorInfo=gohugoio ldflags: -s -w -X github.com/gohugoio/hugo/common/hugo.vendorInfo=gohugoio
env: env:
- CGO_ENABLED=0 - CGO_ENABLED=0
flags: flags:
@ -32,7 +32,7 @@ builds:
- -
binary: hugo binary: hugo
id: hugo_unix id: hugo_unix
ldflags: -s -w -X github.com/gohugoio/hugo/common/hugo.buildDate={{.Date}} -X github.com/gohugoio/hugo/common/hugo.commitHash={{ .ShortCommit }} -X github.com/gohugoio/hugo/common/hugo.vendorInfo=gohugoio ldflags: -s -w -X github.com/gohugoio/hugo/common/hugo.vendorInfo=gohugoio
env: env:
- CGO_ENABLED=0 - CGO_ENABLED=0
flags: flags:
@ -49,7 +49,7 @@ builds:
binary: hugo binary: hugo
id: hugo_extended_windows id: hugo_extended_windows
ldflags: ldflags:
- -s -w -X github.com/gohugoio/hugo/common/hugo.buildDate={{.Date}} -X github.com/gohugoio/hugo/common/hugo.commitHash={{ .ShortCommit }} -X github.com/gohugoio/hugo/common/hugo.vendorInfo=gohugoio - -s -w -X github.com/gohugoio/hugo/common/hugo.vendorInfo=gohugoio
- "-extldflags '-static'" - "-extldflags '-static'"
env: env:
- CGO_ENABLED=1 - CGO_ENABLED=1
@ -66,7 +66,7 @@ builds:
- amd64 - amd64
- binary: hugo - binary: hugo
id: hugo_extended_darwin id: hugo_extended_darwin
ldflags: -s -w -X github.com/gohugoio/hugo/common/hugo.buildDate={{.Date}} -X github.com/gohugoio/hugo/common/hugo.commitHash={{ .ShortCommit }} -X github.com/gohugoio/hugo/common/hugo.vendorInfo=gohugoio ldflags: -s -w -X github.com/gohugoio/hugo/common/hugo.vendorInfo=gohugoio
env: env:
- CGO_ENABLED=1 - CGO_ENABLED=1
- CC=o64-clang - CC=o64-clang
@ -83,7 +83,7 @@ builds:
- arm64 - arm64
- binary: hugo - binary: hugo
id: hugo_extended_linux id: hugo_extended_linux
ldflags: -s -w -X github.com/gohugoio/hugo/common/hugo.buildDate={{.Date}} -X github.com/gohugoio/hugo/common/hugo.commitHash={{ .ShortCommit }} -X github.com/gohugoio/hugo/common/hugo.vendorInfo=gohugoio ldflags: -s -w -X github.com/gohugoio/hugo/common/hugo.vendorInfo=gohugoio
env: env:
- CGO_ENABLED=1 - CGO_ENABLED=1
flags: flags:

View file

@ -25,10 +25,10 @@ import (
const ( const (
packageName = "github.com/gohugoio/hugo" packageName = "github.com/gohugoio/hugo"
noGitLdflags = "-X $PACKAGE/common/hugo.buildDate=$BUILD_DATE" noGitLdflags = "-X github.com/gohugoio/hugo/common/hugo.vendorInfo=mage"
) )
var ldflags = "-X $PACKAGE/common/hugo.commitHash=$COMMIT_HASH -X $PACKAGE/common/hugo.buildDate=$BUILD_DATE" var ldflags = noGitLdflags
// allow user to override go executable by running as GOEXE=xxx make ... on unix-like systems // allow user to override go executable by running as GOEXE=xxx make ... on unix-like systems
var goexe = "go" var goexe = "go"