Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion internal/cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"strings"
"unicode"

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

Expand Down Expand Up @@ -46,7 +47,12 @@ func runLint(confFilePath, fileInput string) (lintResult string, hasError bool,
return "", false, err
}

result, err := linter.ParseAndLint(commitMsg)
cleanMsg, err := cleanupMsg(commitMsg)
if err != nil {
return "", false, err
}

result, err := linter.ParseAndLint(cleanMsg)
if err != nil {
return "", false, err
}
Expand Down Expand Up @@ -115,6 +121,56 @@ func getCommitMsg(fileInput string) (string, error) {
return string(inBytes), nil
}

func trimRightSpace(s string) string {
return strings.TrimRightFunc(s, unicode.IsSpace)
}

func cleanupMsg(dirtyMsg string) (string, error) {
// commit msg cleanup in git is configurable: https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---cleanupltmodegt
// For now we do a combination of the "scissors" behavior and the "strip" behavior
// * remove the scissors line and everything below
// * strip leading and trailing empty lines
// * strip commentary (lines stating with commentChar '#')
// * strip trailing whitespace
// * collapse consecutive empty lines
// TODO: check via "git config --get" if any of those two hardcoded constants was reconfigured
// TODO: find out if commit messages on windows actually

gitCommentChar := "#"
scissors := gitCommentChar + " ------------------------ >8 ------------------------"

cleanMsg := ""
lastLine := ""
for _, line := range strings.Split(dirtyMsg, "\n") {
if line == scissors {
// remove everything below scissors (including the scissors line)
break
}
if strings.HasPrefix(line, gitCommentChar) {
// strip commentary
continue
}
line = trimRightSpace(line)
// strip trailing whitespace
if lastLine == "" && line == "" {
// strip leading empty lines
// collapse consecutive empty lines
continue
}
if cleanMsg == "" {
cleanMsg = line
} else {
cleanMsg += "\n" + line
}
lastLine = line
}
if lastLine == "" {
//strip trailing empty line
cleanMsg = strings.TrimSuffix(cleanMsg, "\n")
}
return cleanMsg, nil
}

func readStdInPipe() (string, error) {
stat, err := os.Stdin.Stat()
if err != nil {
Expand Down