mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-03-29 21:47:01 +02:00
improved git coverage, code, and function
This commit is contained in:
parent
7b1bc7ce70
commit
ceb5c1162c
@ -1,21 +0,0 @@
|
|||||||
package git
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"os/exec"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func commitHash() (string, error) {
|
|
||||||
cmd := exec.Command(
|
|
||||||
"git",
|
|
||||||
"show",
|
|
||||||
"--format='%H'",
|
|
||||||
"HEAD",
|
|
||||||
)
|
|
||||||
bts, err := cmd.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
return "", errors.New(err.Error() + ": " + string(bts))
|
|
||||||
}
|
|
||||||
return strings.Replace(strings.Split(string(bts), "\n")[0], "'", "", -1), err
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package git
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestCommit(t *testing.T) {
|
|
||||||
assert := assert.New(t)
|
|
||||||
commit, err := commitHash()
|
|
||||||
assert.NoError(err)
|
|
||||||
assert.NotEmpty(commit)
|
|
||||||
assert.NotContains(commit, "'")
|
|
||||||
}
|
|
21
pipeline/git/exec.go
Normal file
21
pipeline/git/exec.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package git
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func git(args ...string) (output string, err error) {
|
||||||
|
var cmd = exec.Command("git", args...)
|
||||||
|
bts, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return "", errors.New(string(bts))
|
||||||
|
}
|
||||||
|
return string(bts), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanGit(args ...string) (output string, err error) {
|
||||||
|
output, err = git(args...)
|
||||||
|
return strings.Replace(strings.Split(output, "\n")[0], "'", "", -1), err
|
||||||
|
}
|
@ -28,34 +28,50 @@ 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 := currentTag()
|
tag, err := cleanGit("describe", "--tags", "--abbrev=0", "--always")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
previous, err := previousTag(tag)
|
prev, err := previous(tag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log, err := log(previous, tag)
|
|
||||||
|
log, err := git("log", "--pretty=oneline", "--abbrev-commit", prev+".."+tag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Git = context.GitInfo{
|
ctx.Git = context.GitInfo{
|
||||||
CurrentTag: tag,
|
CurrentTag: tag,
|
||||||
PreviousTag: previous,
|
PreviousTag: prev,
|
||||||
Diff: log,
|
Diff: log,
|
||||||
}
|
}
|
||||||
// removes usual `v` prefix
|
// removes usual `v` prefix
|
||||||
ctx.Version = strings.TrimPrefix(tag, "v")
|
ctx.Version = strings.TrimPrefix(tag, "v")
|
||||||
matches, err := regexp.MatchString("^[0-9.]+", ctx.Version)
|
if versionErr := isVersionValid(ctx.Version); versionErr != nil {
|
||||||
if err != nil || !matches {
|
return versionErr
|
||||||
return ErrInvalidVersionFormat{ctx.Version}
|
|
||||||
}
|
}
|
||||||
commit, err := commitHash()
|
commit, err := cleanGit("show", "--format='%H'", "HEAD")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.Git.Commit = commit
|
ctx.Git.Commit = commit
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func isVersionValid(version string) error {
|
||||||
|
matches, err := regexp.MatchString("^[0-9.]+", version)
|
||||||
|
if err != nil || !matches {
|
||||||
|
return ErrInvalidVersionFormat{version}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package git
|
package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/goreleaser/goreleaser/config"
|
"github.com/goreleaser/goreleaser/config"
|
||||||
@ -13,7 +16,7 @@ func TestDescription(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestValidVersion(t *testing.T) {
|
func TestValidVersion(t *testing.T) {
|
||||||
assert := assert.New(t)
|
var assert = assert.New(t)
|
||||||
|
|
||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Config: config.Project{},
|
Config: config.Project{},
|
||||||
@ -23,3 +26,62 @@ func TestValidVersion(t *testing.T) {
|
|||||||
assert.NotEmpty(ctx.Git.PreviousTag)
|
assert.NotEmpty(ctx.Git.PreviousTag)
|
||||||
assert.NotEmpty(ctx.Git.Diff)
|
assert.NotEmpty(ctx.Git.Diff)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNotAGitFolder(t *testing.T) {
|
||||||
|
var assert = assert.New(t)
|
||||||
|
_, back := createAndChdir(t)
|
||||||
|
defer back()
|
||||||
|
var ctx = &context.Context{
|
||||||
|
Config: config.Project{},
|
||||||
|
}
|
||||||
|
assert.Error(Pipe{}.Run(ctx))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSingleCommit(t *testing.T) {
|
||||||
|
var assert = assert.New(t)
|
||||||
|
_, back := createAndChdir(t)
|
||||||
|
defer back()
|
||||||
|
assert.NoError(exec.Command("git", "init").Run())
|
||||||
|
assert.NoError(exec.Command("git", "commit", "--allow-empty", "-m", "asd").Run())
|
||||||
|
assert.NoError(exec.Command("git", "tag", "v0.0.1").Run())
|
||||||
|
var ctx = &context.Context{
|
||||||
|
Config: config.Project{},
|
||||||
|
}
|
||||||
|
assert.NoError(Pipe{}.Run(ctx))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewRepository(t *testing.T) {
|
||||||
|
var assert = assert.New(t)
|
||||||
|
_, back := createAndChdir(t)
|
||||||
|
defer back()
|
||||||
|
assert.NoError(exec.Command("git", "init").Run())
|
||||||
|
var ctx = &context.Context{
|
||||||
|
Config: config.Project{},
|
||||||
|
}
|
||||||
|
assert.Error(Pipe{}.Run(ctx))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInvalidTagFormat(t *testing.T) {
|
||||||
|
var assert = assert.New(t)
|
||||||
|
_, back := createAndChdir(t)
|
||||||
|
defer back()
|
||||||
|
assert.NoError(exec.Command("git", "init").Run())
|
||||||
|
assert.NoError(exec.Command("git", "commit", "--allow-empty", "-m", "asd").Run())
|
||||||
|
assert.NoError(exec.Command("git", "tag", "sadasd").Run())
|
||||||
|
var ctx = &context.Context{
|
||||||
|
Config: config.Project{},
|
||||||
|
}
|
||||||
|
assert.EqualError(Pipe{}.Run(ctx), "sadasd is not in a valid version format")
|
||||||
|
}
|
||||||
|
|
||||||
|
func createAndChdir(t *testing.T) (current string, back func()) {
|
||||||
|
var assert = assert.New(t)
|
||||||
|
folder, err := ioutil.TempDir("", "gorelasertest")
|
||||||
|
assert.NoError(err)
|
||||||
|
previous, err := os.Getwd()
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.NoError(os.Chdir(folder))
|
||||||
|
return folder, func() {
|
||||||
|
assert.NoError(os.Chdir(previous))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,18 +0,0 @@
|
|||||||
package git
|
|
||||||
|
|
||||||
import "os/exec"
|
|
||||||
|
|
||||||
func log(previous, current string) (str string, err error) {
|
|
||||||
cmd := exec.Command(
|
|
||||||
"git",
|
|
||||||
"log",
|
|
||||||
"--pretty=oneline",
|
|
||||||
"--abbrev-commit",
|
|
||||||
previous+".."+current,
|
|
||||||
)
|
|
||||||
bts, err := cmd.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
return str, err
|
|
||||||
}
|
|
||||||
return string(bts), err
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
package git
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestLog(t *testing.T) {
|
|
||||||
assert := assert.New(t)
|
|
||||||
tag, err := currentTag()
|
|
||||||
assert.NoError(err)
|
|
||||||
tagb, err := previousTag(tag)
|
|
||||||
assert.NoError(err)
|
|
||||||
log, err := log(tagb, tag)
|
|
||||||
assert.NoError(err)
|
|
||||||
assert.NotEmpty(log)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLogInvalidRef(t *testing.T) {
|
|
||||||
assert := assert.New(t)
|
|
||||||
log, err := log("wtfff", "nope")
|
|
||||||
assert.Error(err)
|
|
||||||
assert.Empty(log)
|
|
||||||
}
|
|
@ -1,33 +0,0 @@
|
|||||||
package git
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"os/exec"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func currentTag() (tag string, err error) {
|
|
||||||
return getTag("")
|
|
||||||
}
|
|
||||||
|
|
||||||
func previousTag(base string) (tag string, err error) {
|
|
||||||
return getTag(base + "^")
|
|
||||||
}
|
|
||||||
|
|
||||||
func getTag(ref string) (tag string, err error) {
|
|
||||||
cmd := exec.Command(
|
|
||||||
"git",
|
|
||||||
"describe",
|
|
||||||
"--tags",
|
|
||||||
"--abbrev=0",
|
|
||||||
"--always",
|
|
||||||
)
|
|
||||||
if ref != "" {
|
|
||||||
cmd.Args = append(cmd.Args, ref)
|
|
||||||
}
|
|
||||||
bts, err := cmd.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
return tag, errors.New(err.Error() + ": " + string(bts))
|
|
||||||
}
|
|
||||||
return strings.Split(string(bts), "\n")[0], err
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
package git
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestCurrentTag(t *testing.T) {
|
|
||||||
assert := assert.New(t)
|
|
||||||
tag, err := currentTag()
|
|
||||||
assert.NoError(err)
|
|
||||||
assert.NotEmpty(tag)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPreviousTag(t *testing.T) {
|
|
||||||
assert := assert.New(t)
|
|
||||||
tag, err := currentTag()
|
|
||||||
assert.NoError(err)
|
|
||||||
tag, err = previousTag(tag)
|
|
||||||
assert.NoError(err)
|
|
||||||
assert.NotEmpty(tag)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInvalidRef(t *testing.T) {
|
|
||||||
assert := assert.New(t)
|
|
||||||
tag, err := previousTag("this-should-not-exist")
|
|
||||||
assert.Error(err)
|
|
||||||
assert.Empty(tag)
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user