2019-11-17 16:17:24 +00:00
|
|
|
package sv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-02-01 21:19:38 +00:00
|
|
|
"time"
|
2019-11-17 16:17:24 +00:00
|
|
|
|
|
|
|
"github.com/Masterminds/semver"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
logSeparator = "##"
|
|
|
|
endLine = "~~"
|
2020-02-01 22:43:02 +00:00
|
|
|
breakingChangesKey = "breakingchange"
|
|
|
|
issueIDKey = "issueid"
|
2019-11-17 16:17:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Git commands
|
|
|
|
type Git interface {
|
|
|
|
Describe() string
|
2020-02-01 21:19:38 +00:00
|
|
|
Log(initialTag, endTag string) ([]GitCommitLog, error)
|
2019-11-17 16:17:24 +00:00
|
|
|
Tag(version semver.Version) error
|
2020-02-01 21:19:38 +00:00
|
|
|
Tags() ([]GitTag, error)
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GitCommitLog description of a single commit log
|
|
|
|
type GitCommitLog struct {
|
|
|
|
Hash string `json:"hash,omitempty"`
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Scope string `json:"scope,omitempty"`
|
|
|
|
Subject string `json:"subject,omitempty"`
|
|
|
|
Body string `json:"body,omitempty"`
|
|
|
|
Metadata map[string]string `json:"metadata,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-02-01 21:19:38 +00:00
|
|
|
// GitTag git tag info
|
|
|
|
type GitTag struct {
|
|
|
|
Name string
|
|
|
|
Date time.Time
|
|
|
|
}
|
|
|
|
|
2019-11-17 16:17:24 +00:00
|
|
|
// GitImpl git command implementation
|
|
|
|
type GitImpl struct {
|
2020-02-01 22:43:02 +00:00
|
|
|
messageMetadata map[string][]string
|
2019-11-17 16:17:24 +00:00
|
|
|
tagPattern string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGit constructor
|
2020-02-01 22:43:02 +00:00
|
|
|
func NewGit(breakinChangePrefixes, issueIDPrefixes []string, tagPattern string) *GitImpl {
|
|
|
|
return &GitImpl{
|
|
|
|
messageMetadata: map[string][]string{breakingChangesKey: breakinChangePrefixes, issueIDKey: issueIDPrefixes},
|
|
|
|
tagPattern: tagPattern,
|
|
|
|
}
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Describe runs git describe, it no tag found, return empty
|
|
|
|
func (GitImpl) Describe() string {
|
|
|
|
cmd := exec.Command("git", "describe", "--abbrev=0")
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(strings.Trim(string(out), "\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log return git log
|
2020-02-01 21:19:38 +00:00
|
|
|
func (g GitImpl) Log(initialTag, endTag string) ([]GitCommitLog, error) {
|
2019-11-17 16:17:24 +00:00
|
|
|
format := "--pretty=format:\"%h" + logSeparator + "%s" + logSeparator + "%b" + endLine + "\""
|
2020-02-01 21:19:38 +00:00
|
|
|
var cmd *exec.Cmd
|
|
|
|
if initialTag == "" && endTag == "" {
|
|
|
|
cmd = exec.Command("git", "log", format)
|
|
|
|
} else if endTag == "" {
|
|
|
|
cmd = exec.Command("git", "log", initialTag+"..HEAD", format)
|
|
|
|
} else if initialTag == "" {
|
|
|
|
cmd = exec.Command("git", "log", endTag, format)
|
|
|
|
} else {
|
|
|
|
cmd = exec.Command("git", "log", initialTag+".."+endTag, format)
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return parseLogOutput(g.messageMetadata, string(out)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tag create a git tag
|
|
|
|
func (g GitImpl) Tag(version semver.Version) error {
|
2019-12-04 22:37:50 +00:00
|
|
|
tag := fmt.Sprintf(g.tagPattern, version.Major(), version.Minor(), version.Patch())
|
2019-12-04 22:50:02 +00:00
|
|
|
tagMsg := fmt.Sprintf("Version %d.%d.%d", version.Major(), version.Minor(), version.Patch())
|
2019-12-04 22:37:50 +00:00
|
|
|
|
2019-12-04 22:50:02 +00:00
|
|
|
tagCommand := exec.Command("git", "tag", "-a", tag, "-m", tagMsg)
|
2019-12-04 22:37:50 +00:00
|
|
|
if err := tagCommand.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pushCommand := exec.Command("git", "push", "origin", tag)
|
|
|
|
return pushCommand.Run()
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 21:19:38 +00:00
|
|
|
// Tags list repository tags
|
|
|
|
func (g GitImpl) Tags() ([]GitTag, error) {
|
|
|
|
cmd := exec.Command("git", "tag", "-l", "--format", "%(taggerdate:iso8601)#%(refname:short)")
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return parseTagsOutput(string(out))
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTagsOutput(input string) ([]GitTag, error) {
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(input))
|
|
|
|
var result []GitTag
|
|
|
|
for scanner.Scan() {
|
|
|
|
if line := strings.TrimSpace(scanner.Text()); line != "" {
|
|
|
|
values := strings.Split(line, "#")
|
|
|
|
date, err := time.Parse("2006-01-02 15:04:05 -0700", values[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse tag data, message: %v", err)
|
|
|
|
}
|
|
|
|
result = append(result, GitTag{Name: values[1], Date: date})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-02-01 22:43:02 +00:00
|
|
|
func parseLogOutput(messageMetadata map[string][]string, log string) []GitCommitLog {
|
2019-11-17 16:17:24 +00:00
|
|
|
scanner := bufio.NewScanner(strings.NewReader(log))
|
|
|
|
scanner.Split(splitAt([]byte(endLine)))
|
|
|
|
var logs []GitCommitLog
|
|
|
|
for scanner.Scan() {
|
|
|
|
if text := strings.TrimSpace(strings.Trim(scanner.Text(), "\"")); text != "" {
|
|
|
|
logs = append(logs, parseCommitLog(messageMetadata, text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return logs
|
|
|
|
}
|
|
|
|
|
2020-02-01 22:43:02 +00:00
|
|
|
func parseCommitLog(messageMetadata map[string][]string, commit string) GitCommitLog {
|
2019-11-17 16:17:24 +00:00
|
|
|
content := strings.Split(strings.Trim(commit, "\""), logSeparator)
|
|
|
|
commitType, scope, subject := parseCommitLogMessage(content[1])
|
|
|
|
|
|
|
|
metadata := make(map[string]string)
|
2020-02-01 22:43:02 +00:00
|
|
|
for key, prefixes := range messageMetadata {
|
|
|
|
for _, prefix := range prefixes {
|
|
|
|
if tagValue := extractTag(prefix, content[2]); tagValue != "" {
|
|
|
|
metadata[key] = tagValue
|
|
|
|
break
|
|
|
|
}
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return GitCommitLog{
|
|
|
|
Hash: content[0],
|
|
|
|
Type: commitType,
|
|
|
|
Scope: scope,
|
|
|
|
Subject: subject,
|
|
|
|
Body: content[2],
|
|
|
|
Metadata: metadata,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCommitLogMessage(message string) (string, string, string) {
|
|
|
|
regex := regexp.MustCompile("([a-z]+)(\\((.*)\\))?: (.*)")
|
|
|
|
result := regex.FindStringSubmatch(message)
|
|
|
|
if len(result) != 5 {
|
|
|
|
return "", "", message
|
|
|
|
}
|
|
|
|
return result[1], result[3], strings.TrimSpace(result[4])
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractTag(tag, text string) string {
|
2020-02-01 22:43:02 +00:00
|
|
|
regex := regexp.MustCompile(tag + " (.*)")
|
2019-11-17 16:17:24 +00:00
|
|
|
result := regex.FindStringSubmatch(text)
|
|
|
|
if len(result) < 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return result[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitAt(b []byte) func(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
|
|
|
return func(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
|
|
|
dataLen := len(data)
|
|
|
|
|
|
|
|
if atEOF && dataLen == 0 {
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if i := bytes.Index(data, b); i >= 0 {
|
|
|
|
return i + len(b), data[0:i], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if atEOF {
|
|
|
|
return dataLen, data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
}
|