Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit 660a3b5

Browse files
committed
REORG/MINOR: reduce amount of code in main
1 parent 94435d8 commit 660a3b5

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

check-commit/check.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ func checkSubjectText(subject string) error {
8383
subjectPartsLen := len(subjectParts)
8484

8585
if subject != strings.Join(subjectParts, " ") {
86-
return fmt.Errorf("malformatted subject string (trailing or double spaces?): '%s' (%w)", subject, ErrSubjectMessageFormat)
86+
return fmt.Errorf(
87+
"malformatted subject string (trailing or double spaces?): '%s' (%w)",
88+
subject, ErrSubjectMessageFormat)
8789
}
8890

8991
if subjectPartsLen < minSubjectParts || subjectPartsLen > maxSubjectParts {
@@ -292,6 +294,27 @@ func getCommitSubjects(repo *git.Repository, from, to string) ([]string, error)
292294
return subjects, nil
293295
}
294296

297+
var ErrSubjectList = errors.New("subjects contain errors")
298+
299+
func (c CommitPolicyConfig) CheckSubjectList(subjects []string) error {
300+
errors := false
301+
302+
for _, subject := range subjects {
303+
subject = strings.Trim(subject, "'")
304+
if err := c.CheckSubject([]byte(subject)); err != nil {
305+
log.Printf("%s, original subject message '%s'", err, subject)
306+
307+
errors = true
308+
}
309+
}
310+
311+
if errors {
312+
return ErrSubjectList
313+
}
314+
315+
return nil
316+
}
317+
295318
const requiredCmdlineArgs = 2
296319

297320
func main() {
@@ -327,21 +350,10 @@ func main() {
327350
log.Fatalf("error getting commit subjects: %s", err)
328351
}
329352

330-
errors := false
331-
332-
for _, subject := range subjects {
333-
subject = strings.Trim(subject, "'")
334-
if err := commitPolicy.CheckSubject([]byte(subject)); err != nil {
335-
log.Printf("%s, original subject message '%s'", err, subject)
336-
337-
errors = true
338-
}
339-
}
340-
341-
if errors {
353+
if err := commitPolicy.CheckSubjectList(subjects); err != nil {
342354
log.Printf("encountered one or more commit message errors\n")
343355
log.Fatalf("%s\n", commitPolicy.HelpText)
344-
} else {
345-
log.Printf("check completed without errors\n")
346356
}
357+
358+
log.Printf("check completed without errors\n")
347359
}

0 commit comments

Comments
 (0)