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

84 lines
1.4 KiB
Go
Raw Normal View History

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