diff --git a/git.go b/git.go index 68c65c0..86203cd 100644 --- a/git.go +++ b/git.go @@ -27,6 +27,7 @@ type commit struct { Subject string AuthorName string AuthorEmail string + BumpType semVerBump } func parseCommit(line string) (*commit, error) { @@ -34,12 +35,25 @@ func parseCommit(line string) (*commit, error) { if len(t) != 4 { return nil, errors.New("Unexpected line format") } - return &commit{ + + c := &commit{ ShortHash: t[0], Subject: t[1], AuthorName: t[2], 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) { diff --git a/main.go b/main.go index a04c9cd..ccf9bcd 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "log" "os" "os/exec" + "regexp" "strings" "time" @@ -41,6 +42,8 @@ var ( config configFile version = "dev" + + matchers = make(map[*regexp.Regexp]semVerBump) ) func prepareRun() { @@ -73,6 +76,22 @@ func prepareRun() { if err := loadConfig(); err != nil { 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 { diff --git a/semver.go b/semver.go index 0cf4026..aac79c6 100644 --- a/semver.go +++ b/semver.go @@ -2,7 +2,6 @@ package main import ( "errors" - "regexp" "strconv" "strings" ) @@ -10,8 +9,9 @@ import ( type semVerBump uint const ( - semVerBumpMinor semVerBump = iota + semVerBumpUndecided semVerBump = iota semVerBumpPatch + semVerBumpMinor semVerBumpMajor ) @@ -91,31 +91,18 @@ func (s *semVer) Bump(bumpType semVerBump) { } func selectBumpType(logs []commit) (semVerBump, error) { - bump := semVerBumpMinor - - 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 - } + bump := semVerBumpUndecided for _, l := range logs { - for m, t := range matchers { - if m.MatchString(l.Subject) && t > bump { - bump = t - } + 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 }