Class: FitCommit::Validators::LineLength

Inherits:
Base
  • Object
show all
Defined in:
lib/fit_commit/validators/line_length.rb

Constant Summary collapse

MERGE_COMMIT =
/\AMerge branch '[^']+' into ./
URL =
%r{[a-z]+://}

Instance Attribute Summary

Attributes inherited from Base

#branch_name, #config

Instance Method Summary collapse

Methods inherited from Base

#enabled?, inherited, #initialize, #matches_branch?, #validate

Methods included from HasErrors

#add_error, #add_warning, #clear_errors, #clear_warnings, #errors, #merge_errors, #merge_warnings, #warnings

Constructor Details

This class inherits a constructor from FitCommit::Validators::Base

Instance Method Details

#allow_long_urls?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/fit_commit/validators/line_length.rb', line 40

def allow_long_urls?
  config.fetch("AllowLongUrls")
end

#max_line_lengthObject



32
33
34
# File 'lib/fit_commit/validators/line_length.rb', line 32

def max_line_length
  config.fetch("MaxLineLength")
end

#subject_warn_lengthObject



36
37
38
# File 'lib/fit_commit/validators/line_length.rb', line 36

def subject_warn_length
  config.fetch("SubjectWarnLength")
end

#validate_line(lineno, text) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fit_commit/validators/line_length.rb', line 9

def validate_line(lineno, text)
  if lineno == 1
    if text.empty?
      add_error(lineno, "Subject line cannot be blank.")
    elsif text !~ MERGE_COMMIT
      if text.length > max_line_length
        add_error(lineno, format("Lines should be <= %i chars. (%i)",
          max_line_length, text.length))
      elsif text.length > subject_warn_length
        add_warning(lineno, format("Subject line should be <= %i chars. (%i)",
          subject_warn_length, text.length))
      end
    end
  elsif lineno == 2
    unless text.empty?
      add_error(lineno, "Second line must be blank.")
    end
  elsif text.length > max_line_length && !(allow_long_urls? && text =~ URL)
    add_error(lineno, format("Lines should be <= %i chars. (%i)",
      max_line_length, text.length))
  end
end