diff --git a/internal/pipe/changelog/changelog.go b/internal/pipe/changelog/changelog.go index d0be41ccb..606aabd42 100644 --- a/internal/pipe/changelog/changelog.go +++ b/internal/pipe/changelog/changelog.go @@ -2,12 +2,13 @@ package changelog import ( + "cmp" "errors" "fmt" "os" "path/filepath" "regexp" - "sort" + "slices" "strings" "github.com/caarlos0/log" @@ -211,7 +212,7 @@ func formatChangelog(ctx *context.Context, entries []string) (string, error) { } } - sort.Slice(groups, groupSort(groups)) + slices.SortFunc(groups, groupSort) for _, group := range groups { if len(group.entries) > 0 { result = append(result, group.title) @@ -221,10 +222,8 @@ func formatChangelog(ctx *context.Context, entries []string) (string, error) { return strings.Join(result, newLineFor(ctx)), nil } -func groupSort(groups []changelogGroup) func(i, j int) bool { - return func(i, j int) bool { - return groups[i].order < groups[j].order - } +func groupSort(i, j changelogGroup) int { + return cmp.Compare(i.order, j.order) } func filterAndPrefixItems(ss []string) []string { @@ -306,17 +305,16 @@ func sortEntries(ctx *context.Context, entries []string) []string { if direction == "" { return entries } - result := make([]string, len(entries)) - copy(result, entries) - sort.Slice(result, func(i, j int) bool { - imsg := extractCommitInfo(result[i]) - jmsg := extractCommitInfo(result[j]) + slices.SortFunc(entries, func(i, j string) int { + imsg := extractCommitInfo(i) + jmsg := extractCommitInfo(j) + compareRes := strings.Compare(imsg, jmsg) if direction == "asc" { - return strings.Compare(imsg, jmsg) < 0 + return compareRes } - return strings.Compare(imsg, jmsg) > 0 + return -compareRes }) - return result + return entries } func keep(filter *regexp.Regexp, entries []string) (result []string) { diff --git a/internal/pipe/changelog/changelog_test.go b/internal/pipe/changelog/changelog_test.go index 2dbb316b6..a9ca298a6 100644 --- a/internal/pipe/changelog/changelog_test.go +++ b/internal/pipe/changelog/changelog_test.go @@ -9,14 +9,13 @@ import ( "strings" "testing" - "github.com/stretchr/testify/require" - "github.com/goreleaser/goreleaser/v2/internal/client" "github.com/goreleaser/goreleaser/v2/internal/git" "github.com/goreleaser/goreleaser/v2/internal/testctx" "github.com/goreleaser/goreleaser/v2/internal/testlib" "github.com/goreleaser/goreleaser/v2/pkg/config" "github.com/goreleaser/goreleaser/v2/pkg/context" + "github.com/stretchr/testify/require" ) func TestDescription(t *testing.T) { @@ -289,6 +288,33 @@ func TestChangelogSort(t *testing.T) { } } +func Benchmark_sortEntries(b *testing.B) { + ctx := testctx.New() + entries := []string{ + "added feature 1", + "fixed bug 2", + "ignored: whatever", + "docs: whatever", + "something about cArs we dont need", + "feat: added that thing", + "Merge pull request #999 from goreleaser/some-branch", + "this is not a Merge pull request", + } + + b.Run("asc", func(b *testing.B) { + ctx.Config.Changelog.Sort = "asc" + for range b.N { + sortEntries(ctx, entries) + } + }) + b.Run("desc", func(b *testing.B) { + ctx.Config.Changelog.Sort = "desc" + for range b.N { + sortEntries(ctx, entries) + } + }) +} + func TestChangelogInvalidSort(t *testing.T) { ctx := testctx.NewWithCfg(config.Project{ Changelog: config.Changelog{ @@ -814,12 +840,23 @@ func TestGroup(t *testing.T) { }, }, testctx.WithCurrentTag("v0.0.2"), withFirstCommit(t)) require.NoError(t, Pipe{}.Run(ctx)) - require.Contains(t, ctx.ReleaseNotes, "## Changelog") - require.Contains(t, ctx.ReleaseNotes, "### Bots") - require.Contains(t, ctx.ReleaseNotes, "### Features") - require.Contains(t, ctx.ReleaseNotes, "### Bug Fixes") - require.NotContains(t, ctx.ReleaseNotes, "### Catch nothing") - require.Contains(t, ctx.ReleaseNotes, "### Others") + require.Regexp(t, `## Changelog +### Features +\* \w+ feat: added that thing +### Bug Fixes +\* \w+ bug: Merge pull request #999 from goreleaser\/some-branch +### Bots +\* \w+ feat\(deps\): update foobar \[bot\] +### Others +\* \w+ first +\* \w+ this is not a Merge pull request +\* \w+ chore: something about cArs we dont need +\* \w+ docs: whatever +\* \w+ fix: whatever +\* \w+ ignored: whatever +\* \w+ fixed bug 2 +\* \w+ added feature 1 +`, ctx.ReleaseNotes) } func TestGroupBadRegex(t *testing.T) {