/
cmd.go
141 lines (116 loc) · 2.86 KB
/
cmd.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"sort"
"time"
"github.com/Masterminds/semver"
"github.com/codegangsta/cli"
)
var (
debugGit bool
timeLayout = `Mon, 02 Jan 2006 15:04:05 -0700`
ghRE = regexp.MustCompile(`Merge pull request (\#\d+) from (.+)\/`)
actRE = regexp.MustCompile(`\[([\w\s\.]+)\]`)
changeRe = regexp.MustCompile(`\] ([^\]\n]+)`)
nullbyte = []byte{0x00}
)
type actionsMap map[string][]string
func changelogCmd(c *cli.Context) {
debugGit = c.Bool("debug")
vs := getSemverTags()
context := make([]Version, 0)
for i, v := range vs {
if i == len(vs)-1 {
// skip last tag
continue
}
tagShow := git("show", `--pretty=%cD`, v.Original())
tagTime := bytes.Split(tagShow, []byte("\n"))[0]
tagdatetime, _ := time.Parse(timeLayout, string(tagTime))
version := Version{
Version: v.Original(),
Date: tagdatetime,
}
tagRange := fmt.Sprintf("%s...%s", vs[i+1].Original(), v.Original())
merge := git("log", `--pretty=%B%x00`, "--merges", tagRange)
version.Actions = parseActions(merge)
context = append(context, version)
}
render(context)
}
func git(params ...string) []byte {
cmd := exec.Command("git", params...)
output, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
if debugGit {
log.Printf("%q %q", cmd.Path, cmd.Args)
log.Printf("%s", output)
}
return output
}
func parseActions(commits []byte) []Action {
a := []Action{}
actsMap := make(actionsMap)
commitsSplit := bytes.Split(commits, nullbyte)
if len(commitsSplit) == 0 {
return a
}
for _, c := range commitsSplit {
if ghRE.Match(c) {
ghParse(c, actsMap)
} else if actRE.Match(c) {
regularParse(c, actsMap)
}
}
for k, v := range actsMap {
a = append(a, Action{
Action: k,
Changes: v,
})
}
return a
}
func getSemverTags() []*semver.Version {
rowTags := git("tag")
tags := bytes.Split(rowTags, []byte("\n"))
tags = tags[:len(tags)-1]
vs := make([]*semver.Version, 0)
for _, r := range tags {
v, err := semver.NewVersion(string(r))
if err == nil {
vs = append(vs, v)
}
}
sort.Sort(sort.Reverse(semver.Collection(vs)))
return vs
}
func render(c []Version) {
if err := changeLogTpl.Execute(os.Stdout, c); err != nil {
log.Fatal(err)
}
}
func ghParse(b []byte, m actionsMap) {
match := ghRE.FindAllSubmatch(b, -1)
prauthor := match[0]
ghPR, ghAuthor := prauthor[1], prauthor[2]
actualTitle := bytes.Split(b, []byte("\n\n"))[1]
act, change := extractAction(actualTitle)
change = fmt.Sprintf("%s. PR [%s][] by [@%s][].", change, ghPR, ghAuthor)
m[act] = append(m[act], change)
}
func extractAction(b []byte) (string, string) {
act := actRE.FindAllSubmatch(b, -1)[0][1]
change := changeRe.FindAllSubmatch(b, -1)[0][1]
return string(act), string(change)
}
func regularParse(b []byte, m actionsMap) {
act, change := extractAction(b)
m[act] = append(m[act], change)
}