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

fix: detect if current folder is a subfolder of a parent git repo

We were checking for a .git folder, which would break in cases
where goreleaser is running from a subfolder of a monorepo, for example.

Check 529af6f#commitcomment-25011738

Closes #402 #403
This commit is contained in:
Carlos Alexandro Becker 2017-10-16 15:43:26 -02:00 committed by Carlos Alexandro Becker
parent 529af6fe72
commit 850c2e14f2
3 changed files with 20 additions and 7 deletions

View File

@ -4,8 +4,15 @@ package git
import (
"errors"
"os/exec"
"strings"
)
// IsRepo returns true if current folder is a git repository
func IsRepo() bool {
out, err := Run("rev-parse", "--is-inside-work-tree")
return err == nil && strings.TrimSpace(out) == "true"
}
// 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...)

View File

@ -1,6 +1,7 @@
package git
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
@ -20,3 +21,10 @@ func TestGit(t *testing.T) {
err.Error(),
)
}
func TestRepo(t *testing.T) {
assert.True(t, IsRepo(), "goreleaser folder should be a git repo")
assert.NoError(t, os.Chdir(os.TempDir()))
assert.False(t, IsRepo(), os.TempDir()+" folder should be a git repo")
}

View File

@ -1,25 +1,23 @@
package defaults
import (
"os"
"os/exec"
"strings"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/pkg/errors"
)
// remoteRepo gets the repo name from the Git config.
func remoteRepo() (result config.Repo, err error) {
if _, err = os.Stat(".git"); os.IsNotExist(err) {
return result, errors.Wrap(err, "current folder is not a git repository")
if !git.IsRepo() {
return result, errors.New("current folder is not a git repository")
}
cmd := exec.Command("git", "config", "--get", "remote.origin.url")
bts, err := cmd.CombinedOutput()
out, err := git.Run("config", "--get", "remote.origin.url")
if err != nil {
return result, errors.Wrap(err, "repository doesn't have an `origin` remote")
}
return extractRepoFromURL(string(bts)), nil
return extractRepoFromURL(out), nil
}
func extractRepoFromURL(s string) config.Repo {