Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ commit
test.txt
AGENTS.md
commit~
commit-msg
30 changes: 30 additions & 0 deletions cmd/cli/createMsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -201,6 +203,7 @@ interactionLoop:
continue
}
currentMessage = strings.TrimSpace(edited)
validateCommitMessageLength(currentMessage)
case actionExitOption:
pterm.Info.Println("Exiting without copying commit message.")
return
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

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/utf8 at the top of the file and apply this diff:

-	subjectLength := len(subjectLine)
+	subjectLength := utf8.RuneCountInString(subjectLine)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
subjectLength := len(subjectLine)
subjectLength := utf8.RuneCountInString(subjectLine)
🤖 Prompt for AI Agents
In cmd/cli/createMsg.go around line 597, the code uses len(subjectLine) which
returns byte count and is incorrect for Unicode; import "unicode/utf8" at the
top of the file and replace the byte-length call with
utf8.RuneCountInString(subjectLine) (assign result to the same variable name or
a new one) so the character count reflects Unicode runes rather than bytes.


// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Verify test coverage for the new validation function.

Ensure that the new validateCommitMessageLength function has adequate test coverage, including edge cases such as empty messages, messages at boundary lengths (50, 72), multi-line messages, and messages with Unicode characters.

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=go

Length of output: 408


Add unit tests for validateCommitMessageLength
No tests currently cover the function in cmd/cli/createMsg.go:586—add tests for empty input, boundary lengths (50,72 characters), multi-line messages, and Unicode characters.

🤖 Prompt for AI Agents
In cmd/cli/createMsg.go around lines 584 to 609, add unit tests for
validateCommitMessageLength covering empty input, subject exactly at the
recommended boundary (50 chars), exactly at the allowed boundary (72 chars),
just over those boundaries, a multi-line message (ensure only subject line is
measured), and messages containing Unicode characters to ensure length is
measured correctly; implement tests that capture pterm output (or inject a
logger) to assert warnings/info are printed only for the appropriate cases and
include assertions for no output on empty/valid inputs.

Loading