Skip to content

Commit

Permalink
Merge branch 'master' into features/consistent_output_error_gh_119, u…
Browse files Browse the repository at this point in the history
…pdated after #146 and #181

Conflicts:
	lib/pre-commit/checks/grep.rb
  • Loading branch information
mpapis committed Oct 3, 2014
2 parents c7cc264 + 39447f9 commit e949d2f
Show file tree
Hide file tree
Showing 24 changed files with 218 additions and 59 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Add the ability to pass command line flags to Rubocop. `rubocop.flags`
* Upgrade JSHint to 2.5.4

## 0.19.0
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/pre_commit/checks/before_all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ def files_filter(staged_files)
end

def extra_grep
" | grep -v \/\/"
%w{-v //}
end

def message
"before(:all) found:"
end

def pattern
'-e "before.*:all"'
"before.*:all"
end

def self.description
Expand Down
8 changes: 6 additions & 2 deletions lib/plugins/pre_commit/checks/coffeelint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ def call(staged_files)
staged_files = staged_files.grep(/\.coffee$/)
return if staged_files.empty?

args = (config_file_flag + staged_files).join(' ')
result =
in_groups(staged_files).map do |files|
args = %w{coffeelint} + config_file_flag + files
execute(args)
end.compact

execute("coffeelint #{args}")
result.empty? ? nil : result.join("\n")
end

def config_file_flag
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/pre_commit/checks/console_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ def files_filter(staged_files)
end

def extra_grep
" | grep -v \/\/"
%w{-v //}
end

def message
"console.log found:"
end

def pattern
'-e "console\\.log"'
"console\\.log"
end

def self.description
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/pre_commit/checks/debugger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def message
end

def pattern
'-e "^[ ]*debugger"'
"^[ ]*debugger"
end

def self.description
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/pre_commit/checks/gemfile_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ def message
end

def pattern
"'path:|:path\\s*=>'"
"path:|:path\\s*=>"
end

def extra_grep
" | grep -v '#'"
%w{-v #}
end

def self.description
Expand Down
14 changes: 10 additions & 4 deletions lib/plugins/pre_commit/checks/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ module PreCommit
module Checks
class Local < Plugin

DEFAULT_LOCATION = "config/pre-commit.rb"
attr_writer :script

def call(staged_files, script=Local::DEFAULT_LOCATION)
return unless File.exist?(script)
def call(staged_files)
return unless script
output = `ruby #{script} #{staged_files.join(" ")} 2>&1`
"#{script} failed:\n#{output}" unless $?.success?
end

def self.description
"Executes 'ruby #{DEFAULT_LOCATION}'."
"Executes a custom script located at config/pre_commit.rb"
end

def script
@script ||= ["config/pre_commit.rb", "config/pre-commit.rb"].detect do |file|
File.exist?(file)
end
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/pre_commit/checks/merge_conflict.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def message
end

def pattern
"'<<<<<<<'"
"<<<<<<<"
end

def self.description
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/pre_commit/checks/rspec_focus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def message
end

def pattern
"'(describe|context|it).*(:focus|focus:).*do'"
"(describe|context|it).*(:focus|focus:).*do"
end

def self.description
Expand Down
6 changes: 5 additions & 1 deletion lib/plugins/pre_commit/checks/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def call(staged_files)
staged_files = staged_files.grep(/\.rb$/)
return if staged_files.empty?

args = config_file_flag + ["--force-exclusion"] + staged_files
args = config_file_flag + user_supplied_flags + ["--force-exclusion"] + staged_files

success, captured = capture { ::RuboCop::CLI.new.run(args) == 0 }
captured unless success
Expand All @@ -41,6 +41,10 @@ def config_file_flag
config_file ? ['-c', config_file] : []
end

def user_supplied_flags
Array(config.get('rubocop.flags')).reject(&:empty?)
end

def alternate_config_file
'.rubocop.yml'
end
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/pre_commit/checks/ruby_symbol_hashrockets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def message
end

def pattern
'\'[^:](:{1}(?:\$|@|@@|[_A-Za-z])?\w*[=!?]?\s*=>\s*)\''
'[^:](:{1}(?:\$|@|@@|[_A-Za-z])?\w*[=!?]?\s*=>\s*)'
end

def self.description
Expand Down
8 changes: 6 additions & 2 deletions lib/plugins/pre_commit/checks/scss_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ def call(staged_files)
staged_files = staged_files.grep(/\.scss$/)
return if staged_files.empty?

args = (config_file_flag + staged_files).join(' ')
result =
in_groups(staged_files).map do |files|
args = %w{scss-lint} + config_file_flag + files
execute(args)
end.compact

execute("scss-lint #{args}")
result.empty? ? nil : result.join("\n")
end

def config_file_flag
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/pre_commit/checks/tabs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def message
end

def pattern
"'^ *\t'"
"^ *\t"
end

def self.description
Expand Down
29 changes: 18 additions & 11 deletions lib/pre-commit/checks/grep.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
require 'pre-commit/checks/plugin'
require 'pre-commit/checks/shell'
require 'pre-commit/error_list'
require 'pre-commit/line'
require 'shellwords'

module PreCommit
module Checks
class Grep < Plugin
class Grep < Shell
class PaternNotSet < StandardError
def message
"Please define 'pattern' method."
Expand All @@ -19,7 +18,7 @@ def files_filter(staged_files)
end

def extra_grep
@extra_grep or ""
@extra_grep or []
end

def message
Expand All @@ -33,19 +32,27 @@ def pattern
# general code:

def call(staged_files)
staged_files = files_filter(staged_files).map(&:shellescape)
staged_files = files_filter(staged_files)
return if staged_files.empty?
errors = `#{grep} #{pattern} #{staged_files.join(" ")}#{extra_grep}`
return unless $?.success?
parse_errors(message, errors)

result =
in_groups(staged_files).map do |files|
args = grep + [pattern] + files
args += ["|", "grep"] + extra_grep if !extra_grep.nil? and !extra_grep.empty?
execute(args, success_status: false)
end.compact

result.empty? ? nil : parse_errors(message, result)
end

private

def parse_errors(message, list)
result = PreCommit::ErrorList.new(message)
result.errors +=
list.split(/\n/).map do |line|
list.map do |group|
group.split(/\n/)
end.flatten.compact.map do |line|
PreCommit::Line.new(nil, *parse_error(line))
end
result
Expand All @@ -59,9 +66,9 @@ def parse_error(line)
def grep(grep_version = nil)
grep_version ||= detect_grep_version
if grep_version =~ /FreeBSD/
"grep -EnIH"
%w{grep -EnIH}
else
"grep -PnIH"
%w{grep -PnIH}
end
end

Expand Down
11 changes: 11 additions & 0 deletions lib/pre-commit/checks/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ def config_file
def alternate_config_file
''
end

# group files in packs smaller then 127kB (1000 files)
# 127k based on http://www.in-ulm.de/~mascheck/various/argmax/
# and 262144 limit on OSX - my env size /2 to be safe
# assuming mean file length shorter then 127 chars splitting to
# groups of 1000 files, each_slice for simplicity, doing real
# check could be to time consuming
def in_groups(files, group_size = 1000)
files.each_slice(group_size)
end

end
end
end
21 changes: 17 additions & 4 deletions lib/pre-commit/checks/shell.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
require 'pre-commit/checks/plugin'
require 'open3'
require 'shellwords'

module PreCommit
module Checks
class Shell < Plugin

private

def execute(command)
_, stdout, stderr, process = Open3.popen3(command)
stdout.read + stderr.read unless process.value.success?
def execute(*args)
options = args.last.is_a?(::Hash) ? args.pop : {}
args = build_command(*args)
execute_raw(args, options)
end

def build_command(*args)
args.flatten.map do |arg|
arg = arg.shellescape if arg != '|' && arg != '&&' && arg != '||'
arg
end.join(" ")
end

def execute_raw(command, options = {})
result = `#{command} 2>&1`
$?.success? == (options.fetch(:success_status, true)) ? nil : result
end
end
end
Expand Down
10 changes: 8 additions & 2 deletions lib/pre-commit/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module PreCommit

class Installer

TARGET_HOOK_PATH = '.git/hooks/pre-commit'
TARGET_GIT_PATH = '.git'
TARGET_HOOKS_PATH = 'hooks/pre-commit'
TEMPLATE_DIR = File.expand_path("../../../templates/hooks/", __FILE__)

attr_reader :key
Expand All @@ -19,7 +20,12 @@ def hook
end

def target
TARGET_HOOK_PATH
target_git_path =
if File.directory?(TARGET_GIT_PATH)
then TARGET_GIT_PATH
else File.readlines('.git').first.match(/gitdir: (.*)$/)[1]
end
File.join(target_git_path, TARGET_HOOKS_PATH)
end

def install
Expand Down
8 changes: 8 additions & 0 deletions test/minitest_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ def destroy_temp_dir
FileUtils.rm_rf(@dir)
end

def in_tmpdir
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
yield
end
end
end

def ruby_includes
"-I #{Gem::Specification.find_by_name('pluginator').full_gem_path}/lib -I #{project_dir}/lib"
end
Expand Down
48 changes: 48 additions & 0 deletions test/unit/plugins/pre_commit/checks/local_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'minitest_helper'
require 'plugins/pre_commit/checks/local'

describe PreCommit::Checks::Local do

let(:config_file) { fixture_file("pre-commit.rb") }
let(:check) { PreCommit::Checks::Local.new(nil, nil, []) }

it "succeeds if there is no config" do
check.call([]).must_equal nil
end

it "succeeds if script succeeds" do
check.script = config_file
check.call([]).must_equal nil
end

it "fails if script fails" do
check.script = config_file
check.call(["xxx"]).must_include "pre-commit.rb failed"
end

it "finds a local script" do
in_tmpdir do
FileUtils.mkdir_p("config")
FileUtils.touch(File.join("config", "pre-commit.rb"))
check.script.must_equal "config/pre-commit.rb"
end
end

it "finds a local script with an underscored name" do
in_tmpdir do
FileUtils.mkdir_p("config")
FileUtils.touch(File.join("config", "pre_commit.rb"))
check.script.must_equal "config/pre_commit.rb"
end
end

it "prefers the underscored local script name" do
in_tmpdir do
FileUtils.mkdir_p("config")
FileUtils.touch(File.join("config", "pre_commit.rb"))
FileUtils.touch(File.join("config", "pre-commit.rb"))
check.script.must_equal "config/pre_commit.rb"
end
end

end

0 comments on commit e949d2f

Please sign in to comment.