Skip to content

Commit

Permalink
Parse version from VERSION file
Browse files Browse the repository at this point in the history
This is helpful for our CI tools and release pipeline.
  • Loading branch information
wizeguyy committed Jan 26, 2023
1 parent e135ce9 commit 74a3ab8
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 36 deletions.
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0-pre.0
2 changes: 1 addition & 1 deletion cmd/go-quai/misccmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ and displays information about any security vulnerabilities that affect the curr

func version(ctx *cli.Context) error {
fmt.Println(strings.Title(clientIdentifier))
fmt.Println("Version:", params.VersionWithMeta)
fmt.Println("Version:", params.Version.Full())
if gitCommit != "" {
fmt.Println("Git Commit:", gitCommit)
}
Expand Down
2 changes: 1 addition & 1 deletion core/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (miner *Miner) MakeExtraData(extra []byte) []byte {
if len(extra) == 0 {
// create default extradata
extra, _ = rlp.EncodeToBytes([]interface{}{
uint(params.VersionMajor<<16 | params.VersionMinor<<8 | params.VersionPatch),
uint(params.Version.Major()<<16 | params.Version.Minor()<<8 | params.Version.Patch()),
"geth",
runtime.Version(),
runtime.GOOS,
Expand Down
2 changes: 1 addition & 1 deletion eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {

if !config.SkipBcVersionCheck {
if bcVersion != nil && *bcVersion > core.BlockChainVersion {
return nil, fmt.Errorf("database version is v%d, Geth %s only supports v%d", *bcVersion, params.VersionWithMeta, core.BlockChainVersion)
return nil, fmt.Errorf("database version is v%d, Geth %s only supports v%d", *bcVersion, params.Version.Full(), core.BlockChainVersion)
} else if bcVersion == nil || *bcVersion < core.BlockChainVersion {
if bcVersion != nil { // only print warning on upgrade, not on init
log.Warn("Upgrade blockchain database version", "from", dbVer, "to", core.BlockChainVersion)
Expand Down
147 changes: 114 additions & 33 deletions params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,133 @@
package params

import (
"fmt"
"bytes"
"errors"
"io/ioutil"
"log"
"strconv"
"strings"
"sync/atomic"
)

const (
VersionMajor = 1 // Major version component of the current release
VersionMinor = 10 // Minor version component of the current release
VersionPatch = 7 // Patch version component of the current release
VersionMeta = "stable" // Version metadata to append to the version string
)
var Version CachedVersion

// Version holds the textual version string.
var Version = func() string {
return fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch)
}()
type version struct {
major int
minor int
patch int
meta string
full string
}

// VersionWithMeta holds the textual version string including the metadata.
var VersionWithMeta = func() string {
v := Version
if VersionMeta != "" {
v += "-" + VersionMeta
func readVersionFile() (version, error) {
raw, err := ioutil.ReadFile("VERSION")
if err != nil {
panic(err)
}
full := strings.Replace(string(raw), "\n", "", -1)
// Take a full version string, e.g. 0.0.0-rc.0
// and split it into the version number and version metadata (if it has meta).
// e.g:
// - vnum := 0.0.0
// - vmeta := rc.0
split := bytes.Split(raw, []byte("-"))
vnum := split[0]
var vmeta []byte
if len(split) > 1 {
vmeta = split[0]
}
vnums := bytes.Split(vnum, []byte("."))
if len(vnums) != 3 {
return version{}, errors.New("bad version number format")
}
return v
}()
major, err := strconv.Atoi(string(vnums[0][:]))
if err != nil {
return version{}, err
}
minor, err := strconv.Atoi(string(vnums[1][:]))
if err != nil {
return version{}, err
}
patch, err := strconv.Atoi(string(vnums[2][:]))
if err != nil {
return version{}, err
}
return version{major: major, minor: minor, patch: patch, meta: string(vmeta), full: full}, nil
}

// ArchiveVersion holds the textual version string used for Geth archives.
// e.g. "1.8.11-dea1ce05" for stable releases, or
//
// "1.8.13-unstable-21c059b6" for unstable releases
func ArchiveVersion(gitCommit string) string {
vsn := Version
if VersionMeta != "stable" {
vsn += "-" + VersionMeta
// Version contains software version data parsed from the VERSION file
type CachedVersion struct {
major atomic.Value // Major version component of the current release
minor atomic.Value // Minor version component of the current release
patch atomic.Value // Patch version component of the current release
meta atomic.Value // Version metadata (i.e. stable, pre.X, rx.X)
full atomic.Value // Full version string (e.g. 0.0.0-rc.0)
}

// Load the cached version from the VERSION file
func (v *CachedVersion) load() {
ver, err := readVersionFile()
if err != nil {
log.Fatal("failed to read version file", err)
}
if len(gitCommit) >= 8 {
vsn += "-" + gitCommit[:8]
v.major.Store(ver.major)
v.minor.Store(ver.minor)
v.patch.Store(ver.patch)
v.meta.Store(ver.meta)
v.full.Store(ver.full)
}

// Major loads the cached major version, or reads it from a file
func (v *CachedVersion) Major() int {
if num := v.major.Load(); num != nil {
return num.(int)
}
return vsn
v.load()
return v.major.Load().(int)
}

// Minor loads the cached minor version, or reads it from a file
func (v *CachedVersion) Minor() int {
if num := v.minor.Load(); num != nil {
return num.(int)
}
v.load()
return v.minor.Load().(int)
}

// Patch loads the cached patch version, or reads it from a file
func (v *CachedVersion) Patch() int {
if num := v.patch.Load(); num != nil {
return num.(int)
}
v.load()
return v.patch.Load().(int)
}

// Meta loads the cached version metadata, or reads it from a file
// Metadata may be empty if no metadata was provided
func (v *CachedVersion) Meta() string {
if str := v.meta.Load(); str != nil {
return str.(string)
}
v.load()
return v.meta.Load().(string)
}

// Full loads the cached full version string, or reads it from a file
func (v *CachedVersion) Full() string {
if str := v.full.Load(); str != nil {
return str.(string)
}
v.load()
return v.full.Load().(string)
}

func VersionWithCommit(gitCommit, gitDate string) string {
vsn := VersionWithMeta
vsn := Version.Full()
if len(gitCommit) >= 8 {
vsn += "-" + gitCommit[:8]
}
if (VersionMeta != "stable") && (gitDate != "") {
vsn += "-" + gitDate
}
return vsn
}

0 comments on commit 74a3ab8

Please sign in to comment.