Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tooling fixes #5400

Merged
merged 1 commit into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tools/apidiff/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ func (wt WorkingTree) ListTags(pattern string) ([]string, error) {
if err != nil {
return nil, errors.New(string(output))
}
if len(output) == 0 {
return []string{}, nil
}
tags := strings.Split(strings.TrimSpace(string(output)), "\n")
sort.Strings(tags)
return tags, nil
Expand Down
6 changes: 6 additions & 0 deletions tools/internal/modinfo/modinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,9 @@ func (m module) NewModule() bool {
func (m module) GenerateReport() report.Package {
return report.Generate(m.lhs, m.rhs, false, false)
}

// IsValidModuleVersion returns true if the provided string is a valid module version (e.g. v1.2.3).
func IsValidModuleVersion(v string) bool {
r := regexp.MustCompile(`^v\d+\.\d+\.\d+$`)
return r.MatchString(v)
}
12 changes: 12 additions & 0 deletions tools/internal/modinfo/modinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,15 @@ func TestCreateModuleNameFromPathFail(t *testing.T) {
t.Fatalf("expected empty module name, got %s", n)
}
}

func TestIsValidModuleVersion(t *testing.T) {
if !IsValidModuleVersion("v10.21.23") {
t.Fatal("unexpected invalid module version")
}
if IsValidModuleVersion("1.2.3") {
t.Fatal("unexpected valid module version, missing v")
}
if IsValidModuleVersion("v11.563") {
t.Fatal("unexpected valid module version, missing patch")
}
}
27 changes: 21 additions & 6 deletions tools/versioner/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ import (
)

var rootCmd = &cobra.Command{
Use: "versioner <staging dir>",
Use: "versioner <staging dir> [initial module version]",
Short: "Creates or updates the latest major version for a package from staged content.",
Long: `This tool will compare a staged package against its latest major version to detect
breaking changes. If there are no breaking changes the latest major version is updated
with the staged content. If there are breaking changes the staged content becomes the
next latest major vesion and the go.mod file is updated.
The default version for new modules is v1.0.0 or the value specified for [initial module version].
`,
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
if err := cobra.MinimumNArgs(1)(cmd, args); err != nil {
return err
}
if err := cobra.MaximumNArgs(2)(cmd, args); err != nil {
return err
}
return nil
Expand All @@ -55,6 +59,8 @@ var (
semverRegex = regexp.MustCompile(`v\d+\.\d+\.\d+$`)
// this is used so tests can hook getTags() to return whatever tags
getTagsHook func(string, string) ([]string, error)
// default version to start a module at if not specified
startingModVer = "v1.0.0"
)

func init() {
Expand All @@ -81,6 +87,12 @@ func theCommand(args []string) error {
// does the actual work
func theCommandImpl(args []string) (string, error) {
stage := filepath.Clean(args[0])
if len(args) == 2 {
if !modinfo.IsValidModuleVersion(args[1]) {
return "", fmt.Errorf("the string '%s' is not a valid module version", args[1])
}
startingModVer = args[1]
}
lmv, err := findLatestMajorVersion(stage)
if err != nil {
return "", fmt.Errorf("failed to find latest major version: %v", err)
Expand Down Expand Up @@ -273,13 +285,16 @@ func calculateModuleTag(tags []string, mod modinfo.Provider) (string, error) {
if mod.BreakingChanges() {
return tagPrefix + ".0.0", nil
}
if len(tags) == 0 {
if mod.VersionSuffix() {
panic("module contains a version suffix but no tags were found")
}
// this is the first module version
return tagPrefix + "/" + startingModVer, nil
}
if !mod.VersionSuffix() {
tagPrefix = tagPrefix + "/v1"
}
if len(tags) == 0 {
// this is v1.0.0
return tagPrefix + ".0.0", nil
}
tag := tags[len(tags)-1]
v := semverRegex.FindString(tag)
if v == "" {
Expand Down