84 lines
2.1 KiB
Go
84 lines
2.1 KiB
Go
package healthcheck
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"strings"
|
|
"unicode"
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
|
)
|
|
|
|
// NonAsciiMsg checks for non-ASCII characters in the commit message.
|
|
// It skips the non-ASCII characters check for lines starting with specific keywords like "Co-authored-by", "Reviewed-by", and "Co-committed-by".
|
|
func NonAsciiMsg(root string) error {
|
|
repo, err := git.PlainOpen(root)
|
|
if err != nil {
|
|
slog.Error("opening git repo", "err", err)
|
|
return fmt.Errorf("error opening git repo: %v", err)
|
|
}
|
|
|
|
ref, err := repo.Head()
|
|
if err != nil {
|
|
slog.Error("getting reference", "err", err)
|
|
return fmt.Errorf("error getting reference: %v", err)
|
|
}
|
|
commits, err := repo.Log(&git.LogOptions{From: ref.Hash()})
|
|
if err != nil {
|
|
slog.Error("getting commits", "err", err)
|
|
return fmt.Errorf("error getting commits from reference %s: %v", ref.Hash(), err)
|
|
}
|
|
|
|
var msgs []string
|
|
err = commits.ForEach(func(c *object.Commit) error {
|
|
msgs = append(msgs, c.Message)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
slog.Error("iterating commits", "err", err)
|
|
return fmt.Errorf("error iterating commits: %v", err)
|
|
}
|
|
|
|
var nonAsciiMsgs []string
|
|
// List of prefixes to ignore in the commit message
|
|
ignoredPrefixes := []string{
|
|
"Co-authored-by:",
|
|
"Reviewed-by:",
|
|
"Co-committed-by:",
|
|
"Reviewed-on:",
|
|
}
|
|
|
|
for _, msg := range msgs {
|
|
if msg == "" {
|
|
continue
|
|
}
|
|
// Split message by lines and ignore specific lines with prefixes
|
|
lines := strings.Split(msg, "\n")
|
|
for _, line := range lines {
|
|
trimmedLine := strings.TrimSpace(line)
|
|
ignore := false
|
|
for _, prefix := range ignoredPrefixes {
|
|
if strings.HasPrefix(trimmedLine, prefix) {
|
|
ignore = true
|
|
break
|
|
}
|
|
}
|
|
if ignore {
|
|
continue
|
|
}
|
|
// Check for non-ASCII characters in the rest of the lines
|
|
for _, c := range line {
|
|
if c > unicode.MaxASCII {
|
|
nonAsciiMsgs = append(nonAsciiMsgs, msg)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if len(nonAsciiMsgs) > 0 {
|
|
return fmt.Errorf("Non-ASCII characters in commit messages:\n%s", strings.Join(nonAsciiMsgs, "\n"))
|
|
}
|
|
return nil
|
|
}
|