Skip to content

Commit

Permalink
filter tag with semver version
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Nov 21, 2020
1 parent 7f67e78 commit b47e49c
Show file tree
Hide file tree
Showing 14 changed files with 1,065 additions and 41 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/axetroy/changelog
go 1.15

require (
github.com/blang/semver/v4 v4.0.0
github.com/go-git/go-git/v5 v5.2.0
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.4.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/blang/semver v1.1.0 h1:ol1rO7QQB5uy7umSNV7VAmLugfLRD+17sYJujRNYPhg=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
21 changes: 15 additions & 6 deletions internal/client/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path"
"sort"

"github.com/blang/semver/v4"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
Expand Down Expand Up @@ -117,11 +118,15 @@ func (g *GitClient) Tags() ([]*Tag, error) {
return nil, errors.WithStack(err)
}

tags = append(tags, &Tag{
Name: name.Short(),
Hash: ref.Hash().String(),
Commit: commit,
})
_, invalid := semver.New(name.Short())

if invalid == nil {
tags = append(tags, &Tag{
Name: name.Short(),
Hash: ref.Hash().String(),
Commit: commit,
})
}
}
}
}
Expand All @@ -130,7 +135,11 @@ func (g *GitClient) Tags() ([]*Tag, error) {
prev := tags[i]
next := tags[j]

return prev.Commit.Committer.When.UnixNano() > next.Commit.Committer.When.UnixNano()
prevVersion, _ := semver.New(prev.Name)
nextVersion, _ := semver.New(next.Name)

// return prev.Commit.Committer.When.UnixNano() > next.Commit.Committer.When.UnixNano()
return prevVersion.GT(*nextVersion)
})

return tags, nil
Expand Down
4 changes: 2 additions & 2 deletions internal/printer/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/pkg/errors"
)

func Stdout(commits []*object.Commit) error {
ctx := transform("v1.0.0", commits)
func Stdout(version string, commits []*object.Commit) error {
ctx := transform(version, commits)

t := template.New("stdout")

Expand Down
71 changes: 40 additions & 31 deletions internal/printer/template_default.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
package printer

const defaultTemplate = `{{ .Version }}{{if .Feat}}
New feature:
{{ range .Feat }}
- {{if .Field.Header.Scope }}{{ .Field.Header.Scope }}: {{ end }}{{ .Field.Header.Subject }} (thanks @{{ .Author.Name }}){{if .Field.Footer }} {{if .Field.Footer.Closes }}, Closes: {{ StringsJoin .Field.Footer.Closes "," }} {{- end }} {{- end }} {{- end }}{{- end}}
{{if .Fix}}
Bugs fixed:
{{ range .Fix }}
- {{if .Field.Header.Scope }}{{ .Field.Header.Scope }}: {{ end }}{{ .Field.Header.Subject }} (thanks @{{ .Author.Name }}){{if .Field.Footer }} {{if .Field.Footer.Closes }}, Closes: {{ StringsJoin .Field.Footer.Closes "," }} {{- end }} {{- end }} {{- end }}{{- end }}
{{if .Refactor}}
Code Refactoring:
{{ range .Refactor }}
- {{if .Field.Header.Scope }}{{ .Field.Header.Scope }}: {{ end }}{{ .Field.Header.Subject }} (thanks @{{ .Author.Name }}){{if .Field.Footer }} {{if .Field.Footer.Closes }}, Closes: {{ StringsJoin .Field.Footer.Closes "," }} {{- end }} {{- end }} {{- end }}{{- end }}
{{if .Test}}
Test:
{{ range .Test }}
- {{if .Field.Header.Scope }}{{ .Field.Header.Scope }}: {{ end }}{{ .Field.Header.Subject }} (thanks @{{ .Author.Name }}){{if .Field.Footer }} {{if .Field.Footer.Closes }}, Closes: {{ StringsJoin .Field.Footer.Closes "," }} {{- end }} {{- end }} {{- end }}{{- end }}
{{if .Perf}}
Performance improves:
{{ range .Perf }}
- {{if .Field.Header.Scope }}{{ .Field.Header.Scope }}: {{ end }}{{ .Field.Header.Subject }} (thanks @{{ .Author.Name }}){{if .Field.Footer }} {{if .Field.Footer.Closes }}, Closes: {{ StringsJoin .Field.Footer.Closes "," }} {{- end }} {{- end }} {{- end }}{{- end }}
{{if .Docs}}
Documentation:
{{ range .Docs }}
- {{if .Field.Header.Scope }}{{ .Field.Header.Scope }}: {{ end }}{{ .Field.Header.Subject }} (thanks @{{ .Author.Name }}){{if .Field.Footer }} {{if .Field.Footer.Closes }}, Closes: {{ StringsJoin .Field.Footer.Closes "," }} {{- end }} {{- end }} {{- end }}{{- end }}
{{if .BreakingChanges}}
BREAKING CHANGES:
{{ range .BreakingChanges }}
const defaultTemplate = `# {{ .Version }}
{{- define "body" -}}
{{range . -}}
- {{if .Field.Header.Scope }}**{{ .Field.Header.Scope }}**: {{ end }}{{ .Field.Header.Subject }}({{.Short}}) (thanks @{{ .Author.Name }}){{if .Field.Footer }} {{if .Field.Footer.Closes }}, Closes: {{ StringsJoin .Field.Footer.Closes "," }} {{- end }} {{- end}}
{{ end }}
{{- end -}}
{{if .Feat}}
### New feature:
{{ template "body" .Feat }}
{{- end -}}
{{if .Fix -}}
### Bugs fixed:
{{ template "body" .Fix }}
{{- end -}}
{{if .Refactor -}}
### Code Refactoring:
{{ template "body" .Refactor }}
{{- end -}}
{{if .Test -}}
### Testing:
{{ template "body" .Test }}
{{- end -}}
{{if .Perf -}}
### Performance improves:
{{ template "body" .Perf }}
{{- end -}}
{{if .Docs -}}
### Documentation:
{{ template "body" .Docs }}
{{- end -}}
{{- if .BreakingChanges -}}
### BREAKING CHANGES:
{{ range .BreakingChanges -}}
- {{ .Field.Footer.BreakingChange.Title }} {{ .Field.Title }}
{{ .Field.Footer.BreakingChange.Content }}{{- end }}{{- end }}
Commits({{ len .Commits }}):{{ range .Commits }}
{{ .Short }} {{ .Field.Title }}
{{- end }}`
{{ .Field.Footer.BreakingChange.Content }}
{{- end -}}
{{- end -}}
### Commits({{ len .Commits }}):
{{range .Commits -}}
- {{ .Short }} {{ .Field.Title }}
{{ end }}`
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func run() error {
return errors.WithStack(err)
}

if err := printer.Stdout(commits); err != nil {
if err := printer.Stdout("unreleased", commits); err != nil {
return errors.WithStack(err)
}
} else {
Expand Down Expand Up @@ -94,7 +94,7 @@ func run() error {
return errors.WithStack(err)
}

if err := printer.Stdout(commits); err != nil {
if err := printer.Stdout(version, commits); err != nil {
return errors.WithStack(err)
}
}
Expand Down
22 changes: 22 additions & 0 deletions vendor/github.com/blang/semver/v4/LICENSE

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

3 changes: 3 additions & 0 deletions vendor/github.com/blang/semver/v4/go.mod

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

23 changes: 23 additions & 0 deletions vendor/github.com/blang/semver/v4/json.go

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

Loading

0 comments on commit b47e49c

Please sign in to comment.