1
0
Fork 0
mirror of https://github.com/Luzifer/git-changerelease.git synced 2024-12-20 19:11:17 +00:00

fix wrong bump when patch and minor are included

In previous versions the bump type was not calculated correctly when a
minor commit and a patch type commit were included in a release. Instead
of using the minor-bump it used the patch-bump because minor was the
default and patch overruled minor.
This commit is contained in:
Knut Ahlers 2016-07-21 17:51:08 +02:00
parent bb7a2bedfe
commit fa7cfd9e9e
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
3 changed files with 45 additions and 25 deletions

18
git.go
View file

@ -27,6 +27,7 @@ type commit struct {
Subject string Subject string
AuthorName string AuthorName string
AuthorEmail string AuthorEmail string
BumpType semVerBump
} }
func parseCommit(line string) (*commit, error) { func parseCommit(line string) (*commit, error) {
@ -34,12 +35,25 @@ func parseCommit(line string) (*commit, error) {
if len(t) != 4 { if len(t) != 4 {
return nil, errors.New("Unexpected line format") return nil, errors.New("Unexpected line format")
} }
return &commit{
c := &commit{
ShortHash: t[0], ShortHash: t[0],
Subject: t[1], Subject: t[1],
AuthorName: t[2], AuthorName: t[2],
AuthorEmail: t[3], AuthorEmail: t[3],
}, nil }
for rex, bt := range matchers {
if rex.MatchString(c.Subject) && bt > c.BumpType {
c.BumpType = bt
}
}
if c.BumpType == semVerBumpUndecided {
c.BumpType = semVerBumpMinor
}
return c, nil
} }
func git(stderrEnabled bool, args ...string) (string, error) { func git(stderrEnabled bool, args ...string) (string, error) {

19
main.go
View file

@ -11,6 +11,7 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
"regexp"
"strings" "strings"
"time" "time"
@ -41,6 +42,8 @@ var (
config configFile config configFile
version = "dev" version = "dev"
matchers = make(map[*regexp.Regexp]semVerBump)
) )
func prepareRun() { func prepareRun() {
@ -73,6 +76,22 @@ func prepareRun() {
if err := loadConfig(); err != nil { if err := loadConfig(); err != nil {
log.Fatalf("Unable to load config file: %s", err) log.Fatalf("Unable to load config file: %s", err)
} }
// Collect matchers
for _, m := range config.MatchPatch {
r, err := regexp.Compile(m)
if err != nil {
log.Fatalf("Unable to parse regex '%s': %s", m, err)
}
matchers[r] = semVerBumpPatch
}
for _, m := range config.MatchMajor {
r, err := regexp.Compile(m)
if err != nil {
log.Fatalf("Unable to parse regex '%s': %s", m, err)
}
matchers[r] = semVerBumpMajor
}
} }
func loadConfig() error { func loadConfig() error {

View file

@ -2,7 +2,6 @@ package main
import ( import (
"errors" "errors"
"regexp"
"strconv" "strconv"
"strings" "strings"
) )
@ -10,8 +9,9 @@ import (
type semVerBump uint type semVerBump uint
const ( const (
semVerBumpMinor semVerBump = iota semVerBumpUndecided semVerBump = iota
semVerBumpPatch semVerBumpPatch
semVerBumpMinor
semVerBumpMajor semVerBumpMajor
) )
@ -91,30 +91,17 @@ func (s *semVer) Bump(bumpType semVerBump) {
} }
func selectBumpType(logs []commit) (semVerBump, error) { func selectBumpType(logs []commit) (semVerBump, error) {
bump := semVerBumpMinor bump := semVerBumpUndecided
matchers := map[*regexp.Regexp]semVerBump{}
for _, m := range config.MatchPatch {
r, err := regexp.Compile(m)
if err != nil {
return 0, err
}
matchers[r] = semVerBumpPatch
}
for _, m := range config.MatchMajor {
r, err := regexp.Compile(m)
if err != nil {
return 0, err
}
matchers[r] = semVerBumpMajor
}
for _, l := range logs { for _, l := range logs {
for m, t := range matchers { if l.BumpType > bump {
if m.MatchString(l.Subject) && t > bump { bump = l.BumpType
bump = t
} }
} }
if bump == semVerBumpUndecided {
// Impossible to reach
return semVerBumpUndecided, errors.New("Could not decide for any bump type!")
} }
return bump, nil return bump, nil