// Package version resolves and validates Go module semantic versions and VCS tags.
package version import ( ) // BumpKind identifies a relative semantic version increment. type BumpKind string const ( // BumpNone indicates an explicit version rather than a relative bump. BumpNone BumpKind = "" // BumpPatch increments the patch component (vX.Y.Z -> vX.Y.(Z+1)). BumpPatch BumpKind = "patch" // BumpMinor increments the minor component (vX.Y.Z -> vX.(Y+1).0). BumpMinor BumpKind = "minor" // BumpMajor increments the major component (vX.Y.Z -> v(X+1).0.0). BumpMajor BumpKind = "major" // BumpAuto selects patch, minor, or major automatically from apidiff. BumpAuto BumpKind = "auto" ) // Spec describes how target versions are computed for modules. type Spec struct { // Explicit is a concrete canonical semver (e.g. "v0.2.0") when Bump is BumpNone. Explicit string // Bump is a relative semver bump ("patch", "minor", "major") when Explicit is empty. Bump BumpKind // Prerelease is an optional prerelease suffix (without leading "-", e.g. "rc.1"). Prerelease string } // ParseSpec parses either an explicit semver ("v1.2.3") or a relative bump keyword // ("patch", "minor", "major"). func (, string) (Spec, error) { = strings.TrimSpace() = strings.TrimPrefix(strings.TrimSpace(), "-") if == "" { return Spec{}, errors.New("version or bump (patch, minor, major) is required") } switch BumpKind() { case BumpPatch, BumpMinor, BumpMajor, BumpAuto: if != "" && !semver.IsValid("v0.0.0-"+) { return Spec{}, fmt.Errorf("invalid prerelease %q", ) } return Spec{Bump: BumpKind(), Prerelease: }, nil } if !semver.IsValid() { return Spec{}, fmt.Errorf("invalid version %q (expected semver like v0.1.0 or auto/patch/minor/major)", ) } if := semver.Canonical(); != && semver.Build() == "" { return Spec{}, fmt.Errorf("version %q is not canonical (use %q)", , ) } if != "" { return Spec{}, errors.New("--pre cannot be combined with an explicit version") } return Spec{Explicit: }, nil } // ParseOverrides splits trailing arguments into per-module version overrides // ("target=vX.Y.Z") and plain module targets. func ( []string) ( map[string]string, []string, error) { = make(map[string]string) for , := range { , , := strings.Cut(, "=") if ! { = append(, ) continue } = strings.TrimSpace() = strings.TrimSpace() if == "" { return nil, nil, fmt.Errorf("invalid override %q: empty module target", ) } if !semver.IsValid() { return nil, nil, fmt.Errorf("invalid override %q: %q is not a valid semver", , ) } [] = } return , , nil } // TagFor returns the canonical VCS tag for a module at repo-relative slash dir // and semantic version ver. func (, string) string { := path.Clean(strings.TrimPrefix(, "./")) if == "" || == "." { return } return path.Join(, ) } // ParseTag extracts the semver from tag if it belongs to the module at modDir. func (, string) (string, bool) { := path.Clean(strings.TrimPrefix(, "./")) if == "" || == "." { if strings.Contains(, "/") || !semver.IsValid() { return "", false } return , true } := + "/" if !strings.HasPrefix(, ) { return "", false } := strings.TrimPrefix(, ) if strings.Contains(, "/") || !semver.IsValid() { return "", false } return , true } // LatestTag finds the highest valid semantic version among tags for modDir. // It returns empty strings if no matching semver tag exists. func ( []string, string) (, string) { for , := range { , := ParseTag(, ) if ! { continue } if == "" || semver.Compare(, ) > 0 { = = } } return , } // Resolve computes and validates the target version for a module. func (, , string, Spec, map[string]string) (string, error) { := cmp.Or( [], [], ["./"+], .Explicit, ) if == "" { var error , = Bump(, .Bump, .Prerelease) if != nil { return "", } } if := ValidateModuleVersion(, ); != nil { return "", } return , nil } // Bump increments a semantic version by kind. If latest is empty, it starts // from v0.0.0. func ( string, BumpKind, string) (string, error) { = cmp.Or(, "v0.0.0") if !semver.IsValid() { return "", fmt.Errorf("invalid base version %q", ) } := strings.TrimPrefix(semver.Canonical(), "v") if := strings.IndexByte(, '-'); >= 0 { = [:] } := strings.Split(, ".") if len() != 3 { return "", fmt.Errorf("unexpected canonical version %q", ) } , := strconv.Atoi([0]) if != nil { return "", fmt.Errorf("parse major version %q: %w", [0], ) } , := strconv.Atoi([1]) if != nil { return "", fmt.Errorf("parse minor version %q: %w", [1], ) } , := strconv.Atoi([2]) if != nil { return "", fmt.Errorf("parse patch version %q: %w", [2], ) } // If latest is a prerelease and no new prerelease is requested, a patch // bump graduates the prerelease to its final version (e.g. v1.2.3-rc.1 -> v1.2.3). if semver.Prerelease() != "" && == BumpPatch && == "" { return fmt.Sprintf("v%d.%d.%d", , , ), nil } switch { case BumpPatch: ++ case BumpMinor: ++ = 0 case BumpMajor: ++ = 0 = 0 default: return "", fmt.Errorf("unsupported bump kind %q", ) } := fmt.Sprintf("v%d.%d.%d", , , ) if != "" { += "-" + } if !semver.IsValid() { return "", fmt.Errorf("computed invalid version %q", ) } return , nil } // ValidateModuleVersion checks that ver is valid for modPath according to Go’s // module path and major-version suffix rules. func (, string) error { if := module.Check(, ); != nil { return fmt.Errorf("module %s version %s: %w", , , ) } return nil }