filecheck/nonasciiMsg #35

Merged
周赵嘉程521432910016 merged 3 commits from filecheck/nonasciiMsg into master 2024-09-13 12:11:29 +08:00

View File

@ -15,12 +15,12 @@ import (
// Otherwise, it iterates over each character in the message and checks if it is a non-ASCII character. // 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. // 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. // 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 { func NonAsciiMsg(root string) error {
// cmd := exec.Command("git", "log", "--encoding=UTF-8", "--format=%B")
repo, err := git.PlainOpen(root) repo, err := git.PlainOpen(root)
if err != nil { if err != nil {
slog.Error("openning git repo", "err", err) slog.Error("opening git repo", "err", err)
return fmt.Errorf("error openning git repo: %v", err) return fmt.Errorf("error opening git repo: %v", err)
} }
ref, err := repo.Head() ref, err := repo.Head()
@ -45,16 +45,38 @@ func NonAsciiMsg(root string) error {
} }
var nonAsciiMsgs []string 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 { for _, msg := range msgs {
if msg == "" { if msg == "" {
continue continue
} }
if strings.HasPrefix(msg, "Merge pull request") { // Split message by lines and ignore specific lines with prefixes
continue lines := strings.Split(msg, "\n")
} for _, line := range lines {
for _, c := range msg { trimmedLine := strings.TrimSpace(line)
if c > unicode.MaxASCII { ignore := false
nonAsciiMsgs = append(nonAsciiMsgs, msg) for _, prefix := range ignoredPrefixes {
if strings.HasPrefix(trimmedLine, prefix) {
mac-wang marked this conversation as resolved Outdated

I tried with following code and it fails to fix

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)
	}

	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 {
		if c.NumParents() > 1 {
			return nil
		}

		msgs = append(msgs, c.Message)
		return nil
	})

I tried with following code and it fails to fix ```golang 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) } 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 { if c.NumParents() > 1 { return nil } msgs = append(msgs, c.Message) return nil }) ```

won't be too bad, just 4-5 test for each message.

won't be too bad, just 4-5 test for each message.

we may have to fall back to the silly solution...

we may have to fall back to the silly solution...
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
}
} }
} }
} }