|
1 | 1 | package commitrules |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "fmt" |
5 | 6 | "regexp" |
6 | 7 | "strings" |
7 | 8 | ) |
8 | 9 |
|
9 | | -// CommitRule defines the structure for commit message rules |
| 10 | +// Define static errors for commit message validation. |
| 11 | +var ( |
| 12 | + ErrInvalidFormat = errors.New("invalid commit message format") |
| 13 | + ErrMissingType = errors.New("missing commit type") |
| 14 | + ErrInvalidType = errors.New("invalid commit type") |
| 15 | + ErrTooLong = errors.New("commit message too long") |
| 16 | +) |
| 17 | + |
| 18 | +// CommitRule defines the structure for commit message rules. |
10 | 19 | type CommitRule struct { |
11 | 20 | Type string |
12 | 21 | Description string |
13 | 22 | Examples []string |
14 | 23 | } |
15 | 24 |
|
16 | | -// CommitRules holds all available commit types and their rules |
| 25 | +// CommitRules holds all available commit types and their rules. |
17 | 26 | var CommitRules = map[string]CommitRule{ |
18 | 27 | "feat": { |
19 | 28 | Type: "feat", |
@@ -52,16 +61,16 @@ var CommitRules = map[string]CommitRule{ |
52 | 61 | }, |
53 | 62 | } |
54 | 63 |
|
55 | | -// GetCommitTypes returns all available commit types |
| 64 | +// GetCommitTypes returns all available commit types. |
56 | 65 | func GetCommitTypes() []string { |
57 | | - var types []string |
| 66 | + types := make([]string, 0, len(CommitRules)) |
58 | 67 | for commitType := range CommitRules { |
59 | 68 | types = append(types, commitType) |
60 | 69 | } |
61 | 70 | return types |
62 | 71 | } |
63 | 72 |
|
64 | | -// GetPrompt generates the commit message prompt based on analysis input |
| 73 | +// GetPrompt generates the commit message prompt based on analysis input. |
65 | 74 | func GetPrompt(analysisInput string) string { |
66 | 75 | commitTypesList := strings.Join(GetCommitTypes(), ", ") |
67 | 76 |
|
@@ -92,7 +101,7 @@ Git diff to analyze: |
92 | 101 | return prompt |
93 | 102 | } |
94 | 103 |
|
95 | | -// CleanCommitMessage cleans and formats the generated commit message |
| 104 | +// CleanCommitMessage cleans and formats the generated commit message. |
96 | 105 | func CleanCommitMessage(message string) string { |
97 | 106 | // Remove quotes and extra whitespace |
98 | 107 | message = strings.Trim(message, `"'`) |
@@ -144,31 +153,31 @@ func CleanCommitMessage(message string) string { |
144 | 153 | return firstLine |
145 | 154 | } |
146 | 155 |
|
147 | | -// ValidateCommitMessage validates if a commit message follows the conventional format |
| 156 | +// ValidateCommitMessage validates if a commit message follows the conventional format. |
148 | 157 | func ValidateCommitMessage(message string) error { |
149 | 158 | message = strings.TrimSpace(message) |
150 | 159 |
|
151 | 160 | // Check basic format type(scope): description |
152 | 161 | parts := strings.SplitN(message, ":", 2) |
153 | 162 | if len(parts) != 2 { |
154 | | - return fmt.Errorf("commit message must follow format: type(scope): description") |
| 163 | + return fmt.Errorf("commit message must follow format: type(scope): description: %w", ErrInvalidFormat) |
155 | 164 | } |
156 | 165 |
|
157 | 166 | // Check if type is valid |
158 | 167 | typeAndScope := strings.TrimSpace(parts[0]) |
159 | 168 | scopeParts := strings.SplitN(typeAndScope, "(", 2) |
160 | 169 | if len(scopeParts) == 0 { |
161 | | - return fmt.Errorf("commit message must have a type") |
| 170 | + return fmt.Errorf("commit message must have a type: %w", ErrMissingType) |
162 | 171 | } |
163 | 172 |
|
164 | 173 | commitType := scopeParts[0] |
165 | 174 | if _, exists := CommitRules[commitType]; !exists { |
166 | | - return fmt.Errorf("invalid commit type: %s. Valid types: %s", commitType, strings.Join(GetCommitTypes(), ", ")) |
| 175 | + return fmt.Errorf("invalid commit type: %s. Valid types: %s: %w", commitType, strings.Join(GetCommitTypes(), ", "), ErrInvalidType) |
167 | 176 | } |
168 | 177 |
|
169 | 178 | // Check length |
170 | 179 | if len(message) > 72 { |
171 | | - return fmt.Errorf("commit message is too long: %d characters (maximum: 72)", len(message)) |
| 180 | + return fmt.Errorf("commit message is too long: %d characters (maximum: 72): %w", len(message), ErrTooLong) |
172 | 181 | } |
173 | 182 |
|
174 | 183 | if len(message) > 50 { |
|
0 commit comments