From 319e5d06bb1ce84b63f03765780f0d43e0ff5333 Mon Sep 17 00:00:00 2001 From: aeneasr Date: Fri, 31 Jan 2020 17:41:18 +0100 Subject: [PATCH 1/6] fix(git): Use CI envronment variables to figure out tag This patch detects CI environments and uses the available tag information when collecting the git tag. This resolves issues where one commit has multiple tags. Closes #1163 Closes #1311 --- internal/pipe/changelog/changelog.go | 5 +++ internal/pipe/changelog/changelog_test.go | 24 +++++++++++++ internal/pipe/git/git.go | 8 ++++- internal/pipe/git/git_test.go | 42 ++++++++++++++++++++++- www/content/build.md | 6 ++++ www/content/release.md | 6 ++++ 6 files changed, 89 insertions(+), 2 deletions(-) diff --git a/internal/pipe/changelog/changelog.go b/internal/pipe/changelog/changelog.go index e78f63943f0..ede93695782 100644 --- a/internal/pipe/changelog/changelog.go +++ b/internal/pipe/changelog/changelog.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io/ioutil" + "os" "path/filepath" "regexp" "sort" @@ -189,6 +190,10 @@ func gitLog(refs ...string) (string, error) { } func previous(tag string) (result string, err error) { + if tag := os.Getenv("GORELEASER_PREVIOUS_TAG"); tag != "" { + return tag, nil + } + result, err = git.Clean(git.Run("describe", "--tags", "--abbrev=0", fmt.Sprintf("tags/%s^", tag))) if err != nil { result, err = git.Clean(git.Run("rev-list", "--max-parents=0", "HEAD")) diff --git a/internal/pipe/changelog/changelog_test.go b/internal/pipe/changelog/changelog_test.go index bd43133a78b..fcf7201aed7 100644 --- a/internal/pipe/changelog/changelog_test.go +++ b/internal/pipe/changelog/changelog_test.go @@ -109,6 +109,30 @@ func TestChangelog(t *testing.T) { require.NotEmpty(t, string(bts)) } +func TestChangelogPreviousTagEnv(t *testing.T) { + folder, back := testlib.Mktmp(t) + defer back() + testlib.GitInit(t) + testlib.GitCommit(t, "first") + testlib.GitTag(t, "v0.0.1") + testlib.GitCommit(t, "second") + testlib.GitTag(t, "v0.0.2") + testlib.GitCommit(t, "third") + testlib.GitTag(t, "v0.0.3") + var ctx = context.New(config.Project{ + Dist: folder, + Changelog: config.Changelog{Filters: config.Filters{}}, + }) + ctx.Git.CurrentTag = "v0.0.3" + require.NoError(t, os.Setenv("GORELEASER_PREVIOUS_TAG", "v0.0.1")) + require.NoError(t, Pipe{}.Run(ctx)) + require.NoError(t, os.Setenv("GORELEASER_PREVIOUS_TAG", "")) + require.Contains(t, ctx.ReleaseNotes, "## Changelog") + require.NotContains(t, ctx.ReleaseNotes, "first") + require.Contains(t, ctx.ReleaseNotes, "second") + require.Contains(t, ctx.ReleaseNotes, "third") +} + func TestChangelogForGitlab(t *testing.T) { folder, back := testlib.Mktmp(t) defer back() diff --git a/internal/pipe/git/git.go b/internal/pipe/git/git.go index 9e6f176356c..c354323d240 100644 --- a/internal/pipe/git/git.go +++ b/internal/pipe/git/git.go @@ -1,14 +1,16 @@ package git import ( + "os" "os/exec" "strings" "github.com/apex/log" + "github.com/pkg/errors" + "github.com/goreleaser/goreleaser/internal/git" "github.com/goreleaser/goreleaser/internal/pipe" "github.com/goreleaser/goreleaser/pkg/context" - "github.com/pkg/errors" ) // Pipe that sets up git state @@ -122,6 +124,10 @@ func getFullCommit() (string, error) { } func getTag() (string, error) { + if tag := os.Getenv("GORELEASER_CURRENT_TAG"); tag != "" { + return tag, nil + } + return git.Clean(git.Run("describe", "--tags", "--abbrev=0")) } diff --git a/internal/pipe/git/git_test.go b/internal/pipe/git/git_test.go index dcdef6d2aeb..1d4b19211b5 100644 --- a/internal/pipe/git/git_test.go +++ b/internal/pipe/git/git_test.go @@ -6,10 +6,12 @@ import ( "path/filepath" "testing" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/goreleaser/goreleaser/internal/testlib" "github.com/goreleaser/goreleaser/pkg/config" "github.com/goreleaser/goreleaser/pkg/context" - "github.com/stretchr/testify/assert" ) func TestDescription(t *testing.T) { @@ -189,3 +191,41 @@ func TestGitNotInPath(t *testing.T) { assert.NoError(t, os.Setenv("PATH", "")) assert.EqualError(t, Pipe{}.Run(context.New(config.Project{})), ErrNoGit.Error()) } + +func TestTagFromCI(t *testing.T) { + _, back := testlib.Mktmp(t) + defer back() + testlib.GitInit(t) + testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git") + testlib.GitCommit(t, "commit1") + testlib.GitTag(t, "v0.0.1") + testlib.GitTag(t, "v0.0.2") + + for _, tc := range []struct { + envs map[string]string + expected string + }{ + // It is not possible to concisely figure out the tag if a commit has more than one tags. Git always + // returns the tags in lexicographical order (ASC), which implies that we expect v0.0.1 here. + // More details: https://github.com/goreleaser/goreleaser/issues/1163 + {expected: "v0.0.1"}, + { + envs: map[string]string{"GORELEASER_CURRENT_TAG": "v0.0.2"}, + expected: "v0.0.2", + }, + } { + for name, value := range tc.envs { + require.NoError(t, os.Setenv(name, value)) + } + + var ctx = &context.Context{ + Config: config.Project{}, + } + assert.NoError(t, Pipe{}.Run(ctx)) + assert.Equal(t, tc.expected, ctx.Git.CurrentTag) + + for name := range tc.envs { + require.NoError(t, os.Setenv(name, "")) + } + } +} diff --git a/www/content/build.md b/www/content/build.md index c0cdacf6cdc..ca2edadf7f6 100644 --- a/www/content/build.md +++ b/www/content/build.md @@ -149,3 +149,9 @@ GOVERSION=$(go version) goreleaser ``` [hook]: /hooks + +## Define Build Tag + +Goreleaser uses `git describe` to get the build tag. You can set +a different build tag using the environment variable `GORELEASER_CURRENT_TAG`. +This is useful in scenarios where two tags point to the same commit. diff --git a/www/content/release.md b/www/content/release.md index 190649d2894..f29fda6916a 100644 --- a/www/content/release.md +++ b/www/content/release.md @@ -137,6 +137,12 @@ changelog: - (?i)foo ``` +### Define Previos Tag + +Goreleaser uses `git describe` to get the previous tag used for generating the Changelog. +. You can set a different build tag using the environment variable `GORELEASER_PREVIOUS_TAG`. +This is useful in scenarios where two tags point to the same commit. + ## Custom release notes You can specify a file containing your custom release notes, and From 7c9ad5a3a812b465f3841498e88cfc549fc61761 Mon Sep 17 00:00:00 2001 From: hackerman <3372410+aeneasr@users.noreply.github.com> Date: Fri, 31 Jan 2020 18:51:16 +0100 Subject: [PATCH 2/6] Update www/content/release.md Co-Authored-By: Carlos Alexandro Becker --- www/content/release.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/content/release.md b/www/content/release.md index f29fda6916a..f6293c515db 100644 --- a/www/content/release.md +++ b/www/content/release.md @@ -137,7 +137,7 @@ changelog: - (?i)foo ``` -### Define Previos Tag +### Define Previous Tag Goreleaser uses `git describe` to get the previous tag used for generating the Changelog. . You can set a different build tag using the environment variable `GORELEASER_PREVIOUS_TAG`. From 851a7be2e88c5c7f2a86481b5d44e212f3a8ebb8 Mon Sep 17 00:00:00 2001 From: hackerman <3372410+aeneasr@users.noreply.github.com> Date: Fri, 31 Jan 2020 18:51:29 +0100 Subject: [PATCH 3/6] Update www/content/release.md Co-Authored-By: Carlos Alexandro Becker --- www/content/release.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/content/release.md b/www/content/release.md index f6293c515db..ee16a24d489 100644 --- a/www/content/release.md +++ b/www/content/release.md @@ -139,7 +139,7 @@ changelog: ### Define Previous Tag -Goreleaser uses `git describe` to get the previous tag used for generating the Changelog. +GoReleaser uses `git describe` to get the previous tag used for generating the Changelog. . You can set a different build tag using the environment variable `GORELEASER_PREVIOUS_TAG`. This is useful in scenarios where two tags point to the same commit. From cc103cd728ff9dc2f03c7f4d009d96218bac2a47 Mon Sep 17 00:00:00 2001 From: hackerman <3372410+aeneasr@users.noreply.github.com> Date: Fri, 31 Jan 2020 19:26:27 +0100 Subject: [PATCH 4/6] Update www/content/build.md Co-Authored-By: Carlos Alexandro Becker --- www/content/build.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/content/build.md b/www/content/build.md index ca2edadf7f6..910896eb4cf 100644 --- a/www/content/build.md +++ b/www/content/build.md @@ -152,6 +152,6 @@ GOVERSION=$(go version) goreleaser ## Define Build Tag -Goreleaser uses `git describe` to get the build tag. You can set +GoReleaser uses `git describe` to get the build tag. You can set a different build tag using the environment variable `GORELEASER_CURRENT_TAG`. This is useful in scenarios where two tags point to the same commit. From ae2146e0c1be7f5ba00eb2e5bbcec82f868c0ba5 Mon Sep 17 00:00:00 2001 From: hackerman <3372410+aeneasr@users.noreply.github.com> Date: Fri, 31 Jan 2020 19:26:55 +0100 Subject: [PATCH 5/6] Update www/content/release.md Co-Authored-By: Carlos Alexandro Becker --- www/content/release.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/content/release.md b/www/content/release.md index ee16a24d489..fa585bdea45 100644 --- a/www/content/release.md +++ b/www/content/release.md @@ -140,7 +140,7 @@ changelog: ### Define Previous Tag GoReleaser uses `git describe` to get the previous tag used for generating the Changelog. -. You can set a different build tag using the environment variable `GORELEASER_PREVIOUS_TAG`. +You can set a different build tag using the environment variable `GORELEASER_PREVIOUS_TAG`. This is useful in scenarios where two tags point to the same commit. ## Custom release notes From add1981bf4f1c281b633d6132cb0c1c3a621d97d Mon Sep 17 00:00:00 2001 From: hackerman <3372410+aeneasr@users.noreply.github.com> Date: Fri, 31 Jan 2020 19:33:00 +0100 Subject: [PATCH 6/6] feat(doc): Document git tag override in environment --- www/content/environment.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/www/content/environment.md b/www/content/environment.md index 7e5261f3806..b79003a18b8 100644 --- a/www/content/environment.md +++ b/www/content/environment.md @@ -117,3 +117,7 @@ func main() { ``` You can override this by changing the `ldflags` option in the `build` section. + +## Overriding Git Tags + +You can force the [build tag](/build#define-build-tag) and [previous changelog tag](/release#define-previous-tag) using environment variables. This is useful in cases where one git commit is referenced by multiple git tags.