Skip to content

Commit 1804526

Browse files
authored
(#24) Filter of subject length (#25)
1 parent 4be918f commit 1804526

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

cmd/go-gitlint/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ func main() {
4747
}
4848

4949
func configure() {
50-
var args []string
51-
if len(os.Args) > 1 {
52-
args = append(args, os.Args[1:]...)
53-
}
50+
args := os.Args[1:]
5451
const file = ".gitlint"
5552
if _, err := os.Stat(file); err == nil {
5653
config, err := kingpin.ExpandArgsFromFile(file)

internal/issues/filters.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ import (
2323
)
2424

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

3031
// Filter identifies an issue with a commit.
@@ -41,6 +42,9 @@ func Filters() []Filter {
4142
if bodyRegex != nil {
4243
filters = append(filters, OfBodyRegex(*bodyRegex))
4344
}
45+
if subjectLength != nil && *subjectLength > 0 {
46+
filters = append(filters, OfSubjectLength(*subjectLength))
47+
}
4448
return filters
4549
}
4650

@@ -79,3 +83,17 @@ func OfBodyRegex(regex string) Filter {
7983
return issue
8084
}
8185
}
86+
87+
// OfSubjectLength tests a commit subject's length.
88+
func OfSubjectLength(length int) Filter {
89+
return func(c *commits.Commit) Issue {
90+
var issue Issue
91+
if len(c.Subject()) > length {
92+
issue = Issue{
93+
Desc: fmt.Sprintf("subject is longer than %d", length),
94+
Commit: *c,
95+
}
96+
}
97+
return issue
98+
}
99+
}

internal/issues/filters_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import (
2424
func TestFilters(t *testing.T) {
2525
sr := "abc123"
2626
br := "bodyRegex"
27+
sl := 5
2728
subjectRegex = &sr
2829
bodyRegex = &br
29-
assert.Len(t, Filters(), 2)
30+
subjectLength = &sl
31+
assert.Len(t, Filters(), 3)
3032
}
3133

3234
func TestOfSubjectRegexMatch(t *testing.T) {
@@ -72,3 +74,25 @@ func TestOfBodyRegexNonMatch(t *testing.T) {
7274
"filter.OfBodyRegex() must not match if the commit's subject does not match the regex",
7375
)
7476
}
77+
78+
func TestOfSubjectLengthMatch(t *testing.T) {
79+
assert.NotZero(t,
80+
OfSubjectLength(5)(
81+
&commits.Commit{
82+
Message: "very very very VERY long subject\n\nand body",
83+
},
84+
),
85+
"filter.OfSubjectLength() must match if the commit's subject is too long",
86+
)
87+
}
88+
89+
func TestOfSubjectLengthNonMatch(t *testing.T) {
90+
assert.Zero(t,
91+
OfSubjectLength(10)(
92+
&commits.Commit{
93+
Message: "short\n\nmessage",
94+
},
95+
),
96+
"filter.OfSubjectLength() must not match if the commit's subject is not too long",
97+
)
98+
}

0 commit comments

Comments
 (0)