Skip to content

Commit

Permalink
feat: list semver tags
Browse files Browse the repository at this point in the history
  • Loading branch information
haunt98 committed Jul 16, 2023
1 parent cdb9739 commit dc351c9
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 24 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/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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
23 changes: 0 additions & 23 deletions internal/git/commit.go

This file was deleted.

26 changes: 26 additions & 0 deletions internal/git/example/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
28 changes: 28 additions & 0 deletions internal/git/models.go
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 40 additions & 1 deletion internal/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit dc351c9

Please sign in to comment.