Skip to content

Commit eae785d

Browse files
committed
feat: Add cleanup step before parsing
Similar to git itself, we now add a cleanup step before processing the commit message. Check `git-commit` man page for the `--cleanup` option. In contrast to `git` we do "strip" and "scissors" in one step. closes conventionalcommit#7
1 parent e9a606c commit eae785d

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

internal/cmd/lint.go

+57-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"path/filepath"
88
"strings"
9+
"unicode"
910

1011
cli "github.com/urfave/cli/v2"
1112

@@ -46,7 +47,12 @@ func runLint(confFilePath, fileInput string) (lintResult string, hasError bool,
4647
return "", false, err
4748
}
4849

49-
result, err := linter.ParseAndLint(commitMsg)
50+
cleanMsg, err := cleanupMsg(commitMsg)
51+
if err != nil {
52+
return "", false, err
53+
}
54+
55+
result, err := linter.ParseAndLint(cleanMsg)
5056
if err != nil {
5157
return "", false, err
5258
}
@@ -115,6 +121,56 @@ func getCommitMsg(fileInput string) (string, error) {
115121
return string(inBytes), nil
116122
}
117123

124+
func trimRightSpace(s string) string {
125+
return strings.TrimRightFunc(s, unicode.IsSpace)
126+
}
127+
128+
func cleanupMsg(dirtyMsg string) (string, error) {
129+
// commit msg cleanup in git is configurable: https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---cleanupltmodegt
130+
// For now we do a combination of the "scissors" behavior and the "strip" behavior
131+
// * remove the scissors line and everything below
132+
// * strip leading and trailing empty lines
133+
// * strip commentary (lines stating with commentChar '#')
134+
// * strip trailing whitespace
135+
// * collapse consecutive empty lines
136+
// TODO: check via "git config --get" if any of those two hardcoded constants was reconfigured
137+
// TODO: find out if commit messages on windows actually
138+
139+
gitCommentChar := "#"
140+
scissors := gitCommentChar + " ------------------------ >8 ------------------------"
141+
142+
cleanMsg := ""
143+
lastLine := ""
144+
for _, line := range strings.Split(dirtyMsg, "\n") {
145+
if line == scissors {
146+
// remove everything below scissors (including the scissors line)
147+
break
148+
}
149+
if strings.HasPrefix(line, gitCommentChar) {
150+
// strip commentary
151+
continue
152+
}
153+
line = trimRightSpace(line)
154+
// strip trailing whitespace
155+
if lastLine == "" && line == "" {
156+
// strip leading empty lines
157+
// collapse consecutive empty lines
158+
continue
159+
}
160+
if cleanMsg == "" {
161+
cleanMsg = line
162+
} else {
163+
cleanMsg += "\n" + line
164+
}
165+
lastLine = line
166+
}
167+
if lastLine == "" {
168+
//strip trailing empty line
169+
cleanMsg = strings.TrimSuffix(cleanMsg, "\n")
170+
}
171+
return cleanMsg, nil
172+
}
173+
118174
func readStdInPipe() (string, error) {
119175
stat, err := os.Stdin.Stat()
120176
if err != nil {

0 commit comments

Comments
 (0)