class Overcommit::Hook::CommitMsg::TextWidth

Ensures the number of columns the subject and commit message lines occupy is under the preferred limits.

Public Instance Methods

run() click to toggle source
# File lib/overcommit/hook/commit_msg/text_width.rb, line 7
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 Instance Methods

find_errors_in_body(lines) click to toggle source
# File lib/overcommit/hook/commit_msg/text_width.rb, line 39
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
find_errors_in_subject(subject) click to toggle source
# File lib/overcommit/hook/commit_msg/text_width.rb, line 22
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
special_prefix_length(subject) click to toggle source
# File lib/overcommit/hook/commit_msg/text_width.rb, line 52
def special_prefix_length(subject)
  subject.match(/^(fixup|squash)! /) { |match| match[0].length } || 0
end