package convention import ( "errors" "regexp" "strings" "github.com/haunt98/changeloguru/pkg/git" ) // https://www.conventionalcommits.org/en/v1.0.0/ // [optional scope]: // [optional body] // [optional footer(s)] var ( headerRegex = regexp.MustCompile(`(?P[a-zA-Z]+)(?P\([a-zA-Z]+\))?(?P!)?:\s(?P.+)`) ) type Commit struct { RawHeader string Type string } func NewCommit(c git.Commit) (result Commit, err error) { message := strings.TrimSpace(c.Message) messages := strings.Split(message, "\n") if len(messages) == 0 { err = errors.New("empty commit") return } header := strings.TrimSpace(messages[0]) if err = parseHeader(header, &result); err != nil { return } return } func parseHeader(header string, commit *Commit) error { if !headerRegex.MatchString(header) { return errors.New("wrong header format") } commit.RawHeader = header headerSubmatches := headerRegex.FindStringSubmatch(header) commit.Type = headerSubmatches[1] return nil }