Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Nov 21, 2020
1 parent 585445d commit 770ed02
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
13 changes: 10 additions & 3 deletions internal/client/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"io"
"path"
"regexp"
"sort"

"github.com/blang/semver/v4"
Expand All @@ -12,6 +13,10 @@ import (
"github.com/pkg/errors"
)

var (
versionRegexp = regexp.MustCompile(`^v`)
)

type Tag struct {
Name string // tag name
Hash string // tag hash
Expand Down Expand Up @@ -122,7 +127,9 @@ func (g *GitClient) Tags() ([]*Tag, error) {
return nil, errors.WithStack(err)
}

_, invalid := semver.New(name.Short())
version := versionRegexp.ReplaceAllString(name.Short(), "")

_, invalid := semver.New(version)

if invalid == nil {
tags = append(tags, &Tag{
Expand All @@ -139,8 +146,8 @@ func (g *GitClient) Tags() ([]*Tag, error) {
prev := tags[i]
next := tags[j]

prevVersion, _ := semver.New(prev.Name)
nextVersion, _ := semver.New(next.Name)
prevVersion, _ := semver.New(versionRegexp.ReplaceAllString(prev.Name, ""))
nextVersion, _ := semver.New(versionRegexp.ReplaceAllString(next.Name, ""))

// return prev.Commit.Committer.When.UnixNano() > next.Commit.Committer.When.UnixNano()
return prevVersion.GT(*nextVersion)
Expand Down
2 changes: 1 addition & 1 deletion internal/printer/template_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ const defaultTemplate = `# {{ .Version }}
{{- end -}}
### Commits({{ len .Commits }}):
{{range .Commits -}}
- {{ .Short }} {{ .Field.Title }}
- **{{ .Short }}** {{ .Field.Title }}
{{ end }}`
61 changes: 59 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,72 @@ func run() error {

// If no tag is specified, it will be generate automatically
if version == "" {
commits, err := client.LogsAuto()
head, err := client.Head()

if err != nil {
return errors.WithStack(err)
}

if err := printer.Stdout("unreleased", commits); err != nil {
latestTag, err := client.TagN(0)

if err != nil {
return errors.WithStack(err)
}

if latestTag == nil {
// if there is not tag
// then it's unreleased
commits, err := client.Logs(head.Hash().String(), "")

if err != nil {
return errors.WithStack(err)
}

if err := printer.Stdout("unreleased", commits); err != nil {
return errors.WithStack(err)
}
return nil
} else {
if latestTag.Commit.Hash.String() == head.Hash().String() {
// if the current head is the latest tag
nextTag, err := client.NextTag(latestTag)

if err != nil {
return errors.WithStack(err)
}

toHash := ""

if nextTag != nil {
toHash = nextTag.Commit.Hash.String()
}

commits, err := client.Logs(latestTag.Commit.Hash.String(), toHash)

if err != nil {
return errors.WithStack(err)
}

if err := printer.Stdout(latestTag.Name, commits); err != nil {
return errors.WithStack(err)
}

return nil
} else {
toHash := latestTag.Commit.Hash.String()

commits, err := client.Logs(latestTag.Commit.Hash.String(), toHash)

if err != nil {
return errors.WithStack(err)
}

if err := printer.Stdout(latestTag.Name, commits); err != nil {
return errors.WithStack(err)
}
return nil
}
}
} else {
from, err := client.TagName(version)

Expand Down

0 comments on commit 770ed02

Please sign in to comment.