1
0
Fork 0
mirror of https://github.com/Luzifer/git-changerelease.git synced 2024-12-20 19:11:17 +00:00
git-changerelease/semver.go
Knut Ahlers 810e271c3b
Modernize code, update deps, fix linter errors
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2023-11-11 15:27:37 +01:00

83 lines
1.4 KiB
Go

package main
import (
"errors"
"fmt"
"github.com/Masterminds/semver/v3"
)
type semVerBump uint
const (
semVerBumpUndecided semVerBump = iota
semVerBumpPatch
semVerBumpMinor
semVerBumpMajor
)
type semVer struct {
*semver.Version
}
func (s *semVer) SetMetadata(metadata string) error {
nv, err := s.Version.SetMetadata(metadata)
if err != nil {
return fmt.Errorf("setting metadata: %w", err)
}
s.Version = &nv
return nil
}
func (s *semVer) SetPrerelease(prerelease string) error {
nv, err := s.Version.SetPrerelease(prerelease)
if err != nil {
return fmt.Errorf("setting prerelease: %w", err)
}
s.Version = &nv
return nil
}
func parseSemVer(version string) (*semVer, error) {
v, err := semver.NewVersion(version)
if err != nil {
return nil, fmt.Errorf("parsing semver: %w", err)
}
return &semVer{v}, nil
}
func (s *semVer) Bump(bumpType semVerBump) {
var nv semver.Version
switch bumpType {
case semVerBumpPatch:
nv = s.Version.IncPatch()
case semVerBumpMinor:
nv = s.Version.IncMinor()
case semVerBumpMajor:
nv = s.Version.IncMajor()
}
s.Version = &nv
}
func selectBumpType(logs []commit) (semVerBump, error) {
bump := semVerBumpUndecided
for _, l := range logs {
if l.BumpType > bump {
bump = l.BumpType
}
}
if bump == semVerBumpUndecided {
// Impossible to reach
return semVerBumpUndecided, errors.New("could not decide for any bump type")
}
return bump, nil
}