From d59de7bbd807b0dd3aae2fc310aa1ae028957652 Mon Sep 17 00:00:00 2001 From: Mack Wang Date: Sun, 15 Sep 2024 16:37:32 +0800 Subject: [PATCH 1/4] =?UTF-8?q?test:=20=E3=81=93=E3=82=8C=E3=81=AF?= =?UTF-8?q?=E9=81=95=E6=B3=95=E3=82=B3=E3=83=9F=E3=83=83=E3=83=88=E3=81=AE?= =?UTF-8?q?=E4=B8=80=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/healthcheck/commit.go | 63 +++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 36 deletions(-) diff --git a/pkg/healthcheck/commit.go b/pkg/healthcheck/commit.go index 8b1d579..375046e 100644 --- a/pkg/healthcheck/commit.go +++ b/pkg/healthcheck/commit.go @@ -7,7 +7,6 @@ import ( "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. @@ -28,23 +27,19 @@ func NonAsciiMsg(root string) error { slog.Error("getting reference", "err", err) return fmt.Errorf("error getting reference: %v", err) } - commits, err := repo.Log(&git.LogOptions{From: ref.Hash()}) + + commit, err := repo.CommitObject(ref.Hash()) if err != nil { - slog.Error("getting commits", "err", err) - return fmt.Errorf("error getting commits from reference %s: %v", ref.Hash(), err) + slog.Error("getting latest commit", "err", err) + return fmt.Errorf("error getting latest commit: %v", err) } - var msgs []string - err = commits.ForEach(func(c *object.Commit) error { - msgs = append(msgs, c.Message) + msg := commit.Message + if msg == "" { return nil - }) - if err != nil { - slog.Error("iterating commits", "err", err) - return fmt.Errorf("error iterating commits: %v", err) } - var nonAsciiMsgs []string + var isCommitLegal bool = true // List of prefixes to ignore in the commit message ignoredPrefixes := []string{ "Co-authored-by:", @@ -53,35 +48,31 @@ func NonAsciiMsg(root string) error { "Reviewed-on:", } - for _, msg := range msgs { - if 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 } - // 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 - } + // Check for non-ASCII characters in the rest of the lines + for _, c := range line { + if c > unicode.MaxASCII { + isCommitLegal = false + break } } } - if len(nonAsciiMsgs) > 0 { - return fmt.Errorf("Non-ASCII characters in commit messages:\n%s", strings.Join(nonAsciiMsgs, "\n")) + + if !isCommitLegal { + return fmt.Errorf("Non-ASCII characters in commit messages:\n%s", msg) } return nil } -- 2.30.2 From 7cbce3cb547dd781a5bb202d88ada244b8325f94 Mon Sep 17 00:00:00 2001 From: Mack Wang Date: Sun, 15 Sep 2024 16:37:32 +0800 Subject: [PATCH 2/4] =?UTF-8?q?test:=20=E3=81=93=E3=82=8C=E3=81=AF?= =?UTF-8?q?=E9=81=95=E6=B3=95=E3=82=B3=E3=83=9F=E3=83=83=E3=83=88=E3=81=AE?= =?UTF-8?q?=E4=B8=80=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/healthcheck/commit.go | 63 +++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 36 deletions(-) diff --git a/pkg/healthcheck/commit.go b/pkg/healthcheck/commit.go index 8b1d579..375046e 100644 --- a/pkg/healthcheck/commit.go +++ b/pkg/healthcheck/commit.go @@ -7,7 +7,6 @@ import ( "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. @@ -28,23 +27,19 @@ func NonAsciiMsg(root string) error { slog.Error("getting reference", "err", err) return fmt.Errorf("error getting reference: %v", err) } - commits, err := repo.Log(&git.LogOptions{From: ref.Hash()}) + + commit, err := repo.CommitObject(ref.Hash()) if err != nil { - slog.Error("getting commits", "err", err) - return fmt.Errorf("error getting commits from reference %s: %v", ref.Hash(), err) + slog.Error("getting latest commit", "err", err) + return fmt.Errorf("error getting latest commit: %v", err) } - var msgs []string - err = commits.ForEach(func(c *object.Commit) error { - msgs = append(msgs, c.Message) + msg := commit.Message + if msg == "" { return nil - }) - if err != nil { - slog.Error("iterating commits", "err", err) - return fmt.Errorf("error iterating commits: %v", err) } - var nonAsciiMsgs []string + var isCommitLegal bool = true // List of prefixes to ignore in the commit message ignoredPrefixes := []string{ "Co-authored-by:", @@ -53,35 +48,31 @@ func NonAsciiMsg(root string) error { "Reviewed-on:", } - for _, msg := range msgs { - if 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 } - // 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 - } + // Check for non-ASCII characters in the rest of the lines + for _, c := range line { + if c > unicode.MaxASCII { + isCommitLegal = false + break } } } - if len(nonAsciiMsgs) > 0 { - return fmt.Errorf("Non-ASCII characters in commit messages:\n%s", strings.Join(nonAsciiMsgs, "\n")) + + if !isCommitLegal { + return fmt.Errorf("Non-ASCII characters in commit messages:\n%s", msg) } return nil } -- 2.30.2 From a423f5b7dd042635739723187f7d9adef60e580c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=9F=B5=E6=99=A8520370910012?= Date: Fri, 20 Sep 2024 11:10:27 +0800 Subject: [PATCH 3/4] fix: bad commit msg MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previous commit msg was for testing purposes only. Sorry for the disturbance :( Signed-off-by: 王韵晨520370910012 --- pkg/healthcheck/commit.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/healthcheck/commit.go b/pkg/healthcheck/commit.go index 375046e..5d4d5f8 100644 --- a/pkg/healthcheck/commit.go +++ b/pkg/healthcheck/commit.go @@ -5,7 +5,6 @@ import ( "log/slog" "strings" "unicode" - "github.com/go-git/go-git/v5" ) -- 2.30.2 From d358f05ca401cd5a53a8e87a4f2e274054b88874 Mon Sep 17 00:00:00 2001 From: Mack Wang Date: Fri, 20 Sep 2024 11:58:17 +0800 Subject: [PATCH 4/4] fix: format document --- pkg/healthcheck/commit.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/healthcheck/commit.go b/pkg/healthcheck/commit.go index 5d4d5f8..375046e 100644 --- a/pkg/healthcheck/commit.go +++ b/pkg/healthcheck/commit.go @@ -5,6 +5,7 @@ import ( "log/slog" "strings" "unicode" + "github.com/go-git/go-git/v5" ) -- 2.30.2