Skip to content

Commit 44cabc3

Browse files
committed
fix: apply exclusions in hook mode
1 parent 80b8c2d commit 44cabc3

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

cmd/conch/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,7 @@ func main() {
207207
log.Fatalf("%v", parseErr)
208208
}
209209
origMsg = commit.StripComments(origMsg)
210-
211-
var c *commit.Commit
212-
c, parseErr = commit.ParseMessage(origMsg, cfg)
213-
if parseErr == nil {
214-
commits = []*commit.Commit{c}
215-
}
210+
commits, parseErr = commit.ParseMessage(origMsg, cfg)
216211
} else {
217212
commits, parseErr = commit.ParseRange(repoPath, flag.Arg(0), cfg)
218213
}

internal/commit/commit.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,22 @@ func ParseRange(repoPath string, rangeSpec string, cfg *config.Config) ([]*Commi
269269
return commits, nil
270270
}
271271

272-
func ParseMessage(msg string, cfg *config.Config) (*Commit, error) {
272+
// ParseMessage parses a single commit message and returns a slice of the
273+
// resulting Commit objects. (It may return an empty slice if the commit
274+
// message was excluded.)
275+
func ParseMessage(msg string, cfg *config.Config) ([]*Commit, error) {
276+
commits := make([]*Commit, 0, 1)
277+
if isExcluded(msg, cfg) {
278+
return commits, nil
279+
}
280+
273281
c := NewCommit("0")
274282
err := c.setMessage(msg)
275-
return c, err
283+
if err != nil {
284+
return commits, err
285+
}
286+
commits = append(commits, c)
287+
return commits, nil
276288
}
277289

278290
// ApplyPolicy checks if the commit is semantically valid

internal/commit/commit_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,57 @@ func TestParseRange(t *testing.T) {
537537
}
538538
}
539539

540+
func TestParseMessage(t *testing.T) {
541+
tests := []struct {
542+
description string
543+
msg string
544+
cfg *config.Config
545+
expectedCommits []*Commit
546+
expectedErr error
547+
}{
548+
{
549+
description: "it returns a valid commit",
550+
msg: "feat: a new thing",
551+
cfg: config.Default(),
552+
expectedCommits: []*Commit{
553+
{
554+
Id: "0",
555+
ShortId: "0",
556+
Type: "feat",
557+
Description: "a new thing",
558+
},
559+
},
560+
expectedErr: nil,
561+
},
562+
{
563+
description: "it excludes a commit based on the config",
564+
msg: "revert the thing",
565+
cfg: &config.Config{
566+
Exclude: config.Exclude{
567+
Prefixes: util.NewCaseInsensitiveSet([]string{"revert"}),
568+
},
569+
},
570+
expectedCommits: []*Commit{},
571+
expectedErr: nil,
572+
},
573+
{
574+
description: "it returns an error for an invalid commit message",
575+
msg: "revert the thing",
576+
cfg: config.Default(),
577+
expectedCommits: []*Commit{},
578+
expectedErr: ErrSummary("0"),
579+
},
580+
}
581+
582+
for _, test := range tests {
583+
t.Run(test.description, func(t *testing.T) {
584+
commits, err := ParseMessage(test.msg, test.cfg)
585+
assert.Equal(t, test.expectedCommits, commits)
586+
assert.Equal(t, test.expectedErr, err)
587+
})
588+
}
589+
}
590+
540591
func TestApplyPolicy(t *testing.T) {
541592
commit := &Commit{
542593
Id: "0",

0 commit comments

Comments
 (0)