feat: list semver tags
parent
cdb9739442
commit
dc351c9a22
1
go.mod
1
go.mod
|
@ -3,6 +3,7 @@ module github.com/haunt98/changeloguru
|
|||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/Masterminds/semver/v3 v3.2.1
|
||||
github.com/go-git/go-git/v5 v5.7.0
|
||||
github.com/make-go-great/color-go v0.4.1
|
||||
github.com/make-go-great/date-go v0.5.0
|
||||
|
|
2
go.sum
2
go.sum
|
@ -1,3 +1,5 @@
|
|||
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
|
||||
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
|
||||
github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA=
|
||||
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
|
||||
github.com/ProtonMail/go-crypto v0.0.0-20230518184743-7afd39499903 h1:ZK3C5DtzV2nVAQTx5S5jQvMeDqWtD1By5mOoyY/xJek=
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
)
|
||||
|
||||
// Commit stores all git-commit information
|
||||
type Commit struct {
|
||||
Author Author
|
||||
Message string
|
||||
}
|
||||
|
||||
// Convert from git-commit
|
||||
func newCommit(commit *object.Commit) Commit {
|
||||
return Commit{
|
||||
Message: commit.Message,
|
||||
Author: Author{
|
||||
Name: commit.Author.Name,
|
||||
Email: commit.Author.Email,
|
||||
When: commit.Author.When,
|
||||
},
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/haunt98/changeloguru/internal/git"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r, err := git.NewRepository(".")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
commits, err := r.Log("", "")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("Commits: ", commits)
|
||||
|
||||
tags, err := r.SemVerTags()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("Tags: ", tags)
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
)
|
||||
|
||||
// Commit stores all git-commit information
|
||||
type Commit struct {
|
||||
Author Author
|
||||
Message string
|
||||
}
|
||||
|
||||
// Convert from go-git
|
||||
func newCommit(c *object.Commit) Commit {
|
||||
return Commit{
|
||||
Message: c.Message,
|
||||
Author: Author{
|
||||
Name: c.Author.Name,
|
||||
Email: c.Author.Email,
|
||||
When: c.Author.When,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type SemVerTag struct {
|
||||
Version *semver.Version
|
||||
}
|
|
@ -2,7 +2,9 @@ package git
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
|
@ -12,13 +14,15 @@ import (
|
|||
const (
|
||||
head = "HEAD"
|
||||
|
||||
defaultCommitCount = 10
|
||||
defaultCommitCount = 128
|
||||
defaultTagCount = 32
|
||||
)
|
||||
|
||||
// Repository is an abstraction for git-repository
|
||||
type Repository interface {
|
||||
Log(fromRev, toRev string) ([]Commit, error)
|
||||
Commit(commitMessage string, paths ...string) error
|
||||
SemVerTags() ([]SemVerTag, error)
|
||||
}
|
||||
|
||||
type repo struct {
|
||||
|
@ -161,3 +165,38 @@ func newIterFn(commits *[]Commit, beginFn, endFn stopFn) func(c *object.Commit)
|
|||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Return SemVer tags from earliest to latest
|
||||
func (r *repo) SemVerTags() ([]SemVerTag, error) {
|
||||
iter, err := r.gitRepo.Tags()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
versions := make([]*semver.Version, 0, defaultTagCount)
|
||||
|
||||
if err := iter.ForEach(func(r *plumbing.Reference) error {
|
||||
version, err := semver.NewVersion(r.Name().Short())
|
||||
if err != nil {
|
||||
// Ignore bad tag
|
||||
return nil
|
||||
}
|
||||
|
||||
versions = append(versions, version)
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sort.Sort(semver.Collection(versions))
|
||||
|
||||
tags := make([]SemVerTag, 0, len(versions))
|
||||
for _, version := range versions {
|
||||
tags = append(tags, SemVerTag{
|
||||
Version: version,
|
||||
})
|
||||
}
|
||||
|
||||
return tags, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue