diff --git a/internal/pipeline/git/errors.go b/internal/pipeline/git/errors.go index 6ca5dcc68..6dcc5a16e 100644 --- a/internal/pipeline/git/errors.go +++ b/internal/pipeline/git/errors.go @@ -1,6 +1,9 @@ package git -import "fmt" +import ( + "errors" + "fmt" +) // ErrInvalidVersionFormat is return when the version isnt in a valid format type ErrInvalidVersionFormat struct { @@ -31,8 +34,11 @@ func (e ErrWrongRef) Error() string { // ErrNoTag happens if the underlying git repository doesn't contain any tags // but no snapshot-release was requested. -var ErrNoTag = fmt.Errorf("git doesn't contain any tags. Either add a tag or use --snapshot") +var ErrNoTag = errors.New("git doesn't contain any tags. Either add a tag or use --snapshot") // ErrNotRepository happens if you try to run goreleaser against a folder // which is not a git repository. -var ErrNotRepository = fmt.Errorf("current folder is not a git repository") +var ErrNotRepository = errors.New("current folder is not a git repository") + +// ErrNoGit happens when git is not present in PATH. +var ErrNoGit = errors.New("git not present in PATH") diff --git a/internal/pipeline/git/git.go b/internal/pipeline/git/git.go index ed5d81670..38464a706 100644 --- a/internal/pipeline/git/git.go +++ b/internal/pipeline/git/git.go @@ -2,6 +2,7 @@ package git import ( "fmt" + "os/exec" "regexp" "strings" @@ -22,6 +23,9 @@ func (Pipe) String() string { // Run the pipe func (Pipe) Run(ctx *context.Context) error { + if _, err := exec.LookPath("git"); err != nil { + return ErrNoGit + } info, err := getInfo(ctx) if err != nil { return err diff --git a/internal/pipeline/git/git_test.go b/internal/pipeline/git/git_test.go index df4e6fa5c..513a5b8e0 100644 --- a/internal/pipeline/git/git_test.go +++ b/internal/pipeline/git/git_test.go @@ -215,3 +215,12 @@ func TestShortCommitHash(t *testing.T) { testlib.AssertSkipped(t, Pipe{}.Run(ctx)) assert.Len(t, ctx.Version, 7) } + +func TestGitNotInPath(t *testing.T) { + var path = os.Getenv("PATH") + defer func() { + assert.NoError(t, os.Setenv("PATH", path)) + }() + assert.NoError(t, os.Setenv("PATH", "")) + assert.EqualError(t, Pipe{}.Run(context.New(config.Project{})), ErrNoGit.Error()) +}