2023-10-15 19:29:29 +00:00
|
|
|
package app
|
2019-11-17 16:17:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2023-10-15 19:29:29 +00:00
|
|
|
"errors"
|
2019-11-17 16:17:24 +00:00
|
|
|
"fmt"
|
2020-12-02 02:15:51 +00:00
|
|
|
"os"
|
2019-11-17 16:17:24 +00:00
|
|
|
"os/exec"
|
2022-02-06 23:16:52 +00:00
|
|
|
"strconv"
|
2019-11-17 16:17:24 +00:00
|
|
|
"strings"
|
2020-02-01 21:19:38 +00:00
|
|
|
"time"
|
2019-11-17 16:17:24 +00:00
|
|
|
|
2021-02-13 18:40:09 +00:00
|
|
|
"github.com/Masterminds/semver/v3"
|
2023-10-16 19:41:33 +00:00
|
|
|
"github.com/thegeeklab/git-sv/sv"
|
|
|
|
"github.com/thegeeklab/git-sv/sv/formatter"
|
|
|
|
"github.com/thegeeklab/git-sv/templates"
|
2019-11-17 16:17:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-03-07 15:11:07 +00:00
|
|
|
logSeparator = "###"
|
|
|
|
endLine = "~~~"
|
2019-11-17 16:17:24 +00:00
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
configFilename = "config.yml"
|
|
|
|
configDir = ".gitsv"
|
|
|
|
)
|
2019-11-17 16:17:24 +00:00
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
var errUnknownGitError = errors.New("git command failed")
|
2019-11-17 16:17:24 +00:00
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
// Tag git tag info.
|
|
|
|
type Tag struct {
|
2020-02-01 21:19:38 +00:00
|
|
|
Name string
|
|
|
|
Date time.Time
|
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// LogRangeType type of log range.
|
2021-01-25 05:51:42 +00:00
|
|
|
type LogRangeType string
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// constants for log range type.
|
2021-01-25 05:51:42 +00:00
|
|
|
const (
|
|
|
|
TagRange LogRangeType = "tag"
|
2021-07-31 20:44:42 +00:00
|
|
|
DateRange LogRangeType = "date"
|
|
|
|
HashRange LogRangeType = "hash"
|
2021-01-25 05:51:42 +00:00
|
|
|
)
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// LogRange git log range.
|
2021-01-25 05:51:42 +00:00
|
|
|
type LogRange struct {
|
|
|
|
rangeType LogRangeType
|
|
|
|
start string
|
|
|
|
end string
|
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// NewLogRange LogRange constructor.
|
2021-01-25 05:51:42 +00:00
|
|
|
func NewLogRange(t LogRangeType, start, end string) LogRange {
|
|
|
|
return LogRange{rangeType: t, start: start, end: end}
|
|
|
|
}
|
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
// Impl git command implementation.
|
|
|
|
type GitSV struct {
|
2023-10-15 22:29:02 +00:00
|
|
|
Settings *Settings
|
|
|
|
Config *Config
|
2023-10-15 19:29:29 +00:00
|
|
|
|
|
|
|
MessageProcessor sv.MessageProcessor
|
|
|
|
CommitProcessor sv.CommitProcessor
|
|
|
|
ReleasenotesProcessor sv.ReleaseNoteProcessor
|
2023-10-15 20:20:39 +00:00
|
|
|
OutputFormatter formatter.OutputFormatter
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
// New constructor.
|
|
|
|
func New() GitSV {
|
|
|
|
g := GitSV{
|
2023-10-15 22:29:02 +00:00
|
|
|
Settings: &Settings{},
|
|
|
|
Config: NewConfig(configDir, configFilename),
|
2020-02-01 22:43:02 +00:00
|
|
|
}
|
2023-10-15 19:29:29 +00:00
|
|
|
|
|
|
|
g.MessageProcessor = sv.NewMessageProcessor(g.Config.CommitMessage, g.Config.Branches)
|
2023-10-15 20:20:39 +00:00
|
|
|
g.CommitProcessor = sv.NewSemVerCommitProcessor(g.Config.Versioning, g.Config.CommitMessage)
|
|
|
|
g.ReleasenotesProcessor = sv.NewReleaseNoteProcessor(g.Config.ReleaseNotes)
|
|
|
|
g.OutputFormatter = formatter.NewOutputFormatter(templates.New(configDir))
|
2023-10-15 19:29:29 +00:00
|
|
|
|
|
|
|
return g
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// LastTag get last tag, if no tag found, return empty.
|
2023-10-15 19:29:29 +00:00
|
|
|
func (g GitSV) LastTag() string {
|
2023-10-12 14:18:25 +00:00
|
|
|
//nolint:gosec
|
|
|
|
cmd := exec.Command(
|
|
|
|
"git",
|
|
|
|
"for-each-ref",
|
2023-10-15 19:29:29 +00:00
|
|
|
fmt.Sprintf("refs/tags/%s", *g.Config.Tag.Filter),
|
2023-10-12 14:18:25 +00:00
|
|
|
"--sort",
|
|
|
|
"-creatordate",
|
|
|
|
"--format",
|
|
|
|
"%(refname:short)",
|
|
|
|
"--count",
|
|
|
|
"1",
|
|
|
|
)
|
|
|
|
|
2019-11-17 16:17:24 +00:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2019-11-17 16:17:24 +00:00
|
|
|
return strings.TrimSpace(strings.Trim(string(out), "\n"))
|
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// Log return git log.
|
2023-10-15 19:29:29 +00:00
|
|
|
func (g GitSV) Log(lr LogRange) ([]sv.CommitLog, error) {
|
2023-10-12 14:18:25 +00:00
|
|
|
format := "--pretty=format:\"%ad" + logSeparator +
|
|
|
|
"%at" + logSeparator +
|
|
|
|
"%cN" + logSeparator +
|
|
|
|
"%h" + logSeparator +
|
|
|
|
"%s" + logSeparator +
|
|
|
|
"%b" + endLine + "\""
|
2021-01-25 05:51:42 +00:00
|
|
|
params := []string{"log", "--date=short", format}
|
|
|
|
|
|
|
|
if lr.start != "" || lr.end != "" {
|
|
|
|
switch lr.rangeType {
|
|
|
|
case DateRange:
|
|
|
|
params = append(params, "--since", lr.start, "--until", addDay(lr.end))
|
|
|
|
default:
|
|
|
|
if lr.start == "" {
|
|
|
|
params = append(params, lr.end)
|
|
|
|
} else {
|
|
|
|
params = append(params, lr.start+".."+str(lr.end, "HEAD"))
|
|
|
|
}
|
|
|
|
}
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 05:51:42 +00:00
|
|
|
cmd := exec.Command("git", params...)
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2019-11-17 16:17:24 +00:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2021-01-25 05:51:42 +00:00
|
|
|
return nil, combinedOutputErr(err, out)
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
logs, parseErr := parseLogOutput(g.MessageProcessor, string(out))
|
2022-04-06 09:01:17 +00:00
|
|
|
if parseErr != nil {
|
|
|
|
return nil, parseErr
|
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2022-04-06 09:01:17 +00:00
|
|
|
return logs, nil
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
// Commit runs git sv.
|
|
|
|
func (g GitSV) Commit(header, body, footer string) error {
|
2020-12-02 02:15:51 +00:00
|
|
|
cmd := exec.Command("git", "commit", "-m", header, "-m", "", "-m", body, "-m", "", "-m", footer)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2020-12-02 02:15:51 +00:00
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// Tag create a git tag.
|
2023-10-18 14:09:13 +00:00
|
|
|
func (g GitSV) Tag(version semver.Version, annotate, local bool) (string, error) {
|
2023-10-15 19:29:29 +00:00
|
|
|
tag := fmt.Sprintf(*g.Config.Tag.Pattern, 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
|
|
|
|
2023-10-18 13:51:05 +00:00
|
|
|
tagCommand := exec.Command("git", "tag", tag)
|
|
|
|
if annotate {
|
2023-10-18 14:09:13 +00:00
|
|
|
tagCommand.Args = append(tagCommand.Args, "-a", "-m", tagMsg)
|
2023-10-18 13:51:05 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 20:49:58 +00:00
|
|
|
if out, err := tagCommand.CombinedOutput(); err != nil {
|
2021-09-24 20:59:38 +00:00
|
|
|
return tag, combinedOutputErr(err, out)
|
2019-12-04 22:37:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 14:09:13 +00:00
|
|
|
if local {
|
|
|
|
return tag, nil
|
|
|
|
}
|
|
|
|
|
2019-12-04 22:37:50 +00:00
|
|
|
pushCommand := exec.Command("git", "push", "origin", tag)
|
2021-09-24 20:49:58 +00:00
|
|
|
if out, err := pushCommand.CombinedOutput(); err != nil {
|
2021-09-24 20:59:38 +00:00
|
|
|
return tag, combinedOutputErr(err, out)
|
2021-09-24 20:49:58 +00:00
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2021-09-24 20:59:38 +00:00
|
|
|
return tag, nil
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// Tags list repository tags.
|
2023-10-15 19:29:29 +00:00
|
|
|
func (g GitSV) Tags() ([]Tag, error) {
|
2023-10-12 14:18:25 +00:00
|
|
|
//nolint:gosec
|
|
|
|
cmd := exec.Command(
|
|
|
|
"git",
|
|
|
|
"for-each-ref",
|
|
|
|
"--sort",
|
|
|
|
"creatordate",
|
|
|
|
"--format",
|
|
|
|
"%(creatordate:iso8601)#%(refname:short)",
|
2023-10-15 19:29:29 +00:00
|
|
|
fmt.Sprintf("refs/tags/%s", *g.Config.Tag.Filter),
|
2023-10-12 14:18:25 +00:00
|
|
|
)
|
|
|
|
|
2020-02-01 21:19:38 +00:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2021-01-25 05:51:42 +00:00
|
|
|
return nil, combinedOutputErr(err, out)
|
2020-02-01 21:19:38 +00:00
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2020-02-01 21:19:38 +00:00
|
|
|
return parseTagsOutput(string(out))
|
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// Branch get git branch.
|
2023-10-15 19:29:29 +00:00
|
|
|
func (g GitSV) Branch() string {
|
2020-08-28 01:57:55 +00:00
|
|
|
cmd := exec.Command("git", "symbolic-ref", "--short", "HEAD")
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2020-08-28 01:57:55 +00:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2020-08-28 01:57:55 +00:00
|
|
|
return strings.TrimSpace(strings.Trim(string(out), "\n"))
|
|
|
|
}
|
|
|
|
|
2021-03-04 03:42:51 +00:00
|
|
|
// IsDetached check if is detached.
|
2023-10-15 19:29:29 +00:00
|
|
|
func (g GitSV) IsDetached() (bool, error) {
|
2021-03-04 03:42:51 +00:00
|
|
|
cmd := exec.Command("git", "symbolic-ref", "-q", "HEAD")
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2021-03-04 03:42:51 +00:00
|
|
|
out, err := cmd.CombinedOutput()
|
2023-10-12 14:18:25 +00:00
|
|
|
// -q: do not issue an error message if the <name> is not a symbolic ref, but a detached HEAD;
|
|
|
|
// instead exit with non-zero status silently.
|
|
|
|
if output := string(out); err != nil {
|
2021-03-04 03:42:51 +00:00
|
|
|
if output == "" {
|
|
|
|
return true, nil
|
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
|
|
|
return false, fmt.Errorf("%w: %s", errUnknownGitError, output)
|
2021-03-04 03:42:51 +00:00
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2021-03-04 03:42:51 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
func parseTagsOutput(input string) ([]Tag, error) {
|
2020-02-01 21:19:38 +00:00
|
|
|
scanner := bufio.NewScanner(strings.NewReader(input))
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
var result []Tag
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2020-02-01 21:19:38 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
if line := strings.TrimSpace(scanner.Text()); line != "" {
|
|
|
|
values := strings.Split(line, "#")
|
2020-05-01 03:45:08 +00:00
|
|
|
date, _ := time.Parse("2006-01-02 15:04:05 -0700", values[0]) // ignore invalid dates
|
2023-10-15 19:29:29 +00:00
|
|
|
result = append(result, Tag{Name: values[1], Date: date})
|
2020-02-01 21:19:38 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2020-02-01 21:19:38 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
func parseLogOutput(messageProcessor sv.MessageProcessor, log string) ([]sv.CommitLog, error) {
|
2019-11-17 16:17:24 +00:00
|
|
|
scanner := bufio.NewScanner(strings.NewReader(log))
|
|
|
|
scanner.Split(splitAt([]byte(endLine)))
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
var logs []sv.CommitLog
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2019-11-17 16:17:24 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
if text := strings.TrimSpace(strings.Trim(scanner.Text(), "\"")); text != "" {
|
2022-04-04 06:55:29 +00:00
|
|
|
log, err := parseCommitLog(messageProcessor, text)
|
2022-04-07 09:12:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-04-04 06:55:29 +00:00
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2022-04-07 09:12:14 +00:00
|
|
|
logs = append(logs, log)
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2022-04-06 09:01:17 +00:00
|
|
|
return logs, nil
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
func parseCommitLog(messageProcessor sv.MessageProcessor, c string) (sv.CommitLog, error) {
|
|
|
|
content := strings.Split(strings.Trim(c, "\""), logSeparator)
|
2022-02-06 23:16:52 +00:00
|
|
|
timestamp, _ := strconv.Atoi(content[1])
|
2022-04-04 06:55:29 +00:00
|
|
|
|
2023-10-12 14:18:25 +00:00
|
|
|
message, err := messageProcessor.Parse(content[4], content[5])
|
2022-04-04 06:55:29 +00:00
|
|
|
if err != nil {
|
2023-10-15 19:29:29 +00:00
|
|
|
return sv.CommitLog{}, err
|
2022-04-04 06:55:29 +00:00
|
|
|
}
|
2023-01-15 02:55:18 +00:00
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
return sv.CommitLog{
|
2022-02-06 23:16:52 +00:00
|
|
|
Date: content[0],
|
|
|
|
Timestamp: timestamp,
|
|
|
|
AuthorName: content[2],
|
|
|
|
Hash: content[3],
|
2022-04-04 06:55:29 +00:00
|
|
|
Message: message,
|
|
|
|
}, nil
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func splitAt(b []byte) func(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
2023-10-12 14:18:25 +00:00
|
|
|
return func(data []byte, atEOF bool) (advance int, token []byte, err error) { //nolint:nonamedreturns
|
|
|
|
if atEOF && len(data) == 0 {
|
2019-11-17 16:17:24 +00:00
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if i := bytes.Index(data, b); i >= 0 {
|
|
|
|
return i + len(b), data[0:i], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if atEOF {
|
2023-10-12 14:18:25 +00:00
|
|
|
return len(data), data, nil
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
}
|
2021-01-25 05:51:42 +00:00
|
|
|
|
|
|
|
func addDay(value string) string {
|
|
|
|
if value == "" {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := time.Parse("2006-01-02", value)
|
|
|
|
if err != nil { // keep original value if is not date format
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.AddDate(0, 0, 1).Format("2006-01-02")
|
|
|
|
}
|
|
|
|
|
|
|
|
func str(value, defaultValue string) string {
|
|
|
|
if value != "" {
|
|
|
|
return value
|
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2021-01-25 05:51:42 +00:00
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func combinedOutputErr(err error, out []byte) error {
|
|
|
|
msg := strings.Split(string(out), "\n")
|
2023-10-12 14:18:25 +00:00
|
|
|
|
|
|
|
return fmt.Errorf("%w - %s", err, msg[0])
|
2021-01-25 05:51:42 +00:00
|
|
|
}
|