2017-04-14 20:39:32 +02:00
|
|
|
// Package git implements the Pipe interface extracting usefull data from
|
|
|
|
// git and putting it in the context.
|
2017-01-14 16:34:22 +02:00
|
|
|
package git
|
|
|
|
|
2017-01-30 01:55:32 +02:00
|
|
|
import (
|
2017-01-30 12:08:42 +02:00
|
|
|
"regexp"
|
2017-01-30 01:55:32 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/goreleaser/goreleaser/context"
|
|
|
|
)
|
2017-01-14 16:34:22 +02:00
|
|
|
|
2017-01-30 12:08:42 +02:00
|
|
|
// ErrInvalidVersionFormat is return when the version isnt in a valid format
|
|
|
|
type ErrInvalidVersionFormat struct {
|
|
|
|
version string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrInvalidVersionFormat) Error() string {
|
|
|
|
return e.version + " is not in a valid version format"
|
|
|
|
}
|
|
|
|
|
2017-01-14 16:34:22 +02:00
|
|
|
// Pipe for brew deployment
|
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-01-14 20:41:32 +02:00
|
|
|
// Description of the pipe
|
2017-01-14 19:14:35 +02:00
|
|
|
func (Pipe) Description() string {
|
2017-01-19 11:04:41 +02:00
|
|
|
return "Getting Git info"
|
2017-01-14 16:34:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the pipe
|
2017-04-15 19:00:49 +02:00
|
|
|
func (Pipe) Run(ctx *context.Context) (err error) {
|
|
|
|
tag, err := cleanGit("describe", "--tags", "--abbrev=0", "--always")
|
2017-01-14 16:34:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-04-15 19:00:49 +02:00
|
|
|
prev, err := previous(tag)
|
2017-01-14 16:34:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-04-15 18:05:54 +02:00
|
|
|
|
2017-04-15 19:00:49 +02:00
|
|
|
log, err := git("log", "--pretty=oneline", "--abbrev-commit", prev+".."+tag)
|
2017-01-14 16:34:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-18 19:08:48 +02:00
|
|
|
ctx.Git = context.GitInfo{
|
2017-01-14 18:06:57 +02:00
|
|
|
CurrentTag: tag,
|
2017-04-15 18:05:54 +02:00
|
|
|
PreviousTag: prev,
|
2017-01-14 18:06:57 +02:00
|
|
|
Diff: log,
|
|
|
|
}
|
2017-04-15 18:58:18 +02:00
|
|
|
// removes usual `v` prefix
|
|
|
|
ctx.Version = strings.TrimPrefix(tag, "v")
|
|
|
|
if versionErr := isVersionValid(ctx.Version); versionErr != nil {
|
|
|
|
return versionErr
|
|
|
|
}
|
2017-04-15 19:00:49 +02:00
|
|
|
commit, err := cleanGit("show", "--format='%H'", "HEAD")
|
2017-04-15 19:00:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Git.Commit = commit
|
2017-01-14 16:34:22 +02:00
|
|
|
return
|
|
|
|
}
|
2017-04-15 18:05:54 +02:00
|
|
|
|
2017-04-15 19:00:49 +02:00
|
|
|
func previous(tag string) (previous string, err error) {
|
|
|
|
previous, err = cleanGit("describe", "--tags", "--abbrev=0", "--always", tag+"^")
|
2017-04-15 18:05:54 +02:00
|
|
|
if err != nil {
|
2017-04-15 19:00:49 +02:00
|
|
|
previous, err = cleanGit("rev-list", "--max-parents=0", "HEAD")
|
2017-04-15 18:05:54 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func isVersionValid(version string) error {
|
|
|
|
matches, err := regexp.MatchString("^[0-9.]+", version)
|
|
|
|
if err != nil || !matches {
|
|
|
|
return ErrInvalidVersionFormat{version}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|