forked from git-chglog/git-chglog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chglog.go
345 lines (293 loc) · 8.73 KB
/
chglog.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Package chglog implements main logic for the CHANGELOG generate.
package chglog
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"text/template"
"time"
gitcmd "github.com/tsuyoshiwada/go-gitcmd"
)
// Options is an option used to process commits
type Options struct {
Processor Processor
NextTag string // Treat unreleased commits as specified tags (EXPERIMENTAL)
CommitFilters map[string][]string // Filter by using `Commit` properties and values. Filtering is not done by specifying an empty value
CommitSortBy string // Property name to use for sorting `Commit` (e.g. `Scope`)
CommitGroupBy string // Property name of `Commit` to be grouped into `CommitGroup` (e.g. `Type`)
CommitGroupSortBy string // Property name to use for sorting `CommitGroup` (e.g. `Title`)
CommitGroupTitleMaps map[string]string // Map for `CommitGroup` title conversion
HeaderPattern string // A regular expression to use for parsing the commit header
HeaderPatternMaps []string // A rule for mapping the result of `HeaderPattern` to the property of `Commit`
IssuePrefix []string // Prefix used for issues (e.g. `#`, `gh-`)
RefActions []string // Word list of `Ref.Action`
MergePattern string // A regular expression to use for parsing the merge commit
MergePatternMaps []string // Similar to `HeaderPatternMaps`
RevertPattern string // A regular expression to use for parsing the revert commit
RevertPatternMaps []string // Similar to `HeaderPatternMaps`
NoteKeywords []string // Keyword list to find `Note`. A semicolon is a separator, like `<keyword>:` (e.g. `BREAKING CHANGE`)
}
// Info is metadata related to CHANGELOG
type Info struct {
Title string // Title of CHANGELOG
RepositoryURL string // URL of git repository
}
// RenderData is the data passed to the template
type RenderData struct {
Info *Info
Unreleased *Unreleased
Versions []*Version
}
// Config for generating CHANGELOG
type Config struct {
Bin string // Git execution command
WorkingDir string // Working directory
Template string // Path for template file. If a relative path is specified, it depends on the value of `WorkingDir`.
Info *Info
Options *Options
}
func normalizeConfig(config *Config) {
opts := config.Options
if opts.HeaderPattern == "" {
opts.HeaderPattern = "^(.*)$"
opts.HeaderPatternMaps = []string{
"Subject",
}
}
if opts.MergePattern == "" {
opts.MergePattern = "^Merge branch '(\\w+)'$"
opts.MergePatternMaps = []string{
"Source",
}
}
if opts.RevertPattern == "" {
opts.RevertPattern = "^Revert \"([\\s\\S]*)\"$"
opts.RevertPatternMaps = []string{
"Header",
}
}
config.Options = opts
}
// Generator of CHANGELOG
type Generator struct {
client gitcmd.Client
config *Config
tagReader *tagReader
tagSelector *tagSelector
commitParser *commitParser
commitExtractor *commitExtractor
}
// NewGenerator receives `Config` and create an new `Generator`
func NewGenerator(config *Config) *Generator {
client := gitcmd.New(&gitcmd.Config{
Bin: config.Bin,
})
if config.Options.Processor != nil {
config.Options.Processor.Bootstrap(config)
}
normalizeConfig(config)
return &Generator{
client: client,
config: config,
tagReader: newTagReader(client),
tagSelector: newTagSelector(),
commitParser: newCommitParser(client, config),
commitExtractor: newCommitExtractor(config.Options),
}
}
// Generate gets the commit based on the specified tag `query` and writes the result to `io.Writer`
//
// tag `query` can be specified with the following rule
// <old>..<new> - Commit contained in `<new>` tags from `<old>` (e.g. `1.0.0..2.0.0`)
// <tagname>.. - Commit from the `<tagname>` to the latest tag (e.g. `1.0.0..`)
// ..<tagname> - Commit from the oldest tag to `<tagname>` (e.g. `..1.0.0`)
// <tagname> - Commit contained in `<tagname>` (e.g. `1.0.0`)
func (gen *Generator) Generate(w io.Writer, query string) error {
back, err := gen.workdir()
if err != nil {
return err
}
defer back()
tags, first, err := gen.getTags(query)
if err != nil {
return err
}
unreleased, err := gen.readUnreleased(tags)
if err != nil {
return err
}
versions, err := gen.readVersions(tags, first)
if err != nil {
return err
}
if len(versions) == 0 {
return fmt.Errorf("commits corresponding to \"%s\" was not found", query)
}
return gen.render(w, unreleased, versions)
}
func (gen *Generator) readVersions(tags []*Tag, first string) ([]*Version, error) {
next := gen.config.Options.NextTag
versions := []*Version{}
for i, tag := range tags {
var (
isNext = next == tag.Name
rev string
)
if isNext {
if tag.Previous != nil {
rev = tag.Previous.Name + "..HEAD"
} else {
rev = "HEAD"
}
} else {
if i+1 < len(tags) {
rev = tags[i+1].Name + ".." + tag.Name
} else {
if first != "" {
rev = first + ".." + tag.Name
} else {
rev = tag.Name
}
}
}
commits, err := gen.commitParser.Parse(rev)
if err != nil {
return nil, err
}
commitGroups, mergeCommits, revertCommits, noteGroups := gen.commitExtractor.Extract(commits)
versions = append(versions, &Version{
Tag: tag,
CommitGroups: commitGroups,
Commits: commits,
MergeCommits: mergeCommits,
RevertCommits: revertCommits,
NoteGroups: noteGroups,
})
// Instead of `getTags()`, assign the date to the tag
if isNext && len(commits) != 0 {
tag.Date = commits[0].Author.Date
}
}
return versions, nil
}
func (gen *Generator) readUnreleased(tags []*Tag) (*Unreleased, error) {
if gen.config.Options.NextTag != "" {
return &Unreleased{}, nil
}
rev := "HEAD"
if len(tags) > 0 {
rev = tags[0].Name + "..HEAD"
}
commits, err := gen.commitParser.Parse(rev)
if err != nil {
return nil, err
}
commitGroups, mergeCommits, revertCommits, noteGroups := gen.commitExtractor.Extract(commits)
unreleased := &Unreleased{
CommitGroups: commitGroups,
Commits: commits,
MergeCommits: mergeCommits,
RevertCommits: revertCommits,
NoteGroups: noteGroups,
}
return unreleased, nil
}
func (gen *Generator) getTags(query string) ([]*Tag, string, error) {
tags, err := gen.tagReader.ReadAll()
if err != nil {
return nil, "", err
}
next := gen.config.Options.NextTag
if next != "" {
for _, tag := range tags {
if next == tag.Name {
return nil, "", fmt.Errorf("\"%s\" tag already exists", next)
}
}
var previous *RelateTag
if len(tags) > 0 {
previous = &RelateTag{
Name: tags[0].Name,
Subject: tags[0].Subject,
Date: tags[0].Date,
}
}
// Assign the date with `readVersions()`
tags = append([]*Tag{
&Tag{
Name: next,
Subject: next,
Previous: previous,
},
}, tags...)
}
if len(tags) == 0 {
return nil, "", errors.New("git-tag does not exist")
}
first := ""
if query != "" {
tags, first, err = gen.tagSelector.Select(tags, query)
if err != nil {
return nil, "", err
}
}
return tags, first, nil
}
func (gen *Generator) workdir() (func() error, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
err = os.Chdir(gen.config.WorkingDir)
if err != nil {
return nil, err
}
return func() error {
return os.Chdir(cwd)
}, nil
}
func (gen *Generator) render(w io.Writer, unreleased *Unreleased, versions []*Version) error {
if _, err := os.Stat(gen.config.Template); err != nil {
return err
}
fmap := template.FuncMap{
// format the input time according to layout
"datetime": func(layout string, input time.Time) string {
return input.Format(layout)
},
// check whether substs is withing s
"contains": func(s, substr string) bool {
return strings.Contains(s, substr)
},
// check whether s begins with prefix
"hasPrefix": func(s, prefix string) bool {
return strings.HasPrefix(s, prefix)
},
// check whether s ends with suffix
"hasSuffix": func(s, suffix string) bool {
return strings.HasSuffix(s, suffix)
},
// replace the first n instances of old with new
"replace": func(s, old, new string, n int) string {
return strings.Replace(s, old, new, n)
},
// lower case a string
"lower": func(s string) string {
return strings.ToLower(s)
},
// upper case a string
"upper": func(s string) string {
return strings.ToUpper(s)
},
}
fname := filepath.Base(gen.config.Template)
t := template.Must(template.New(fname).Funcs(fmap).ParseFiles(gen.config.Template))
return t.Execute(w, &RenderData{
Info: gen.config.Info,
Unreleased: unreleased,
Versions: versions,
})
}