Skip to content

Commit

Permalink
fix: types with ! should be breaking changes
Browse files Browse the repository at this point in the history
closes #22

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed May 16, 2021
1 parent 48b219b commit c5fffd5
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 12 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ require (
github.com/alecthomas/kingpin v2.2.6+incompatible
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 // indirect
github.com/matryer/is v1.4.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 h1:AUNCr9CiJuwrRY
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=
github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
4 changes: 2 additions & 2 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ func IsRepo() bool {

// Run runs a git command and returns its output or errors
func Run(args ...string) (string, error) {
var extraArgs = []string{
extraArgs := []string{
"-c", "log.showSignature=false",
}
args = append(extraArgs, args...)
/* #nosec */
var cmd = exec.Command("git", args...)
cmd := exec.Command("git", args...)
bts, err := cmd.CombinedOutput()
if err != nil {
return "", errors.New(string(bts))
Expand Down
34 changes: 24 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ var (
)

func main() {
app.Author("Carlos Alexandro Becker <caarlos0@gmail.com>")
app.Author("Carlos Alexandro Becker <carlos@becker.software>")
app.Version("svu version " + version)
app.VersionFlag.Short('v')
app.HelpFlag.Short('h')
var cmd = kingpin.MustParse(app.Parse(os.Args[1:]))
cmd := kingpin.MustParse(app.Parse(os.Args[1:]))

tag, err := getTag()
app.FatalIfError(err, "failed to get current tag for repo")
Expand Down Expand Up @@ -72,10 +72,12 @@ func main() {
fmt.Printf("%s%s\n", prefix, result.String())
}

var breaking = regexp.MustCompile("(?im).*breaking change:.*")
var breakingBang = regexp.MustCompile("(?im).*(feat|fix)(\\(.*\\))?!:.*")
var feature = regexp.MustCompile("(?im).*feat(\\(.*\\))?:.*")
var patch = regexp.MustCompile("(?im).*fix(\\(.*\\))?:.*")
var (
breaking = regexp.MustCompile("(?im).*breaking change:.*")
breakingBang = regexp.MustCompile(`(?im).*(\w+)(\(.*\))?!:.*`)
feature = regexp.MustCompile(`(?im).*feat(\(.*\))?:.*`)
patch = regexp.MustCompile(`(?im).*fix(\(.*\))?:.*`)
)

func unsetPreRelease(current *semver.Version) *semver.Version {
newV, _ := current.SetPrerelease("")
Expand All @@ -97,21 +99,33 @@ func findNext(current *semver.Version, tag string) semver.Version {
log, err := getChangelog(tag)
app.FatalIfError(err, "failed to get changelog")

if breaking.MatchString(log) || breakingBang.MatchString(log) {
if isBreaking(log) {
return current.IncMajor()
}

if feature.MatchString(log) {
if isFeature(log) {
return current.IncMinor()
}

if patch.MatchString(log) || *forcePatchIncrement {
if isPatch(log, *forcePatchIncrement) {
return current.IncPatch()
}

return *current
}

func isBreaking(log string) bool {
return breaking.MatchString(log) || breakingBang.MatchString(log)
}

func isFeature(log string) bool {
return feature.MatchString(log)
}

func isPatch(log string, force bool) bool {
return force || patch.MatchString(log)
}

func getTag() (string, error) {
if *tagMode == "all-branches" {
tagHash, err := git.Clean(git.Run("rev-list", "--tags", "--max-count=1"))
Expand All @@ -130,7 +144,7 @@ func getChangelog(tag string) (string, error) {
}

func gitLog(refs ...string) (string, error) {
var args = []string{"log", "--no-decorate", "--no-color"}
args := []string{"log", "--no-decorate", "--no-color"}
args = append(args, refs...)
return git.Run(args...)
}
81 changes: 81 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"testing"

"github.com/matryer/is"
)

func TestIsBreaking(t *testing.T) {
for _, log := range []string{
"feat!: foo",
"chore(lala)!: foo",
"docs: lalala\nBREAKING CHANGE: lalal",
} {
t.Run(log, func(t *testing.T) {
is.New(t).True(isBreaking(log)) // should be a major change
})
}

for _, log := range []string{
"feat: foo",
"chore(lol): foo",
"docs: lalala",
} {
t.Run(log, func(t *testing.T) {
is.New(t).True(!isBreaking(log)) // should NOT be a major change
})
}
}

func TestIsFeature(t *testing.T) {
for _, log := range []string{
"feat: foo",
"feat(lalal): foobar",
} {
t.Run(log, func(t *testing.T) {
is.New(t).True(isFeature(log)) // should be a minor change
})
}

for _, log := range []string{
"fix: foo",
"chore: foo",
"docs: lalala",
} {
t.Run(log, func(t *testing.T) {
is.New(t).True(!isFeature(log)) // should NOT be a minor change
})
}
}

func TestIsPatch(t *testing.T) {
for _, log := range []string{
"fix: foo",
"fix(lalal): lalala",
} {
t.Run(log, func(t *testing.T) {
is.New(t).True(isPatch(log, false)) // should be a patch change
})
}

for _, log := range []string{
"chore: foobar",
"docs: something",
"invalid commit",
} {
t.Run(log, func(t *testing.T) {
is.New(t).True(!isPatch(log, false)) // should NOT be a patch change
})
}

for _, log := range []string{
"chore: foobar",
"docs: something",
"invalid commit",
} {
t.Run(log+" (force)", func(t *testing.T) {
is.New(t).True(isPatch(log, true)) // should NOT be a patch change
})
}
}

0 comments on commit c5fffd5

Please sign in to comment.