11package main
22
33import (
4+ "encoding/hex"
45 "errors"
56 "fmt"
67 "io/ioutil"
@@ -9,6 +10,7 @@ import (
910 "path"
1011 "regexp"
1112 "strings"
13+ "unicode"
1214 "unicode/utf8"
1315
1416 "github.com/go-git/go-git/v5"
@@ -139,29 +141,39 @@ func (c CommitPolicyConfig) CheckPatchTypes(tag, severity string, patchTypeName
139141var ErrTagScope = errors .New ("invalid tag and or severity" )
140142
141143func (c CommitPolicyConfig ) CheckSubject (rawSubject []byte ) error {
144+ // check for ascii-only before anything else
145+ for i := 0 ; i < len (rawSubject ); i ++ {
146+ if rawSubject [i ] > unicode .MaxASCII {
147+ log .Printf ("non-ascii characters detected in in subject:\n %s" , hex .Dump (rawSubject ))
148+
149+ return fmt .Errorf ("non-ascii characters in commit subject: %w" , ErrTagScope )
150+ }
151+ }
142152 // 5 subgroups, 4. is "/severity", 5. is "severity"
143153 r := regexp .MustCompile (`^(?P<match>(?P<tag>[A-Z]+)(\/(?P<severity>[A-Z]+))?: )` )
144154
145155 tTag := []byte ("$tag" )
146156 tScope := []byte ("$severity" )
147157 result := []byte {}
148158
159+ candidates := []string {}
160+
149161 var tag , severity string
150162
151163 for _ , tagAlternative := range c .TagOrder {
152164 tagOK := tagAlternative .Optional
153165
154- for _ , pType := range tagAlternative .PatchTypes { // we allow more than one set of tags in a position
155- submatch := r .FindSubmatchIndex (rawSubject )
156- if len (submatch ) == 0 { // no match
157- continue
158- }
166+ submatch := r .FindSubmatchIndex (rawSubject )
167+ if len (submatch ) == 0 { // no match
168+ continue
169+ }
159170
160- tagPart := rawSubject [submatch [0 ]:submatch [1 ]]
171+ tagPart := rawSubject [submatch [0 ]:submatch [1 ]]
161172
162- tag = string (r .Expand (result , tTag , tagPart , submatch ))
163- severity = string (r .Expand (result , tScope , tagPart , submatch ))
173+ tag = string (r .Expand (result , tTag , tagPart , submatch ))
174+ severity = string (r .Expand (result , tScope , tagPart , submatch ))
164175
176+ for _ , pType := range tagAlternative .PatchTypes { // we allow more than one set of tags in a position
165177 if c .CheckPatchTypes (tag , severity , pType ) { // we found what we were looking for, so consume input
166178 rawSubject = rawSubject [submatch [1 ]:]
167179 tagOK = tagOK || true
@@ -170,8 +182,13 @@ func (c CommitPolicyConfig) CheckSubject(rawSubject []byte) error {
170182 }
171183 }
172184
185+ candidates = append (candidates , string (tagPart ))
186+
173187 if ! tagOK {
174- return fmt .Errorf ("invalid tag or no tag found: %w" , ErrTagScope )
188+ log .Printf ("unable to find match in %s\n " , candidates )
189+
190+ return fmt .Errorf ("invalid tag or no tag found, searched through [%s]: %w" ,
191+ strings .Join (tagAlternative .PatchTypes , ", " ), ErrTagScope )
175192 }
176193 }
177194
0 commit comments