WIP: commit msg parser #39

Closed
周赵嘉程521432910016 wants to merge 24 commits from commit-parser into master
Showing only changes of commit d299849c78 - Show all commits

View File

@ -1,7 +1,9 @@
package main
import (
"fmt"
"log/slog"
"regexp"
"strings"
"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage"
@ -70,6 +72,17 @@ func parseConfFile(path string) (conf Conf, err error) {
return
}
func validateHw(hw string) error {
matched, err := regexp.MatchString(`^hw[0-9]+$`, hw)
if err != nil {
return fmt.Errorf("error compiling regex: %w", err)
}
if !matched {
return fmt.Errorf("error: hw does not match the required pattern")
}
return nil
}
func commitMsgToConf() (conf Conf, err error) {
r, err := git.PlainOpen(".")
if err != nil {
@ -87,19 +100,36 @@ func commitMsgToConf() (conf Conf, err error) {
slog.Debug("commit msg to conf", "msg", msg)
line := strings.Split(msg, "\n")[0]
words := strings.TrimSpace(line)
head := string(words[0])
words := strings.Fields(line)
file := "conf.toml"
head := words[0]
var hw string
if strings.HasSuffix(head, ":") || strings.HasSuffix(head, ".") {
head = head[:len(head)-1]
}
file := "conf.toml"
switch head {
case "feat", "fix", "refactor", "perf", "test", "build", "revert":
strings.Replace(file, "conf", "conf-cp", 1)
if len(words) < 2 {
return Conf{}, fmt.Errorf("error: hw not assigned")
}
hw = words[1]
if err = validateHw(hw); err != nil {
return
}
file = strings.Replace(file, "conf", "conf-"+hw+"-cq", 1)
case "joj", "grading":
strings.Replace(file, "conf", "conf-oj", 1)
if len(words) < 2 {
return Conf{}, fmt.Errorf("error: hw not assigned")
}
hw = words[1]
if err = validateHw(hw); err != nil {
return
}
file = strings.Replace(file, "conf", "conf-"+hw+"-oj", 1)
}
conf, err = parseConfFile(file)