2017-01-21 19:11:54 -02:00
|
|
|
// Package source provides pipes to take care of validating the current
|
|
|
|
// git repo state.
|
2017-01-19 09:47:17 +01:00
|
|
|
// For the releasing process we need the files of the tag we are releasing.
|
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"github.com/goreleaser/goreleaser/context"
|
|
|
|
)
|
|
|
|
|
2017-01-21 19:11:54 -02:00
|
|
|
// ErrDirty happens when the repo has uncommitted/unstashed changes
|
2017-01-30 23:46:28 -02:00
|
|
|
type ErrDirty struct {
|
|
|
|
status string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrDirty) Error() string {
|
|
|
|
return "git is currently in a dirty state: " + e.status
|
|
|
|
}
|
2017-01-21 19:11:54 -02:00
|
|
|
|
2017-01-21 19:33:30 -02:00
|
|
|
// ErrWrongRef happens when the HEAD reference is different from the tag being built
|
2017-01-30 23:46:28 -02:00
|
|
|
type ErrWrongRef struct {
|
|
|
|
status string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrWrongRef) Error() string {
|
|
|
|
return "current tag ref is different from HEAD ref: " + e.status
|
|
|
|
}
|
2017-01-21 19:11:54 -02:00
|
|
|
|
|
|
|
// Pipe to make sure we are in the latest Git tag as source.
|
|
|
|
type Pipe struct{}
|
2017-01-19 09:47:17 +01:00
|
|
|
|
|
|
|
// Description of the pipe
|
2017-01-23 09:47:35 -02:00
|
|
|
func (p Pipe) Description() string {
|
2017-01-21 19:11:54 -02:00
|
|
|
return "Validating current git state"
|
2017-01-19 09:47:17 +01:00
|
|
|
}
|
|
|
|
|
2017-01-21 19:11:54 -02:00
|
|
|
// Run errors we the repo is dirty or if the current ref is different from the
|
|
|
|
// tag ref
|
2017-01-23 09:47:35 -02:00
|
|
|
func (p Pipe) Run(ctx *context.Context) error {
|
2017-01-19 09:47:17 +01:00
|
|
|
cmd := exec.Command("git", "diff-index", "--quiet", "HEAD", "--")
|
2017-01-21 19:11:54 -02:00
|
|
|
if err := cmd.Run(); err != nil {
|
2017-01-30 23:46:28 -02:00
|
|
|
status, err := status()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ErrDirty{status}
|
2017-01-19 09:47:17 +01:00
|
|
|
}
|
|
|
|
|
2017-01-21 19:28:30 -02:00
|
|
|
cmd = exec.Command("git", "describe", "--exact-match", "--tags", "--match", ctx.Git.CurrentTag)
|
2017-01-21 19:11:54 -02:00
|
|
|
if err := cmd.Run(); err != nil {
|
2017-01-30 23:46:28 -02:00
|
|
|
status, err := status()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ErrWrongRef{status}
|
2017-01-19 10:24:04 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-01-30 23:46:28 -02:00
|
|
|
|
|
|
|
func status() (string, error) {
|
|
|
|
bts, err := exec.Command("git", "status", "-sb").CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "\n\n" + string(bts), nil
|
|
|
|
}
|