Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom tag.sort #3611

Merged
merged 1 commit into from Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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