1
0
Fork 0
mirror of https://github.com/Luzifer/git-changerelease.git synced 2024-12-20 19:11:17 +00:00
git-changerelease/git.go

79 lines
1.3 KiB
Go
Raw Normal View History

2016-07-14 11:44:21 +00:00
package main
import (
"bytes"
"errors"
"os"
"os/exec"
"strings"
)
var (
gitLogFormat string
gitLogFormatParts = []string{
`%h`, // ShortHash
`%s`, // Subject
`%an`, // AuthorName
`%ae`, // AuthorEmail
}
)
func init() {
gitLogFormat = strings.Join(gitLogFormatParts, `%x09`)
}
type commit struct {
ShortHash string
Subject string
AuthorName string
AuthorEmail string
BumpType semVerBump
2016-07-14 11:44:21 +00:00
}
func parseCommit(line string) (*commit, error) {
t := strings.Split(line, "\t")
if len(t) != 4 {
return nil, errors.New("Unexpected line format")
}
c := &commit{
2016-07-14 11:44:21 +00:00
ShortHash: t[0],
Subject: t[1],
AuthorName: t[2],
AuthorEmail: t[3],
}
for rex, bt := range matchers {
if rex.MatchString(c.Subject) && bt > c.BumpType {
c.BumpType = bt
}
}
if c.BumpType == semVerBumpUndecided {
c.BumpType = semVerBumpMinor
}
return c, nil
2016-07-14 11:44:21 +00:00
}
func git(stderrEnabled bool, args ...string) (string, error) {
buf := bytes.NewBuffer([]byte{})
cmd := exec.Command("git", args...)
cmd.Stdout = buf
if stderrEnabled {
cmd.Stderr = os.Stderr
}
err := cmd.Run()
return strings.TrimSpace(buf.String()), err
}
func gitErr(args ...string) (string, error) {
return git(true, args...)
}
func gitSilent(args ...string) (string, error) {
return git(false, args...)
}