-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
102 lines (98 loc) · 2.69 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"regexp"
"github.com/Masterminds/semver/v3"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"go.uber.org/zap"
)
// nextVersion returns a string containing the next version based on the state
// of the git repository in path.
func nextVersion(path string) (string, error) {
// open repository
r, err := git.PlainOpenWithOptions(path, &git.PlainOpenOptions{DetectDotGit: true})
if err != nil {
return "", fmt.Errorf("couldn't open git repository: %w", err)
}
tags, err := r.Tags()
if err != nil {
return "", fmt.Errorf("couldn't get tags: %w", err)
}
// map tags to commit hashes
tagRefs := map[string]string{}
err = tags.ForEach(func(r *plumbing.Reference) error {
tagRefs[r.Hash().String()] = r.Name().Short()
return nil
})
if err != nil {
return "", fmt.Errorf("couldn't iterate tags: %w", err)
}
if len(tagRefs) == 0 {
// no existing tags
return "v0.1.0", nil
}
// walk commit hashes back from HEAD
commits, err := r.Log(&git.LogOptions{Order: git.LogOrderDFSPost})
if err != nil {
return "", fmt.Errorf("couldn't get commits: %w", err)
}
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 patchRegex.MatchString(c.Message) {
patch = true
}
if minorRegex.MatchString(c.Message) {
minor = true
}
if majorRegex.MatchString(c.Message) {
major = true
}
return nil
})
if err != nil && err != stopIter {
return "", fmt.Errorf("couldn't determine latest tag: %w", err)
}
// not tagged yet,
if latestTag == "" {
return "", fmt.Errorf("couldn't determine latest tag")
}
// found a tag: parse, increment, and return.
latestVersion, err := semver.NewVersion(latestTag)
if err != nil {
return "", fmt.Errorf(`couldn't parse tag "%v": %w`, latestTag, err)
}
var newVersion semver.Version
switch {
case major:
newVersion = latestVersion.IncMajor()
case minor:
newVersion = latestVersion.IncMinor()
case patch:
newVersion = latestVersion.IncPatch()
default:
newVersion = *latestVersion
}
return fmt.Sprintf("%s%s", "v", newVersion.String()), nil
}
func main() {
log, err := zap.NewProduction()
if err != nil {
panic(err)
}
next, err := nextVersion(`.`)
if err != nil {
log.Fatal("couldn't get next version", zap.Error(err))
}
fmt.Println(next)
}