Skip to content

Commit 8b70427

Browse files
committed
feat(core): Truncate commit message title
This commit introduces truncation logic for commit message titles to ensure they remain within a 50-character limit. It prevents excessively long commit messages from creating formatting issues and improves readability. A test has also been added to verify this truncation behavior. Affected files: - M smart_git_commit.py - M tests/test_smart_git_commit.py
1 parent 399c157 commit 8b70427

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

smart_git_commit.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,19 @@ def generate_commit_message(self) -> str:
421421
scope = "-".join(sorted(components)[:2]) if components else "general"
422422

423423
# Create the subject line (first line of commit)
424-
subject = f"{self.commit_type.value}({scope}): {self.name}"
425-
if len(subject) > 50:
426-
# Truncate if too long
427-
subject = subject[:47] + "..."
428-
424+
# Just limit to 50 characters for GitHub compatibility
425+
max_subject_length = 50
426+
427+
# Start with type and scope
428+
prefix = f"{self.commit_type.value}({scope}): "
429+
available_chars = max_subject_length - len(prefix)
430+
431+
# Ensure the name is no longer than available chars
432+
name = self.name if len(self.name) <= available_chars else self.name[:available_chars]
433+
434+
# Combine to form subject
435+
subject = f"{prefix}{name}"
436+
429437
# Create the body with file list
430438
body = self.description if self.description else f"Update {self.file_count} files in {scope}"
431439

@@ -2230,6 +2238,7 @@ def _generate_ai_commit_message(self, group: CommitGroup) -> str:
22302238
Use present tense (e.g., "Add feature" not "Added feature").
22312239
Focus on technical details rather than trivial changes.
22322240
Do not list the filenames again.
2241+
Keep the first sentence brief as it will be used in the subject line.
22332242
22342243
Description:
22352244
"""

tests/test_smart_git_commit.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,27 @@ def test_commit_message_generation(self):
157157
self.assertIn("Affected files:", message)
158158
self.assertIn("M app/main.py", message)
159159
self.assertIn("+ app/new_feature.py", message)
160+
161+
def test_commit_message_title_truncation(self):
162+
"""Test that long commit message titles are properly limited to 50 characters."""
163+
# Create a commit group with a very long name
164+
very_long_name = "This is an extremely long commit title that exceeds the 50 character limit"
165+
group = CommitGroup(name=very_long_name, commit_type=CommitType.FEAT)
166+
group.add_change(GitChange(status="M", filename="app/main.py"))
167+
168+
message = group.generate_commit_message()
169+
170+
# Get the first line (subject)
171+
subject = message.split('\n')[0]
172+
173+
# Check that the subject is within the length limit
174+
self.assertLessEqual(len(subject), 50)
175+
176+
# The type and scope should be preserved
177+
self.assertTrue(subject.startswith('feat(app):'))
178+
179+
# No "Full title:" text should appear in the body
180+
self.assertNotIn("Full title:", message)
160181

161182

162183
class TestMockGitRepository:

0 commit comments

Comments
 (0)