-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
206 lines (182 loc) · 5.25 KB
/
generate.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
package cmd
import (
"github.com/jitesoft/cc-gen/gitwrapper"
"github.com/jitesoft/cc-gen/gitwrapper/conventional"
"github.com/jitesoft/cc-gen/internal/template"
"github.com/spf13/cobra"
"io/ioutil"
"log"
"os"
"path/filepath"
)
var (
commitUri string // Uri to where commits should be linked.
full bool // If true, _all_ tags should be generated in the changelog.
stdout bool // If output is to go to stdout instead of file.
prepend bool // If changes should be prepended in case a changelog already exist.
untilTag string // Tag name to stop at, will override 'full'.
fromTag string // Tag name to use as start.
fileName string // Name of file, if not stdout.
)
var generateCmd = &cobra.Command{
Use: "generate [tag name]",
Aliases: []string{"gen", "build"},
Short: "Generate a changelog.",
Long: `Generates a changelog in the current project.
Passing a tag name (optional) will mark the latest as under the given tag.
If the latest commit is tagged, that will be used instead of the tag name argument.
If the latest commit is not tagged and tag name is excluded 'latest' will be used instead.`,
Example: "cc-gen generate ./ 1.2.3",
Args: cobra.MaximumNArgs(2),
Version: Version,
RunE: func(cmd *cobra.Command, args []string) error {
path := "./"
if len(args) > 0 {
path = args[0]
}
branch, err := gitwrapper.GetCurrentBranch(path)
latestTag := "latest"
if len(args) > 1 {
latestTag = args[0]
}
ap, err := filepath.Abs(path)
if err != nil {
log.Panic(err)
}
log.Printf("Building changelog for %s", ap)
path = ap
if err != nil {
log.Panic(err)
}
tags, err := gitwrapper.GetTags(path)
if err != nil {
log.Panic(err)
}
if len(branch.Commits) <= 0 {
log.Println("Can't generate changelog without any commits")
os.Exit(0)
}
if len(tags) > 0 && branch.Commits[0].Hash == tags[0].Hash {
latestTag = tags[0].Name
}
firstHash := branch.Commits[0].Hash
lastHash := branch.Commits[len(branch.Commits)-1].Hash
if !full {
if len(fromTag) > 0 {
t := gitwrapper.FindTag(tags, fromTag)
latestTag = t.Name
firstHash = t.Hash
}
if len(untilTag) > 0 {
lastHash = gitwrapper.FindTag(tags, untilTag).Hash
}
}
tagData := make(map[string][]*conventional.Commit)
currentTag := latestTag
hasFirst := false
tagData[currentTag] = []*conventional.Commit{}
for _, c := range branch.Commits {
// Check if the hash is a tag, if it is, we want to use another 'current tag'.
// Have to be done even if we haven't reached the first hash.
if tag := gitwrapper.FindTagByHash(tags, c.Hash); tag != nil {
currentTag = tag.Name
}
// Find first hash or continue.
if !hasFirst && c.Hash != firstHash {
continue
}
hasFirst = true
// Last hash? then break!
if c.Hash == lastHash {
break
}
// If the commit is conventional, we add it to the list.
if conventional.IsConventional(c) {
cc, _ := conventional.ParseConventional(c)
// currentTag is the tag name, we add the commit to its array.
tagData[currentTag] = append(tagData[currentTag], cc)
}
}
// After collecting all commits, we have to create the tag data and group
// the commits under their 'type'. Can't be done above, as we don't have
// all in the list at that point.
var outputData []*template.TagData
for t, c := range tagData {
outputData = append(outputData, &template.TagData{
Commits: conventional.GroupByType(c),
Name: t,
})
}
output := os.Stdout
extra := ""
if !stdout {
if !prepend {
output, _ = os.Create(fileName)
} else {
output, _ = os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0755)
fileData, err := ioutil.ReadFile(fileName)
if err != nil {
extra = ""
} else {
extra = string(fileData)
}
}
}
err = template.RenderTemplate(&template.Data{
CommitUri: commitUri,
Extra: extra,
Tags: outputData,
}, "default", output)
if err != nil {
log.Panic(err)
}
return nil
},
}
func init() {
generateCmd.Flags().StringVar(
&fromTag,
"from",
"",
"Specific tag to use as start tag for changelog (uses latest commit as default)",
)
generateCmd.Flags().StringVar(
&untilTag,
"to",
"",
"Specific tag to stop at (defaults to latest tag or, in case it shares hash with latest commit, the one before)",
)
generateCmd.Flags().BoolVar(
&full,
"full",
false,
"Setting this to true indicates that a full changelog should be generated instead of just since last tag (defaults to false)",
)
generateCmd.Flags().BoolVar(
&prepend,
"prepend",
true,
"If true and a [output] file exists, the tool will prepend to the file, else it will create or replace the [output] file (default true)",
)
generateCmd.Flags().BoolVar(
&stdout,
"stdout",
false,
"Setting this to true will print the changelog to stdout instead of a file (default false)",
)
generateCmd.Flags().StringVarP(
&commitUri,
"commit-uri",
"c",
"",
"Sets base URI to use as link to commits. Uses sprintf format with a single %s where hash will be inserted",
)
generateCmd.Flags().StringVarP(
&fileName,
"output",
"o",
"CHANGELOG.md",
"Filename to write the generated changelog to. Not used in `stdout` mode (defaults to CHANGELOG.md)",
)
rootCmd.AddCommand(generateCmd)
}