-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
git.go
85 lines (72 loc) · 1.62 KB
/
git.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
package git
import (
"errors"
"fmt"
"os/exec"
"strings"
"github.com/gobwas/glob"
)
// copied from goreleaser
// IsRepo returns true if current folder is a git repository
func IsRepo() bool {
out, err := run("rev-parse", "--is-inside-work-tree")
return err == nil && strings.TrimSpace(out) == "true"
}
func getAllTags(args ...string) ([]string, error) {
tags, err := run(append([]string{"tag", "--sort=-version:refname"}, args...)...)
if err != nil {
return nil, err
}
return strings.Split(tags, "\n"), nil
}
func DescribeTag(tagMode string, pattern string) (string, error) {
args := []string{}
if tagMode == "current-branch" {
args = []string{"--merged"}
}
tags, err := getAllTags(args...)
if err != nil {
return "", err
}
if len(tags) == 0 {
return "", nil
}
if pattern == "" {
return tags[0], nil
}
g, err := glob.Compile(pattern)
if err != nil {
return "", err
}
for _, tag := range tags {
if g.Match(tag) {
return tag, nil
}
}
return "", fmt.Errorf("no tags match '%s'", pattern)
}
func Changelog(tag string) (string, error) {
if tag == "" {
return gitLog("HEAD")
} else {
return gitLog(fmt.Sprintf("tags/%s..HEAD", tag))
}
}
func run(args ...string) (string, error) {
extraArgs := []string{
"-c", "log.showSignature=false",
}
args = append(extraArgs, args...)
/* #nosec */
cmd := exec.Command("git", args...)
bts, err := cmd.CombinedOutput()
if err != nil {
return "", errors.New(string(bts))
}
return string(bts), nil
}
func gitLog(refs ...string) (string, error) {
args := []string{"log", "--no-decorate", "--no-color"}
args = append(args, refs...)
return run(args...)
}