From 3fcf22ed5d790772a791d1f2e86544c925eb9d00 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sat, 20 Jun 2020 08:42:06 +0200 Subject: [PATCH] build: Change version strings to not have plus in them (ref #6758) (#6760) Currently a random dev version has a version string like this: v1.7.0-rc.1+23-gef3441bd6 That is, the tag name followed by a plus sign and the git describe metadata (number of commits and hash) plus -dirty or -branchname in some cases. We introduced the plus sign in #473, where a dev version would previously be called v0.9.0-42-gwhatever which is considered older than v0.9.0 and hence caused a downgrade. The problem with the plus is that per semver everything after the plus is ignored as build metadata, which means we won't upgrade from v1.7.0-rc.1 to v1.7.0-rc.1+22-g946170f3f. With this change the we instead either just add a dev suffix (if we're already on a prerelease version) or we wind the patch version and add a dev suffix. v1.7.0-rc.1+23-gef3441bd6 => v1.7.0-rc.1.dev.23.gef3441bd6 v1.6.1+80-gef3441bd6 => v1.6.2-dev.80.gef3441bd6 This should preserve the ordering and keep versions semver-ish. --- build.go | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/build.go b/build.go index ace772256..2f89ac689 100644 --- a/build.go +++ b/build.go @@ -34,7 +34,6 @@ import ( ) var ( - versionRe = regexp.MustCompile(`-[0-9]{1,3}-g[0-9a-f]{5,10}`) goarch string goos string noupgrade bool @@ -852,15 +851,36 @@ func getReleaseVersion() (string, error) { } func getGitVersion() (string, error) { - v, err := runError("git", "describe", "--always", "--dirty") + // The current version as Git sees it + bs, err := runError("git", "describe", "--always", "--dirty") if err != nil { return "", err } - v = versionRe.ReplaceAllFunc(v, func(s []byte) []byte { - s[0] = '+' - return s - }) - return string(v), nil + vcur := string(bs) + + // The closest current tag name + bs, err = runError("git", "describe", "--always", "--abbrev=0") + if err != nil { + return "", err + } + v0 := string(bs) + + versionRe := regexp.MustCompile(`-([0-9]{1,3}-g[0-9a-f]{5,10})`) + if m := versionRe.FindStringSubmatch(vcur); len(m) > 0 { + suffix := strings.ReplaceAll(m[1], "-", ".") + + if strings.Contains(v0, "-") { + // We're based of a tag with a prerelease string. We can just + // add our dev stuff directly. + return fmt.Sprintf("%s.dev.%s", v0, suffix), nil + } + + // We're based on a release version. We need to bump the patch + // version and then add a -dev prerelease string. + next := nextPatchVersion(v0) + return fmt.Sprintf("%s-dev.%s", next, suffix), nil + } + return vcur, nil } func getVersion() string { @@ -1362,3 +1382,11 @@ func trimTagMessage(msg, tag string) string { } return strings.TrimSpace(msg) } + +func nextPatchVersion(ver string) string { + parts := strings.SplitN(ver, "-", 2) + digits := strings.Split(parts[0], ".") + n, _ := strconv.Atoi(digits[len(digits)-1]) + digits[len(digits)-1] = strconv.Itoa(n + 1) + return strings.Join(digits, ".") +}