CleanUpWhitespace removes extra whitespace for the multiline string passed as parameter. The intended usage is to clean up commit messages.
(message string)
| 23 | // CleanUpWhitespace removes extra whitespace for the multiline string passed as parameter. |
| 24 | // The intended usage is to clean up commit messages. |
| 25 | func CleanUpWhitespace(message string) string { |
| 26 | var messageStarted bool |
| 27 | var isLastLineEmpty bool |
| 28 | |
| 29 | const eol = '\n' |
| 30 | |
| 31 | builder := strings.Builder{} |
| 32 | |
| 33 | scan := bufio.NewScanner(strings.NewReader(message)) |
| 34 | for scan.Scan() { |
| 35 | line := strings.TrimRightFunc(scan.Text(), unicode.IsSpace) |
| 36 | |
| 37 | if len(line) == 0 { |
| 38 | if messageStarted { |
| 39 | isLastLineEmpty = true |
| 40 | } |
| 41 | continue |
| 42 | } |
| 43 | |
| 44 | if isLastLineEmpty { |
| 45 | builder.WriteByte(eol) |
| 46 | } |
| 47 | |
| 48 | builder.WriteString(line) |
| 49 | builder.WriteByte(eol) |
| 50 | isLastLineEmpty = false |
| 51 | messageStarted = true |
| 52 | } |
| 53 | |
| 54 | return builder.String() |
| 55 | } |
| 56 | |
| 57 | // ExtractSubject extracts subject from a commit message. The result should be like output of |
| 58 | // the one line commit summary, like "git log --oneline" or "git log --format=%s". |
searching dependent graphs…