Skip to content

Commit f332d57

Browse files
authored
(#41) Add --body-maxlen (#45)
1 parent 8457a8e commit f332d57

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Flags:
2424
--subject-maxlen=2147483646 Max length for commit subject line (default: math.MaxInt32 - 1).
2525
--subject-minlen=0 Min length for commit subject line (default: 0).
2626
--body-regex=".*" Commit message body must conform to this regular expression (default: ".*").
27+
--body-maxlen=2147483646 Max length for commit body (default: math.MaxInt32 - 1)
2728
--since="1970-01-01" A date in "yyyy-MM-dd" format starting from which commits will be analyzed (default: "1970-01-01").
2829
--msg-file="" Only analyze the commit message found in this file (default: "").
2930
--max-parents=1 Max number of parents a commit can have in order to be analyzed (default: 1). Useful for excluding merge commits.

cmd/go-gitlint/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var (
3535
subjectMaxLength = kingpin.Flag("subject-maxlen", "Max length for commit subject line (default: math.MaxInt32 - 1).").Default(strconv.Itoa(math.MaxInt32 - 1)).Int() //nolint[gochecknoglobals]
3636
subjectMinLength = kingpin.Flag("subject-minlen", "Min length for commit subject line (default: 0).").Default("0").Int() //nolint[gochecknoglobals]
3737
bodyRegex = kingpin.Flag("body-regex", `Commit message body must conform to this regular expression (default: ".*").`).Default(".*").String() //nolint[gochecknoglobals]
38+
bodyMaxLength = kingpin.Flag("body-maxlen", `Max length for commit body (default: math.MaxInt32 - 1)`).Default(strconv.Itoa(math.MaxInt32 - 1)).Int() //nolint[gochecknoglobals]
3839
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]
3940
msgFile = kingpin.Flag("msg-file", `Only analyze the commit message found in this file (default: "").`).Default("").String() //nolint[gochecknoglobals]
4041
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() {
4950
issues.Collected(
5051
[]issues.Filter{
5152
issues.OfSubjectRegex(*subjectRegex),
52-
issues.OfBodyRegex(*bodyRegex),
5353
issues.OfSubjectMaxLength(*subjectMaxLength),
5454
issues.OfSubjectMinLength(*subjectMinLength),
55+
issues.OfBodyRegex(*bodyRegex),
56+
issues.OfBodyMaxLength(*bodyMaxLength),
5557
},
5658
try(
5759
len(*msgFile) > 0,

internal/issues/filters.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,18 @@ func OfSubjectMinLength(min int) Filter {
9090
return issue
9191
}
9292
}
93+
94+
// OfBodyMaxLength checks that a commit's body's length doesn't exceed this
95+
// max number of characters.
96+
func OfBodyMaxLength(max int) Filter {
97+
return func(c *commits.Commit) Issue {
98+
var issue Issue
99+
if len(c.Body()) > max {
100+
issue = Issue{
101+
Desc: fmt.Sprintf("body length exceeds max [%d]", max),
102+
Commit: *c,
103+
}
104+
}
105+
return issue
106+
}
107+
}

internal/issues/filters_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package issues
1616

1717
import (
18+
"math"
1819
"testing"
1920

2021
"github.com/llorllale/go-gitlint/internal/commits"
@@ -108,3 +109,23 @@ func TestOfSubjectMinLengthNonMatch(t *testing.T) {
108109
"filter.OfSubjectMinLength() must not match if the commit's subject is not too short",
109110
)
110111
}
112+
113+
func TestOfBodyMaxLengthMatch(t *testing.T) {
114+
assert.NotZero(t,
115+
OfBodyMaxLength(1)(
116+
&commits.Commit{
117+
Message: "subject\n\nclearly, this commit has a long body",
118+
},
119+
),
120+
)
121+
}
122+
123+
func TestOfBodyMaxLengthNonMatch(t *testing.T) {
124+
assert.Zero(t,
125+
OfBodyMaxLength(math.MaxInt32)(
126+
&commits.Commit{
127+
Message: "subject\n\nclearly, this commit cannot exceed this max",
128+
},
129+
),
130+
)
131+
}

0 commit comments

Comments
 (0)