-
Notifications
You must be signed in to change notification settings - Fork 18
Fix DFanso#112 : add commit message length validation #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,3 +30,4 @@ commit | |
| test.txt | ||
| AGENTS.md | ||
| commit~ | ||
| commit-msg | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,6 +129,7 @@ func CreateCommitMsg(Store *store.StoreMethods, dryRun bool, autoCommit bool) { | |
| spinnerGenerating.Success("Commit message generated successfully!") | ||
|
|
||
| currentMessage := strings.TrimSpace(commitMsg) | ||
| validateCommitMessageLength(currentMessage) | ||
| currentStyleLabel := stylePresets[0].Label | ||
| var currentStyleOpts *types.GenerationOptions | ||
| accepted := false | ||
|
|
@@ -190,6 +191,7 @@ interactionLoop: | |
| spinner.Success("Commit message regenerated!") | ||
| attempt = nextAttempt | ||
| currentMessage = strings.TrimSpace(updatedMessage) | ||
| validateCommitMessageLength(currentMessage) | ||
| case actionEditOption: | ||
| edited, editErr := editCommitMessage(currentMessage) | ||
| if editErr != nil { | ||
|
|
@@ -201,6 +203,7 @@ interactionLoop: | |
| continue | ||
| } | ||
| currentMessage = strings.TrimSpace(edited) | ||
| validateCommitMessageLength(currentMessage) | ||
| case actionExitOption: | ||
| pterm.Info.Println("Exiting without copying commit message.") | ||
| return | ||
|
|
@@ -577,3 +580,30 @@ func maskAPIKey(apiKey string) string { | |
| func estimateTokens(text string) int { | ||
| return len(text) / 4 | ||
| } | ||
|
|
||
| // validateCommitMessageLength checks if the commit message exceeds recommended length limits | ||
| // and displays appropriate warnings | ||
| func validateCommitMessageLength(message string) { | ||
| if message == "" { | ||
| return | ||
| } | ||
|
|
||
| lines := strings.Split(message, "\n") | ||
| if len(lines) == 0 { | ||
| return | ||
| } | ||
|
|
||
| subjectLine := strings.TrimSpace(lines[0]) | ||
| subjectLength := len(subjectLine) | ||
|
|
||
| // Git recommends subject lines be 50 characters or less, but allows up to 72 | ||
| const maxRecommendedLength = 50 | ||
| const maxAllowedLength = 72 | ||
|
|
||
| if subjectLength > maxAllowedLength { | ||
| pterm.Warning.Printf("Commit message subject line is %d characters (exceeds %d character limit)\n", subjectLength, maxAllowedLength) | ||
| pterm.Info.Println("Consider shortening the subject line for better readability") | ||
| } else if subjectLength > maxRecommendedLength { | ||
| pterm.Warning.Printf("Commit message subject line is %d characters (recommended limit is %d)\n", subjectLength, maxRecommendedLength) | ||
| } | ||
| } | ||
|
Comment on lines
+584
to
+609
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainVerify test coverage for the new validation function. Ensure that the new Run the following script to check for test coverage: 🏁 Script executed: #!/bin/bash
# Description: Search for test files covering validateCommitMessageLength
# Search for test files related to createMsg
fd -e go -e test.go 'createMsg.*test'
# Search for test cases that reference the validation function
rg -n 'validateCommitMessageLength' --type=goLength of output: 408 Add unit tests for validateCommitMessageLength 🤖 Prompt for AI Agents |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Character count may be incorrect for Unicode text.
len(subjectLine)counts bytes, not Unicode characters. For commit messages containing multi-byte characters (emoji, CJK characters, etc.), this will report an inflated length.Import
unicode/utf8at the top of the file and apply this diff:📝 Committable suggestion
🤖 Prompt for AI Agents