/
main.go
144 lines (128 loc) · 3.28 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
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
142
143
144
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os/exec"
"strings"
"github.com/richterrettich/changelog/domain"
)
func main() {
var from = flag.String("from", "HEAD", "startpoint for changelog generation")
var to = flag.String("to", "", "endpoint for changelog generation")
var dir = flag.String("dir", ".", "the directory the git repository is located")
flag.Parse()
groupedCommits := groupConsumer(objectPipe(source(*dir, *from, *to)))
printMarkdown(groupedCommits)
}
func source(dir, from, to string) <-chan string {
var cmd *exec.Cmd
if to == "" {
cmd = exec.Command("git", "-C", dir, "log", `--pretty=format:hash: %H%n-----%nauthor: %an <%ae>%n-----%nrawSubject: %s%n-----%nrawBody: %b%n-----%nEND-COMMIT%n`, from)
} else {
cmd = exec.Command("git", "-C", dir, "log", `--pretty=format:hash: %H%n-----%nauthor: %an <%ae>%n-----%nrawSubject: %s%n-----%nrawBody: %b%n-----%nEND-COMMIT%n`, to+".."+from)
}
outputChannel := make(chan string)
output, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
go func() {
scanner := bufio.NewScanner(output)
for scanner.Scan() {
outputChannel <- scanner.Text()
}
defer output.Close()
close(outputChannel)
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}()
return outputChannel
}
func objectPipe(input <-chan string) <-chan *domain.Commit {
outputChannel := make(chan *domain.Commit)
go func() {
chunk := ""
result := &domain.Commit{}
for line := range input {
if line == "-----" {
processChunk(chunk, result)
chunk = ""
} else {
chunk = chunk + "\n" + line
}
if line == "END-COMMIT" {
result.ParseBody()
result.ParseSubject()
outputChannel <- result
result = &domain.Commit{}
}
}
close(outputChannel)
}()
return outputChannel
}
func processChunk(chunk string, obj *domain.Commit) {
chunk = strings.TrimSpace(chunk)
parts := strings.Split(chunk, ":")
key, value := parts[0], strings.Join(parts[1:], ":")
switch key {
case "hash":
obj.Hash = value
case "rawSubject":
obj.RawSubject = value
case "author":
obj.Author = value
case "rawBody":
obj.RawBody = value
}
}
func groupConsumer(input <-chan *domain.Commit) map[domain.CommitType][]*domain.Commit {
result := make(map[domain.CommitType][]*domain.Commit)
for commit := range input {
if result[commit.Type] == nil {
result[commit.Type] = make([]*domain.Commit, 0)
}
result[commit.Type] = append(result[commit.Type], commit)
}
return result
}
func printMarkdown(input map[domain.CommitType][]*domain.Commit) {
breakingChanges := make([]string, 0)
for k, v := range input {
switch k {
case domain.Fix:
fmt.Println("### Bug Fixes")
printCommits(v)
case domain.Feature:
fmt.Println("### Features")
printCommits(v)
case domain.Refactoring:
fmt.Println("### Refactoring")
printCommits(v)
}
for _, commit := range v {
breakingChanges = append(breakingChanges, commit.BreakingChanges...)
}
}
if len(breakingChanges) > 0 {
fmt.Println("### BREAKING CHANGES")
fmt.Println()
for _, v := range breakingChanges {
fmt.Println("- " + v)
}
}
}
func printCommits(commits []*domain.Commit) {
for _, commit := range commits {
fmt.Println("- " + commit.Subject)
fmt.Println(commit.Body)
fmt.Println()
}
}