2017-01-14 16:34:22 +02:00
|
|
|
package git
|
|
|
|
|
2017-01-30 01:55:32 +02:00
|
|
|
import (
|
2018-08-21 04:18:43 +02:00
|
|
|
"os/exec"
|
2017-01-30 01:55:32 +02:00
|
|
|
"strings"
|
|
|
|
|
2017-06-22 05:09:14 +02:00
|
|
|
"github.com/apex/log"
|
2017-08-19 17:47:04 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/git"
|
2018-09-12 19:18:01 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/pipe"
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
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
|
|
|
|
2018-11-25 21:30:30 +02:00
|
|
|
// Pipe that sets up git state
|
2017-01-14 16:34:22 +02:00
|
|
|
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 {
|
2018-08-21 04:18:43 +02:00
|
|
|
if _, err := exec.LookPath("git"); err != nil {
|
|
|
|
return ErrNoGit
|
|
|
|
}
|
2018-02-26 01:17:45 +02:00
|
|
|
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)
|
2018-12-13 14:09:36 +02:00
|
|
|
ctx.Version = strings.TrimPrefix(ctx.Git.CurrentTag, "v")
|
2018-02-26 01:17:45 +02:00
|
|
|
return validate(ctx)
|
|
|
|
}
|
|
|
|
|
2018-11-08 02:04:49 +02:00
|
|
|
// nolint: gochecknoglobals
|
2018-03-02 01:42:47 +02:00
|
|
|
var fakeInfo = context.GitInfo{
|
2018-10-03 22:44:31 +02:00
|
|
|
CurrentTag: "v0.0.0",
|
|
|
|
Commit: "none",
|
|
|
|
ShortCommit: "none",
|
|
|
|
FullCommit: "none",
|
2018-03-02 01:42:47 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2019-01-19 22:05:15 +02:00
|
|
|
info, err := getGitInfo()
|
2018-02-26 01:17:45 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-01-19 22:05:15 +02:00
|
|
|
func getGitInfo() (context.GitInfo, error) {
|
2018-10-03 22:44:31 +02:00
|
|
|
short, err := getShortCommit()
|
|
|
|
if err != nil {
|
|
|
|
return context.GitInfo{}, errors.Wrap(err, "couldn't get current commit")
|
|
|
|
}
|
|
|
|
full, err := getFullCommit()
|
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-10-04 14:38:19 +02:00
|
|
|
url, err := getURL()
|
|
|
|
if err != nil {
|
|
|
|
return context.GitInfo{}, errors.Wrap(err, "couldn't get remote URL")
|
|
|
|
}
|
2018-02-26 01:17:45 +02:00
|
|
|
tag, err := getTag()
|
|
|
|
if err != nil {
|
|
|
|
return context.GitInfo{
|
2019-01-19 21:37:42 +02:00
|
|
|
Commit: full,
|
2018-10-03 22:44:31 +02:00
|
|
|
FullCommit: full,
|
|
|
|
ShortCommit: short,
|
2018-10-04 14:38:19 +02:00
|
|
|
URL: url,
|
2018-10-03 22:44:31 +02:00
|
|
|
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{
|
2018-10-03 22:44:31 +02:00
|
|
|
CurrentTag: tag,
|
2019-01-19 21:37:42 +02:00
|
|
|
Commit: full,
|
2018-10-03 22:44:31 +02:00
|
|
|
FullCommit: full,
|
|
|
|
ShortCommit: short,
|
2018-10-04 14:38:19 +02:00
|
|
|
URL: url,
|
2018-02-26 01:17:45 +02:00
|
|
|
}, nil
|
2017-05-01 15:39:57 +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-09-12 19:18:01 +02:00
|
|
|
return pipe.ErrSnapshotEnabled
|
2018-03-08 13:42:33 +02:00
|
|
|
}
|
|
|
|
if ctx.SkipValidate {
|
2018-09-12 19:18:01 +02:00
|
|
|
return pipe.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 {
|
2018-06-19 20:53:14 +02:00
|
|
|
return ErrDirty{status: out}
|
2017-04-15 21:11:47 +02:00
|
|
|
}
|
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-06-19 20:53:14 +02:00
|
|
|
return ErrWrongRef{
|
|
|
|
commit: ctx.Git.Commit,
|
|
|
|
tag: 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-10-03 22:44:31 +02:00
|
|
|
func getShortCommit() (string, error) {
|
2019-02-06 21:51:09 +02:00
|
|
|
return git.Clean(git.Run("show", "--format='%h'", "HEAD", "-q"))
|
2018-10-03 22:44:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getFullCommit() (string, error) {
|
2019-02-06 21:51:09 +02:00
|
|
|
return git.Clean(git.Run("show", "--format='%H'", "HEAD", "-q"))
|
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
|
|
|
}
|
2018-10-04 14:38:19 +02:00
|
|
|
|
|
|
|
func getURL() (string, error) {
|
|
|
|
return git.Clean(git.Run("ls-remote", "--get-url"))
|
|
|
|
}
|