Skip to content

Commit 3d2e536

Browse files
authored
Merge pull request #114 from adeeshperera/main
Fix #112 : add commit message length validation
2 parents 19c7279 + 3fa4b75 commit 3d2e536

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ commit
3030
test.txt
3131
AGENTS.md
3232
commit~
33+
commit-msg

cmd/cli/createMsg.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func CreateCommitMsg(Store *store.StoreMethods, dryRun bool, autoCommit bool) {
129129
spinnerGenerating.Success("Commit message generated successfully!")
130130

131131
currentMessage := strings.TrimSpace(commitMsg)
132+
validateCommitMessageLength(currentMessage)
132133
currentStyleLabel := stylePresets[0].Label
133134
var currentStyleOpts *types.GenerationOptions
134135
accepted := false
@@ -190,6 +191,7 @@ interactionLoop:
190191
spinner.Success("Commit message regenerated!")
191192
attempt = nextAttempt
192193
currentMessage = strings.TrimSpace(updatedMessage)
194+
validateCommitMessageLength(currentMessage)
193195
case actionEditOption:
194196
edited, editErr := editCommitMessage(currentMessage)
195197
if editErr != nil {
@@ -201,6 +203,7 @@ interactionLoop:
201203
continue
202204
}
203205
currentMessage = strings.TrimSpace(edited)
206+
validateCommitMessageLength(currentMessage)
204207
case actionExitOption:
205208
pterm.Info.Println("Exiting without copying commit message.")
206209
return
@@ -577,3 +580,30 @@ func maskAPIKey(apiKey string) string {
577580
func estimateTokens(text string) int {
578581
return len(text) / 4
579582
}
583+
584+
// validateCommitMessageLength checks if the commit message exceeds recommended length limits
585+
// and displays appropriate warnings
586+
func validateCommitMessageLength(message string) {
587+
if message == "" {
588+
return
589+
}
590+
591+
lines := strings.Split(message, "\n")
592+
if len(lines) == 0 {
593+
return
594+
}
595+
596+
subjectLine := strings.TrimSpace(lines[0])
597+
subjectLength := len(subjectLine)
598+
599+
// Git recommends subject lines be 50 characters or less, but allows up to 72
600+
const maxRecommendedLength = 50
601+
const maxAllowedLength = 72
602+
603+
if subjectLength > maxAllowedLength {
604+
pterm.Warning.Printf("Commit message subject line is %d characters (exceeds %d character limit)\n", subjectLength, maxAllowedLength)
605+
pterm.Info.Println("Consider shortening the subject line for better readability")
606+
} else if subjectLength > maxRecommendedLength {
607+
pterm.Warning.Printf("Commit message subject line is %d characters (recommended limit is %d)\n", subjectLength, maxRecommendedLength)
608+
}
609+
}

0 commit comments

Comments
 (0)