Skip to content

Commit 4ddaec6

Browse files
authored
Allow URL-only lines to exceed TextWidth body limit (sds#900)
## Description `CommitMsg::TextWidth` warns on every body line longer than `max_body_width`, including lines that contain nothing but a URL. A URL can't be wrapped without breaking it (in the terminal and on GitHub), so these warnings can't be fixed without making the message worse. This PR exempts body lines that consist solely of a URL (`scheme://...`, surrounding whitespace ignored), optionally written as a Markdown-style link reference such as `[1]: https://...`. Lines that mix a URL with other text are still checked as before, and the subject line checks are unchanged. No new configuration option is added; the exemption is always on, since an over-long URL-only line is never actionable. **Motivation / disclosure:** I had some spare AI-assistant quota (Claude Code) and am using it to try to help projects with open good-first-issues. The change was prepared with Claude Code and verified as listed below. If it doesn't fit, isn't up to your bar, or you'd simply rather not take it — please feel free to close it, no hard feelings at all 🙂 ## Related issue Closes sds#822 ## Checklist - [x] Tests pass locally (`bundle exec rspec spec/overcommit/hook/commit_msg/text_width_spec.rb`: 19 examples, 0 failures; the 2 new URL-only specs fail without the fix. Full `bundle exec rspec` on Ruby 3.4: 1647 examples, 1 failure (`utils_spec.rb:35`, `repo_root` with no `.git` directory), which fails identically on `main` in my environment. `bundle exec overcommit --run`: all pre-commit hooks passed) - [ ] `CHANGELOG.md` is updated (if applicable) — n/a, entries appear to be added when a version is cut; happy to add one if you prefer - [ ] Documentation is updated (if applicable) — n/a, the behavior is described in the hook's source comment
1 parent c06c0f5 commit 4ddaec6

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

‎lib/overcommit/hook/commit_msg/text_width.rb‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ module Overcommit::Hook::CommitMsg
44
# Ensures the number of columns the subject and commit message lines occupy is
55
# under the preferred limits.
66
class TextWidth < Base
7+
# A line consisting only of a URL (optionally as a Markdown-style link
8+
# reference such as `[1]: https://...`) cannot be wrapped without breaking
9+
# the link, so it is exempt from the body width limit.
10+
URL_ONLY_LINE = %r{\A(\[[^\]]+\]:\s*)?[a-z][a-z0-9+.-]*://\S+\z}i.freeze
11+
712
def run
813
return :pass if empty_message?
914

@@ -42,13 +47,17 @@ def find_errors_in_body(lines)
4247
max_body_width = config['max_body_width']
4348

4449
lines[2..].each_with_index do |line, index|
45-
if line.chomp.size > max_body_width
46-
@errors << "Line #{index + 3} of commit message has > " \
47-
"#{max_body_width} characters"
48-
end
50+
next if line.chomp.size <= max_body_width || url_only_line?(line)
51+
52+
@errors << "Line #{index + 3} of commit message has > " \
53+
"#{max_body_width} characters"
4954
end
5055
end
5156

57+
def url_only_line?(line)
58+
URL_ONLY_LINE.match?(line.strip)
59+
end
60+
5261
def special_prefix_length(subject)
5362
subject.match(/^(fixup|squash)! /) { |match| match[0].length } || 0
5463
end

‎spec/overcommit/hook/commit_msg/text_width_spec.rb‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,37 @@
7373
it { should warn('Line 3 of commit message has > 72 characters') }
7474
end
7575

76+
context 'when a line longer than 72 characters contains only a URL' do
77+
let(:commit_msg) { <<-MSG }
78+
Some summary
79+
80+
See:
81+
https://example.com/some/very/long/path/that/cannot/be/wrapped/without/breaking/it
82+
MSG
83+
84+
it { should pass }
85+
end
86+
87+
context 'when a line longer than 72 characters is a link reference' do
88+
let(:commit_msg) { <<-MSG }
89+
Some summary
90+
91+
[1]: https://example.com/some/very/long/path/that/cannot/be/wrapped/without/breaking
92+
MSG
93+
94+
it { should pass }
95+
end
96+
97+
context 'when a line longer than 72 characters contains a URL and other text' do
98+
let(:commit_msg) { <<-MSG }
99+
Some summary
100+
101+
See https://example.com/some/very/long/path/that/cannot/be/wrapped for details
102+
MSG
103+
104+
it { should warn('Line 3 of commit message has > 72 characters') }
105+
end
106+
76107
context 'when all lines in the message are fewer than 72 characters' do
77108
let(:commit_msg) { <<-MSG }
78109
Some summary

0 commit comments

Comments
 (0)