Skip to content

Commit a02f2d1

Browse files
committed
fix(defaultlinter): remove strict commit types
Removed the set of allowed commit types. This is not part of the conventional commits specifications. Any type is allowed now. Made improvements to how rules work to make it more extensible.
1 parent 8f50c90 commit a02f2d1

File tree

4 files changed

+49
-55
lines changed

4 files changed

+49
-55
lines changed

internal/commitlinter/defaultlinter/defaultlinter.go

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package defaultlinter
22

33
import (
44
"errors"
5-
"fmt"
65
"strings"
7-
"unicode"
8-
"unicode/utf8"
96

107
"github.com/go-git/go-git/v5/plumbing/object"
118

@@ -16,31 +13,22 @@ import (
1613
type Linter struct {
1714
// AllowInitialCommit will cause the linter to ignore initial commit if it's written correctly.
1815
AllowInitialCommit bool
19-
// AllowedTypes is a set of commit types that should be allowed. Defaults to conventional commits and angular types.
20-
AllowedTypes map[string]struct{}
16+
// Rules is a list of rules to enforce on commit messages.
17+
Rules []RuleFunc
2118
}
2219

23-
var (
24-
ErrUnconventionalType = errors.New("non-conforming commit type")
25-
ErrInvalidSubject = errors.New("invalid subject in commit message")
26-
ErrInvalidCharacter = errors.New("invalid character in commit message")
27-
)
28-
29-
var (
30-
conventionalCommitTypes = []string{"fix", "feat", "BREAKING CHANGE"}
31-
angularCommitTypes = []string{"docs", "ci", "chore", "style", "refactor", "improvement", "perf", "test"}
32-
)
33-
3420
// New returns a commit linter that adheres to Conventional Commits 1.0.0 and Angular project's recommended scopes.
3521
func New(options ...Option) *Linter {
3622
var linter Linter
23+
3724
for _, opt := range options {
3825
opt(&linter)
3926
}
4027

41-
if len(linter.AllowedTypes) == 0 {
42-
WithCommitTypes(conventionalCommitTypes...)(&linter)
43-
WithCommitTypes(angularCommitTypes...)(&linter)
28+
if linter.Rules == nil {
29+
linter.Rules = []RuleFunc{
30+
RuleSubjectNoLeadingUpperCase,
31+
}
4432
}
4533

4634
return &linter
@@ -72,32 +60,12 @@ func (l *Linter) Lint(commit *object.Commit) error {
7260
return nil
7361
}
7462

75-
// Commit type must a known one.
76-
if _, ok := l.AllowedTypes[msg.Type]; !ok {
77-
return commitlinter.LintError{
78-
Err: ErrUnconventionalType,
79-
Hash: commit.Hash,
80-
}
81-
}
82-
83-
// Subject must not start with an upper case letter.
84-
first, size := utf8.DecodeRuneInString(msg.Subject)
85-
if first == utf8.RuneError && size == 0 {
86-
return commitlinter.LintError{
87-
Err: fmt.Errorf("no subject detected: %w", ErrInvalidSubject),
88-
Hash: commit.Hash,
89-
}
90-
} else if first == utf8.RuneError && size == 1 {
91-
return commitlinter.LintError{
92-
Err: fmt.Errorf("bad subject: %w", ErrInvalidCharacter),
93-
Hash: commit.Hash,
94-
}
95-
}
96-
97-
if unicode.IsUpper(first) {
98-
return commitlinter.LintError{
99-
Err: fmt.Errorf("subject must not start with upper case %q: %w", msg.Subject, ErrInvalidCharacter),
100-
Hash: commit.Hash,
63+
for _, rule := range l.Rules {
64+
if err = rule(msg); err != nil {
65+
return commitlinter.LintError{
66+
Err: err,
67+
Hash: commit.Hash,
68+
}
10169
}
10270
}
10371

internal/commitlinter/defaultlinter/defaultlinter_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ func TestLinter_Lint(t *testing.T) {
102102
Rev: "HEAD",
103103
Linter: defaultlinter.New(),
104104
},
105-
wantErr: true,
106105
},
107106
{
108107
repoOps: []repobuilder.OperationFunc{
@@ -142,7 +141,7 @@ func TestLinter_Lint(t *testing.T) {
142141
},
143142
fields: fields{
144143
Rev: "HEAD",
145-
Linter: defaultlinter.New(),
144+
Linter: defaultlinter.New(defaultlinter.WithRule(defaultlinter.RuleSubjectNoLeadingUpperCase)),
146145
},
147146
wantErr: true,
148147
},

internal/commitlinter/defaultlinter/options.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,8 @@ func AllowInitialCommit() Option {
88
}
99
}
1010

11-
func WithCommitTypes(types ...string) Option {
11+
func WithRule(rules ...RuleFunc) Option {
1212
return func(l *Linter) {
13-
if l.AllowedTypes == nil {
14-
l.AllowedTypes = make(map[string]struct{})
15-
}
16-
17-
for _, typ := range types {
18-
l.AllowedTypes[typ] = struct{}{}
19-
}
13+
l.Rules = append(l.Rules, rules...)
2014
}
2115
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package defaultlinter
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"unicode"
7+
"unicode/utf8"
8+
9+
"github.com/somebadcode/commit-tool/internal/commitmsg"
10+
)
11+
12+
type RuleFunc func(message commitmsg.CommitMessage) error
13+
14+
var (
15+
ErrInvalidSubject = errors.New("invalid subject in commit message")
16+
ErrInvalidCharacter = errors.New("invalid character in commit message")
17+
)
18+
19+
func RuleSubjectNoLeadingUpperCase(msg commitmsg.CommitMessage) error {
20+
first, size := utf8.DecodeRuneInString(msg.Subject)
21+
if first == utf8.RuneError && size == 0 {
22+
return fmt.Errorf("no subject detected: %w", ErrInvalidSubject)
23+
} else if first == utf8.RuneError && size == 1 {
24+
return fmt.Errorf("bad subject: %w", ErrInvalidCharacter)
25+
26+
}
27+
28+
if unicode.IsUpper(first) {
29+
return fmt.Errorf("subject must not start with upper case %q: %w", msg.Subject, ErrInvalidCharacter)
30+
}
31+
32+
return nil
33+
}

0 commit comments

Comments
 (0)