Skip to content

Commit f3c9069

Browse files
committed
feat: latest tag
adds handlers for getting commits only till latest tag if it exists
1 parent 0553e17 commit f3c9069

File tree

5 files changed

+151
-70
lines changed

5 files changed

+151
-70
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/barelyhuman/commitlog
22

33
go 1.15
44

5-
require github.com/go-git/go-git/v5 v5.2.0
5+
require (
6+
github.com/go-git/go-git v4.7.0+incompatible // indirect
7+
github.com/go-git/go-git/v5 v5.2.0
8+
)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ github.com/go-git/go-billy/v5 v5.0.0 h1:7NQHvd9FVid8VL4qVUMm8XifBK+2xCoZ2lSk0agR
1414
github.com/go-git/go-billy/v5 v5.0.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0=
1515
github.com/go-git/go-git v1.0.0 h1:YcN9iDGDoXuIw0vHls6rINwV416HYa0EB2X+RBsyYp4=
1616
github.com/go-git/go-git v4.7.0+incompatible h1:+W9rgGY4DOKKdX2x6HxSR7HNeTxqiKrOvKnuittYVdA=
17+
github.com/go-git/go-git v4.7.0+incompatible/go.mod h1:6+421e08gnZWn30y26Vchf7efgYLe4dl5OQbBSUXShE=
1718
github.com/go-git/go-git-fixtures/v4 v4.0.2-0.20200613231340-f56387b50c12/go.mod h1:m+ICp2rF3jDhFgEZ/8yziagdT1C+ZpZcrJjappBCDSw=
1819
github.com/go-git/go-git/v5 v5.2.0 h1:YPBLG/3UK1we1ohRkncLjaXWLW+HKp5QNM/jTli2JgI=
1920
github.com/go-git/go-git/v5 v5.2.0/go.mod h1:kh02eMX+wdqqxgNMEyq8YgwlIOsDOa9homkUq1PoTMs=

logcategory/log-category.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package logcategory
2+
3+
// LogsByCategory - Type to hold logs by each's category
4+
type LogsByCategory struct {
5+
CI []string
6+
FIX []string
7+
REFACTOR []string
8+
FEATURE []string
9+
DOCS []string
10+
OTHER []string
11+
}
12+
13+
// GenerateMarkdown - Generate markdown output for the collected commits
14+
func (logContainer *LogsByCategory) GenerateMarkdown() string {
15+
markDownString := ""
16+
17+
markDownString += "# Changelog \n"
18+
19+
if len(logContainer.CI) > 0 {
20+
markDownString += "\n\n## CI Changes \n"
21+
22+
for _, item := range logContainer.CI {
23+
markDownString += item + "\n"
24+
}
25+
}
26+
27+
if len(logContainer.FIX) > 0 {
28+
markDownString += "\n\n## Fixes \n"
29+
for _, item := range logContainer.FIX {
30+
markDownString += item + "\n"
31+
}
32+
}
33+
34+
if len(logContainer.REFACTOR) > 0 {
35+
markDownString += "\n\n## Performance Fixes \n"
36+
37+
for _, item := range logContainer.REFACTOR {
38+
markDownString += item + "\n"
39+
}
40+
}
41+
42+
if len(logContainer.FEATURE) > 0 {
43+
44+
markDownString += "\n\n## Feature Fixes \n"
45+
for _, item := range logContainer.FEATURE {
46+
markDownString += item + "\n"
47+
}
48+
}
49+
50+
if len(logContainer.DOCS) > 0 {
51+
52+
markDownString += "\n\n## Doc Updates \n"
53+
for _, item := range logContainer.DOCS {
54+
markDownString += item + "\n"
55+
}
56+
}
57+
58+
if len(logContainer.OTHER) > 0 {
59+
60+
markDownString += "\n\n## Other Changes \n"
61+
for _, item := range logContainer.OTHER {
62+
markDownString += item + "\n"
63+
}
64+
}
65+
66+
return markDownString
67+
}

main.go

Lines changed: 31 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,16 @@ package main
22

33
import (
44
"fmt"
5+
"log"
56
"os"
67
"strings"
78

9+
"github.com/barelyhuman/commitlog/logcategory"
10+
"github.com/barelyhuman/commitlog/utils"
811
"github.com/go-git/go-git/v5"
912
"github.com/go-git/go-git/v5/plumbing/object"
1013
)
1114

