Skip to content

Commit

Permalink
(#41) Add --body-maxlen (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
llorllale authored Mar 6, 2019
1 parent 8457a8e commit f332d57
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
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",
},
),
)
}

0 comments on commit f332d57

Please sign in to comment.