forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_width.rb
56 lines (43 loc) · 1.46 KB
/
text_width.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true
module Overcommit::Hook::CommitMsg
# Ensures the number of columns the subject and commit message lines occupy is
# under the preferred limits.
class TextWidth < Base
def run
return :pass if empty_message?
@errors = []
find_errors_in_subject(commit_message_lines.first.chomp)
find_errors_in_body(commit_message_lines)
return :warn, @errors.join("\n") if @errors.any?
:pass
end
private
def find_errors_in_subject(subject)
max_subject_width =
config['max_subject_width'] +
special_prefix_length(subject)
if subject.length > max_subject_width
@errors << "Commit message subject must be <= #{max_subject_width} characters"
return
end
min_subject_width = config['min_subject_width']
if subject.length < min_subject_width
@errors << "Commit message subject must be >= #{min_subject_width} characters"
return
end
end
def find_errors_in_body(lines)
return unless lines.count > 2
max_body_width = config['max_body_width']
lines[2..-1].each_with_index do |line, index|
if line.chomp.size > max_body_width
@errors << "Line #{index + 3} of commit message has > " \
"#{max_body_width} characters"
end
end
end
def special_prefix_length(subject)
subject.match(/^(fixup|squash)! /) { |match| match[0].length } || 0
end
end
end