diff --git a/internal/generate.go b/internal/generate.go index 2ace8a9..20be1c9 100644 --- a/internal/generate.go +++ b/internal/generate.go @@ -14,7 +14,7 @@ type GenerateVersionOptions struct { PrereleaseSuffix string PrereleasePrefix string PrereleaseTimestamped bool - NextRelease string + NextRelease string Format string } @@ -65,15 +65,12 @@ func GenerateVersion(tagName string, counter int, headHash string, timestamp tim } } } + version = version.Bump(opts.NextRelease) if opts.DropTagNamePrefix { version.Prefix = "" } var result string - if opts.NextRelease != "" { - result = version.Version(opts.NextRelease) - } else { - result = version.String() - } + result = version.String() if opts.Format != "" { result = strings.ReplaceAll(opts.Format, "", result) } diff --git a/internal/semver.go b/internal/semver.go index 6217f17..090e7be 100644 --- a/internal/semver.go +++ b/internal/semver.go @@ -28,9 +28,14 @@ func (v SemVer) Equal(v2 SemVer) bool { equalStringSlice(v.BuildMetadata, v2.BuildMetadata) } -func (v SemVer) Version(nextRelease string) string { - patch := v.Patch +// Bump ... +func (v SemVer) Bump(nextRelease string) *SemVer { + if nextRelease == "" { + return &v + } + isPrerelease := len(v.Prerelease) > 0 + patch := v.Patch if nextRelease == "patch" && !isPrerelease { patch++ } @@ -49,17 +54,23 @@ func (v SemVer) Version(nextRelease string) string { minor = 0 patch = 0 } - return fmt.Sprintf("%s%d.%d.%d", v.Prefix, major, minor, patch) + v2 := SemVer{ + Prefix: v.Prefix, + Major: major, + Minor: minor, + Patch: patch, + } + return &v2 } // String ... func (v SemVer) String() string { - str := v.Version("") + str := fmt.Sprintf("%s%d.%d.%d", v.Prefix, v.Major, v.Minor, v.Patch) if len(v.Prerelease) > 0 { - str += "-" + strings.Join(v.Prerelease, ".") + str = str + "-" + strings.Join(v.Prerelease, ".") } if len(v.BuildMetadata) > 0 { - str += "+" + strings.Join(v.BuildMetadata, ".") + str = str + "+" + strings.Join(v.BuildMetadata, ".") } return str }