Skip to content

Commit d014797

Browse files
authored
feat(log): add commit scope support (#7)
1 parent 28f9eb7 commit d014797

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

log/log.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ package commitlog
55
import (
66
"bytes"
77
"fmt"
8+
"regexp"
89
"strings"
910

1011
"github.com/go-git/go-git/v5"
1112
"github.com/go-git/go-git/v5/plumbing/object"
1213
)
1314

15+
// SupportedKeys - keys that are supported by the package
16+
const SupportedKeys = "ci|refactor|docs|fix|feat|test|chore|other"
17+
1418
// ErrMessage - simple interface around error with a custom message
1519
type ErrMessage struct {
1620
Message string
@@ -171,8 +175,9 @@ func CommitLog(path string, startCommitString string, endCommitString string, in
171175
return "", ErrMessage{"Error getting commits : ", err}
172176
}
173177

174-
var inclusions commitTypeInclusions = bytes.SplitN([]byte(inclusionFlags), []byte(","), -1)
175-
178+
var inclusions commitTypeInclusions
179+
inclusions = append(inclusions, bytes.SplitN([]byte(inclusionFlags), []byte("|"), -1)...)
180+
inclusions = append(inclusions, bytes.SplitN([]byte(inclusionFlags), []byte(","), -1)...)
176181
logContainer := logsByCategory{}
177182

178183
logContainer.Setup()
@@ -188,7 +193,8 @@ func CommitLog(path string, startCommitString string, endCommitString string, in
188193

189194
for _, c := range commits {
190195
normalizedHash := c.Hash.String() + " - " + normalizeCommit(c.Message)
191-
key := strings.SplitN(strings.TrimSpace(c.Message), ":", 2)[0]
196+
key := findKeyInCommit(SupportedKeys, c.Message)
197+
key = strings.SplitN(strings.TrimSpace(key), ":", 2)[0]
192198

193199
logContainer.AddCommit(key, normalizedHash, skipClassification)
194200

@@ -212,3 +218,16 @@ func (inclusions *commitTypeInclusions) checkInclusion(flagToCheck string) bool
212218
}
213219
return false
214220
}
221+
222+
func findKeyInCommit(key string, commitMessage string) string {
223+
re := regexp.MustCompile(`^(` + key + `)[:]|^((` + key + `)\((\w+[, /\\]*)+\)[:])`)
224+
if len(re.FindAllStringSubmatch(commitMessage, -1)) > 0 {
225+
keyCheckOne := re.FindAllStringSubmatch(commitMessage, -1)[0][3]
226+
if keyCheckOne == "" {
227+
keyCheckTwo := re.FindAllStringSubmatch(commitMessage, -1)[0][1]
228+
return keyCheckTwo
229+
}
230+
return keyCheckOne
231+
}
232+
return ""
233+
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func main() {
1414
var repoPath = flag.String("p", ".", "path to the repository, points to the current working directory by default")
1515
var startCommit = flag.String("s", "", "commit hash string / revision (ex. HEAD, HEAD^, HEAD~2) \n to start collecting commit messages from")
1616
var endCommit = flag.String("e", "", "commit hash string / revision (ex. HEAD, HEAD^, HEAD~2) \n to stop collecting commit message at")
17-
var inclusionFlags = flag.String("i", "ci,refactor,docs,fix,feat,test,chore,other", "commit types to be includes")
17+
var inclusionFlags = flag.String("i", clog.SupportedKeys, "commit types to be includes")
1818
var skipClassification = flag.Bool("skip", false, "if true will skip trying to classify and just give a list of changes")
1919

2020
flag.Parse()

0 commit comments

Comments
 (0)