Skip to content

Commit

Permalink
feat: custom tag.sort (#3611)
Browse files Browse the repository at this point in the history
closes #3609

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Dec 3, 2022
1 parent 4c62c9b commit f5696d0
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
11 changes: 10 additions & 1 deletion internal/pipe/git/git.go
Expand Up @@ -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
Expand Down Expand Up @@ -275,7 +284,7 @@ func gitTagsPointingAt(ctx *context.Context, ref string) ([]string, error) {
"--points-at",
ref,
"--sort",
"-version:refname",
ctx.Config.Git.TagSort,
))
}

Expand Down
18 changes: 18 additions & 0 deletions internal/pipe/git/git_test.go
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/config.go
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`

Expand Down
2 changes: 1 addition & 1 deletion www/docs/customization/build.md
Expand Up @@ -572,7 +572,7 @@ builds:

## Complex templated environment variables

> Since v1.14.
> Since v1.14.0.
Builds environment variables are templateable.

Expand Down
15 changes: 15 additions & 0 deletions 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
```
1 change: 1 addition & 0 deletions www/mkdocs.yml
Expand Up @@ -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
Expand Down

0 comments on commit f5696d0

Please sign in to comment.