feat: add option to filter commits in a case insensitive way

This commit is contained in:
Thierno IB. BARRY 2020-03-25 23:57:34 +01:00
parent a94e3f9a80
commit 72fb3eac14
9 changed files with 88 additions and 26 deletions

View file

@ -163,6 +163,7 @@ OPTIONS:
--silent disable stdout output --silent disable stdout output
--no-color disable color output [$NO_COLOR] --no-color disable color output [$NO_COLOR]
--no-emoji disable emoji output [$NO_EMOJI] --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 --tag-filter-pattern value, -p value regular expression of tag filter. Is specified, only matched tags will be picked
--help, -h show help --help, -h show help
--version, -v print the version --version, -v print the version
@ -548,4 +549,4 @@ See [CHANGELOG.md](./CHANGELOG.md)
[doc-commit]: https://godoc.org/github.com/git-chglog/git-chglog#Commit [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-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-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

View file

@ -19,6 +19,7 @@ type Options struct {
Processor Processor Processor Processor
NextTag string // Treat unreleased commits as specified tags (EXPERIMENTAL) NextTag string // Treat unreleased commits as specified tags (EXPERIMENTAL)
TagFilterPattern string // Filter tag by regexp 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 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`) 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`) CommitGroupBy string // Property name of `Commit` to be grouped into `CommitGroup` (e.g. `Type`)

View file

@ -259,6 +259,7 @@ func (config *Config) Convert(ctx *CLIContext) *chglog.Config {
Options: &chglog.Options{ Options: &chglog.Options{
NextTag: ctx.NextTag, NextTag: ctx.NextTag,
TagFilterPattern: ctx.TagFilterPattern, TagFilterPattern: ctx.TagFilterPattern,
NoCaseSensitive: ctx.NoCaseSensitive,
CommitFilters: opts.Commits.Filters, CommitFilters: opts.Commits.Filters,
CommitSortBy: opts.Commits.SortBy, CommitSortBy: opts.Commits.SortBy,
CommitGroupBy: opts.CommitGroups.GroupBy, CommitGroupBy: opts.CommitGroups.GroupBy,

View file

@ -14,6 +14,7 @@ type CLIContext struct {
Silent bool Silent bool
NoColor bool NoColor bool
NoEmoji bool NoEmoji bool
NoCaseSensitive bool
Query string Query string
NextTag string NextTag string
TagFilterPattern string TagFilterPattern string

View file

@ -114,10 +114,16 @@ func CreateApp(actionFunc cli.ActionFunc) *cli.App {
EnvVar: "NO_EMOJI", EnvVar: "NO_EMOJI",
}, },
// no-case
cli.BoolFlag{
Name: "no-case",
Usage: "disable case sensitive filters",
},
// tag-filter-pattern // tag-filter-pattern
cli.StringFlag{ cli.StringFlag{
Name: "tag-filter-pattern, p", Name: "tag-filter-pattern, p",
Usage: "Regular expression of tag filter. Is specified, only matched tags will be picked", Usage: "Regular expression of tag filter. Is specified, only matched tags will be picked",
}, },
// help & version // help & version
@ -162,16 +168,17 @@ func AppAction(c *cli.Context) error {
// chglog // chglog
chglogCLI := NewCLI( chglogCLI := NewCLI(
&CLIContext{ &CLIContext{
WorkingDir: wd, WorkingDir: wd,
Stdout: colorable.NewColorableStdout(), Stdout: colorable.NewColorableStdout(),
Stderr: colorable.NewColorableStderr(), Stderr: colorable.NewColorableStderr(),
ConfigPath: c.String("config"), ConfigPath: c.String("config"),
OutputPath: c.String("output"), OutputPath: c.String("output"),
Silent: c.Bool("silent"), Silent: c.Bool("silent"),
NoColor: c.Bool("no-color"), NoColor: c.Bool("no-color"),
NoEmoji: c.Bool("no-emoji"), NoEmoji: c.Bool("no-emoji"),
Query: c.Args().First(), NoCaseSensitive: c.Bool("no-case"),
NextTag: c.String("next-tag"), Query: c.Args().First(),
NextTag: c.String("next-tag"),
TagFilterPattern: c.String("tag-filter-pattern"), TagFilterPattern: c.String("tag-filter-pattern"),
}, },
fs, fs,

View file

@ -26,7 +26,7 @@ func TestCreateApp(t *testing.T) {
gAssert = assert gAssert = assert
app := CreateApp(mock_app_action) app := CreateApp(mock_app_action)
args := []string { args := []string{
"git-chglog", "git-chglog",
"--silent", "--silent",
"--no-color", "--no-color",

View file

@ -21,7 +21,7 @@ func (e *commitExtractor) Extract(commits []*Commit) ([]*CommitGroup, []*Commit,
mergeCommits := []*Commit{} mergeCommits := []*Commit{}
revertCommits := []*Commit{} revertCommits := []*Commit{}
filteredCommits := commitFilter(commits, e.opts.CommitFilters) filteredCommits := commitFilter(commits, e.opts.CommitFilters, e.opts.NoCaseSensitive)
for _, commit := range commits { for _, commit := range commits {
if commit.Merge != nil { if commit.Merge != nil {
@ -37,7 +37,7 @@ func (e *commitExtractor) Extract(commits []*Commit) ([]*CommitGroup, []*Commit,
for _, commit := range filteredCommits { for _, commit := range filteredCommits {
if commit.Merge == nil && commit.Revert == nil { if commit.Merge == nil && commit.Revert == nil {
e.processCommitGroups(&commitGroups, commit) e.processCommitGroups(&commitGroups, commit, e.opts.NoCaseSensitive)
} }
e.processNoteGroups(&noteGroups, commit) e.processNoteGroups(&noteGroups, commit)
@ -49,14 +49,23 @@ func (e *commitExtractor) Extract(commits []*Commit) ([]*CommitGroup, []*Commit,
return commitGroups, mergeCommits, revertCommits, noteGroups 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 var group *CommitGroup
// commit group // commit group
raw, ttl := e.commitGroupTitle(commit) raw, ttl := e.commitGroupTitle(commit)
for _, g := range *groups { 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 group = g
} }
} }

View file

@ -1,6 +1,10 @@
package chglog 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{} res := []*Commit{}
for _, commit := range commits { for _, commit := range commits {
@ -23,9 +27,17 @@ func commitFilter(commits []*Commit, filters map[string][]string) []*Commit {
break break
} }
if noCaseSensitive {
str = strings.ToLower(str)
}
exist := false exist := false
for _, val := range values { for _, val := range values {
if noCaseSensitive {
val = strings.ToLower(val)
}
if str == val { if str == val {
exist = true exist = true
} }

View file

@ -38,6 +38,11 @@ func TestCommitFilter(t *testing.T) {
Scope: "fuga", Scope: "fuga",
Subject: "4", Subject: "4",
}, },
&Commit{
Type: "Bar",
Scope: "hogera",
Subject: "5",
},
} }
assert.Equal( assert.Equal(
@ -46,8 +51,9 @@ func TestCommitFilter(t *testing.T) {
"2", "2",
"3", "3",
"4", "4",
"5",
}, },
pickCommitSubjects(commitFilter(fixtures, map[string][]string{})), pickCommitSubjects(commitFilter(fixtures, map[string][]string{}, false)),
) )
assert.Equal( assert.Equal(
@ -59,7 +65,20 @@ func TestCommitFilter(t *testing.T) {
}, },
pickCommitSubjects(commitFilter(fixtures, map[string][]string{ pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Type": {"foo", "bar"}, "Type": {"foo", "bar"},
})), }, false)),
)
assert.Equal(
[]string{
"1",
"2",
"3",
"4",
"5",
},
pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Type": {"foo", "bar"},
}, true)),
) )
assert.Equal( assert.Equal(
@ -69,7 +88,7 @@ func TestCommitFilter(t *testing.T) {
}, },
pickCommitSubjects(commitFilter(fixtures, map[string][]string{ pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Type": {"foo"}, "Type": {"foo"},
})), }, false)),
) )
assert.Equal( assert.Equal(
@ -79,7 +98,18 @@ func TestCommitFilter(t *testing.T) {
}, },
pickCommitSubjects(commitFilter(fixtures, map[string][]string{ pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Type": {"bar"}, "Type": {"bar"},
})), }, false)),
)
assert.Equal(
[]string{
"3",
"4",
"5",
},
pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Type": {"bar"},
}, true)),
) )
assert.Equal( assert.Equal(
@ -89,7 +119,7 @@ func TestCommitFilter(t *testing.T) {
}, },
pickCommitSubjects(commitFilter(fixtures, map[string][]string{ pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Scope": {"fuga"}, "Scope": {"fuga"},
})), }, false)),
) )
assert.Equal( assert.Equal(
@ -99,7 +129,7 @@ func TestCommitFilter(t *testing.T) {
pickCommitSubjects(commitFilter(fixtures, map[string][]string{ pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Type": {"bar"}, "Type": {"bar"},
"Scope": {"hoge"}, "Scope": {"hoge"},
})), }, false)),
) )
assert.Equal( assert.Equal(
@ -110,6 +140,6 @@ func TestCommitFilter(t *testing.T) {
pickCommitSubjects(commitFilter(fixtures, map[string][]string{ pickCommitSubjects(commitFilter(fixtures, map[string][]string{
"Type": {"foo"}, "Type": {"foo"},
"Scope": {"fuga", "hoge"}, "Scope": {"fuga", "hoge"},
})), }, false)),
) )
} }