Skip to content

Commit 9993827

Browse files
committed
refactor: removed un-needed package separation
takes reference commits from PR#2 removed unneeded package separation and separation of concerns from main to sub and helper functions
1 parent 2a0e1c9 commit 9993827

File tree

5 files changed

+144
-154
lines changed

5 files changed

+144
-154
lines changed

utils/latest-tag.go renamed to gitutils.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// SPDX-License-Identifier: MIT
22

3-
package utils
3+
package main
44

55
import (
6+
"log"
7+
"strings"
8+
69
"github.com/go-git/go-git/v5"
710
"github.com/go-git/go-git/v5/plumbing"
811
"github.com/go-git/go-git/v5/plumbing/object"
@@ -55,3 +58,51 @@ func GetLatestTagFromRepository(repository *git.Repository) (*plumbing.Reference
5558

5659
return latestTagName, previousTagReturn, nil
5760
}
61+
62+
// isCommitToNearestTag - go through git revisions to find the latest tag and the nearest next tag
63+
func isCommitToNearestTag(repo *git.Repository, commit *object.Commit) bool {
64+
latestTag, previousTag, err := GetLatestTagFromRepository(repo)
65+
66+
ref, err := repo.Head()
67+
68+
if err != nil {
69+
log.Fatal("Unable to get repository HEAD:", err)
70+
}
71+
72+
tillLatest := latestTag != nil && latestTag.Hash().String() != ref.Hash().String()
73+
74+
if err != nil {
75+
log.Fatal("Couldn't get latest tag...", err)
76+
}
77+
78+
if latestTag != nil {
79+
if tillLatest {
80+
return latestTag.Hash().String() == commit.Hash.String()
81+
}
82+
return previousTag.Hash().String() == commit.Hash.String()
83+
84+
}
85+
return false
86+
}
87+
88+
// normalizeCommit - reduces the commit message to the first line and ignore the description text of the commit
89+
func normalizeCommit(commitMessage string) string {
90+
var message string
91+
for i, msg := range strings.Split(commitMessage, "\n") {
92+
if i == 0 {
93+
message = msg
94+
break
95+
}
96+
}
97+
return strings.TrimSuffix(message, "\n")
98+
}
99+
100+
// openRepository - open the git repository and return repository reference
101+
func openRepository(path string) *git.Repository {
102+
r, err := git.PlainOpen(path)
103+
if err != nil {
104+
log.Fatal("Error opening Repository: ", err)
105+
}
106+
107+
return r
108+
}

logcategory.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"strings"
8+
)
9+
10+
// logContainer - Container of log strings
11+
type logContainer []string
12+
13+
// logsByCategory - Type to hold logs by each's category
14+
// to be left as ALLCAPS to be considered as symbols instead of selectors
15+
type logsByCategory struct {
16+
CI logContainer
17+
FIX logContainer
18+
REFACTOR logContainer
19+
FEATURE logContainer
20+
DOCS logContainer
21+
OTHER logContainer
22+
}
23+
24+
// printLog - loops through the collected logs to write them to string builder
25+
func (logs logContainer) printLog(out *strings.Builder, title string) {
26+
if len(logs) > 0 {
27+
out.WriteString(fmt.Sprintf("\n\n## %s \n", title))
28+
for _, item := range logs {
29+
out.WriteString(item + "\n")
30+
}
31+
}
32+
}
33+
34+
// ToMarkdown - Generate markdown output for the collected commits
35+
func (logs *logsByCategory) ToMarkdown() string {
36+
var markdownString strings.Builder
37+
38+
markdownString.WriteString("# Changelog \n")
39+
40+
logs.FEATURE.printLog(&markdownString, "Feature Fixes")
41+
logs.REFACTOR.printLog(&markdownString, "Performance Fixes")
42+
logs.CI.printLog(&markdownString, "CI Changes")
43+
logs.DOCS.printLog(&markdownString, "Doc Updates")
44+
logs.OTHER.printLog(&markdownString, "Other Changes")
45+
46+
return markdownString.String()
47+
}

logcategory/log-category.go

Lines changed: 0 additions & 69 deletions
This file was deleted.

main.go

Lines changed: 42 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,46 @@ import (
88
"os"
99
"strings"
1010

11-
"github.com/barelyhuman/commitlog/logcategory"
12-
"github.com/barelyhuman/commitlog/utils"
1311
"github.com/go-git/go-git/v5"
1412
"github.com/go-git/go-git/v5/plumbing/object"
1513
)
1614

