This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbumptag.go
420 lines (367 loc) · 9.46 KB
/
bumptag.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// The bumptag creates a new tag to release a new version of your code.
//
// The tool finds the last git tag, increments it and create new tag with a changelog.
// https://github.com/sv-tools/bumptag/blob/master/README.md
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"github.com/coreos/go-semver/semver"
)
var (
version = "0.0.0"
tagPrefix = "v"
)
const (
defaultRemote = "origin"
defaultEditor = "vim"
)
func realGit(input string, arg ...string) (string, error) {
cmd := exec.Command("git", arg...)
if len(input) > 0 {
cmd.Stdin = strings.NewReader(input)
}
var stdout bytes.Buffer
cmd.Stdout = &stdout
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
text := fmt.Sprintf(
"command '%s' failed: %s",
strings.Join(cmd.Args, " "),
err.Error(),
)
errText := strings.TrimSpace(stderr.String())
if len(errText) > 0 {
text += "\n" + errText
}
return "", errors.New(text)
}
return strings.TrimSpace(stdout.String()), nil
}
var git = realGit
func noOutputGit(input string, arg ...string) error {
_, err := git(input, arg...)
return err
}
func disableGPG() (string, error) {
output, _ := git("", "config", "--local", "--get", "log.showSignature")
if err := noOutputGit("", "config", "--local", "log.showSignature", "false"); err != nil {
return "", err
}
return output, nil
}
func restoreGPG(oldValue string) error {
if len(oldValue) > 0 {
return noOutputGit("", "config", "--local", "log.showSignature", oldValue)
}
return noOutputGit("", "config", "--local", "--unset", "log.showSignature")
}
func setUpGPG() (func(), error) {
oldValue, err := disableGPG()
if err != nil {
return nil, err
}
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
go func() {
<-signalChan
_ = restoreGPG(oldValue)
os.Exit(42)
}()
return func() {
signal.Stop(signalChan)
_ = restoreGPG(oldValue)
}, nil
}
func gitConfig(name, defaultValue string) string {
output, err := git("", "config", "--get", name)
if err != nil {
return defaultValue
}
return output
}
func gitConfigBool(name string, defaultValue bool) bool {
output := gitConfig(name, strconv.FormatBool(defaultValue))
value, err := strconv.ParseBool(output)
if err != nil {
return defaultValue
}
return value
}
func findTag() (*semver.Version, string, error) {
output, err := git("", "tag")
if err != nil {
return nil, "", err
}
if output == "" {
return &semver.Version{}, "", nil
}
output, err = git("", "describe", "--tags", "--abbrev=0")
if err != nil {
return nil, "", err
}
currentTagName := output
if !strings.HasPrefix(output, tagPrefix) {
tagPrefix = ""
}
dotParts := strings.SplitN(output, ".", 3)
for i := 3 - len(dotParts); i > 0; i-- {
output += ".0"
}
tag, err := semver.NewVersion(strings.TrimPrefix(output, tagPrefix))
if err != nil {
return nil, "", err
}
return tag, currentTagName, nil
}
func createTag(tagName, annotation string, sign bool) error {
args := []string{"tag", "-F-"}
if sign {
args = append(args, "--sign")
}
args = append(args, tagName)
return noOutputGit(annotation, args...)
}
func showTag(tagName string) (string, error) {
return git("", "show", tagName)
}
func getChangeLog(tagName string) (string, error) {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
output, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return "", err
}
return string(output), nil
}
args := []string{"log", "--pretty=%h %s", "--no-merges"}
if len(tagName) > 0 {
args = append(args, tagName+"..HEAD")
}
output, err := git("", args...)
if err != nil {
return "", err
}
var res []string
for _, line := range strings.Split(output, "\n") {
res = append(res, "* "+line)
}
return strings.Join(res, "\n"), nil
}
func parseRemote(remote string) (string, error) {
for _, part := range strings.Split(remote, " ") {
if strings.HasPrefix(part, "[") {
part = strings.Trim(part, "[]")
names := strings.SplitN(part, "/", 2)
if len(names) != 2 {
return "", fmt.Errorf("cannot determine a remote name: %s", part)
}
return names[0], nil
}
}
return "", fmt.Errorf("remote for the active branch '%s' not found", remote)
}
func getRemote() (string, error) {
output, err := git("", "branch", "--list", "-vv")
if err != nil {
return "", err
}
for _, remote := range strings.Split(output, "\n") {
remote = strings.TrimSpace(remote)
if strings.HasPrefix(remote, "*") {
return parseRemote(remote)
}
}
return defaultRemote, nil
}
func pushTag(remote, tagName string) error {
return noOutputGit("", "push", remote, tagName)
}
func makeAnnotation(changeLog string, tagName string) string {
output := []string{
"Bump version " + tagName,
"",
changeLog,
}
return strings.Join(output, "\n")
}
func createFlag(flagSet *flag.FlagSet, name, short string, value bool, usage string) *bool {
p := flagSet.Bool(name, value, usage)
if len(short) > 0 {
flagSet.BoolVar(p, short, value, usage)
}
return p
}
type bumptagArgs struct {
flagSet *flag.FlagSet
edit *bool
dryRun *bool
silent *bool
autoPush *bool
major *bool
minor *bool
patch *bool
version *bool
findTag *bool
}
func (f *bumptagArgs) usage() {
output := `Usage: bumptag [<tagname>]
<tagname> The name of the tag to create, must be Semantic Versions 2.0.0 (http://semver.org)
-e, --edit Edit an annotation
-r, --dry-run Prints an annotation for the new tag
-s, --silent Do not show the created tag
-a, --auto-push Push the created tag automatically
-m, --major Increment the MAJOR version
-n, --minor Increment the MINOR version (default)
-p, --patch Increment the PATCH version
--version Show a version of the bumptag tool
--find-tag Show the last tag, can be useful for CI tools
The change log is automatically generated from git commits from the previous tag or can be passed by <stdin>.`
fmt.Println(output)
}
func (f *bumptagArgs) parse() error {
f.flagSet.Usage = f.usage
return f.flagSet.Parse(os.Args[1:])
}
func newBumptagArgs() *bumptagArgs {
flagSet := flag.NewFlagSet("Bumptag", flag.ExitOnError)
return &bumptagArgs{
flagSet: flagSet,
edit: createFlag(flagSet, "edit", "e", false, "Edit an annotation"),
dryRun: createFlag(flagSet, "dry-run", "r", false, "Prints an annotation for the new tag"),
silent: createFlag(flagSet, "silent", "s", false, "Do not show the created tag"),
autoPush: createFlag(flagSet, "auto-push", "a", false, "Push the created tag automatically"),
major: createFlag(flagSet, "major", "m", false, "Increment the MAJOR version"),
minor: createFlag(flagSet, "minor", "n", false, "Increment the MINOR version (default)"),
patch: createFlag(flagSet, "patch", "p", false, "Increment the PATCH version"),
version: createFlag(flagSet, "version", "", false, "Show a version of the bumptag tool"),
findTag: createFlag(flagSet, "find-tag", "", false, "Show the latest tag, can be useful for CI tools"),
}
}
func setTag(flagSet *flag.FlagSet, tag *semver.Version, args *bumptagArgs) {
if flagSet.NArg() > 0 {
if err := tag.Set(strings.TrimPrefix(flagSet.Arg(0), tagPrefix)); err != nil {
panic(err)
}
} else {
switch true {
case *args.major:
tag.BumpMajor()
case *args.minor:
tag.BumpMinor()
case *args.patch:
tag.BumpPatch()
default:
tag.BumpMinor()
}
}
}
func panicIfError(err error) {
if err != nil {
panic(err)
}
}
func openEditor(filename string) error {
editor := os.Getenv("EDITOR")
if editor == "" {
editor = defaultEditor
}
executable, err := exec.LookPath(editor)
if err != nil {
return err
}
tty := os.Stdin
stat, _ := tty.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
tty, err = os.Open("/dev/tty")
if err != nil {
return err
}
defer tty.Close()
}
cmd := exec.Command(executable, filename)
cmd.Stdin = tty
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func edit(annotation string) (string, error) {
file, err := ioutil.TempFile(os.TempDir(), "*")
if err != nil {
return "", err
}
filename := file.Name()
defer os.Remove(filename)
if _, err := file.WriteString(annotation); err != nil {
return "", err
}
if err := file.Close(); err != nil {
return "", err
}
if err := openEditor(filename); err != nil {
return "", err
}
data, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
return string(data), nil
}
func main() {
args := newBumptagArgs()
panicIfError(args.parse())
if *args.version {
fmt.Print(version)
return
}
tearDownGPG, err := setUpGPG()
panicIfError(err)
defer tearDownGPG()
tag, currentTagName, err := findTag()
panicIfError(err)
if *args.findTag {
fmt.Print(currentTagName)
return
}
changeLog, err := getChangeLog(currentTagName)
panicIfError(err)
setTag(args.flagSet, tag, args)
tagName := tagPrefix + tag.String()
annotation := makeAnnotation(changeLog, tagName)
if *args.edit {
annotation, err = edit(annotation)
panicIfError(err)
}
if *args.dryRun {
fmt.Println(annotation)
return
}
sign := gitConfigBool("commit.gpgsign", false)
panicIfError(createTag(tagName, annotation, sign))
if *args.autoPush {
remote, err := getRemote()
panicIfError(err)
panicIfError(pushTag(remote, tagName))
if !*args.silent {
fmt.Printf(
"The tag '%s' has been pushed to the remote '%s'",
tagName,
remote,
)
}
}
if !*args.silent {
output, err := showTag(tagName)
panicIfError(err)
fmt.Println(output)
}
}