Skip to content

Commit c31a1be

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 c31a1be

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

internal/cmd/lint.go

+59-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,58 @@ 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+
fmt.Println("Dirty >>" + dirtyMsg + "<<")
143+
cleanMsg := ""
144+
lastLine := ""
145+
for _, line := range strings.Split(dirtyMsg, "\n") {
146+
if line == scissors {
147+
// remove everything below scissors (including the scissors line)
148+
break
149+
}
150+
if strings.HasPrefix(line, gitCommentChar) {
151+
// strip commentary
152+
continue
153+
}
154+
line = trimRightSpace(line)
155+
// strip trailing whitespace
156+
if lastLine == "" && line == "" {
157+
// strip leading empty lines
158+
// collapse consecutive empty lines
159+
continue
160+
}
161+
if cleanMsg == "" {
162+
cleanMsg = line
163+
} else {
164+
cleanMsg += "\n" + line
165+
}
166+
lastLine = line
167+
}
168+
if lastLine == "" {
169+
//strip trailing empty line
170+
cleanMsg = strings.TrimSuffix(cleanMsg, "\n")
171+
}
172+
fmt.Println("Clean >>" + cleanMsg + "<<")
173+
return cleanMsg, nil
174+
}
175+
118176
func readStdInPipe() (string, error) {
119177
stat, err := os.Stdin.Stat()
120178
if err != nil {

0 commit comments

Comments
 (0)