-
Notifications
You must be signed in to change notification settings - Fork 17.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/go/internal/modfetch: move to new pseudo-version design
The original pseudo-version design used versions of the form v0.0.0-yyyymmddhhmmss-abcdef123456 These were intentionally chosen to be valid semantic versions that sort below any explicitly-chosen semantic version (even v0.0.0), so that they could be used before anything was tagged but after that would essentially only be useful in replace statements (because the max operation during MVS would always prefer a tagged version). Then we changed the go command to accept hashes on the command line, so that you can say go get github.com/my/proj@abcdef and it will download and use v0.0.0-yyyymmddhhmmss-abcdef123456. If you were using v1.10.1 before and this commit is just little bit newer than that commit, calling it v0.0.0-xxx is confusing but also harmful: the go command sees the change from v1.10.1 to the v0.0.0 pseudoversion as a downgrade, and it downgrades other modules in the build. In particular if some other module has a requirement of github.com/my/proj v1.9.0 (or later), the pseudo-version appears to be before that, so go get would downgrade that module too. It might even remove it entirely, if every available version needs a post-v0.0.0 version of my/proj. This CL introduces new pseudo-version forms that can be used to slot in after the most recent explicit tag before the commit. If the most recent tagged commit before abcdef is v1.10.1, then now we will use v1.10.2-0.yyyymmddhhmmss-abcdef123456 This has the right properties for downgrades and the like, since it is after v1.10.1 but before almost any possible successor, such as v1.10.2, v1.10.2-1, or v1.10.2-pre. This CL also uses those pseudo-version forms as appropriate when mapping a hash to a pseudo-version. This fixes the downgrade problem. Overall, this CL reflects our growing recognition of pseudo-versions as being like "untagged prereleases". Issue #26150 was about documenting best practices for how to work around this kind of accidental downgrade problem with additional steps. Now there are no additional steps: the problem is avoided by default. Fixes #26150. Change-Id: I402feeccb93e8e937bafcaa26402d88572e9b14c Reviewed-on: https://go-review.googlesource.com/124515 Reviewed-by: Bryan C. Mills <bcmills@google.com>
- Loading branch information
Showing
10 changed files
with
356 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright 2018 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// Pseudo-versions | ||
// | ||
// Code authors are expected to tag the revisions they want users to use, | ||
// including prereleases. However, not all authors tag versions at all, | ||
// and not all commits a user might want to try will have tags. | ||
// A pseudo-version is a version with a special form that allows us to | ||
// address an untagged commit and order that version with respect to | ||
// other versions we might encounter. | ||
// | ||
// A pseudo-version takes one of the general forms: | ||
// | ||
// (1) vX.0.0-yyyymmddhhmmss-abcdef123456 | ||
// (2) vX.Y.(Z+1)-0.yyyymmddhhmmss-abcdef123456 | ||
// (3) vX.Y.(Z+1)-0.yyyymmddhhmmss-abcdef123456+incompatible | ||
// (4) vX.Y.Z-pre.0.yyyymmddhhmmss-abcdef123456 | ||
// (5) vX.Y.Z-pre.0.yyyymmddhhmmss-abcdef123456+incompatible | ||
// | ||
// If there is no recently tagged version with the right major version vX, | ||
// then form (1) is used, creating a space of pseudo-versions at the bottom | ||
// of the vX version range, less than any tagged version, including the unlikely v0.0.0. | ||
// | ||
// If the most recent tagged version before the target commit is vX.Y.Z or vX.Y.Z+incompatible, | ||
// then the pseudo-version uses form (2) or (3), making it a prerelease for the next | ||
// possible semantic version after vX.Y.Z. The leading 0 segment in the prerelease string | ||
// ensures that the pseudo-version compares less than possible future explicit prereleases | ||
// like vX.Y.(Z+1)-rc1 or vX.Y.(Z+1)-1. | ||
// | ||
// If the most recent tagged version before the target commit is vX.Y.Z-pre or vX.Y.Z-pre+incompatible, | ||
// then the pseudo-version uses form (4) or (5), making it a slightly later prerelease. | ||
|
||
package modfetch | ||
|
||
import ( | ||
"cmd/go/internal/semver" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// PseudoVersion returns a pseudo-version for the given major version ("v1") | ||
// preexisting older tagged version ("" or "v1.2.3" or "v1.2.3-pre"), revision time, | ||
// and revision identifier (usually a 12-byte commit hash prefix). | ||
func PseudoVersion(major, older string, t time.Time, rev string) string { | ||
if major == "" { | ||
major = "v0" | ||
} | ||
segment := fmt.Sprintf("%s-%s", t.UTC().Format("20060102150405"), rev) | ||
build := semver.Build(older) | ||
older = semver.Canonical(older) | ||
if older == "" { | ||
return major + ".0.0-" + segment // form (1) | ||
} | ||
if semver.Prerelease(older) != "" { | ||
return older + ".0." + segment + build // form (4), (5) | ||
} | ||
|
||
// Form (2), (3). | ||
// Extract patch from vMAJOR.MINOR.PATCH | ||
v := older[:len(older)] | ||
i := strings.LastIndex(v, ".") + 1 | ||
v, patch := v[:i], v[i:] | ||
|
||
// Increment PATCH by adding 1 to decimal: | ||
// scan right to left turning 9s to 0s until you find a digit to increment. | ||
// (Number might exceed int64, but math/big is overkill.) | ||
digits := []byte(patch) | ||
for i = len(digits) - 1; i >= 0 && digits[i] == '9'; i-- { | ||
digits[i] = '0' | ||
} | ||
if i >= 0 { | ||
digits[i]++ | ||
} else { | ||
// digits is all zeros | ||
digits[0] = '1' | ||
digits = append(digits, '0') | ||
} | ||
patch = string(digits) | ||
|
||
// Reassemble. | ||
return v + patch + "-0." + segment + build | ||
} | ||
|
||
var pseudoVersionRE = regexp.MustCompile(`^v[0-9]+\.(0\.0-|\d+\.\d+-([^+]*\.)?0\.)\d{14}-[A-Za-z0-9]+(\+incompatible)?$`) | ||
|
||
// IsPseudoVersion reports whether v is a pseudo-version. | ||
func IsPseudoVersion(v string) bool { | ||
return strings.Count(v, "-") >= 2 && semver.IsValid(v) && pseudoVersionRE.MatchString(v) | ||
} | ||
|
||
// PseudoVersionTime returns the time stamp of the pseudo-version v. | ||
// It returns an error if v is not a pseudo-version or if the time stamp | ||
// embedded in the pseudo-version is not a valid time. | ||
func PseudoVersionTime(v string) (time.Time, error) { | ||
timestamp, _, err := parsePseudoVersion(v) | ||
t, err := time.Parse("20060102150405", timestamp) | ||
if err != nil { | ||
return time.Time{}, fmt.Errorf("pseudo-version with malformed time %s: %q", timestamp, v) | ||
} | ||
return t, nil | ||
} | ||
|
||
// PseudoVersionRev returns the revision identifier of the pseudo-version v. | ||
// It returns an error if v is not a pseudo-version. | ||
func PseudoVersionRev(v string) (rev string, err error) { | ||
_, rev, err = parsePseudoVersion(v) | ||
return | ||
} | ||
|
||
func parsePseudoVersion(v string) (timestamp, rev string, err error) { | ||
if !IsPseudoVersion(v) { | ||
return "", "", fmt.Errorf("malformed pseudo-version %q", v) | ||
} | ||
v = strings.TrimSuffix(v, "+incompatible") | ||
j := strings.LastIndex(v, "-") | ||
v, rev = v[:j], v[j+1:] | ||
i := strings.LastIndex(v, "-") | ||
if j := strings.LastIndex(v, "."); j > i { | ||
timestamp = v[j+1:] | ||
} else { | ||
timestamp = v[i+1:] | ||
} | ||
return timestamp, rev, nil | ||
} |
Oops, something went wrong.