2017-01-14 16:34:22 +02:00
|
|
|
package git
|
|
|
|
|
2017-01-30 01:55:32 +02:00
|
|
|
import (
|
2017-04-29 12:49:22 +02:00
|
|
|
"bytes"
|
2018-03-02 14:20:27 +02:00
|
|
|
"fmt"
|
2017-01-30 12:08:42 +02:00
|
|
|
"regexp"
|
2017-01-30 01:55:32 +02:00
|
|
|
"strings"
|
2017-04-29 12:49:22 +02:00
|
|
|
"text/template"
|
2017-06-22 05:09:14 +02:00
|
|
|
"time"
|
2017-01-30 01:55:32 +02:00
|
|
|
|
2017-06-22 05:09:14 +02:00
|
|
|
"github.com/apex/log"
|
2017-01-30 01:55:32 +02:00
|
|
|
"github.com/goreleaser/goreleaser/context"
|
2017-08-19 17:47:04 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/git"
|
2018-03-08 13:42:33 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pipeline"
|
2017-10-15 21:46:21 +02:00
|
|
|
"github.com/pkg/errors"
|
2017-01-30 01:55:32 +02:00
|
|
|
)
|
2017-01-14 16:34:22 +02:00
|
|
|
|
|
|
|
// Pipe for brew deployment
|
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-12-02 23:53:19 +02:00
|
|
|
func (Pipe) String() string {
|
|
|
|
return "getting and validating git state"
|
2017-01-14 16:34:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the pipe
|
2018-02-26 01:17:45 +02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
|
|
|
info, err := getInfo(ctx)
|
2017-01-14 16:34:22 +02:00
|
|
|
if err != nil {
|
2018-02-26 01:17:45 +02:00
|
|
|
return err
|
2017-01-14 16:34:22 +02:00
|
|
|
}
|
2018-02-26 01:17:45 +02:00
|
|
|
ctx.Git = info
|
|
|
|
log.Infof("releasing %s, commit %s", info.CurrentTag, info.Commit)
|
|
|
|
if err := setVersion(ctx); err != nil {
|
|
|
|
return err
|
2017-04-29 12:49:22 +02:00
|
|
|
}
|
2018-02-26 01:17:45 +02:00
|
|
|
return validate(ctx)
|
|
|
|
}
|
|
|
|
|
2018-03-02 01:42:47 +02:00
|
|
|
var fakeInfo = context.GitInfo{
|
|
|
|
CurrentTag: "v0.0.0",
|
|
|
|
Commit: "none",
|
|
|
|
}
|
|
|
|
|
2018-02-26 01:17:45 +02:00
|
|
|
func getInfo(ctx *context.Context) (context.GitInfo, error) {
|
|
|
|
if !git.IsRepo() && ctx.Snapshot {
|
2018-03-02 01:42:47 +02:00
|
|
|
log.Warn("accepting to run without a git repo because this is a snapshot")
|
|
|
|
return fakeInfo, nil
|
2018-02-26 01:17:45 +02:00
|
|
|
}
|
|
|
|
if !git.IsRepo() {
|
|
|
|
return context.GitInfo{}, ErrNotRepository
|
|
|
|
}
|
|
|
|
info, err := getGitInfo(ctx)
|
|
|
|
if err != nil && ctx.Snapshot {
|
2018-03-02 01:42:47 +02:00
|
|
|
log.WithError(err).Warn("ignoring errors because this is a snapshot")
|
|
|
|
if info.Commit == "" {
|
|
|
|
info = fakeInfo
|
|
|
|
}
|
2018-02-26 01:17:45 +02:00
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGitInfo(ctx *context.Context) (context.GitInfo, error) {
|
2018-03-02 14:20:27 +02:00
|
|
|
commit, err := getCommit(ctx)
|
2018-02-26 01:17:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return context.GitInfo{}, errors.Wrap(err, "couldn't get current commit")
|
2017-01-14 18:06:57 +02:00
|
|
|
}
|
2018-02-26 01:17:45 +02:00
|
|
|
tag, err := getTag()
|
|
|
|
if err != nil {
|
|
|
|
return context.GitInfo{
|
2018-03-02 01:42:47 +02:00
|
|
|
Commit: commit,
|
|
|
|
CurrentTag: "v0.0.0",
|
2018-02-26 01:17:45 +02:00
|
|
|
}, ErrNoTag
|
2017-05-01 15:39:57 +02:00
|
|
|
}
|
2018-02-26 01:17:45 +02:00
|
|
|
return context.GitInfo{
|
|
|
|
CurrentTag: tag,
|
|
|
|
Commit: commit,
|
|
|
|
}, nil
|
2017-05-01 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2018-02-26 01:17:45 +02:00
|
|
|
func setVersion(ctx *context.Context) error {
|
2017-05-01 15:39:57 +02:00
|
|
|
if ctx.Snapshot {
|
2018-02-26 01:17:45 +02:00
|
|
|
snapshotName, err := getSnapshotName(ctx)
|
2017-04-29 12:49:22 +02:00
|
|
|
if err != nil {
|
2017-10-15 21:46:21 +02:00
|
|
|
return errors.Wrap(err, "failed to generate snapshot name")
|
2017-04-29 12:49:22 +02:00
|
|
|
}
|
|
|
|
ctx.Version = snapshotName
|
2017-05-01 15:39:57 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// removes usual `v` prefix
|
2018-02-26 01:17:45 +02:00
|
|
|
ctx.Version = strings.TrimPrefix(ctx.Git.CurrentTag, "v")
|
|
|
|
return nil
|
2017-05-01 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2017-04-29 12:49:22 +02:00
|
|
|
type snapshotNameData struct {
|
|
|
|
Commit string
|
|
|
|
Tag string
|
|
|
|
Timestamp int64
|
|
|
|
}
|
|
|
|
|
2018-02-26 01:17:45 +02:00
|
|
|
func getSnapshotName(ctx *context.Context) (string, error) {
|
2017-04-29 12:49:22 +02:00
|
|
|
tmpl, err := template.New("snapshot").Parse(ctx.Config.Snapshot.NameTemplate)
|
|
|
|
var out bytes.Buffer
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-05-01 15:39:57 +02:00
|
|
|
var data = snapshotNameData{
|
2018-02-26 01:17:45 +02:00
|
|
|
Commit: ctx.Git.Commit,
|
|
|
|
Tag: ctx.Git.CurrentTag,
|
2017-05-01 15:39:57 +02:00
|
|
|
Timestamp: time.Now().Unix(),
|
2017-04-29 12:49:22 +02:00
|
|
|
}
|
2017-05-01 15:39:57 +02:00
|
|
|
err = tmpl.Execute(&out, data)
|
|
|
|
return out.String(), err
|
2017-04-15 22:04:26 +02:00
|
|
|
}
|
|
|
|
|
2018-02-26 01:17:45 +02:00
|
|
|
func validate(ctx *context.Context) error {
|
2018-02-24 22:59:08 +02:00
|
|
|
if ctx.Snapshot {
|
2018-03-08 13:42:33 +02:00
|
|
|
return pipeline.ErrSnapshotEnabled
|
|
|
|
}
|
|
|
|
if ctx.SkipValidate {
|
|
|
|
return pipeline.ErrSkipValidateEnabled
|
2018-02-24 22:59:08 +02:00
|
|
|
}
|
2017-08-19 17:47:04 +02:00
|
|
|
out, err := git.Run("status", "--porcelain")
|
2017-04-15 21:11:47 +02:00
|
|
|
if strings.TrimSpace(out) != "" || err != nil {
|
|
|
|
return ErrDirty{out}
|
|
|
|
}
|
2017-05-01 15:57:37 +02:00
|
|
|
if !regexp.MustCompile("^[0-9.]+").MatchString(ctx.Version) {
|
|
|
|
return ErrInvalidVersionFormat{ctx.Version}
|
|
|
|
}
|
2018-02-26 01:17:45 +02:00
|
|
|
_, err = git.Clean(git.Run("describe", "--exact-match", "--tags", "--match", ctx.Git.CurrentTag))
|
2017-05-01 15:57:37 +02:00
|
|
|
if err != nil {
|
2018-02-26 01:17:45 +02:00
|
|
|
return ErrWrongRef{ctx.Git.Commit, ctx.Git.CurrentTag}
|
2017-04-15 21:11:47 +02:00
|
|
|
}
|
|
|
|
return nil
|
2017-01-14 16:34:22 +02:00
|
|
|
}
|
2017-04-15 18:05:54 +02:00
|
|
|
|
2018-03-02 14:20:27 +02:00
|
|
|
func getCommit(ctx *context.Context) (string, error) {
|
|
|
|
format := "%H"
|
|
|
|
if ctx.Config.Git.ShortHash {
|
|
|
|
format = "%h"
|
|
|
|
}
|
|
|
|
return git.Clean(git.Run("show", fmt.Sprintf("--format='%s'", format), "HEAD"))
|
2018-02-26 01:17:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getTag() (string, error) {
|
|
|
|
return git.Clean(git.Run("describe", "--tags", "--abbrev=0"))
|
2017-04-23 21:33:44 +02:00
|
|
|
}
|