1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00

Merge pull request #339 from goreleaser/git

removed duplicated git-related code
This commit is contained in:
Carlos Alexandro Becker 2017-08-19 20:11:38 -03:00 committed by GitHub
commit d5f043cbeb
5 changed files with 53 additions and 28 deletions

17
internal/git/git.go Normal file
View File

@ -0,0 +1,17 @@
// Package git provides an integration with the git command
package git
import (
"errors"
"os/exec"
)
// Run runs a git command and returns its output or errors
func Run(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
}

26
internal/git/git_test.go Normal file
View File

@ -0,0 +1,26 @@
package git_test
import (
"testing"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/stretchr/testify/assert"
)
func TestGit(t *testing.T) {
var assert = assert.New(t)
_, back := testlib.Mktmp(t)
defer back()
out, err := git.Run("init")
assert.NoError(err)
assert.Contains(out, "Initialized empty Git repository")
out, err = git.Run("command-that-dont-exist")
assert.Error(err)
assert.Empty(out)
assert.Equal(
"git: 'command-that-dont-exist' is not a git command. See 'git --help'.\n",
err.Error(),
)
}

View File

@ -1,17 +1,16 @@
package testlib
import (
"errors"
"os/exec"
"testing"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/stretchr/testify/assert"
)
// GitInit inits a new git project
func GitInit(t *testing.T) {
var assert = assert.New(t)
out, err := git("init")
out, err := fakeGit("init")
assert.NoError(err)
assert.Contains(out, "Initialized empty Git repository")
assert.NoError(err)
@ -44,7 +43,7 @@ func GitTag(t *testing.T, tag string) {
// GitAdd adds all files to stage
func GitAdd(t *testing.T) {
var assert = assert.New(t)
out, err := git("add", "-A")
out, err := fakeGit("add", "-A")
assert.NoError(err)
assert.Empty(out)
}
@ -56,14 +55,5 @@ func fakeGit(args ...string) (string, error) {
"-c", "commit.gpgSign=false",
}
allArgs = append(allArgs, args...)
return git(allArgs...)
}
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
return git.Run(allArgs...)
}

View File

@ -1,21 +1,12 @@
package git
import (
"errors"
"os/exec"
"strings"
"github.com/goreleaser/goreleaser/internal/git"
)
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...)
output, err = git.Run(args...)
return strings.Replace(strings.Split(output, "\n")[0], "'", "", -1), err
}

View File

@ -12,6 +12,7 @@ import (
"github.com/apex/log"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/git"
)
// Pipe for brew deployment
@ -101,7 +102,7 @@ func getSnapshotName(ctx *context.Context, tag, commit string) (string, error) {
}
func validate(ctx *context.Context, commit, tag string) error {
out, err := git("status", "--porcelain")
out, err := git.Run("status", "--porcelain")
if strings.TrimSpace(out) != "" || err != nil {
return ErrDirty{out}
}
@ -132,7 +133,7 @@ func getChangelog(tag string) (string, error) {
func gitLog(refs ...string) (string, error) {
var args = []string{"log", "--pretty=oneline", "--abbrev-commit"}
args = append(args, refs...)
return git(args...)
return git.Run(args...)
}
func getInfo() (tag, commit string, err error) {