Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#41) --body-maxlen #45

Merged
merged 1 commit into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Flags:
--subject-maxlen=2147483646 Max length for commit subject line (default: math.MaxInt32 - 1).
--subject-minlen=0 Min length for commit subject line (default: 0).
--body-regex=".*" Commit message body must conform to this regular expression (default: ".*").
--body-maxlen=2147483646 Max length for commit body (default: math.MaxInt32 - 1)
--since="1970-01-01" A date in "yyyy-MM-dd" format starting from which commits will be analyzed (default: "1970-01-01").
--msg-file="" Only analyze the commit message found in this file (default: "").
--max-parents=1 Max number of parents a commit can have in order to be analyzed (default: 1). Useful for excluding merge commits.
Expand Down
4 changes: 3 additions & 1 deletion cmd/go-gitlint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
subjectMaxLength = kingpin.Flag("subject-maxlen", "Max length for commit subject line (default: math.MaxInt32 - 1).").Default(strconv.Itoa(math.MaxInt32 - 1)).Int() //nolint[gochecknoglobals]
subjectMinLength = kingpin.Flag("subject-minlen", "Min length for commit subject line (default: 0).").Default("0").Int() //nolint[gochecknoglobals]
bodyRegex = kingpin.Flag("body-regex", `Commit message body must conform to this regular expression (default: ".*").`).Default(".*").String() //nolint[gochecknoglobals]
bodyMaxLength = kingpin.Flag("body-maxlen", `Max length for commit body (default: math.MaxInt32 - 1)`).Default(strconv.Itoa(math.MaxInt32 - 1)).Int() //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]
msgFile = kingpin.Flag("msg-file", `Only analyze the commit message found in this file (default: "").`).Default("").String() //nolint[gochecknoglobals]
maxParents = kingpin.Flag("max-parents", `Max number of parents a commit can have in order to be analyzed (default: 1). Useful for excluding merge commits.`).Default("1").Int() //nolint[gochecknoglobals]
Expand All @@ -49,9 +50,10 @@ func main() {
issues.Collected(
[]issues.Filter{
issues.OfSubjectRegex(*subjectRegex),
issues.OfBodyRegex(*bodyRegex),
issues.OfSubjectMaxLength(*subjectMaxLength),
issues.OfSubjectMinLength(*subjectMinLength),
issues.OfBodyRegex(*bodyRegex),
issues.OfBodyMaxLength(*bodyMaxLength),
},
try(
len(*msgFile) > 0,
Expand Down
15 changes: 15 additions & 0 deletions internal/issues/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,18 @@ func OfSubjectMinLength(min int) Filter {
return issue
}
}

// OfBodyMaxLength checks that a commit's body's length doesn't exceed this
// max number of characters.
func OfBodyMaxLength(max int) Filter {
return func(c *commits.Commit) Issue {
var issue Issue
if len(c.Body()) > max {
issue = Issue{
Desc: fmt.Sprintf("body length exceeds max [%d]", max),
Commit: *c,
}
}
return issue
}
}
21 changes: 21 additions & 0 deletions internal/issues/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package issues

import (
"math"
"testing"

"github.com/llorllale/go-gitlint/internal/commits"
Expand Down Expand Up @@ -108,3 +109,23 @@ func TestOfSubjectMinLengthNonMatch(t *testing.T) {
"filter.OfSubjectMinLength() must not match if the commit's subject is not too short",
)
}

func TestOfBodyMaxLengthMatch(t *testing.T) {
assert.NotZero(t,
OfBodyMaxLength(1)(
&commits.Commit{
Message: "subject\n\nclearly, this commit has a long body",
},
),
)
}

func TestOfBodyMaxLengthNonMatch(t *testing.T) {
assert.Zero(t,
OfBodyMaxLength(math.MaxInt32)(
&commits.Commit{
Message: "subject\n\nclearly, this commit cannot exceed this max",
},
),
)
}