12-
// LogsByCategory - Type to hold logs by each's category
13-
type LogsByCategory struct {
14-
CI []string
15-
FIX []string
16-
REFACTOR []string
17-
FEATURE []string
18-
DOCS []string
19-
OTHER []string
20-
}
21-
22-
// GenerateMarkdown - Generate markdown output for the collected commits
23-
func (logContainer *LogsByCategory) GenerateMarkdown() string {
24-
markDownString := ""
25-
26-
markDownString += "# Changelog \n"
27-
28-
if len(logContainer.CI) > 0 {
29-
markDownString += "\n\n## CI Changes \n"
30-
31-
for _, item := range logContainer.CI {
32-
markDownString += item + "\n"
33-
}
34-
}
35-
36-
if len(logContainer.FIX) > 0 {
37-
markDownString += "\n\n## Fixes \n"
38-
for _, item := range logContainer.FIX {
39-
markDownString += item + "\n"
40-
}
41-
}
42-
43-
if len(logContainer.REFACTOR) > 0 {
44-
markDownString += "\n\n## Performance Fixes \n"
45-
46-
for _, item := range logContainer.REFACTOR {
47-
markDownString += item + "\n"
48-
}
49-
}
50-
51-
if len(logContainer.FEATURE) > 0 {
52-
53-
markDownString += "\n\n## Feature Fixes \n"
54-
for _, item := range logContainer.FEATURE {
55-
markDownString += item + "\n"
56-
}
57-
}
58-
59-
if len(logContainer.DOCS) > 0 {
60-
61-
markDownString += "\n\n## Doc Updates \n"
62-
for _, item := range logContainer.DOCS {
63-
markDownString += item + "\n"
64-
}
65-
}
66-
67-
if len(logContainer.OTHER) > 0 {
68-
69-
markDownString += "\n\n## Other Changes \n"
70-
for _, item := range logContainer.OTHER {
71-
markDownString += item + "\n"
72-
}
73-
}
74-
75-
return markDownString
76-
}
77-
7815
func normalizeCommit(commitMessage string) string {
7916
var message string
8017
for i, msg := range strings.Split(commitMessage, "\n") {
@@ -92,9 +29,16 @@ func main() {
9229
ref, _ := r.Head()
9330
cIter, _ := r.Log(&git.LogOptions{From: ref.Hash()})
9431

95-
logContainer := new(LogsByCategory)
32+
var commits []*object.Commit
9633

9734
_ = cIter.ForEach(func(c *object.Commit) error {
35+
commits = append(commits, c)
36+
return nil
37+
})
38+
39+
logContainer := new(logcategory.LogsByCategory)
40+
41+
for _, c := range commits {
9842
switch {
9943
case strings.Contains(c.Message, "ci:"):
10044
{
@@ -121,9 +65,27 @@ func main() {
12165
logContainer.OTHER = append(logContainer.OTHER, c.Hash.String()+" - "+normalizeCommit(c.Message))
12266
}
12367
}
124-
return nil
125-
})
68+
69+
if isCommitToNearestTag(r, c) {
70+
break
71+
}
72+
}
12673

12774
fmt.Println(logContainer.GenerateMarkdown())
12875

12976
}
77+
78+
func isCommitToNearestTag(repo *git.Repository, commit *object.Commit) bool {
79+
latestTag, err := utils.GetLatestTagFromRepository(repo)
80+
if err != nil {
81+
log.Fatal("Couldn't get latest tag...", err)
82+
}
83+
if err != nil {
84+
log.Fatal("Couldn't access tag...", err)
85+
}
86+
87+
if latestTag != nil {
88+
return latestTag.Hash().String() == commit.Hash.String()
89+
}
90+
return false
91+
}

utils/latest-tag.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package utils
2+
3+
import (
4+
"github.com/go-git/go-git/v5"
5+
"github.com/go-git/go-git/v5/plumbing"
6+
"github.com/go-git/go-git/v5/plumbing/object"
7+
)
8+
9+
// GetLatestTagFromRepository - Get the latest Tag reference from the repo
10+
func GetLatestTagFromRepository(repository *git.Repository) (*plumbing.Reference, error) {
11+
tagRefs, err := repository.Tags()
12+
if err != nil {
13+
return nil, err
14+
}
15+
16+
var latestTagCommit *object.Commit
17+
var latestTagName *plumbing.Reference
18+
err = tagRefs.ForEach(func(tagRef *plumbing.Reference) error {
19+
revision := plumbing.Revision(tagRef.Name().String())
20+
21+
tagCommitHash, err := repository.ResolveRevision(revision)
22+
if err != nil {
23+
return err
24+
}
25+
26+
commit, err := repository.CommitObject(*tagCommitHash)
27+
if err != nil {
28+
return err
29+
}
30+
31+
if latestTagCommit == nil {
32+
latestTagCommit = commit
33+
latestTagName = tagRef
34+
}
35+
36+
if commit.Committer.When.After(latestTagCommit.Committer.When) {
37+
latestTagCommit = commit
38+
latestTagName = tagRef
39+
}
40+
41+
return nil
42+
})
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
return latestTagName, nil
48+
}

0 commit comments

Comments
 (0)