Skip to content

Conversation

@adeeshperera
Copy link

@adeeshperera adeeshperera commented Oct 11, 2025

  • Add validation for commit message subject line length (50-72 char limit)
  • Display warnings when messages exceed recommended limits
  • Integrate validation into generation, regeneration, and editing workflows

Summary by CodeRabbit

  • New Features

    • Adds warnings when the commit message subject exceeds recommended or hard length limits, shown after generating, regenerating, or editing a message.
  • Chores

    • Updated ignore rules to exclude commit message-related files.

adeeshperera and others added 2 commits October 11, 2025 23:49
- Add validation for commit message subject line length (50-72 char limit)
- Display warnings when messages exceed recommended limits
- Integrate validation into generation, regeneration, and editing workflows
Fix DFanso#112 : add commit message length validation
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 11, 2025

Walkthrough

Adds a commit message subject-length validator to the CLI flow and invokes it after generation, regeneration, and edits. Updates .gitignore to ignore commit-msg patterns. No exported API changes.

Changes

Cohort / File(s) Summary
Git ignore updates
\.gitignore
Added ignore pattern for commit-msg-related files.
CLI commit message validation
cmd/cli/createMsg.go
Introduced validateCommitMessageLength(message string) to warn when subject line exceeds 50 (recommended) or 72 (maximum) characters; called after initial generation, regeneration, and editor-based edits.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User
  participant CLI
  participant Generator as Msg Generator
  participant Validator as Length Validator
  participant Editor
  participant Logger

  User->>CLI: create commit message
  CLI->>Msg Generator: generate()
  Msg Generator-->>CLI: message
  CLI->>Validator: validate(subject line)
  Validator-->>Logger: warn if >50 / >72
  Logger-->>User: warnings (if any)

  alt User regenerates
    User->>CLI: regenerate
    CLI->>Msg Generator: generate()
    Msg Generator-->>CLI: message
    CLI->>Validator: validate()
    Validator-->>Logger: warnings (if any)
  end

  alt User edits
    User->>CLI: open editor
    CLI->>Editor: edit(message)
    Editor-->>CLI: edited message
    CLI->>Validator: validate()
    Validator-->>Logger: warnings (if any)
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

enhancement, go

Poem

I nibble on lines, a tidy byte,
Sniffing subjects: keep them light.
Fifty’s sweet, seventy-two’s the gate—
Trim the tail, commit it straight.
In the burrow of logs I cheer,
“Short and crisp!” with floppy ear. 🐰✍️

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title clearly summarizes the core change of adding commit message length validation and correctly references the related issue, making it directly relevant and understandable in the context of the pull request.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
cmd/cli/createMsg.go (1)

592-594: Remove redundant empty check.

strings.Split always returns at least one element (even for empty strings), and the function already returns early if message == "", making this check unreachable.

Apply this diff to remove the redundant check:

 	lines := strings.Split(message, "\n")
-	if len(lines) == 0 {
-		return
-	}
-
 	subjectLine := strings.TrimSpace(lines[0])
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 19c7279 and 3fa4b75.

📒 Files selected for processing (2)
  • .gitignore (1 hunks)
  • cmd/cli/createMsg.go (4 hunks)
🔇 Additional comments (4)
.gitignore (1)

33-33: LGTM!

Adding commit-msg to the ignore patterns is appropriate for temporary commit message files that may be created during the validation workflow.

cmd/cli/createMsg.go (3)

132-132: LGTM!

Validation call is appropriately placed after initial message generation.


194-194: LGTM!

Validation call is appropriately placed after message regeneration.


206-206: LGTM!

Validation call is appropriately placed after message editing.

Comment on lines +584 to +609
// 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)
}
}
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.

}

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.

@DFanso DFanso self-requested a review October 11, 2025 18:36
@DFanso DFanso added enhancement New feature or request hacktoberfest Eligible for Hacktoberfest hacktoberfest-accepted Approved Hacktoberfest contribution go Pull requests that update go code labels Oct 11, 2025
Copy link
Owner

@DFanso DFanso left a comment

Choose a reason for hiding this comment

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

LGTM 🎊

@DFanso DFanso merged commit 3d2e536 into DFanso:main Oct 11, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request go Pull requests that update go code hacktoberfest Eligible for Hacktoberfest hacktoberfest-accepted Approved Hacktoberfest contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants