1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-03 13:11:48 +02:00

115 lines
2.7 KiB
Go
Raw Normal View History

2017-04-15 16:12:32 -03:00
// Package git implements the Pipe interface getting and validating the
// current git repository state
2017-01-14 12:34:22 -02:00
package git
2017-01-29 21:55:32 -02:00
import (
2017-04-15 17:04:26 -03:00
"fmt"
"log"
2017-01-30 08:08:42 -02:00
"regexp"
2017-01-29 21:55:32 -02:00
"strings"
"github.com/goreleaser/goreleaser/context"
)
2017-01-14 12:34:22 -02:00
2017-01-30 08: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 {
2017-04-15 17:04:26 -03:00
return fmt.Sprintf("%v is not in a valid version format", e.version)
2017-01-30 08:08:42 -02:00
}
2017-04-15 16:11:47 -03:00
// ErrDirty happens when the repo has uncommitted/unstashed changes
type ErrDirty struct {
status string
}
func (e ErrDirty) Error() string {
2017-04-15 17:04:26 -03:00
return fmt.Sprintf("git is currently in a dirty state:\n%v", e.status)
2017-04-15 16:11:47 -03:00
}
// ErrWrongRef happens when the HEAD reference is different from the tag being built
type ErrWrongRef struct {
2017-04-15 17:04:26 -03:00
commit, tag string
2017-04-15 16:11:47 -03:00
}
func (e ErrWrongRef) Error() string {
2017-04-15 17:04:26 -03:00
return fmt.Sprintf("git tag %v was not made against commit %v", e.tag, e.commit)
2017-04-15 16:11:47 -03:00
}
2017-01-14 12:34:22 -02:00
// Pipe for brew deployment
type Pipe struct{}
2017-01-14 19:41:32 +01:00
// Description of the pipe
2017-01-14 15:14:35 -02:00
func (Pipe) Description() string {
2017-04-15 16:11:47 -03:00
return "Getting and validating git state"
2017-01-14 12:34:22 -02:00
}
// Run the pipe
func (Pipe) Run(ctx *context.Context) (err error) {
2017-04-19 17:05:10 -03:00
tag, commit, err := getInfo()
2017-01-14 12:34:22 -02:00
if err != nil {
return
}
2017-01-18 15:08:48 -02:00
ctx.Git = context.GitInfo{
2017-04-19 17:05:10 -03:00
CurrentTag: tag,
Commit: commit,
2017-01-14 14:06:57 -02:00
}
2017-04-19 17:05:10 -03:00
if ctx.ReleaseNotes == "" {
log, err := getChangelog(tag)
2017-04-19 16:59:26 -03:00
if err != nil {
return err
}
2017-04-19 17:05:10 -03:00
ctx.ReleaseNotes = fmt.Sprintf("## Changelog\n\n%v", log)
2017-04-19 16:59:26 -03:00
}
2017-04-15 13:58:18 -03:00
// removes usual `v` prefix
ctx.Version = strings.TrimPrefix(tag, "v")
if !ctx.Validate {
2017-04-21 11:50:58 -03:00
log.Println("Skipped validations because --skip-validate is set")
return nil
}
2017-04-15 17:04:26 -03:00
return validate(commit, tag, ctx.Version)
}
func validate(commit, tag, version string) error {
matches, err := regexp.MatchString("^[0-9.]+", version)
if err != nil || !matches {
return ErrInvalidVersionFormat{version}
}
2017-04-15 17:04:26 -03:00
out, err := git("status", "-s")
2017-04-15 16:11:47 -03:00
if strings.TrimSpace(out) != "" || err != nil {
return ErrDirty{out}
}
_, err = cleanGit("describe", "--exact-match", "--tags", "--match", tag)
if err != nil {
2017-04-15 17:04:26 -03:00
return ErrWrongRef{commit, tag}
2017-04-15 16:11:47 -03:00
}
return nil
2017-01-14 12:34:22 -02:00
}
2017-04-19 17:05:10 -03:00
func getChangelog(tag string) (string, error) {
prev, err := previous(tag)
if err != nil {
2017-04-19 17:05:10 -03:00
return "", err
2017-04-15 17:04:26 -03:00
}
2017-04-19 17:05:10 -03:00
return git("log", "--pretty=oneline", "--abbrev-commit", prev+".."+tag)
}
func getInfo() (tag, commit string, err error) {
tag, err = cleanGit("describe", "--tags", "--abbrev=0", "--always")
2017-04-15 17:04:26 -03:00
if err != nil {
return
}
commit, err = cleanGit("show", "--format='%H'", "HEAD")
return
}
2017-04-15 17:04:26 -03:00
func previous(tag string) (previous string, err error) {
previous, err = cleanGit("describe", "--tags", "--abbrev=0", "--always", tag+"^")
if err != nil {
previous, err = cleanGit("rev-list", "--max-parents=0", "HEAD")
}
2017-04-15 17:04:26 -03:00
return
}