Skip to content

Commit

Permalink
fix: replaces \r\n (windows) and \r mac with \n
Browse files Browse the repository at this point in the history
Dependabot uses `\r\n` as newlines in commit messages. Now parsing
of Dependabot commit messages is possible too.
  • Loading branch information
zbindenren committed Feb 8, 2022
1 parent 6838c63 commit 92f8281
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package cc

import (
"bytes"
"strings"

"github.com/bbuck/go-lexer"
Expand Down Expand Up @@ -63,7 +64,7 @@ func (c Commit) BreakingMessage() string {

// Parse parses the conventional commit. If it fails, an error is returned.
func Parse(s string) (*Commit, error) {
l := lexer.New(strings.TrimSpace(s), typeState)
l := lexer.New(normalizeNewlines(strings.TrimSpace(s)), typeState)
l.ErrorHandler = func(string) {}

l.Start()
Expand Down Expand Up @@ -118,3 +119,13 @@ func (f Footers) breakingMessage() string {

return ""
}

func normalizeNewlines(s string) string {
d := []byte(s)
// replace CR LF \r\n (windows) with LF \n (unix)
d = bytes.ReplaceAll(d, []byte{13, 10}, []byte{10})
// replace CF \r (mac) with LF \n (unix)
d = bytes.ReplaceAll(d, []byte{13}, []byte{10})

return string(d)
}

0 comments on commit 92f8281

Please sign in to comment.