|
6 | 6 | "os"
|
7 | 7 | "path/filepath"
|
8 | 8 | "strings"
|
| 9 | + "unicode" |
9 | 10 |
|
10 | 11 | cli "github.com/urfave/cli/v2"
|
11 | 12 |
|
@@ -46,7 +47,12 @@ func runLint(confFilePath, fileInput string) (lintResult string, hasError bool,
|
46 | 47 | return "", false, err
|
47 | 48 | }
|
48 | 49 |
|
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) |
50 | 56 | if err != nil {
|
51 | 57 | return "", false, err
|
52 | 58 | }
|
@@ -115,6 +121,58 @@ func getCommitMsg(fileInput string) (string, error) {
|
115 | 121 | return string(inBytes), nil
|
116 | 122 | }
|
117 | 123 |
|
| 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 | + |
118 | 176 | func readStdInPipe() (string, error) {
|
119 | 177 | stat, err := os.Stdin.Stat()
|
120 | 178 | if err != nil {
|
|
0 commit comments