WIP: commit msg parser #39
|
@ -1,7 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage"
|
"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage"
|
||||||
|
@ -70,6 +72,17 @@ func parseConfFile(path string) (conf Conf, err error) {
|
||||||
return
|
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) {
|
func commitMsgToConf() (conf Conf, err error) {
|
||||||
r, err := git.PlainOpen(".")
|
r, err := git.PlainOpen(".")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -87,19 +100,36 @@ func commitMsgToConf() (conf Conf, err error) {
|
||||||
slog.Debug("commit msg to conf", "msg", msg)
|
slog.Debug("commit msg to conf", "msg", msg)
|
||||||
line := strings.Split(msg, "\n")[0]
|
line := strings.Split(msg, "\n")[0]
|
||||||
|
|
||||||
words := strings.TrimSpace(line)
|
words := strings.Fields(line)
|
||||||
head := string(words[0])
|
|
||||||
|
|
||||||
file := "conf.toml"
|
head := words[0]
|
||||||
|
var hw string
|
||||||
|
|
||||||
if strings.HasSuffix(head, ":") || strings.HasSuffix(head, ".") {
|
if strings.HasSuffix(head, ":") || strings.HasSuffix(head, ".") {
|
||||||
head = head[:len(head)-1]
|
head = head[:len(head)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file := "conf.toml"
|
||||||
|
|
||||||
switch head {
|
switch head {
|
||||||
case "feat", "fix", "refactor", "perf", "test", "build", "revert":
|
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":
|
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)
|
conf, err = parseConfFile(file)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user