Skip to content

Commit

Permalink
Merge pull request #65 from barryib/case-sensitive-option
Browse files Browse the repository at this point in the history
feat: add option to filter commits in a case insensitive way
  • Loading branch information
wadackel committed Apr 14, 2020
2 parents a94e3f9 + 0a4450a commit db79696
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 31 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ OPTIONS:
--silent disable stdout output
--no-color disable color output [$NO_COLOR]
--no-emoji disable emoji output [$NO_EMOJI]
--no-case disable case sensitive filters
--tag-filter-pattern value, -p value regular expression of tag filter. Is specified, only matched tags will be picked
--help, -h show help
--version, -v print the version
Expand Down Expand Up @@ -548,4 +549,4 @@ See [CHANGELOG.md](./CHANGELOG.md)
[doc-commit]: https://godoc.org/github.com/git-chglog/git-chglog#Commit
[doc-commit-group]: https://godoc.org/github.com/git-chglog/git-chglog#Commit
[doc-ref]: https://godoc.org/github.com/git-chglog/git-chglog#Ref
[doc-render-data]: https://godoc.org/github.com/git-chglog/git-chglog#RenderData
[doc-render-data]: https://godoc.org/github.com/git-chglog/git-chglog#RenderData
1 change: 1 addition & 0 deletions chglog.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Options struct {
Processor Processor
NextTag string // Treat unreleased commits as specified tags (EXPERIMENTAL)
TagFilterPattern string // Filter tag by regexp
NoCaseSensitive bool // Filter commits in a case insensitive way
CommitFilters map[string][]string // Filter by using `Commit` properties and values. Filtering is not done by specifying an empty value
CommitSortBy string // Property name to use for sorting `Commit` (e.g. `Scope`)
CommitGroupBy string // Property name of `Commit` to be grouped into `CommitGroup` (e.g. `Type`)
Expand Down
1 change: 1 addition & 0 deletions cmd/git-chglog/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ func (config *Config) Convert(ctx *CLIContext) *chglog.Config {
Options: &chglog.Options{
NextTag: ctx.NextTag,
TagFilterPattern: ctx.TagFilterPattern,
NoCaseSensitive: ctx.NoCaseSensitive,
CommitFilters: opts.Commits.Filters,
CommitSortBy: opts.Commits.SortBy,
CommitGroupBy: opts.CommitGroups.GroupBy,
Expand Down
1 change: 1 addition & 0 deletions cmd/git-chglog/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type CLIContext struct {
Silent bool
NoColor bool
NoEmoji bool
NoCaseSensitive bool
Query string
NextTag string
TagFilterPattern string
Expand Down
31 changes: 19 additions & 12 deletions cmd/git-chglog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,16 @@ func CreateApp(actionFunc cli.ActionFunc) *cli.App {
EnvVar: "NO_EMOJI",
},

// no-case
cli.BoolFlag{
Name: "no-case",
Usage: "disable case sensitive filters",
},

// tag-filter-pattern
cli.StringFlag{
Name: "tag-filter-pattern, p",
Usage: "Regular expression of tag filter. Is specified, only matched tags will be picked",
Name: "tag-filter-pattern, p",
Usage: "Regular expression of tag filter. Is specified, only matched tags will be picked",
},

// help & version
Expand Down Expand Up @@ -162,16 +168,17 @@ func AppAction(c *cli.Context) error {
// chglog
chglogCLI := NewCLI(
&CLIContext{
WorkingDir: wd,
Stdout: colorable.NewColorableStdout(),
Stderr: colorable.NewColorableStderr(),
ConfigPath: c.String("config"),
OutputPath: c.String("output"),
Silent: c.Bool("silent"),
NoColor: c.Bool("no-color"),
NoEmoji: c.Bool("no-emoji"),
Query: c.Args().First(),
NextTag: c.String("next-tag"),
WorkingDir: wd,
Stdout: colorable.NewColorableStdout(),
Stderr: colorable.NewColorableStderr(),
ConfigPath: c.String("config"),
OutputPath: c.String("output"),
Silent: c.Bool("silent"),
NoColor: c.Bool("no-color"),
NoEmoji: c.Bool("no-emoji"),
NoCaseSensitive: c.Bool("no-case"),
Query: c.Args().First(),
NextTag: c.String("next-tag"),
TagFilterPattern: c.String("tag-filter-pattern"),
},
fs,
Expand Down
2 changes: 1 addition & 1 deletion cmd/git-chglog/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestCreateApp(t *testing.T) {
gAssert = assert

app := CreateApp(mock_app_action)
args := []string {
args := []string{
"git-chglog",
"--silent",
"--no-color",
Expand Down
10 changes: 5 additions & 5 deletions cmd/git-chglog/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (

type typeSample struct {
typeName string
title string
title string
}

// CommitMessageFormat ...
Expand Down Expand Up @@ -115,7 +115,7 @@ var (
patternMaps: []string{"Type", "Scope", "Subject"},
typeSamples: []typeSample{
{"feat", "Features"}, {"fix", "Bug Fixes"},
{"perf", "Performance Improvements"}, {"refactor", "Code Refactoring"},},
{"perf", "Performance Improvements"}, {"refactor", "Code Refactoring"}},
}
fmtTypeSubject = &CommitMessageFormat{
display: "<type>: <subject>",
Expand All @@ -124,7 +124,7 @@ var (
patternMaps: []string{"Type", "Subject"},
typeSamples: []typeSample{
{"feat", "Features"}, {"fix", "Bug Fixes"},
{"perf", "Performance Improvements"}, {"refactor", "Code Refactoring"},},
{"perf", "Performance Improvements"}, {"refactor", "Code Refactoring"}},
}
fmtGitBasic = &CommitMessageFormat{
display: "<<type> subject>",
Expand All @@ -133,7 +133,7 @@ var (
patternMaps: []string{"Subject", "Type"},
typeSamples: []typeSample{
{"feat", "Features"}, {"fix", "Bug Fixes"},
{"perf", "Performance Improvements"}, {"refactor", "Code Refactoring"},},
{"perf", "Performance Improvements"}, {"refactor", "Code Refactoring"}},
}
fmtSubject = &CommitMessageFormat{
display: "<subject>",
Expand All @@ -149,7 +149,7 @@ var (
patternMaps: []string{"Type", "Subject"},
typeSamples: []typeSample{
{"sparkles", "Features"}, {"bug", "Bug Fixes"},
{"zap", "Performance Improvements"}, {"recycle", "Code Refactoring"},},
{"zap", "Performance Improvements"}, {"recycle", "Code Refactoring"}},
}
formats = []Previewable{
fmtTypeScopeSubject,
Expand Down
17 changes: 13 additions & 4 deletions commit_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (e *commitExtractor) Extract(commits []*Commit) ([]*CommitGroup, []*Commit,
mergeCommits := []*Commit{}
revertCommits := []*Commit{}

filteredCommits := commitFilter(commits, e.opts.CommitFilters)
filteredCommits := commitFilter(commits, e.opts.CommitFilters, e.opts.NoCaseSensitive)

for _, commit := range commits {
if commit.Merge != nil {
Expand All @@ -37,7 +37,7 @@ func (e *commitExtractor) Extract(commits []*Commit) ([]*CommitGroup, []*Commit,

for _, commit := range filteredCommits {
if commit.Merge == nil && commit.Revert == nil {
e.processCommitGroups(&commitGroups, commit)
e.processCommitGroups(&commitGroups, commit, e.opts.NoCaseSensitive)
}

e.processNoteGroups(&noteGroups, commit)
Expand All @@ -49,14 +49,23 @@ func (e *commitExtractor) Extract(commits []*Commit) ([]*CommitGroup, []*Commit,
return commitGroups, mergeCommits, revertCommits, noteGroups
}

func (e *commitExtractor) processCommitGroups(groups *[]*CommitGroup, commit *Commit) {
func (e *commitExtractor) processCommitGroups(groups *[]*CommitGroup, commit *Commit, noCaseSensitive bool) {
var group *CommitGroup

// commit group
raw, ttl := e.commitGroupTitle(commit)

for _, g := range *groups {
if g.RawTitle == raw {
rawTitleTmp := g.RawTitle
if noCaseSensitive {
rawTitleTmp = strings.ToLower(g.RawTitle)
}

rawTmp := raw
if noCaseSensitive {
rawTmp = strings.ToLower(raw)
}
if rawTitleTmp == rawTmp {
group = g
}
}
Expand Down
14 changes: 13 additions & 1 deletion commit_filter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package chglog

func commitFilter(commits []*Commit, filters map[string][]string) []*Commit {
import (
"strings"
)

func commitFilter(commits []*Commit, filters map[string][]string, noCaseSensitive bool) []*Commit {
res := []*Commit{}

for _, commit := range commits {
Expand All @@ -23,9 +27,17 @@ func commitFilter(commits []*Commit, filters map[string][]string) []*Commit {
break
}

if noCaseSensitive {
str = strings.ToLower(str)
}

exist := false

for _, val := range values {
if noCaseSensitive {
val = strings.ToLower(val)
}

if str == val {
exist = true
}
Expand Down
44 changes: 37 additions & 7 deletions commit_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,34 @@ func TestCommitFilter(t *testing.T) {
Scope: "fuga",
Subject: "4",
},
&Commit{
Type: "Bar",
Scope: "hogera",
Subject: "5",
},
}

assert.Equal(
[]string{
"1",
"2",
"3",
"4",
"5",
},
pickCommitSubjects(commitFilter(fixtures, map[string][]string{}, false)),
)

assert.Equal(
[]string{
"1",
"2",
"3",
"4",
},
pickCommitSubjects(commitFilter(fixtures, map[string][]string{})),
pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Type": {"foo", "bar"},
}, false)),
)

assert.Equal(
Expand All @@ -56,10 +74,11 @@ func TestCommitFilter(t *testing.T) {
"2",
"3",
"4",
"5",
},
pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Type": {"foo", "bar"},
})),
}, true)),
)

assert.Equal(
Expand All @@ -69,17 +88,28 @@ func TestCommitFilter(t *testing.T) {
},
pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Type": {"foo"},
})),
}, false)),
)

assert.Equal(
[]string{
"3",
"4",
},
pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Type": {"bar"},
}, false)),
)

assert.Equal(
[]string{
"3",
"4",
"5",
},
pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Type": {"bar"},
})),
}, true)),
)

assert.Equal(
Expand All @@ -89,7 +119,7 @@ func TestCommitFilter(t *testing.T) {
},
pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Scope": {"fuga"},
})),
}, false)),
)

assert.Equal(
Expand All @@ -99,7 +129,7 @@ func TestCommitFilter(t *testing.T) {
pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Type": {"bar"},
"Scope": {"hoge"},
})),
}, false)),
)

assert.Equal(
Expand All @@ -110,6 +140,6 @@ func TestCommitFilter(t *testing.T) {
pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Type": {"foo"},
"Scope": {"fuga", "hoge"},
})),
}, false)),
)
}

0 comments on commit db79696

Please sign in to comment.