From 64afb7ca51ef5fd5d4a0afa121183217292daa5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 17 Mar 2022 09:27:11 +0100 Subject: Use revision etc. from debug.BuildInfo Fixes #9680 --- common/hugo/hugo.go | 69 +++++++++++++++++++++++++++++++++++++++++------- common/hugo/hugo_test.go | 8 ++++-- common/hugo/version.go | 13 ++++++--- 3 files changed, 75 insertions(+), 15 deletions(-) (limited to 'common') diff --git a/common/hugo/hugo.go b/common/hugo/hugo.go index 5bbfe7c65..2c6e5f2a0 100644 --- a/common/hugo/hugo.go +++ b/common/hugo/hugo.go @@ -21,6 +21,7 @@ import ( "runtime/debug" "sort" "strings" + "sync" "time" "github.com/gohugoio/hugo/hugofs/files" @@ -37,13 +38,6 @@ const ( ) 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 string ) @@ -90,6 +84,17 @@ func NewInfo(environment string, deps []*Dependency) Info { if environment == "" { environment = EnvironmentProduction } + var ( + commitHash string + buildDate string + ) + + bi := getBuildInfo() + if bi != nil { + commitHash = bi.Revision + buildDate = bi.RevisionTime + } + return Info{ CommitHash: commitHash, BuildDate: buildDate, @@ -125,6 +130,52 @@ func GetExecEnviron(workDir string, cfg config.Provider, fs afero.Fs) []string { 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". // It includes both Go dependencies and (a manually maintained) list of C(++) dependencies. func GetDependencyList() []string { @@ -143,8 +194,8 @@ func GetDependencyList() []string { ) } - bi, ok := debug.ReadBuildInfo() - if !ok { + bi := getBuildInfo() + if bi == nil { return deps } diff --git a/common/hugo/hugo_test.go b/common/hugo/hugo_test.go index ff36cab7c..3bc95684b 100644 --- a/common/hugo/hugo_test.go +++ b/common/hugo/hugo_test.go @@ -27,8 +27,12 @@ func TestHugoInfo(t *testing.T) { c.Assert(hugoInfo.Version(), qt.Equals, CurrentVersion.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(string(hugoInfo.Generator()), qt.Contains, fmt.Sprintf("Hugo %s", hugoInfo.Version())) c.Assert(hugoInfo.IsProduction(), qt.Equals, true) diff --git a/common/hugo/version.go b/common/hugo/version.go index 531f4483d..b085744c9 100644 --- a/common/hugo/version.go +++ b/common/hugo/version.go @@ -131,16 +131,21 @@ func BuildVersionString() string { program := "hugo" 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 { version += "+extended" } - osArch := runtime.GOOS + "/" + runtime.GOARCH + osArch := bi.GoOS + "/" + bi.GoArch - date := buildDate + date := bi.RevisionTime if date == "" { date = "unknown" } -- cgit v1.2.3