Skip to content

Commit 22ded61

Browse files
committed
refactor: reduce addToCommit complexity
splits out the addToCommit function into smaller deciding functions to avoid mulitple conditionals during addition of has to list
1 parent 4b693a2 commit 22ded61

File tree

3 files changed

+30
-51
lines changed

3 files changed

+30
-51
lines changed

go.mod

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

33
go 1.15
44

5-
require (
5+
require (
66
github.com/go-git/go-git/v5 v5.2.0
77
)

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
1212
github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E=
1313
github.com/go-git/go-billy/v5 v5.0.0 h1:7NQHvd9FVid8VL4qVUMm8XifBK+2xCoZ2lSk0agRrHM=
1414
github.com/go-git/go-billy/v5 v5.0.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0=
15-
github.com/go-git/go-git v1.0.0 h1:YcN9iDGDoXuIw0vHls6rINwV416HYa0EB2X+RBsyYp4=
16-
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=
1815
github.com/go-git/go-git-fixtures/v4 v4.0.2-0.20200613231340-f56387b50c12/go.mod h1:m+ICp2rF3jDhFgEZ/8yziagdT1C+ZpZcrJjappBCDSw=
1916
github.com/go-git/go-git/v5 v5.2.0 h1:YPBLG/3UK1we1ohRkncLjaXWLW+HKp5QNM/jTli2JgI=
2017
github.com/go-git/go-git/v5 v5.2.0/go.mod h1:kh02eMX+wdqqxgNMEyq8YgwlIOsDOa9homkUq1PoTMs=

logcategory.go

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ func (logs logsByCategory) Setup() {
4141
}
4242

4343
// printLog - loops through the collected logs to write them to string builder
44-
func (logs logContainer) printLog(out *strings.Builder, title string, skipped bool) {
45-
if !logs.include {
44+
func (container logContainer) printLog(out *strings.Builder, title string, skipped bool) {
45+
if !container.include {
4646
return
4747
}
48-
if len(logs.commits) > 0 {
48+
if len(container.commits) > 0 {
4949
if !skipped {
5050
out.WriteString(fmt.Sprintf("\n\n## %s \n", title))
5151
}
52-
for _, item := range logs.commits {
52+
for _, item := range container.commits {
5353
out.WriteString(item + "\n")
5454
}
5555
}
@@ -80,59 +80,41 @@ func (logs *logsByCategory) ToMarkdown(skipped bool) string {
8080

8181
// AddCommit - Add a commit to the needed logContainer based on skip and include flag
8282
func (logs *logsByCategory) AddCommit(key, commitHash string, skip bool) {
83-
var addCommitToContainer *logContainer
83+
addCommitToContainer := logs.findContainerByKey(key)
84+
if !addCommitToContainer.canAddToContainer(skip) {
85+
addCommitToContainer = &logs.UNCLASSIFIED
86+
}
87+
if addCommitToContainer != nil {
88+
addCommitToContainer.commits = append(addCommitToContainer.commits, commitHash)
89+
}
90+
}
91+
92+
func (logs *logsByCategory) findContainerByKey(key string) *logContainer {
8493
switch key {
8594
case "ci":
86-
if logs.CI.include && !skip {
87-
addCommitToContainer = &logs.CI
88-
} else if skip && logs.CI.include {
89-
addCommitToContainer = &logs.UNCLASSIFIED
90-
}
95+
return &logs.CI
9196
case "fix":
92-
if logs.FIX.include && !skip {
93-
addCommitToContainer = &logs.FIX
94-
} else if skip && logs.FIX.include {
95-
addCommitToContainer = &logs.UNCLASSIFIED
96-
}
97+
return &logs.FIX
9798
case "refactor":
98-
if logs.REFACTOR.include && !skip {
99-
addCommitToContainer = &logs.REFACTOR
100-
} else if skip && logs.REFACTOR.include {
101-
addCommitToContainer = &logs.UNCLASSIFIED
102-
}
99+
return &logs.REFACTOR
103100
case "feat", "feature":
104-
if logs.FEATURE.include && !skip {
105-
addCommitToContainer = &logs.FEATURE
106-
} else if skip && logs.FEATURE.include {
107-
addCommitToContainer = &logs.UNCLASSIFIED
108-
}
101+
return &logs.FEATURE
109102
case "docs":
110-
if logs.DOCS.include && !skip {
111-
addCommitToContainer = &logs.DOCS
112-
} else if skip && logs.DOCS.include {
113-
addCommitToContainer = &logs.UNCLASSIFIED
114-
}
103+
return &logs.DOCS
115104
case "test":
116-
if logs.TEST.include && !skip {
117-
addCommitToContainer = &logs.TEST
118-
} else if skip && logs.TEST.include {
119-
addCommitToContainer = &logs.UNCLASSIFIED
120-
}
105+
return &logs.TEST
121106
case "chore":
122-
if logs.CHORE.include && !skip {
123-
addCommitToContainer = &logs.CHORE
124-
} else if skip && logs.CHORE.include {
125-
addCommitToContainer = &logs.UNCLASSIFIED
126-
}
107+
return &logs.CHORE
127108
default:
128-
if logs.OTHER.include && !skip {
129-
addCommitToContainer = &logs.OTHER
130-
} else if skip && logs.OTHER.include {
131-
addCommitToContainer = &logs.UNCLASSIFIED
132-
}
109+
return &logs.OTHER
133110
}
111+
}
134112

135-
if addCommitToContainer != nil {
136-
addCommitToContainer.commits = append(addCommitToContainer.commits, commitHash)
113+
func (container *logContainer) canAddToContainer(skip bool) bool {
114+
if container.include && !skip {
115+
return true
116+
} else if skip && container.include {
117+
return false
137118
}
119+
return true
138120
}

0 commit comments

Comments
 (0)