From f31dd8f0ab0f172c89b1543eb3801e5917bdd18a Mon Sep 17 00:00:00 2001 From: zzjc1234 <2359047351@qq.com> Date: Thu, 12 Sep 2024 16:08:40 +0800 Subject: [PATCH 1/2] fix: ignore pull request nonascii --- pkg/healthcheck/commit.go | 46 +++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/pkg/healthcheck/commit.go b/pkg/healthcheck/commit.go index ada9e5a..36f2be6 100644 --- a/pkg/healthcheck/commit.go +++ b/pkg/healthcheck/commit.go @@ -10,17 +10,13 @@ import ( "github.com/go-git/go-git/v5/plumbing/object" ) -// nonAsciiMsg checks for non-ASCII characters in the commit message. -// If the message starts with "Merge pull request", it skips the non-ASCII characters check. -// Otherwise, it iterates over each character in the message and checks if it is a non-ASCII character. -// If a non-ASCII character is found, it returns an error indicating not to use non-ASCII characters in commit messages. -// Otherwise, it returns nil indicating that the commit message is valid. +// 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 { - // cmd := exec.Command("git", "log", "--encoding=UTF-8", "--format=%B") repo, err := git.PlainOpen(root) if err != nil { - slog.Error("openning git repo", "err", err) - return fmt.Errorf("error openning git repo: %v", err) + slog.Error("opening git repo", "err", err) + return fmt.Errorf("error opening git repo: %v", err) } ref, err := repo.Head() @@ -45,16 +41,38 @@ func NonAsciiMsg(root string) error { } 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 } - if strings.HasPrefix(msg, "Merge pull request") { - continue - } - for _, c := range msg { - if c > unicode.MaxASCII { - nonAsciiMsgs = append(nonAsciiMsgs, msg) + // 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 + } } } } -- 2.30.2 From 0bbb49f2d1d933ffcf96d02e64ab0e48406f2b43 Mon Sep 17 00:00:00 2001 From: zzjc1234 <2359047351@qq.com> Date: Thu, 12 Sep 2024 16:10:44 +0800 Subject: [PATCH 2/2] chore: comment of code --- pkg/healthcheck/commit.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/healthcheck/commit.go b/pkg/healthcheck/commit.go index 36f2be6..8b1d579 100644 --- a/pkg/healthcheck/commit.go +++ b/pkg/healthcheck/commit.go @@ -10,7 +10,11 @@ import ( "github.com/go-git/go-git/v5/plumbing/object" ) -// NonAsciiMsg checks for non-ASCII characters in the commit message. +// nonAsciiMsg checks for non-ASCII characters in the commit message. +// If the message starts with "Merge pull request", it skips the non-ASCII characters check. +// Otherwise, it iterates over each character in the message and checks if it is a non-ASCII character. +// If a non-ASCII character is found, it returns an error indicating not to use non-ASCII characters in commit messages. +// Otherwise, it returns nil indicating that the commit message is valid. // 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) -- 2.30.2