mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-02-03 13:11:48 +02:00
commit
7eec7d20d0
@ -3,6 +3,7 @@
|
|||||||
package git
|
package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -15,7 +16,7 @@ type ErrInvalidVersionFormat struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e ErrInvalidVersionFormat) Error() string {
|
func (e ErrInvalidVersionFormat) Error() string {
|
||||||
return e.version + " is not in a valid version format"
|
return fmt.Sprintf("%v is not in a valid version format", e.version)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrDirty happens when the repo has uncommitted/unstashed changes
|
// ErrDirty happens when the repo has uncommitted/unstashed changes
|
||||||
@ -24,16 +25,16 @@ type ErrDirty struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e ErrDirty) Error() string {
|
func (e ErrDirty) Error() string {
|
||||||
return "git is currently in a dirty state:\n" + e.status
|
return fmt.Sprintf("git is currently in a dirty state:\n%v", e.status)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrWrongRef happens when the HEAD reference is different from the tag being built
|
// ErrWrongRef happens when the HEAD reference is different from the tag being built
|
||||||
type ErrWrongRef struct {
|
type ErrWrongRef struct {
|
||||||
status string
|
commit, tag string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e ErrWrongRef) Error() string {
|
func (e ErrWrongRef) Error() string {
|
||||||
return e.status
|
return fmt.Sprintf("git tag %v was not made against commit %v", e.tag, e.commit)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pipe for brew deployment
|
// Pipe for brew deployment
|
||||||
@ -46,46 +47,54 @@ func (Pipe) Description() string {
|
|||||||
|
|
||||||
// Run the pipe
|
// Run the pipe
|
||||||
func (Pipe) Run(ctx *context.Context) (err error) {
|
func (Pipe) Run(ctx *context.Context) (err error) {
|
||||||
tag, err := cleanGit("describe", "--tags", "--abbrev=0", "--always")
|
tag, prev, commit, log, err := getInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
prev, err := previous(tag)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log, err := git("log", "--pretty=oneline", "--abbrev-commit", prev+".."+tag)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.Git = context.GitInfo{
|
ctx.Git = context.GitInfo{
|
||||||
CurrentTag: tag,
|
CurrentTag: tag,
|
||||||
PreviousTag: prev,
|
PreviousTag: prev,
|
||||||
Diff: log,
|
Diff: log,
|
||||||
|
Commit: commit,
|
||||||
}
|
}
|
||||||
// removes usual `v` prefix
|
// removes usual `v` prefix
|
||||||
ctx.Version = strings.TrimPrefix(tag, "v")
|
ctx.Version = strings.TrimPrefix(tag, "v")
|
||||||
if versionErr := isVersionValid(ctx.Version); versionErr != nil {
|
return validate(commit, tag, ctx.Version)
|
||||||
return versionErr
|
}
|
||||||
|
|
||||||
|
func validate(commit, tag, version string) error {
|
||||||
|
matches, err := regexp.MatchString("^[0-9.]+", version)
|
||||||
|
if err != nil || !matches {
|
||||||
|
return ErrInvalidVersionFormat{version}
|
||||||
}
|
}
|
||||||
commit, err := cleanGit("show", "--format='%H'", "HEAD")
|
out, err := git("status", "-s")
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ctx.Git.Commit = commit
|
|
||||||
out, err := git("diff")
|
|
||||||
if strings.TrimSpace(out) != "" || err != nil {
|
if strings.TrimSpace(out) != "" || err != nil {
|
||||||
return ErrDirty{out}
|
return ErrDirty{out}
|
||||||
}
|
}
|
||||||
_, err = cleanGit("describe", "--exact-match", "--tags", "--match", tag)
|
_, err = cleanGit("describe", "--exact-match", "--tags", "--match", tag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrWrongRef{err.Error()}
|
return ErrWrongRef{commit, tag}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getInfo() (tag, prev, commit, log string, err error) {
|
||||||
|
tag, err = cleanGit("describe", "--tags", "--abbrev=0", "--always")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
prev, err = previous(tag)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log, err = git("log", "--pretty=oneline", "--abbrev-commit", prev+".."+tag)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
commit, err = cleanGit("show", "--format='%H'", "HEAD")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func previous(tag string) (previous string, err error) {
|
func previous(tag string) (previous string, err error) {
|
||||||
previous, err = cleanGit("describe", "--tags", "--abbrev=0", "--always", tag+"^")
|
previous, err = cleanGit("describe", "--tags", "--abbrev=0", "--always", tag+"^")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -93,11 +102,3 @@ func previous(tag string) (previous string, err error) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func isVersionValid(version string) error {
|
|
||||||
matches, err := regexp.MatchString("^[0-9.]+", version)
|
|
||||||
if err != nil || !matches {
|
|
||||||
return ErrInvalidVersionFormat{version}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
@ -96,7 +96,7 @@ func TestTagIsNotLastCommit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
err := Pipe{}.Run(ctx)
|
err := Pipe{}.Run(ctx)
|
||||||
assert.Error(err)
|
assert.Error(err)
|
||||||
assert.Contains(err.Error(), "fatal: no tag exactly matches")
|
assert.Contains(err.Error(), "git tag v0.0.1 was not made against commit")
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
Loading…
x
Reference in New Issue
Block a user