Skip to content

Commit

Permalink
(#34) Remove issues.Filter() (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
llorllale authored Mar 3, 2019
1 parent 48bb99a commit c4374b4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 45 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ $ ./gitlint --help
usage: gitlint [<flags>]
Flags:
--help Show context-sensitive help (also try --help-long and --help-man).
--subject-regex=SUBJECT-REGEX Filters commit subjects based on a regular expression.
--subject-len=SUBJECT-LEN Filters commit subjects based on length.
--body-regex=BODY-REGEX Filters commit message bodies based on a regular expression.
--path="." Path to the git repo ("." by default).
--since="1970-01-01" A date in "yyyy-MM-dd" format starting from which commits will be analyzed (default: "1970-01-01")
--help Show context-sensitive help (also try --help-long and --help-man).
--path="." Path to the git repo (default: ".").
--subject-regex=".*" Commit subject line must conform to this regular expression (default: ".*").
--subject-len=4294967295 Commit subject line cannot exceed this length (default: math.MaxUint32).
--body-regex=".*" Commit message body must conform to this regular expression (default: ".*").
--since="1970-01-01" A date in "yyyy-MM-dd" format starting from which commits will be analyzed (default: "1970-01-01")
```
Additionally, it will look for configurations in a file `.gitlint` in the current directory if it exists. This file's format is just the same command line flags but each on a separate line. *Flags passed through the command line take precedence.*

Expand Down
15 changes: 12 additions & 3 deletions cmd/go-gitlint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package main

import (
"math"
"os"
"strconv"

"github.com/llorllale/go-gitlint/internal/commits"
"github.com/llorllale/go-gitlint/internal/issues"
Expand All @@ -28,8 +30,11 @@ import (
// Figure out a way to remove these global variables. Whatever command line
// parser we choose should be able to auto-generate usage.
var (
path = kingpin.Flag("path", `Path to the git repo ("." by default).`).Default(".").String() //nolint[gochecknoglobals]
since = kingpin.Flag("since", `A date in "yyyy-MM-dd" format starting from which commits will be analyzed (default: "1970-01-01")`).Default("1970-01-01").String() //nolint[gochecknoglobals]
path = kingpin.Flag("path", `Path to the git repo (default: ".").`).Default(".").String() //nolint[gochecknoglobals]
subjectRegex = kingpin.Flag("subject-regex", `Commit subject line must conform to this regular expression (default: ".*").`).Default(".*").String() //nolint[gochecknoglobals]
subjectLength = kingpin.Flag("subject-len", "Commit subject line cannot exceed this length (default: math.MaxUint32).").Default(strconv.Itoa(math.MaxUint32)).Int() //nolint[gochecknoglobals]
bodyRegex = kingpin.Flag("body-regex", `Commit message body must conform to this regular expression (default: ".*").`).Default(".*").String() //nolint[gochecknoglobals]
since = kingpin.Flag("since", `A date in "yyyy-MM-dd" format starting from which commits will be analyzed (default: "1970-01-01")`).Default("1970-01-01").String() //nolint[gochecknoglobals]
)

func main() {
Expand All @@ -39,7 +44,11 @@ func main() {
issues.Printed(
os.Stdout, "\n",
issues.Collected(
issues.Filters(),
[]issues.Filter{
issues.OfSubjectRegex(*subjectRegex),
issues.OfBodyRegex(*bodyRegex),
issues.OfSubjectLength(*subjectLength),
},
commits.Since(
*since,
commits.In(
Expand Down
26 changes: 0 additions & 26 deletions internal/issues/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,13 @@ import (
"regexp"

"github.com/llorllale/go-gitlint/internal/commits"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

var (
subjectRegex = kingpin.Flag("subject-regex", "Filters commit subjects based on a regular expression.").String() //nolint[gochecknoglobals]
subjectLength = kingpin.Flag("subject-len", "Filters commit subjects based on length.").Int() //nolint[gochecknoglobals]
bodyRegex = kingpin.Flag("body-regex", "Filters commit message bodies based on a regular expression.").String() //nolint[gochecknoglobals]
)

// Filter identifies an issue with a commit.
// A filter returning a zero-valued Issue signals that it found no issue
// with the commit.
type Filter func(*commits.Commit) Issue

// Filters returns all filters configured by the user.
// @todo #31 Function issues.Filters() can be removed by providing
// default values for each filter that will effectively render them
// disabled. For example, OfSubjectRegex() can be effectively disabled
// by using ".*" as regex.
func Filters() []Filter {
filters := make([]Filter, 0)
if subjectRegex != nil {
filters = append(filters, OfSubjectRegex(*subjectRegex))
}
if bodyRegex != nil {
filters = append(filters, OfBodyRegex(*bodyRegex))
}
if subjectLength != nil && *subjectLength > 0 {
filters = append(filters, OfSubjectLength(*subjectLength))
}
return filters
}

// OfSubjectRegex tests a commit's subject with the regex.
func OfSubjectRegex(regex string) Filter {
return func(c *commits.Commit) Issue {
Expand Down
10 changes: 0 additions & 10 deletions internal/issues/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestFilters(t *testing.T) {
sr := "abc123"
br := "bodyRegex"
sl := 5
subjectRegex = &sr
bodyRegex = &br
subjectLength = &sl
assert.Len(t, Filters(), 3)
}

func TestOfSubjectRegexMatch(t *testing.T) {
assert.Zero(t,
OfSubjectRegex(`\(#\d+\) [\w ]{10,50}`)(
Expand Down

1 comment on commit c4374b4

@0pdd
Copy link

@0pdd 0pdd commented on c4374b4 Mar 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 31-07b6963e disappeared from internal/issues/filters.go, that's why I closed #34. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.