From 7d4742d0ab68074e49c75914fe7f320d70d06304 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Wed, 30 Nov 2022 07:52:41 -0300 Subject: [PATCH] feat: custom tag.sort Signed-off-by: Carlos A Becker --- internal/pipe/git/git.go | 11 ++++++++++- internal/pipe/git/git_test.go | 18 ++++++++++++++++++ pkg/config/config.go | 6 ++++++ www/docs/customization/build.md | 2 +- www/docs/customization/git.md | 15 +++++++++++++++ www/mkdocs.yml | 1 + 6 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 www/docs/customization/git.md diff --git a/internal/pipe/git/git.go b/internal/pipe/git/git.go index 3b752064683..94836b7174b 100644 --- a/internal/pipe/git/git.go +++ b/internal/pipe/git/git.go @@ -22,11 +22,20 @@ func (Pipe) String() string { return "getting and validating git state" } +// this pipe does not implement Defaulter because it runs before the defaults +// pipe, and we need to set some defaults of our own first. +func setDefaults(ctx *context.Context) { + if ctx.Config.Git.TagSort == "" { + ctx.Config.Git.TagSort = "-version:refname" + } +} + // Run the pipe. func (Pipe) Run(ctx *context.Context) error { if _, err := exec.LookPath("git"); err != nil { return ErrNoGit } + setDefaults(ctx) info, err := getInfo(ctx) if err != nil { return err @@ -275,7 +284,7 @@ func gitTagsPointingAt(ctx *context.Context, ref string) ([]string, error) { "--points-at", ref, "--sort", - "-version:refname", + ctx.Config.Git.TagSort, )) } diff --git a/internal/pipe/git/git_test.go b/internal/pipe/git/git_test.go index 3c9b3f59e3e..4b12d2687cc 100644 --- a/internal/pipe/git/git_test.go +++ b/internal/pipe/git/git_test.go @@ -172,6 +172,24 @@ func TestShallowClone(t *testing.T) { }) } +func TestTagSortOrder(t *testing.T) { + testlib.Mktmp(t) + testlib.GitInit(t) + testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git") + testlib.GitCommit(t, "commit1") + testlib.GitCommit(t, "commit2") + testlib.GitCommit(t, "commit3") + testlib.GitTag(t, "v0.0.1-rc.2") + testlib.GitTag(t, "v0.0.1") + ctx := context.New(config.Project{ + Git: config.Git{ + TagSort: "-version:creatordate", + }, + }) + require.NoError(t, Pipe{}.Run(ctx)) + require.Equal(t, "v0.0.1", ctx.Git.CurrentTag) +} + func TestTagIsNotLastCommit(t *testing.T) { testlib.Mktmp(t) testlib.GitInit(t) diff --git a/pkg/config/config.go b/pkg/config/config.go index 367a6d0cee5..fbb8fa103a1 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -16,6 +16,11 @@ import ( "github.com/invopop/jsonschema" ) +// Git configs. +type Git struct { + TagSort string `yaml:"tag_sort,omitempty" json:"tag_sort,omitempty"` +} + // GitHubURLs holds the URLs to be used when using github enterprise. type GitHubURLs struct { API string `yaml:"api,omitempty" json:"api,omitempty"` @@ -931,6 +936,7 @@ type Project struct { Announce Announce `yaml:"announce,omitempty" json:"announce,omitempty"` SBOMs []SBOM `yaml:"sboms,omitempty" json:"sboms,omitempty"` Chocolateys []Chocolatey `yaml:"chocolateys,omitempty" json:"chocolatey,omitempty"` + Git Git `yaml:"git,omitempty" json:"git,omitempty"` UniversalBinaries []UniversalBinary `yaml:"universal_binaries,omitempty" json:"universal_binaries,omitempty"` diff --git a/www/docs/customization/build.md b/www/docs/customization/build.md index e6d6b9611f6..71a4e935256 100644 --- a/www/docs/customization/build.md +++ b/www/docs/customization/build.md @@ -572,7 +572,7 @@ builds: ## Complex templated environment variables -> Since v1.14. +> Since v1.14.0. Builds environment variables are templateable. diff --git a/www/docs/customization/git.md b/www/docs/customization/git.md new file mode 100644 index 00000000000..b251240dc8c --- /dev/null +++ b/www/docs/customization/git.md @@ -0,0 +1,15 @@ +# Git + +> Since v1.14.0. + +This allows you to change the behavior of some Git commands. + +```yaml +# .goreleaser.yaml +git: + # What should be used to sort tags when gathering the current and previous + # tags if there are more than one tag in the same commit. + # + # Default: `-version:refname` + tag_sort: -version:creatordate +``` diff --git a/www/mkdocs.yml b/www/mkdocs.yml index 5af39c87a88..d05835bcc33 100644 --- a/www/mkdocs.yml +++ b/www/mkdocs.yml @@ -88,6 +88,7 @@ nav: - customization/hooks.md - customization/dist.md - customization/project.md + - customization/git.md - Build: - customization/build.md - customization/verifiable_builds.md