Skip to content

Commit

Permalink
feat: include prerelease suffix in git (#3841)
Browse files Browse the repository at this point in the history
<!--

Hi, thanks for contributing!

Please make sure you read our CONTRIBUTING guide.

Also, add tests and the respective documentation changes as well.

-->


<!-- If applied, this commit will... -->

This commit will fix bad version tag sort if there is a prerelease on
the same commit as a release tag. Current output is shown below
```
❯ git tag --points-at HEAD --sort=-version:refname --format='%(creatordate)%09%(refname)'
Thu Mar 2 21:38:51 2023 +0300   refs/tags/v1.13.0-rc3
Thu Mar 2 21:38:51 2023 +0300   refs/tags/v1.13.0
```
Test is changed to match current default value so it will fail without
this fix.

Default value `-` is set to the one that is described inside
[docs](https://goreleaser.com/how-it-works/?h=prerelease#how-it-works),
but people are still allowed to change it.

Output with fix applied
```
❯ git -c versionsort.suffix=- tag --points-at HEAD --sort -version:refname --format='%(creatordate)%09%(refname)'
Thu Mar 2 21:38:51 2023 +0300   refs/tags/v1.13.0
Thu Mar 2 21:38:51 2023 +0300   refs/tags/v1.13.0-rc3
```

<!-- # Provide links to any relevant tickets, URLs or other resources
-->
More info about `versionsort.suffix` can be found
[here](https://github.com/git/git/blob/master/Documentation/config/versionsort.txt#L5)

Docs as well both schemas are updated as well.

I am not sure if users should be allowed to change this option at all.
  • Loading branch information
vandot committed Mar 20, 2023
1 parent dfdbad1 commit 8877fe1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
15 changes: 12 additions & 3 deletions internal/pipe/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,23 @@ func getPreviousTag(ctx *context.Context, current string) (string, error) {
}

func gitTagsPointingAt(ctx *context.Context, ref string) ([]string, error) {
return git.CleanAllLines(git.Run(
ctx,
args := []string{}
if ctx.Config.Git.PrereleaseSuffix != "" {
args = append(
args,
"-c",
"versionsort.suffix="+ctx.Config.Git.PrereleaseSuffix,
)
}
args = append(
args,
"tag",
"--points-at",
ref,
"--sort",
ctx.Config.Git.TagSort,
))
)
return git.CleanAllLines(git.Run(ctx, args...))
}

func gitDescribe(ctx *context.Context, ref string) (string, error) {
Expand Down
21 changes: 20 additions & 1 deletion internal/pipe/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,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.2")
testlib.GitTag(t, "v0.0.1")
ctx := testctx.NewWithCfg(config.Project{
Git: config.Git{
TagSort: "-version:refname",
},
})
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "v0.0.2", ctx.Git.CurrentTag)
}

func TestTagSortOrderPrerelease(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
Expand All @@ -180,7 +198,8 @@ func TestTagSortOrder(t *testing.T) {
testlib.GitTag(t, "v0.0.1")
ctx := testctx.NewWithCfg(config.Project{
Git: config.Git{
TagSort: "-version:creatordate",
TagSort: "-version:refname",
PrereleaseSuffix: "-",
},
})
require.NoError(t, Pipe{}.Run(ctx))
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (

// Git configs.
type Git struct {
TagSort string `yaml:"tag_sort,omitempty" json:"tag_sort,omitempty"`
TagSort string `yaml:"tag_sort,omitempty" json:"tag_sort,omitempty"`
PrereleaseSuffix string `yaml:"prerelease_suffix,omitempty" json:"prerelease_suffix,omitempty"`
}

// GitHubURLs holds the URLs to be used when using github enterprise.
Expand Down
5 changes: 5 additions & 0 deletions www/docs/customization/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ git:
#
# Default: `-version:refname`
tag_sort: -version:creatordate

# What should be used to specify prerelease suffix while sorting tags when gathering
# the current and previous tags if there are more than one tag in the same commit.
#
prerelease_suffix: "-"
```
3 changes: 3 additions & 0 deletions www/docs/static/schema-pro.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions www/docs/static/schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8877fe1

Please sign in to comment.