Sha256: 81f69ea8e3f3efe93d5bc498c21277aaa18347174d1c08c311dbe40944791b4b

Contents?: true

Size: 1.46 KB

Versions: 6

Compression:

Stored size: 1.46 KB

Contents

# 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"
        nil
      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

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
overcommit-0.62.0 lib/overcommit/hook/commit_msg/text_width.rb
overcommit-0.61.0 lib/overcommit/hook/commit_msg/text_width.rb
overcommit-0.60.0 lib/overcommit/hook/commit_msg/text_width.rb
overcommit-0.59.1 lib/overcommit/hook/commit_msg/text_width.rb
overcommit-0.59.0 lib/overcommit/hook/commit_msg/text_width.rb
overcommit-jeygeethanmedia-0.58.0 lib/overcommit/hook/commit_msg/text_width.rb