Class: Overcommit::Hook::PreCommit::Rubocop

Inherits:
Base
  • Object
show all
Defined in:
lib/overcommit/hook/pre_commit/rubocop.rb

Overview

Runs ‘rubocop` against any modified Ruby files.

Instance Method Summary collapse

Methods inherited from Base

#applicable_files, #description, #enabled?, #execute, #in_path?, #initialize, #name, #quiet?, #required?, #run?, #skip?

Constructor Details

This class inherits a constructor from Overcommit::Hook::Base

Instance Method Details

#runObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/overcommit/hook/pre_commit/rubocop.rb', line 4

def run
  unless in_path?('rubocop')
    return :warn, 'Rubocop not installed -- run `gem install rubocop`'
  end

  result = execute(%w[rubocop --format=emacs --force-exclusion] + applicable_files)
  return :good if result.success?

  output = result.stdout + result.stderr

  # Keep lines from the output for files that we actually modified
  error_lines, warning_lines = output.split("\n").partition do |output_line|
    if match = output_line.match(/^([^:]+):(\d+)/)
      file = match[1]
      line = match[2]
    end
    modified_lines(file).include?(line.to_i)
  end

  return :bad, error_lines.join("\n") unless error_lines.empty?

  [:warn, "Modified files have lints (on lines you didn't modify)\n" <<
          warning_lines.join("\n")]
end