From 228ba9539d6795d021969f1706517e4f6dd996b2 Mon Sep 17 00:00:00 2001 From: George Aristy Date: Tue, 5 Mar 2019 19:57:44 -0500 Subject: [PATCH] (#41) --body-maxlen --- README.md | 1 + cmd/go-gitlint/main.go | 4 +++- internal/issues/filters.go | 15 +++++++++++++++ internal/issues/filters_test.go | 21 +++++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 927b6cd..b98adba 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/cmd/go-gitlint/main.go b/cmd/go-gitlint/main.go index cd1889b..e7c724f 100644 --- a/cmd/go-gitlint/main.go +++ b/cmd/go-gitlint/main.go @@ -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] @@ -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, diff --git a/internal/issues/filters.go b/internal/issues/filters.go index 9b7afc5..2d5c9bf 100644 --- a/internal/issues/filters.go +++ b/internal/issues/filters.go @@ -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 + } +} diff --git a/internal/issues/filters_test.go b/internal/issues/filters_test.go index 044aec2..b301486 100644 --- a/internal/issues/filters_test.go +++ b/internal/issues/filters_test.go @@ -15,6 +15,7 @@ package issues import ( + "math" "testing" "github.com/llorllale/go-gitlint/internal/commits" @@ -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", + }, + ), + ) +}