Repository: goreleaser/goreleaser
Pull request #1962 (2020-12-22 14:02:51)

Revert "feat: allow automatic snapshot on dirty state"
Reverts goreleaser/goreleaser#1958. Needs to be handled in git pipe, we don't have enough context in cmd.

vercel[bot]: This pull request is being automatically deployed with Vercel ([learn more](https://vercel.link/github-learn-more)).  
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: [https://vercel.com/goreleaser/goreleaser/ffjzeop5p](https://vercel.com/goreleaser/goreleaser/ffjzeop5p)  
✅ Preview: [https://goreleaser-git-revert-1958-snapshot-auto.goreleaser.now.sh](https://goreleaser-git-revert-1958-snapshot-auto.goreleaser.now.sh)



Patch:
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index d709d51b..52cd0a42 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -78,11 +78,15 @@ jobs:
           snapcraft login --with <(echo "$SNAPCRAFT_LOGIN")
       -
         name: GoReleaser
-        if: success() && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/master')
+        if: success()
         env:
           GITHUB_TOKEN: ${{ secrets.GH_PAT }}
         run: |
-          ./goreleaser --snapshot-auto
+          if [[ $GITHUB_REF == refs/tags/v* ]]; then
+            ./goreleaser
+          elif [[ $GITHUB_REF == refs/heads/master ]]; then
+            ./goreleaser --snapshot
+          fi
       -
         name: Clear
         if: always() && startsWith(github.ref, 'refs/tags/v')
diff --git a/cmd/build.go b/cmd/build.go
index 5f78d91c..79a5dde4 100644
--- a/cmd/build.go
+++ b/cmd/build.go
@@ -6,7 +6,6 @@ import (
 	"github.com/apex/log"
 	"github.com/caarlos0/ctrlc"
 	"github.com/fatih/color"
-	"github.com/goreleaser/goreleaser/internal/git"
 	"github.com/goreleaser/goreleaser/internal/middleware"
 	"github.com/goreleaser/goreleaser/internal/pipeline"
 	"github.com/goreleaser/goreleaser/pkg/context"
@@ -21,7 +20,6 @@ type buildCmd struct {
 type buildOpts struct {
 	config        string
 	snapshot      bool
-	snapshotAuto  bool
 	skipValidate  bool
 	skipPostHooks bool
 	rmDist        bool
@@ -61,7 +59,6 @@ func newBuildCmd() *buildCmd {
 
 	cmd.Flags().StringVarP(&root.opts.config, "config", "f", "", "Load configuration from file")
 	cmd.Flags().BoolVar(&root.opts.snapshot, "snapshot", false, "Generate an unversioned snapshot build, skipping all validations and without publishing any artifacts")
-	cmd.Flags().BoolVar(&root.opts.snapshotAuto, "snapshot-auto", false, "Same as snapshot but only if Git is currently in a dirty state")
 	cmd.Flags().BoolVar(&root.opts.skipValidate, "skip-validate", false, "Skips several sanity checks")
 	cmd.Flags().BoolVar(&root.opts.skipPostHooks, "skip-post-hooks", false, "Skips all post-build hooks")
 	cmd.Flags().BoolVar(&root.opts.rmDist, "rm-dist", false, "Remove the dist folder before building")
@@ -100,9 +97,6 @@ func setupBuildContext(ctx *context.Context, options buildOpts) *context.Context
 	ctx.Parallelism = options.parallelism
 	log.Debugf("parallelism: %v", ctx.Parallelism)
 	ctx.Snapshot = options.snapshot
-	if _, gitDirty := git.Status(); options.snapshotAuto && gitDirty {
-		ctx.Snapshot = true
-	}
 	ctx.SkipValidate = ctx.Snapshot || options.skipValidate
 	ctx.SkipPostBuildHooks = options.skipPostHooks
 	ctx.RmDist = options.rmDist
diff --git a/cmd/release.go b/cmd/release.go
index 0b69fec9..5f2641bb 100644
--- a/cmd/release.go
+++ b/cmd/release.go
@@ -6,7 +6,6 @@ import (
 	"github.com/apex/log"
 	"github.com/caarlos0/ctrlc"
 	"github.com/fatih/color"
-	"github.com/goreleaser/goreleaser/internal/git"
 	"github.com/goreleaser/goreleaser/internal/middleware"
 	"github.com/goreleaser/goreleaser/internal/pipeline"
 	"github.com/goreleaser/goreleaser/pkg/context"
@@ -24,7 +23,6 @@ type releaseOpts struct {
 	releaseHeader string
 	releaseFooter string
 	snapshot      bool
-	snapshotAuto  bool
 	skipPublish   bool
 	skipSign      bool
 	skipValidate  bool
@@ -68,7 +66,6 @@ func newReleaseCmd() *releaseCmd {
 	cmd.Flags().StringVar(&root.opts.releaseHeader, "release-header", "", "Load custom release notes header from a markdown file")
 	cmd.Flags().StringVar(&root.opts.releaseFooter, "release-footer", "", "Load custom release notes footer from a markdown file")
 	cmd.Flags().BoolVar(&root.opts.snapshot, "snapshot", false, "Generate an unversioned snapshot release, skipping all validations and without publishing any artifacts")
-	cmd.Flags().BoolVar(&root.opts.snapshotAuto, "snapshot-auto", false, "Same as snapshot but only if Git is currently in a dirty state")
 	cmd.Flags().BoolVar(&root.opts.skipPublish, "skip-publish", false, "Skips publishing artifacts")
 	cmd.Flags().BoolVar(&root.opts.skipSign, "skip-sign", false, "Skips signing the artifacts")
 	cmd.Flags().BoolVar(&root.opts.skipValidate, "skip-validate", false, "Skips several sanity checks")
@@ -111,9 +108,6 @@ func setupReleaseContext(ctx *context.Context, options releaseOpts) *context.Con
 	ctx.ReleaseHeader = options.releaseHeader
 	ctx.ReleaseFooter = options.releaseFooter
 	ctx.Snapshot = options.snapshot
-	if _, gitDirty := git.Status(); options.snapshotAuto && gitDirty {
-		ctx.Snapshot = true
-	}
 	ctx.SkipPublish = ctx.Snapshot || options.skipPublish
 	ctx.SkipValidate = ctx.Snapshot || options.skipValidate
 	ctx.SkipSign = options.skipSign
diff --git a/internal/git/git.go b/internal/git/git.go
index b539224f..45ff1bcf 100644
--- a/internal/git/git.go
+++ b/internal/git/git.go
@@ -16,12 +16,6 @@ func IsRepo() bool {
 	return err == nil && strings.TrimSpace(out) == "true"
 }
 
-// Status returns git status in its current state.
-func Status() (string, bool) {
-	out, err := Run("status", "--porcelain")
-	return out, strings.TrimSpace(out) != "" || err != nil
-}
-
 // RunEnv runs a git command with the specified env vars and returns its output or errors.
 func RunEnv(env map[string]string, args ...string) (string, error) {
 	// TODO: use exex.CommandContext here and refactor.
diff --git a/internal/pipe/git/errors.go b/internal/pipe/git/errors.go
index cc8b21ff..e4360f5a 100644
--- a/internal/pipe/git/errors.go
+++ b/internal/pipe/git/errors.go
@@ -25,7 +25,7 @@ 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 = errors.New("git doesn't contain any tags. Either add a tag or use --snapshot or --snapshot-auto")
+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.
diff --git a/internal/pipe/git/git.go b/internal/pipe/git/git.go
index d6ce9829..2d2ba64a 100644
--- a/internal/pipe/git/git.go
+++ b/internal/pipe/git/git.go
@@ -116,12 +116,11 @@ func validate(ctx *context.Context) error {
 	if ctx.SkipValidate {
 		return pipe.ErrSkipValidateEnabled
 	}
-	if status, dirty := git.Status(); dirty {
-		return ErrDirty{
-			status: status,
-		}
+	out, err := git.Run("status", "--porcelain")
+	if strings.TrimSpace(out) != "" || err != nil {
+		return ErrDirty{status: out}
 	}
-	_, err := git.Clean(git.Run("describe", "--exact-match", "--tags", "--match", ctx.Git.CurrentTag))
+	_, err = git.Clean(git.Run("describe", "--exact-match", "--tags", "--match", ctx.Git.CurrentTag))
 	if err != nil {
 		return ErrWrongRef{
 			commit: ctx.Git.Commit,
diff --git a/www/docs/customization/snapshots.md b/www/docs/customization/snapshots.md
index d80f3426..75a3bdf6 100644
--- a/www/docs/customization/snapshots.md
+++ b/www/docs/customization/snapshots.md
@@ -34,6 +34,3 @@ get the snapshot version.
 Note that the idea behind GoReleaser's snapshots if mostly for local builds
 or to validate your build on the CI pipeline. Artifacts shouldn't be uploaded
 anywhere, and will only be generated to the `dist` folder.
-
-You can also use the `--snapshot-auto` flag to automatically switch to snapshot
-if Git is currently in a dirty state.


Test patch:
diff --git a/cmd/build_test.go b/cmd/build_test.go
index b8241f5f..97882c86 100644
--- a/cmd/build_test.go
+++ b/cmd/build_test.go
@@ -1,11 +1,8 @@
 package cmd
 
 import (
-	"io/ioutil"
-	"path/filepath"
 	"testing"
 
-	"github.com/goreleaser/goreleaser/internal/testlib"
 	"github.com/goreleaser/goreleaser/pkg/config"
 	"github.com/goreleaser/goreleaser/pkg/context"
 	"github.com/stretchr/testify/require"
@@ -48,37 +45,6 @@ func TestBuildFlags(t *testing.T) {
 		require.True(t, ctx.SkipTokenCheck)
 	})
 
-	t.Run("snapshot auto clean", func(t *testing.T) {
-		testlib.Mktmp(t)
-		testlib.GitInit(t)
-		testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
-
-		var ctx = setup(buildOpts{
-			snapshotAuto: true,
-		})
-
-		require.False(t, ctx.Snapshot)
-		require.False(t, ctx.SkipValidate)
-	})
-
-	t.Run("snapshot auto dirty", func(t *testing.T) {
-		var folder = testlib.Mktmp(t)
-		testlib.GitInit(t)
-		testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
-		testlib.GitAdd(t)
-		testlib.GitCommit(t, "whatever")
-		testlib.GitTag(t, "v0.0.1")
-		require.NoError(t, ioutil.WriteFile(filepath.Join(folder, "foo"), []byte("foobar"), 0644))
-
-		var ctx = setup(buildOpts{
-			snapshotAuto: true,
-		})
-
-		require.True(t, ctx.Snapshot)
-		require.True(t, ctx.SkipValidate)
-		require.True(t, ctx.SkipTokenCheck)
-	})
-
 	t.Run("skips", func(t *testing.T) {
 		var ctx = setup(buildOpts{
 			skipValidate:  true,
diff --git a/cmd/release_test.go b/cmd/release_test.go
index 0e1a43d2..d145d221 100644
--- a/cmd/release_test.go
+++ b/cmd/release_test.go
@@ -1,11 +1,8 @@
 package cmd
 
 import (
-	"io/ioutil"
-	"path/filepath"
 	"testing"
 
-	"github.com/goreleaser/goreleaser/internal/testlib"
 	"github.com/goreleaser/goreleaser/pkg/config"
 	"github.com/goreleaser/goreleaser/pkg/context"
 	"github.com/stretchr/testify/require"
@@ -45,35 +42,6 @@ func TestReleaseFlags(t *testing.T) {
 		})
 		require.True(t, ctx.Snapshot)
 		require.True(t, ctx.SkipPublish)
-	})
-
-	t.Run("snapshot auto clean", func(t *testing.T) {
-		testlib.Mktmp(t)
-		testlib.GitInit(t)
-		testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
-
-		var ctx = setup(releaseOpts{
-			snapshotAuto: true,
-		})
-
-		require.False(t, ctx.Snapshot)
-		require.False(t, ctx.SkipPublish)
-	})
-
-	t.Run("snapshot auto dirty", func(t *testing.T) {
-		var folder = testlib.Mktmp(t)
-		testlib.GitInit(t)
-		testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
-		testlib.GitAdd(t)
-		testlib.GitCommit(t, "whatever")
-		testlib.GitTag(t, "v0.0.1")
-		require.NoError(t, ioutil.WriteFile(filepath.Join(folder, "foo"), []byte("foobar"), 0644))
-
-		var ctx = setup(releaseOpts{
-			snapshotAuto: true,
-		})
-
-		require.True(t, ctx.Snapshot)
 		require.True(t, ctx.SkipPublish)
 	})
 
diff --git a/internal/git/git_test.go b/internal/git/git_test.go
index 732b789d..71bcf5d0 100644
--- a/internal/git/git_test.go
+++ b/internal/git/git_test.go
@@ -1,9 +1,7 @@
 package git_test
 
 import (
-	"io/ioutil"
 	"os"
-	"path/filepath"
 	"testing"
 
 	"github.com/goreleaser/goreleaser/internal/git"
@@ -48,21 +46,6 @@ func TestRepo(t *testing.T) {
 	require.False(t, git.IsRepo(), os.TempDir()+" folder should be a git repo")
 }
 
-func TestStatus(t *testing.T) {
-	var folder = testlib.Mktmp(t)
-	testlib.GitInit(t)
-	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
-	dummy, err := os.Create(filepath.Join(folder, "dummy"))
-	require.NoError(t, err)
-	testlib.GitAdd(t)
-	testlib.GitCommit(t, "commit2")
-	testlib.GitTag(t, "v0.0.1")
-	require.NoError(t, ioutil.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0644))
-	status, dirty := git.Status()
-	require.True(t, dirty, "git is currently in a dirty state")
-	require.NotEmpty(t, status, "git status")
-}
-
 func TestClean(t *testing.T) {
 	out, err := git.Clean("asdasd 'ssadas'\nadasd", nil)
 	require.NoError(t, err)
diff --git a/internal/pipe/git/git_test.go b/internal/pipe/git/git_test.go
index 24068b50..9e3bbb16 100644
--- a/internal/pipe/git/git_test.go
+++ b/internal/pipe/git/git_test.go
@@ -84,7 +84,7 @@ func TestNoTagsNoSnapshot(t *testing.T) {
 	testlib.GitCommit(t, "first")
 	var ctx = context.New(config.Project{})
 	ctx.Snapshot = false
-	require.EqualError(t, Pipe{}.Run(ctx), `git doesn't contain any tags. Either add a tag or use --snapshot or --snapshot-auto`)
+	require.EqualError(t, Pipe{}.Run(ctx), `git doesn't contain any tags. Either add a tag or use --snapshot`)
 }
 
 func TestDirty(t *testing.T) {