Skip to content

Commit 9b0956b

Browse files
committed
Modify TextWidth error messages to show only one issue
Previously we were showing both ends of the range of widths. This is usually unnecessary information. Let's show one or the other, based on which threshold was surpassed.
1 parent fc03dde commit 9b0956b

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

lib/overcommit/hook/commit_msg/text_width.rb

+10-4
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ def find_errors_in_subject(subject)
2121
max_subject_width =
2222
config['max_subject_width'] +
2323
special_prefix_length(subject)
24-
min_subject_width = config['min_subject_width']
25-
return unless subject.length > max_subject_width || subject.length < min_subject_width
2624

27-
@errors << "Please keep the subject <= #{max_subject_width} and >= " \
28-
"#{min_subject_width} characters"
25+
if subject.length > max_subject_width
26+
@errors << "Commit message subject must be <= #{max_subject_width} characters"
27+
return
28+
end
29+
30+
min_subject_width = config['min_subject_width']
31+
if subject.length < min_subject_width
32+
@errors << "Commit message subject must be >= #{min_subject_width} characters"
33+
return
34+
end
2935
end
3036

3137
def find_errors_in_body(lines)

spec/overcommit/hook/commit_msg/text_width_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
A message line that is way too long. A message line that is way too long.
9191
MSG
9292

93-
it { should warn /keep.*subject <= 60.*\n.*line 3.*> 72.*/im }
93+
it { should warn /subject.*<= 60.*\n.*line 3.*> 72.*/im }
9494
end
9595

9696
context 'when custom lengths are specified' do
@@ -109,13 +109,13 @@
109109
context 'when subject is longer than 70 characters' do
110110
let(:commit_msg) { 'A' * 71 }
111111

112-
it { should warn /subject/ }
112+
it { should warn /subject must be <= 70/ }
113113
end
114114

115115
context 'when subject is less than 4 characters' do
116116
let(:commit_msg) { 'A' * 3 }
117117

118-
it { should warn /subject/ }
118+
it { should warn /subject must be >= 4/ }
119119
end
120120

121121
context 'when subject is 70 characters or fewer' do
@@ -153,7 +153,7 @@
153153
This line is longer than #{'A' * 80} characters.
154154
MSG
155155

156-
it { should warn /keep.*subject <= 70.*\n.*line 3.*> 80.*/im }
156+
it { should warn /subject.*<= 70.*\n.*line 3.*> 80/im }
157157
end
158158
end
159159
end

0 commit comments

Comments
 (0)