From 72fb3eac14ad73a2a9ac82a679075bc9bfc72ea9 Mon Sep 17 00:00:00 2001 From: "Thierno IB. BARRY" Date: Wed, 25 Mar 2020 23:57:34 +0100 Subject: [PATCH 1/2] feat: add option to filter commits in a case insensitive way --- README.md | 3 ++- chglog.go | 1 + cmd/git-chglog/config.go | 1 + cmd/git-chglog/context.go | 1 + cmd/git-chglog/main.go | 31 ++++++++++++++++---------- cmd/git-chglog/main_test.go | 2 +- commit_extractor.go | 17 ++++++++++---- commit_filter.go | 14 +++++++++++- commit_filter_test.go | 44 +++++++++++++++++++++++++++++++------ 9 files changed, 88 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 091c4030..94961589 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 \ No newline at end of file +[doc-render-data]: https://godoc.org/github.com/git-chglog/git-chglog#RenderData diff --git a/chglog.go b/chglog.go index 2b0ac836..1e58c587 100644 --- a/chglog.go +++ b/chglog.go @@ -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`) diff --git a/cmd/git-chglog/config.go b/cmd/git-chglog/config.go index 6f13b723..4f6cc89e 100644 --- a/cmd/git-chglog/config.go +++ b/cmd/git-chglog/config.go @@ -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, diff --git a/cmd/git-chglog/context.go b/cmd/git-chglog/context.go index 79b497e3..a33cc6d9 100644 --- a/cmd/git-chglog/context.go +++ b/cmd/git-chglog/context.go @@ -14,6 +14,7 @@ type CLIContext struct { Silent bool NoColor bool NoEmoji bool + NoCaseSensitive bool Query string NextTag string TagFilterPattern string diff --git a/cmd/git-chglog/main.go b/cmd/git-chglog/main.go index d4582ba8..08a09f70 100644 --- a/cmd/git-chglog/main.go +++ b/cmd/git-chglog/main.go @@ -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 @@ -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, diff --git a/cmd/git-chglog/main_test.go b/cmd/git-chglog/main_test.go index a07284f0..48bc1178 100644 --- a/cmd/git-chglog/main_test.go +++ b/cmd/git-chglog/main_test.go @@ -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", diff --git a/commit_extractor.go b/commit_extractor.go index b0cc6bcd..610897b2 100644 --- a/commit_extractor.go +++ b/commit_extractor.go @@ -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 { @@ -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(¬eGroups, commit) @@ -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 } } diff --git a/commit_filter.go b/commit_filter.go index 00c14439..bd7ee797 100644 --- a/commit_filter.go +++ b/commit_filter.go @@ -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 { @@ -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 } diff --git a/commit_filter_test.go b/commit_filter_test.go index 1c3af8a4..ec84ef10 100644 --- a/commit_filter_test.go +++ b/commit_filter_test.go @@ -38,6 +38,11 @@ func TestCommitFilter(t *testing.T) { Scope: "fuga", Subject: "4", }, + &Commit{ + Type: "Bar", + Scope: "hogera", + Subject: "5", + }, } assert.Equal( @@ -46,8 +51,9 @@ func TestCommitFilter(t *testing.T) { "2", "3", "4", + "5", }, - pickCommitSubjects(commitFilter(fixtures, map[string][]string{})), + pickCommitSubjects(commitFilter(fixtures, map[string][]string{}, false)), ) assert.Equal( @@ -59,7 +65,20 @@ func TestCommitFilter(t *testing.T) { }, pickCommitSubjects(commitFilter(fixtures, map[string][]string{ "Type": {"foo", "bar"}, - })), + }, false)), + ) + + assert.Equal( + []string{ + "1", + "2", + "3", + "4", + "5", + }, + pickCommitSubjects(commitFilter(fixtures, map[string][]string{ + "Type": {"foo", "bar"}, + }, true)), ) assert.Equal( @@ -69,7 +88,7 @@ func TestCommitFilter(t *testing.T) { }, pickCommitSubjects(commitFilter(fixtures, map[string][]string{ "Type": {"foo"}, - })), + }, false)), ) assert.Equal( @@ -79,7 +98,18 @@ func TestCommitFilter(t *testing.T) { }, 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( @@ -89,7 +119,7 @@ func TestCommitFilter(t *testing.T) { }, pickCommitSubjects(commitFilter(fixtures, map[string][]string{ "Scope": {"fuga"}, - })), + }, false)), ) assert.Equal( @@ -99,7 +129,7 @@ func TestCommitFilter(t *testing.T) { pickCommitSubjects(commitFilter(fixtures, map[string][]string{ "Type": {"bar"}, "Scope": {"hoge"}, - })), + }, false)), ) assert.Equal( @@ -110,6 +140,6 @@ func TestCommitFilter(t *testing.T) { pickCommitSubjects(commitFilter(fixtures, map[string][]string{ "Type": {"foo"}, "Scope": {"fuga", "hoge"}, - })), + }, false)), ) } From 0a4450abc1f453e685bc76de720f5c669a4f5ee7 Mon Sep 17 00:00:00 2001 From: "Thierno IB. BARRY" Date: Fri, 10 Apr 2020 21:33:32 +0200 Subject: [PATCH 2/2] apply go fmt --- cmd/git-chglog/main.go | 4 ++-- cmd/git-chglog/variables.go | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/git-chglog/main.go b/cmd/git-chglog/main.go index 08a09f70..32a1c954 100644 --- a/cmd/git-chglog/main.go +++ b/cmd/git-chglog/main.go @@ -116,8 +116,8 @@ func CreateApp(actionFunc cli.ActionFunc) *cli.App { // no-case cli.BoolFlag{ - Name: "no-case", - Usage: "disable case sensitive filters", + Name: "no-case", + Usage: "disable case sensitive filters", }, // tag-filter-pattern diff --git a/cmd/git-chglog/variables.go b/cmd/git-chglog/variables.go index b5b6c491..60a7cbc9 100644 --- a/cmd/git-chglog/variables.go +++ b/cmd/git-chglog/variables.go @@ -34,7 +34,7 @@ var ( type typeSample struct { typeName string - title string + title string } // CommitMessageFormat ... @@ -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: ": ", @@ -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: "< subject>", @@ -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: "", @@ -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,