0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-14 17:50:40 +00:00
git-sv/sv/commit.go

139 lines
3.4 KiB
Go
Raw Normal View History

package sv
2021-02-13 18:40:09 +00:00
import "github.com/Masterminds/semver/v3"
type versionType int
const (
none versionType = iota
patch
minor
major
)
// CommitLog description of a single commit log.
type CommitLog struct {
Date string `json:"date,omitempty"`
Timestamp int `json:"timestamp,omitempty"`
AuthorName string `json:"authorName,omitempty"`
Hash string `json:"hash,omitempty"`
Message CommitMessage `json:"message,omitempty"`
}
// IsValidVersion return true when a version is valid.
func IsValidVersion(value string) bool {
_, err := semver.NewVersion(value)
2023-10-12 14:18:25 +00:00
return err == nil
}
2021-07-31 19:03:58 +00:00
// ToVersion parse string to semver.Version.
func ToVersion(value string) (*semver.Version, error) {
version := value
if version == "" {
version = "0.0.0"
}
2023-10-12 14:18:25 +00:00
return semver.NewVersion(version)
}
// CommitProcessor interface.
type CommitProcessor interface {
NextVersion(version *semver.Version, commits []CommitLog) (*semver.Version, bool)
}
// SemVerCommitProcessor process versions using commit log.
type SemVerCommitProcessor struct {
MajorVersionTypes map[string]struct{}
MinorVersionTypes map[string]struct{}
PatchVersionTypes map[string]struct{}
KnownTypes []string
IncludeUnknownTypeAsPatch bool
}
// VersioningConfig versioning preferences.
type VersioningConfig struct {
UpdateMajor []string `yaml:"update-major,flow"`
UpdateMinor []string `yaml:"update-minor,flow"`
UpdatePatch []string `yaml:"update-patch,flow"`
IgnoreUnknown bool `yaml:"ignore-unknown"`
}
// NewSemVerCommitProcessor SemanticVersionCommitProcessorImpl constructor.
func NewSemVerCommitProcessor(vcfg VersioningConfig, mcfg CommitMessageConfig) *SemVerCommitProcessor {
return &SemVerCommitProcessor{
IncludeUnknownTypeAsPatch: !vcfg.IgnoreUnknown,
MajorVersionTypes: toMap(vcfg.UpdateMajor),
MinorVersionTypes: toMap(vcfg.UpdateMinor),
PatchVersionTypes: toMap(vcfg.UpdatePatch),
KnownTypes: mcfg.Types,
}
}
2021-07-31 19:03:58 +00:00
// NextVersion calculates next version based on commit log.
func (p SemVerCommitProcessor) NextVersion(
version *semver.Version, commits []CommitLog,
2023-10-12 14:18:25 +00:00
) (*semver.Version, bool) {
2021-07-31 19:03:58 +00:00
versionToUpdate := none
for _, commit := range commits {
if v := p.versionTypeToUpdate(commit); v > versionToUpdate {
versionToUpdate = v
}
}
updated := versionToUpdate != none
if version == nil {
return nil, updated
}
2023-10-12 14:18:25 +00:00
newVersion := updateVersion(*version, versionToUpdate)
2023-10-12 14:18:25 +00:00
return &newVersion, updated
}
func updateVersion(version semver.Version, versionToUpdate versionType) semver.Version {
switch versionToUpdate {
case major:
return version.IncMajor()
case minor:
return version.IncMinor()
case patch:
return version.IncPatch()
default:
return version
}
}
func (p SemVerCommitProcessor) versionTypeToUpdate(commit CommitLog) versionType {
if commit.Message.IsBreakingChange {
return major
}
2023-10-12 14:18:25 +00:00
if _, exists := p.MajorVersionTypes[commit.Message.Type]; exists {
return major
}
2023-10-12 14:18:25 +00:00
if _, exists := p.MinorVersionTypes[commit.Message.Type]; exists {
return minor
}
2023-10-12 14:18:25 +00:00
if _, exists := p.PatchVersionTypes[commit.Message.Type]; exists {
return patch
}
2023-10-12 14:18:25 +00:00
if !contains(commit.Message.Type, p.KnownTypes) && p.IncludeUnknownTypeAsPatch {
return patch
}
2023-10-12 14:18:25 +00:00
return none
}
func toMap(values []string) map[string]struct{} {
result := make(map[string]struct{})
for _, v := range values {
result[v] = struct{}{}
}
2023-10-12 14:18:25 +00:00
return result
}