17-
func normalizeCommit(commitMessage string) string {
18-
var message string
19-
for i, msg := range strings.Split(commitMessage, "\n") {
20-
if i == 0 {
21-
message = msg
22-
break
23-
}
24-
}
25-
return strings.TrimSuffix(message, "\n")
15+
// ErrMessage - simple interface around error with a custom message
16+
type ErrMessage struct {
17+
message string
18+
err error
2619
}
2720

2821
func main() {
22+
23+
if len(os.Args) < 2 {
24+
fmt.Println("Usage:\n > commitlog path/to/repo")
25+
os.Exit(0)
26+
}
27+
2928
path := os.Args[1]
30-
r, err := git.PlainOpen(path)
31-
if err != nil {
32-
log.Fatal("Error opening Repository: ", err)
29+
30+
err := CommitLog(path)
31+
32+
if err.err != nil {
33+
log.Fatal(err.message, err.err)
3334
}
35+
}
3436

35-
ref, err := r.Head()
37+
// CommitLog - Generate commit log
38+
func CommitLog(path string) ErrMessage {
39+
currentRepository := openRepository(path)
40+
41+
baseCommitReference, err := currentRepository.Head()
3642

3743
if err != nil {
38-
log.Fatal("Unable to get repository HEAD:", err)
44+
return ErrMessage{"Unable to get repository HEAD:", err}
3945
}
4046

41-
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
47+
cIter, err := currentRepository.Log(&git.LogOptions{From: baseCommitReference.Hash()})
4248

4349
if err != nil {
44-
log.Fatal("Unable to get repository commits:", err)
50+
return ErrMessage{"Unable to get repository commits:", err}
4551
}
4652

4753
var commits []*object.Commit
@@ -52,80 +58,32 @@ func main() {
5258
})
5359

5460
if err != nil {
55-
log.Fatal("Error getting commits : ", err)
56-
}
57-
58-
logContainer := new(logcategory.LogsByCategory)
59-
latestTag, _, err := utils.GetLatestTagFromRepository(r)
60-
61-
if err != nil {
62-
log.Fatal("Error Getting Tag Pairs", err)
61+
return ErrMessage{"Error getting commits : ", err}
6362
}
6463

65-
tillLatest := false
66-
67-
if latestTag != nil {
68-
if latestTag.Hash().String() == ref.Hash().String() {
69-
tillLatest = false
70-
} else {
71-
tillLatest = true
72-
}
73-
}
64+
logContainer := logsByCategory{}
7465

7566
for _, c := range commits {
76-
switch {
77-
case strings.Contains(c.Message, "ci:"):
78-
{
79-
logContainer.CI = append(logContainer.CI, c.Hash.String()+" - "+normalizeCommit(c.Message))
80-
}
81-
case strings.Contains(c.Message, "fix:"):
82-
{
83-
logContainer.FIX = append(logContainer.FIX, c.Hash.String()+" - "+normalizeCommit(c.Message))
84-
}
85-
case strings.Contains(c.Message, "refactor:"):
86-
{
87-
logContainer.REFACTOR = append(logContainer.REFACTOR, c.Hash.String()+" - "+normalizeCommit(c.Message))
88-
}
89-
case strings.Contains(c.Message, "feat:"):
90-
{
91-
logContainer.FEATURE = append(logContainer.FEATURE, c.Hash.String()+" - "+normalizeCommit(c.Message))
92-
}
93-
case strings.Contains(c.Message, "feature:"):
94-
{
95-
logContainer.FEATURE = append(logContainer.FEATURE, c.Hash.String()+" - "+normalizeCommit(c.Message))
96-
}
97-
case strings.Contains(c.Message, "docs:"):
98-
{
99-
logContainer.DOCS = append(logContainer.DOCS, c.Hash.String()+" - "+normalizeCommit(c.Message))
100-
}
67+
normalizedHash := c.Hash.String() + " - " + normalizeCommit(c.Message)
68+
switch strings.SplitN(strings.TrimSpace(c.Message), ":", 2)[0] {
69+
case "ci":
70+
logContainer.CI = append(logContainer.CI, normalizedHash)
71+
case "fix":
72+
logContainer.FIX = append(logContainer.FIX, normalizedHash)
73+
case "refactor":
74+
logContainer.REFACTOR = append(logContainer.REFACTOR, normalizedHash)
75+
case "feat", "feature":
76+
logContainer.FEATURE = append(logContainer.FEATURE, normalizedHash)
77+
case "docs":
78+
logContainer.DOCS = append(logContainer.DOCS, normalizedHash)
10179
default:
102-
{
103-
logContainer.OTHER = append(logContainer.OTHER, c.Hash.String()+" - "+normalizeCommit(c.Message))
104-
}
80+
logContainer.OTHER = append(logContainer.OTHER, normalizedHash)
10581
}
106-
107-
if isCommitToNearestTag(r, c, tillLatest) {
82+
if isCommitToNearestTag(currentRepository, c) {
10883
break
10984
}
11085
}
86+
fmt.Println(logContainer.ToMarkdown())
11187

112-
fmt.Println(logContainer.GenerateMarkdown())
113-
114-
}
115-
116-
func isCommitToNearestTag(repo *git.Repository, commit *object.Commit, tillLatest bool) bool {
117-
latestTag, previousTag, err := utils.GetLatestTagFromRepository(repo)
118-
119-
if err != nil {
120-
log.Fatal("Couldn't get latest tag...", err)
121-
}
122-
123-
if latestTag != nil {
124-
if tillLatest {
125-
return latestTag.Hash().String() == commit.Hash.String()
126-
}
127-
return previousTag.Hash().String() == commit.Hash.String()
128-
129-
}
130-
return false
88+
return ErrMessage{}
13189
}

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ b0f1b1d2bc4265cb72b70b3ae5b60f8e65f47b12 - initial commit
3434
- Cannot Specify a commit/tag to consider as the source of log
3535
- No Tests added so is probably unstable right now
3636

37+
## Note
38+
The current code base is a prototypal solution and while usable will be heavily refactored to follow best practices, if you'd like to take help of a better codebase if you are creating your own fork then you can take a refactored codebase from [https://github.com/percybolmer/commitlog](https://github.com/percybolmer/commitlog)
39+
3740
## Contribution
3841

3942
You are free to raise PR's for any kind of improvements and or feature additions, they'll be added to the repository accordingly, you can also fork up your own version

0 commit comments

Comments
 (0)