Skip to content

Commit

Permalink
pkg/buildinfo: Update version for release branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaq committed Feb 27, 2023
1 parent 6ba012a commit 66b9ec4
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 135 deletions.
57 changes: 1 addition & 56 deletions pkg/buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
"fmt"
"os"
"runtime"
"runtime/debug"
"strings"
"time"

"src.elv.sh/pkg/must"
"src.elv.sh/pkg/prog"
Expand All @@ -21,14 +18,6 @@ import (
// identifies the next release.
const VersionBase = "0.19.0"

// VCSOverride may be set during compilation to "time-commit" (e.g.
// "20220320172241-5dc8c02a32cf") for identifying the version of development
// builds.
//
// It is only needed if the automatic population of version information
// implemented in devVersion fails.
var VCSOverride string

// BuildVariant may be set during compilation to identify a particular
// build variant, such as a build by a specific distribution, with modified
// dependencies, or with a non-standard toolchain.
Expand All @@ -48,7 +37,7 @@ func (Type) IsStructMap() {}
var Value = Type{
// On a release branch, change to addVariant(VersionBase, BuildVariant) and
// remove unneeded code.
Version: addVariant(devVersion(VersionBase, VCSOverride), BuildVariant),
Version: addVariant(VersionBase, BuildVariant),
GoVersion: runtime.Version(),
}

Expand All @@ -59,50 +48,6 @@ func addVariant(version, variant string) string {
return version
}

var readBuildInfo = debug.ReadBuildInfo

func devVersion(next, vcsOverride string) string {
if vcsOverride != "" {
return next + "-dev.0." + vcsOverride
}
fallback := next + "-dev.unknown"
bi, ok := readBuildInfo()
if !ok {
return fallback
}
// If the main module's version is known, use it, but without the "v"
// prefix. This is the case when Elvish is built with "go install
// src.elv.sh/cmd/elvish@version".
if v := bi.Main.Version; v != "" && v != "(devel)" {
return strings.TrimPrefix(v, "v")
}
// If VCS information is available (i.e. when Elvish is built from a checked
// out repo), build the version string with it. Emulate the format of pseudo
// version (https://go.dev/ref/mod#pseudo-versions).
m := make(map[string]string)
for _, s := range bi.Settings {
if k := strings.TrimPrefix(s.Key, "vcs."); k != s.Key {
m[k] = s.Value
}
}
if m["revision"] == "" || m["time"] == "" || m["modified"] == "" {
return fallback
}
t, err := time.Parse(time.RFC3339Nano, m["time"])
if err != nil {
return fallback
}
revision := m["revision"]
if len(revision) > 12 {
revision = revision[:12]
}
version := fmt.Sprintf("%s-dev.0.%s-%s", next, t.Format("20060102150405"), revision)
if m["modified"] == "true" {
return version + "-dirty"
}
return version
}

// Program is the buildinfo subprogram.
type Program struct {
version, buildinfo bool
Expand Down
79 changes: 0 additions & 79 deletions pkg/buildinfo/buildinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package buildinfo
import (
"fmt"
"runtime"
"runtime/debug"
"testing"

"src.elv.sh/pkg/eval/vals"
"src.elv.sh/pkg/prog/progtest"
"src.elv.sh/pkg/testutil"
)

var ThatElvish = progtest.ThatElvish
Expand All @@ -27,83 +25,6 @@ func TestProgram(t *testing.T) {
)
}

var devVersionTests = []struct {
name string
next string
vcsOverride string
buildInfo *debug.BuildInfo
want string
}{
{
name: "no BuildInfo",
next: "0.42.0",
want: "0.42.0-dev.unknown",
},
{
name: "BuildInfo with Main.Version = (devel)",
next: "0.42.0",
buildInfo: &debug.BuildInfo{Main: debug.Module{Version: "(devel)"}},
want: "0.42.0-dev.unknown",
},
{
name: "BuildInfo with non-empty Main.Version != (devel)",
next: "0.42.0",
buildInfo: &debug.BuildInfo{Main: debug.Module{Version: "v0.42.0-dev.foobar"}},
want: "0.42.0-dev.foobar",
},
{
name: "BuildInfo with VCS data from clean checkout",
next: "0.42.0",
buildInfo: &debug.BuildInfo{Settings: []debug.BuildSetting{
{Key: "vcs.revision", Value: "1234567890123456"},
{Key: "vcs.time", Value: "2022-04-01T23:59:58Z"},
{Key: "vcs.modified", Value: "false"},
}},
want: "0.42.0-dev.0.20220401235958-123456789012",
},
{
name: "BuildInfo with VCS data from dirty checkout",
next: "0.42.0",
buildInfo: &debug.BuildInfo{Settings: []debug.BuildSetting{
{Key: "vcs.revision", Value: "1234567890123456"},
{Key: "vcs.time", Value: "2022-04-01T23:59:58Z"},
{Key: "vcs.modified", Value: "true"},
}},
want: "0.42.0-dev.0.20220401235958-123456789012-dirty",
},
{
name: "BuildInfo with unknown VCS timestamp format",
next: "0.42.0",
buildInfo: &debug.BuildInfo{Settings: []debug.BuildSetting{
{Key: "vcs.revision", Value: "1234567890123456"},
{Key: "vcs.time", Value: "April First"},
{Key: "vcs.modified", Value: "false"},
}},
want: "0.42.0-dev.unknown",
},
{
name: "vcsOverride",
next: "0.42.0",
vcsOverride: "20220401235958-123456789012",
want: "0.42.0-dev.0.20220401235958-123456789012",
},
}

func TestDevVersion(t *testing.T) {
for _, test := range devVersionTests {
t.Run(test.name, func(t *testing.T) {
testutil.Set(t, &readBuildInfo,
func() (*debug.BuildInfo, bool) {
return test.buildInfo, test.buildInfo != nil
})
got := devVersion(test.next, test.vcsOverride)
if got != test.want {
t.Errorf("got %q, want %q", got, test.want)
}
})
}
}

func TestAddVariant(t *testing.T) {
got := addVariant("0.42.0", "")
want := "0.42.0"
Expand Down

0 comments on commit 66b9ec4

Please sign in to comment.