Skip to content

Commit

Permalink
feat: improve conventional commits support
Browse files Browse the repository at this point in the history
Add full support for parts 1 to 3 of the summary at
https://www.conventionalcommits.org/en/v1.0.0/.
  • Loading branch information
smlx committed Feb 9, 2021
1 parent f05cd53 commit 12c1f76
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
4 changes: 1 addition & 3 deletions helper_test.go
@@ -1,8 +1,6 @@
package main

import "go.uber.org/zap"

// NextVersion is a test fixture to expose the private nextVersion() function.
func NextVersion(log *zap.Logger, path string) (string, error) {
func NextVersion(path string) (string, error) {
return nextVersion(path)
}
11 changes: 7 additions & 4 deletions main.go
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"strings"
"regexp"

"github.com/Masterminds/semver/v3"
"github.com/go-git/go-git/v5"
Expand Down Expand Up @@ -40,18 +40,21 @@ func nextVersion(path string) (string, error) {
var major, minor, patch bool
var stopIter error = fmt.Errorf("stop commit iteration")
var latestTag string
patchRegex := regexp.MustCompile(`^fix(\(.+\))?: `)
minorRegex := regexp.MustCompile(`^feat(\(.+\))?: `)
majorRegex := regexp.MustCompile(`^(fix|feat)(\(.+\))?!: |BREAKING CHANGE: `)
err = commits.ForEach(func(c *object.Commit) error {
if latestTag = tagRefs[c.Hash.String()]; latestTag != "" {
return stopIter
}
// analyze commit message
if strings.HasPrefix(c.Message, "fix: ") {
if patchRegex.MatchString(c.Message) {
patch = true
}
if strings.HasPrefix(c.Message, "feat: ") {
if minorRegex.MatchString(c.Message) {
minor = true
}
if strings.Contains(c.Message, "BREAKING CHANGE: ") {
if majorRegex.MatchString(c.Message) {
major = true
}
return nil
Expand Down
52 changes: 33 additions & 19 deletions main_test.go
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

ccv "github.com/smlx/ccv"
"go.uber.org/zap"
)

func TestNextVersion(t *testing.T) {
Expand All @@ -15,29 +14,38 @@ func TestNextVersion(t *testing.T) {
expect string
}{
"none": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "feat: initial commit"},
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "chore: not much"},
}, expect: "v0.1.0"},
"patch": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "feat: initial commit"},
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "fix: minor bug"},
}, expect: "v0.1.1"},
"minor": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "feat: initial commit"},
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "feat: cool new feature"},
}, expect: "v0.2.0"},
"major": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "feat: initial commit"},
{"tag", "v0.1.0"},
{"commit", "--allow-empty", "-m", "feat: major refactor\nBREAKING CHANGE: new stuff"},
}, expect: "v1.0.0"},
}
log, err := zap.NewDevelopment()
if err != nil {
t.Fatalf("couldn't get logger: %v", err)
"major fix bang": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "fix!: major bug"},
}, expect: "v1.0.0"},
"major feat bang": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "feat!: major change"},
}, expect: "v1.0.0"},
"patch with scope": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "fix(lasers): minor bug"},
}, expect: "v0.1.1"},
"minor with scope": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "feat(phasers): cool new feature"},
}, expect: "v0.2.0"},
"major with scope": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "feat(blasters): major refactor\nBREAKING CHANGE: new stuff"},
}, expect: "v1.0.0"},
"major fix bang with scope": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "fix(lightsaber)!: major bug"},
}, expect: "v1.0.0"},
"major feat bang with scope": {gitCmds: [][]string{
{"commit", "--allow-empty", "-m", "feat(bowcaster)!: major change"},
}, expect: "v1.0.0"},
}
for name, tc := range testCases {
t.Run(name, func(tt *testing.T) {
Expand All @@ -47,10 +55,17 @@ func TestNextVersion(t *testing.T) {
tt.Fatalf("couldn't get a tempdir: %v", err)
}
// init git repo
initCmd := exec.Command("git", "init")
initCmd.Dir = dir
if err = initCmd.Run(); err != nil {
tt.Fatalf("couldn't git init: %v", err)
initCmds := [][]string{
{"init"},
{"commit", "--allow-empty", "-m", "feat: initial commit"},
{"tag", "v0.1.0"},
}
for _, c := range initCmds {
cmd := exec.Command("git", c...)
cmd.Dir = dir
if output, err := cmd.CombinedOutput(); err != nil {
tt.Fatalf("couldn't run init cmd git %v: %v (%s)", c, err, output)
}
}
for _, c := range tc.gitCmds {
cmd := exec.Command("git", c...)
Expand All @@ -59,8 +74,7 @@ func TestNextVersion(t *testing.T) {
tt.Fatalf("couldn't run git %v: %v (%s)", c, err, output)
}
}
tt.Log(dir)
next, err := ccv.NextVersion(log, dir)
next, err := ccv.NextVersion(dir)
if err != nil {
tt.Fatalf("error from main.nextVersion(): %v", err)
}
Expand Down

0 comments on commit 12c1f76

Please sign in to comment.