Skip to content

Commit

Permalink
Merge 15ce9db into 6050f20
Browse files Browse the repository at this point in the history
  • Loading branch information
momotaro98 committed Nov 27, 2019
2 parents 6050f20 + 15ce9db commit 5a69e9c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 15 deletions.
14 changes: 4 additions & 10 deletions cmd/git-chglog/config_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,9 @@ info:
options:
commits:
# filters:
# Type:
# - feat
# - fix
# - perf
# - refactor
# Type:%s
commit_groups:
# title_maps:
# feat: Features
# fix: Bug Fixes
# perf: Performance Improvements
# refactor: Code Refactoring
# title_maps:%s
header:
pattern: "%s"
pattern_maps:%s
Expand All @@ -66,6 +58,8 @@ options:
ans.Style,
defaultTemplateFilename,
repoURL,
msgFormat.FilterTypesString(),
msgFormat.TitleMapsString(),
msgFormat.pattern,
msgFormat.PatternMapString(),
)
Expand Down
66 changes: 61 additions & 5 deletions cmd/git-chglog/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@ var (
}
)

type typeSample struct {
typeName string
title string
}

// CommitMessageFormat ...
type CommitMessageFormat struct {
display string
preview string
pattern string
patternMaps []string
typeSamples []typeSample
}

// Display ...
Expand All @@ -52,13 +58,11 @@ func (f *CommitMessageFormat) Preview() string {

// PatternMapString ...
func (f *CommitMessageFormat) PatternMapString() string {
s := " []"
l := len(f.patternMaps)
if l == 0 {
return s
if len(f.patternMaps) == 0 {
return " []"
}

arr := make([]string, l)
arr := make([]string, len(f.patternMaps))
for i, p := range f.patternMaps {
arr[i] = fmt.Sprintf(
"%s- %s",
Expand All @@ -70,37 +74,89 @@ func (f *CommitMessageFormat) PatternMapString() string {
return fmt.Sprintf("\n%s", strings.Join(arr, "\n"))
}

// FilterTypeString ...
func (f *CommitMessageFormat) FilterTypesString() string {
if len(f.typeSamples) == 0 {
return " []"
}

arr := make([]string, len(f.typeSamples))
for i, t := range f.typeSamples {
arr[i] = fmt.Sprintf(
"%s#%s- %s",
strings.Repeat(" ", 4), strings.Repeat(" ", 5),
t.typeName)
}
return fmt.Sprintf("\n%s", strings.Join(arr, "\n"))
}

// TitleMapsString ...
func (f *CommitMessageFormat) TitleMapsString() string {
if len(f.typeSamples) == 0 {
return " []"
}

arr := make([]string, len(f.typeSamples))
for i, t := range f.typeSamples {
arr[i] = fmt.Sprintf(
"%s#%s%s: %s",
strings.Repeat(" ", 4), strings.Repeat(" ", 3),
t.typeName, t.title)
}
return fmt.Sprintf("\n%s", strings.Join(arr, "\n"))
}

// Formats
var (
fmtTypeScopeSubject = &CommitMessageFormat{
display: "<type>(<scope>): <subject>",
preview: "feat(core): Add new feature",
pattern: `^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$`,
patternMaps: []string{"Type", "Scope", "Subject"},
typeSamples: []typeSample{
{"feat", "Features"}, {"fix", "Bug Fixes"},
{"perf", "Performance Improvements"}, {"refactor", "Code Refactoring"},},
}
fmtTypeSubject = &CommitMessageFormat{
display: "<type>: <subject>",
preview: "feat: Add new feature",
pattern: `^(\\w*)\\:\\s(.*)$`,
patternMaps: []string{"Type", "Subject"},
typeSamples: []typeSample{
{"feat", "Features"}, {"fix", "Bug Fixes"},
{"perf", "Performance Improvements"}, {"refactor", "Code Refactoring"},},
}
fmtGitBasic = &CommitMessageFormat{
display: "<<type> subject>",
preview: "Add new feature",
pattern: `^((\\w+)\\s.*)$`,
patternMaps: []string{"Subject", "Type"},
typeSamples: []typeSample{
{"feat", "Features"}, {"fix", "Bug Fixes"},
{"perf", "Performance Improvements"}, {"refactor", "Code Refactoring"},},
}
fmtSubject = &CommitMessageFormat{
display: "<subject>",
preview: "Add new feature (Not detect `type` field)",
pattern: `^(.*)$`,
patternMaps: []string{"Subject"},
typeSamples: []typeSample{},
}
fmtCommitEmoji = &CommitMessageFormat{
display: ":<type>: <subject>",
preview: ":sparkles: Add new feature (Commit message with emoji format)",
pattern: `^:(\\w*)\\:\\s(.*)$`,
patternMaps: []string{"Type", "Subject"},
typeSamples: []typeSample{
{"sparkles", "Features"}, {"bug", "Bug Fixes"},
{"zap", "Performance Improvements"}, {"recycle", "Code Refactoring"},},
}
formats = []Previewable{
fmtTypeScopeSubject,
fmtTypeSubject,
fmtGitBasic,
fmtSubject,
fmtCommitEmoji,
}
)

Expand Down
46 changes: 46 additions & 0 deletions cmd/git-chglog/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,49 @@ func TestCommitMessageFormatPatternMaps(t *testing.T) {

assert.Equal(" []", f.PatternMapString())
}

func TestCommitMessageFormatFilterTypes(t *testing.T) {
assert := assert.New(t)

f := &CommitMessageFormat{
typeSamples: []typeSample{
{"feat", "Features"}, {"fix", "Bug Fixes"},
{"perf", "Performance Improvements"}, {"refactor", "Code Refactoring"},
},
}

assert.Equal(`
# - feat
# - fix
# - perf
# - refactor`, f.FilterTypesString())

f = &CommitMessageFormat{
patternMaps: []string{},
}

assert.Equal(" []", f.FilterTypesString())
}

func TestCommitMessageFormatTitleMaps(t *testing.T) {
assert := assert.New(t)

f := &CommitMessageFormat{
typeSamples: []typeSample{
{"feat", "Features"}, {"fix", "Bug Fixes"},
{"perf", "Performance Improvements"}, {"refactor", "Code Refactoring"},
},
}

assert.Equal(`
# feat: Features
# fix: Bug Fixes
# perf: Performance Improvements
# refactor: Code Refactoring`, f.TitleMapsString())

f = &CommitMessageFormat{
patternMaps: []string{},
}

assert.Equal(" []", f.TitleMapsString())
}

0 comments on commit 5a69e9c

Please sign in to comment.