Skip to content

Commit

Permalink
(#40) --subject-minlen (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
llorllale committed Mar 5, 2019
1 parent 8ee76f1 commit 8457a8e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Flags:
--path="." Path to the git repo (default: ".").
--subject-regex=".*" Commit subject line must conform to this regular expression (default: ".*").
--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: ".*").
--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: "").
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 @@ -33,6 +33,7 @@ var (
path = kingpin.Flag("path", `Path to the git repo (default: ".").`).Default(".").String() //nolint[gochecknoglobals]
subjectRegex = kingpin.Flag("subject-regex", `Commit subject line must conform to this regular expression (default: ".*").`).Default(".*").String() //nolint[gochecknoglobals]
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]
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]
Expand All @@ -49,7 +50,8 @@ func main() {
[]issues.Filter{
issues.OfSubjectRegex(*subjectRegex),
issues.OfBodyRegex(*bodyRegex),
issues.OfSubjectLength(*subjectMaxLength),
issues.OfSubjectMaxLength(*subjectMaxLength),
issues.OfSubjectMinLength(*subjectMinLength),
},
try(
len(*msgFile) > 0,
Expand Down
21 changes: 18 additions & 3 deletions internal/issues/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,28 @@ func OfBodyRegex(regex string) Filter {
}
}

// OfSubjectLength tests a commit subject's length.
func OfSubjectLength(length int) Filter {
// OfSubjectMaxLength checks that a commit's subject does not exceed this length.
func OfSubjectMaxLength(length int) Filter {
return func(c *commits.Commit) Issue {
var issue Issue
if len(c.Subject()) > length {
issue = Issue{
Desc: fmt.Sprintf("subject exceeds length [%d]", length),
Desc: fmt.Sprintf("subject length exceeds max [%d]", length),
Commit: *c,
}
}
return issue
}
}

// OfSubjectMinLength checks that a commit's subject's length is at least
// of length min.
func OfSubjectMinLength(min int) Filter {
return func(c *commits.Commit) Issue {
var issue Issue
if len(c.Subject()) < min {
issue = Issue{
Desc: fmt.Sprintf("subject length less than min [%d]", min),
Commit: *c,
}
}
Expand Down
34 changes: 28 additions & 6 deletions internal/issues/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,46 @@ func TestOfBodyRegexNonMatch(t *testing.T) {
)
}

func TestOfSubjectLengthMatch(t *testing.T) {
func TestOfSubjectMaxLengthMatch(t *testing.T) {
assert.NotZero(t,
OfSubjectLength(5)(
OfSubjectMaxLength(5)(
&commits.Commit{
Message: "very very very VERY long subject\n\nand body",
},
),
"filter.OfSubjectLength() must match if the commit's subject is too long",
"filter.OfSubjectMaxLength() must match if the commit's subject is too long",
)
}

func TestOfSubjectLengthNonMatch(t *testing.T) {
func TestOfSubjectMaxLengthNonMatch(t *testing.T) {
assert.Zero(t,
OfSubjectLength(10)(
OfSubjectMaxLength(10)(
&commits.Commit{
Message: "short\n\nmessage",
},
),
"filter.OfSubjectLength() must not match if the commit's subject is not too long",
"filter.OfSubjectMaxLength() must not match if the commit's subject is not too long",
)
}

func TestOfSubjectMinLengthMatch(t *testing.T) {
assert.NotZero(t,
OfSubjectMinLength(10)(
&commits.Commit{
Message: "short\n\nand body",
},
),
"filter.OfSubjectMinLength() must match if the commit's subject is too short",
)
}

func TestOfSubjectMinLengthNonMatch(t *testing.T) {
assert.Zero(t,
OfSubjectMinLength(10)(
&commits.Commit{
Message: "not too short subject\n\nmessage",
},
),
"filter.OfSubjectMinLength() must not match if the commit's subject is not too short",
)
}

0 comments on commit 8457a8e

Please sign in to comment.