github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/project/commit_msg/commit_msg.go (about)

     1  package commit_msg
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strings"
     7  )
     8  
     9  var RxCommit = regexp.MustCompile("^(FIX|TASK)(\\s+)?([A-Z]+-[0-9]+)?")
    10  
    11  func CheckCommit(commit string) error {
    12  	lines := strings.Split(commit, "\n")
    13  	matched := RxCommit.FindStringSubmatch(lines[0])
    14  	if len(matched) > 0 {
    15  		summary := strings.TrimSpace(RxCommit.ReplaceAllString(lines[0], ""))
    16  		if len(summary) < 6 {
    17  			return fmt.Errorf("commit summary is too short, at least 6 characters, now %d", len(summary))
    18  		}
    19  		return nil
    20  	}
    21  	return fmt.Errorf("commit format incorrect: %s\nmust be (FIX|TASK) ([A-Z]+-[0-9]+)?", commit)
    22  }