pre-commit 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +14 -5
- data/bin/pre-commit +7 -5
- data/lib/plugins/pre_commit/checks/before_all.rb +13 -0
- data/lib/plugins/pre_commit/checks/ci.rb +12 -0
- data/lib/plugins/pre_commit/checks/closure.rb +19 -0
- data/lib/plugins/pre_commit/checks/coffeelint.rb +19 -0
- data/lib/plugins/pre_commit/checks/console_log.rb +13 -0
- data/lib/plugins/pre_commit/checks/debugger.rb +21 -0
- data/lib/plugins/pre_commit/checks/gemfile_path.rb +14 -0
- data/lib/plugins/pre_commit/checks/js.rb +36 -0
- data/lib/plugins/pre_commit/checks/jshint.rb +24 -0
- data/lib/plugins/pre_commit/checks/jslint.rb +24 -0
- data/lib/plugins/pre_commit/checks/local.rb +13 -0
- data/lib/plugins/pre_commit/checks/merge_conflict.rb +12 -0
- data/lib/plugins/pre_commit/checks/migration.rb +38 -0
- data/lib/plugins/pre_commit/checks/nb_space.rb +23 -0
- data/lib/plugins/pre_commit/checks/php.rb +27 -0
- data/lib/plugins/pre_commit/checks/pry.rb +12 -0
- data/lib/plugins/pre_commit/checks/rspec_focus.rb +13 -0
- data/lib/plugins/pre_commit/checks/rubocop.rb +46 -0
- data/lib/plugins/pre_commit/checks/ruby_symbol_hashrockets.rb +16 -0
- data/lib/plugins/pre_commit/checks/tabs.rb +14 -0
- data/lib/plugins/pre_commit/checks/whitespace.rb +21 -0
- data/lib/pre-commit/checks.rb +66 -58
- data/lib/pre-commit/cli.rb +29 -2
- data/lib/pre-commit/support/templates/automatic_hook +35 -0
- data/lib/pre-commit/support/templates/default_hook +35 -0
- data/lib/pre-commit/support/templates/manual_hook +14 -0
- metadata +101 -26
- data/lib/pre-commit/checks/ci_check.rb +0 -10
- data/lib/pre-commit/checks/closure_check.rb +0 -13
- data/lib/pre-commit/checks/console_log_check.rb +0 -11
- data/lib/pre-commit/checks/debugger_check.rb +0 -19
- data/lib/pre-commit/checks/gemfile_path_check.rb +0 -12
- data/lib/pre-commit/checks/js_check.rb +0 -35
- data/lib/pre-commit/checks/jshint_check.rb +0 -26
- data/lib/pre-commit/checks/jslint_check.rb +0 -22
- data/lib/pre-commit/checks/local_check.rb +0 -11
- data/lib/pre-commit/checks/merge_conflict_check.rb +0 -10
- data/lib/pre-commit/checks/migration_check.rb +0 -33
- data/lib/pre-commit/checks/nb_space_check.rb +0 -21
- data/lib/pre-commit/checks/php_check.rb +0 -25
- data/lib/pre-commit/checks/pry_check.rb +0 -10
- data/lib/pre-commit/checks/rspec_focus_check.rb +0 -11
- data/lib/pre-commit/checks/rubocop_check.rb +0 -38
- data/lib/pre-commit/checks/ruby_symbol_hashrockets.rb +0 -14
- data/lib/pre-commit/checks/tabs_check.rb +0 -12
- data/lib/pre-commit/checks/whitespace_check.rb +0 -16
- data/lib/pre-commit/support/templates/pre-commit-hook +0 -23
@@ -1,13 +0,0 @@
|
|
1
|
-
module PreCommit
|
2
|
-
class ClosureCheck
|
3
|
-
CLOSURE_PATH = File.expand_path("../../support/closure/compiler.jar", __FILE__)
|
4
|
-
|
5
|
-
def self.call(staged_files)
|
6
|
-
return if staged_files.empty?
|
7
|
-
js_args = staged_files.map {|arg| "--js #{arg}"}.join(' ')
|
8
|
-
errors = `java -jar #{CLOSURE_PATH} #{js_args} --js_output_file /tmp/jammit.js 2>&1`.strip
|
9
|
-
return if errors.empty?
|
10
|
-
errors
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
@@ -1,11 +0,0 @@
|
|
1
|
-
module PreCommit
|
2
|
-
class ConsoleLogCheck
|
3
|
-
def self.call(staged_files)
|
4
|
-
staged_files.reject! { |f| File.extname(f) != ".js" }
|
5
|
-
return if staged_files.empty?
|
6
|
-
errors = `#{Utils.grep} -e "console\\.log" #{staged_files.join(" ")} | grep -v \/\/`.strip
|
7
|
-
return unless $?.success?
|
8
|
-
"console.log found:\n#{errors}"
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'pre-commit/utils'
|
2
|
-
|
3
|
-
module PreCommit
|
4
|
-
class DebuggerCheck
|
5
|
-
def self.call(staged_files)
|
6
|
-
files = files_to_check(staged_files)
|
7
|
-
return if files.empty?
|
8
|
-
|
9
|
-
errors = `#{Utils.grep} debugger #{files.join(" ")}`.strip
|
10
|
-
return unless $?.success?
|
11
|
-
|
12
|
-
"debugger statement(s) found:\n#{errors}"
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.files_to_check(files)
|
16
|
-
files.reject { |file| File.basename(file) =~ /^Gemfile/ }
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'pre-commit/utils'
|
2
|
-
|
3
|
-
module PreCommit
|
4
|
-
class GemfilePathCheck
|
5
|
-
def self.call(staged_files)
|
6
|
-
return unless staged_files.include?("Gemfile")
|
7
|
-
errors = `#{Utils.grep} 'path:|:path\\s*=>' Gemfile`.strip
|
8
|
-
return unless $?.success?
|
9
|
-
"local path found in Gemfile:\n#{errors}"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'pre-commit/utils'
|
2
|
-
require 'execjs'
|
3
|
-
|
4
|
-
module PreCommit
|
5
|
-
class JsCheck
|
6
|
-
def self.call(staged_files)
|
7
|
-
staged_files = staged_files.select { |f| File.extname(f) == ".js" }
|
8
|
-
return if staged_files.empty?
|
9
|
-
|
10
|
-
errors = []
|
11
|
-
staged_files.each do |file|
|
12
|
-
error_list = Array(run_check(file))
|
13
|
-
error_list.each { |error_object| errors << display_error(error_object, file) }
|
14
|
-
end
|
15
|
-
|
16
|
-
return if errors.empty?
|
17
|
-
errors.join("\n")
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.check_name
|
21
|
-
raise "Must be defined by subclass"
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.linter_src
|
25
|
-
raise "Must be defined by subclass"
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.display_error(error_object, file)
|
29
|
-
return "" unless error_object
|
30
|
-
|
31
|
-
line = error_object['line'].to_i + 1
|
32
|
-
"#{error_object['reason']}\n#{file}:#{line} #{error_object['evidence']}"
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'pre-commit/checks/js_check'
|
2
|
-
|
3
|
-
module PreCommit
|
4
|
-
class JshintCheck < JsCheck
|
5
|
-
def self.config
|
6
|
-
if config_file = [ENV['JSHINT_CONFIG'], ".jshintrc"].compact.detect { |f| File.exist?(f) }
|
7
|
-
ExecJS.exec("return (#{File.read(config_file)});")
|
8
|
-
else
|
9
|
-
{}
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.check_name
|
14
|
-
"JSHint"
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.run_check(file)
|
18
|
-
context = ExecJS.compile(File.read(linter_src))
|
19
|
-
context.call('JSHINT', File.read(file), config)
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.linter_src
|
23
|
-
File.expand_path("../../support/jshint/jshint.js", __FILE__)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'pre-commit/checks/js_check'
|
2
|
-
|
3
|
-
module PreCommit
|
4
|
-
class JslintCheck < JsCheck
|
5
|
-
def self.check_name
|
6
|
-
"JSLint"
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.run_check(file)
|
10
|
-
context = ExecJS.compile(File.read(linter_src))
|
11
|
-
if !(context.call('JSLINT', File.read(file)))
|
12
|
-
context.exec('return JSLINT.errors;')
|
13
|
-
else
|
14
|
-
[]
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.linter_src
|
19
|
-
File.expand_path("../../support/jslint/lint.js", __FILE__)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,11 +0,0 @@
|
|
1
|
-
module PreCommit
|
2
|
-
class LocalCheck
|
3
|
-
DEFAULT_LOCATION = "config/pre-commit.rb"
|
4
|
-
|
5
|
-
def self.call(staged_files, script=DEFAULT_LOCATION)
|
6
|
-
return unless File.exist?(script)
|
7
|
-
output = `ruby #{script} #{staged_files.join(" ")} 2>&1`
|
8
|
-
"#{script} failed:\n#{output}" unless $?.success?
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
@@ -1,10 +0,0 @@
|
|
1
|
-
module PreCommit
|
2
|
-
class MergeConflictCheck
|
3
|
-
def self.call(staged_files)
|
4
|
-
return if staged_files.empty?
|
5
|
-
errors = `#{Utils.grep} '<<<<<<<' #{staged_files.join(" ")}`.strip
|
6
|
-
return unless $?.success?
|
7
|
-
"detected a merge conflict\n#{errors}"
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
module PreCommit
|
2
|
-
class MigrationCheck
|
3
|
-
class << self
|
4
|
-
def call(staged_files)
|
5
|
-
migration_files = migration_files(staged_files)
|
6
|
-
schema_files = schema_files(staged_files)
|
7
|
-
|
8
|
-
if migration_files.any? && schema_files.none?
|
9
|
-
"It looks like you're adding a migration, but did not update the schema file"
|
10
|
-
elsif migration_files.none? && schema_files.any?
|
11
|
-
"You're trying to change the schema without adding a migration file"
|
12
|
-
elsif migration_files.any? && schema_files.any?
|
13
|
-
versions = migration_files.map { |f| f[/\d+/] }
|
14
|
-
schema = schema_files.map { |f| File.read(f) }.join
|
15
|
-
missing_versions = versions.select { |version| !schema.include?(version) }
|
16
|
-
if missing_versions.any?
|
17
|
-
"You did not add the schema versions for #{versions.join(', ')} to #{schema_files.join(' or ')}"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def migration_files(staged_files)
|
25
|
-
staged_files.grep(/db\/migrate\/.*\.rb/)
|
26
|
-
end
|
27
|
-
|
28
|
-
def schema_files(staged_files)
|
29
|
-
staged_files.select { |f| File.basename(f) =~ /schema\.rb|structure.*\.sql/ }
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module PreCommit
|
3
|
-
class NbSpaceCheck
|
4
|
-
def self.call(staged_files)
|
5
|
-
nb_space = " "
|
6
|
-
raise "you messed that up" unless nb_space.bytes.to_a == [194, 160]
|
7
|
-
|
8
|
-
staged_files.reject! { |f| f =~ /^vendor\// || !File.read(f).include?(nb_space) }
|
9
|
-
|
10
|
-
bad = staged_files.map do |file|
|
11
|
-
content = File.read(file).lines.to_a
|
12
|
-
line_no = content.index { |l| l.include?(nb_space) }
|
13
|
-
char_no = content[line_no].index(nb_space)
|
14
|
-
[file, line_no, char_no]
|
15
|
-
end
|
16
|
-
|
17
|
-
return if bad.empty?
|
18
|
-
"Detected non-breaking space in #{bad.map { |f,l,c| "#{f}:#{l+1} character:#{c+1}" }.join(" and")}, remove it!"
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'pre-commit/utils'
|
2
|
-
|
3
|
-
module PreCommit
|
4
|
-
class PhpCheck
|
5
|
-
def self.call(staged_files)
|
6
|
-
staged_files = staged_files.grep /\.(php|engine|theme|install|inc|module|test)$/
|
7
|
-
return if staged_files.empty?
|
8
|
-
|
9
|
-
errors = staged_files.map { |file| run_check(file) }.compact
|
10
|
-
return if errors.empty?
|
11
|
-
|
12
|
-
errors.join("\n")
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.run_check(file)
|
16
|
-
# We force PHP to display errors otherwise they will likely end up in the
|
17
|
-
# error_log and not in stdout.
|
18
|
-
result = `php -d display_errors=1 -l #{file}`
|
19
|
-
# Filter out the obvious note from PHP.
|
20
|
-
result = result.split($/).find_all {|line| line !~ /Errors/}.join($/)
|
21
|
-
# If PHP exited non-zero then there was a parse error.
|
22
|
-
result.strip unless $? == 0
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,11 +0,0 @@
|
|
1
|
-
module PreCommit
|
2
|
-
class RSpecFocusCheck
|
3
|
-
def self.call(staged_files)
|
4
|
-
staged_files = staged_files.grep(/_spec\.rb$/)
|
5
|
-
return if staged_files.empty?
|
6
|
-
result = `#{Utils.grep} ':focus' #{staged_files.join(" ")}`.strip
|
7
|
-
return unless $?.success?
|
8
|
-
":focus found in specs:\n#{result}"
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
require 'pre-commit/utils'
|
2
|
-
require 'rubocop'
|
3
|
-
require 'stringio'
|
4
|
-
|
5
|
-
module PreCommit
|
6
|
-
class RubocopCheck
|
7
|
-
def self.call(staged_files)
|
8
|
-
staged_files = staged_files.grep(/\.rb$/)
|
9
|
-
return if staged_files.empty?
|
10
|
-
config_file = `git config pre-commit.rubocop.config`.chomp
|
11
|
-
|
12
|
-
args = staged_files
|
13
|
-
if !config_file.empty?
|
14
|
-
if !File.exist? config_file
|
15
|
-
$stderr.puts "Warning: rubocop config file '" + config_file + "' does not exist"
|
16
|
-
$stderr.puts "Set the path to the config file using:"
|
17
|
-
$stderr.puts "\tgit config pre-commit.rubocop.config 'path/relative/to/git/dir/rubocop.yml'"
|
18
|
-
$stderr.puts "rubocop will use its default configuration or look for a .rubocop.yml file\n\n"
|
19
|
-
else
|
20
|
-
args = ['-c', config_file] + args
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
success, captured = capture { Rubocop::CLI.new.run(args) == 0 }
|
25
|
-
captured unless success
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.capture
|
29
|
-
$stdout, stdout = StringIO.new, $stdout
|
30
|
-
$stderr, stderr = StringIO.new, $stderr
|
31
|
-
result = yield
|
32
|
-
[result, $stdout.string + $stderr.string]
|
33
|
-
ensure
|
34
|
-
$stdout = stdout
|
35
|
-
$stderr = stderr
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
require 'pre-commit/utils'
|
2
|
-
|
3
|
-
module PreCommit
|
4
|
-
class RubySymbolHashrockets
|
5
|
-
HASHROCKET_PATTERN = '[^:](:{1}(?:\$|@|@@|[_A-Za-z])?\w*[=!?]?\s*=>\s*)'
|
6
|
-
|
7
|
-
def self.call(staged_files)
|
8
|
-
return if staged_files.empty?
|
9
|
-
lines = `#{Utils.grep} '#{HASHROCKET_PATTERN}' #{staged_files.join(" ")}`.strip
|
10
|
-
return unless $?.success?
|
11
|
-
"detected :symbol => value hashrocket:\n#{lines}"
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
module PreCommit
|
2
|
-
class TabsCheck
|
3
|
-
LEADING_TAB_PATTERN = '^ *\t'
|
4
|
-
|
5
|
-
def self.call(staged_files)
|
6
|
-
return if staged_files.empty?
|
7
|
-
errors = `#{Utils.grep} '#{LEADING_TAB_PATTERN}' #{staged_files.join(" ")}`.strip
|
8
|
-
return unless $?.success?
|
9
|
-
"detected tab before initial space:\n#{errors}"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
module PreCommit
|
2
|
-
class WhiteSpaceCheck
|
3
|
-
def self.call(_)
|
4
|
-
errors = `git diff-index --check --cached HEAD -- 2>&1`
|
5
|
-
return if $?.success?
|
6
|
-
|
7
|
-
# Initial commit: diff against the empty tree object
|
8
|
-
if errors =~ /fatal: bad revision 'HEAD'/
|
9
|
-
errors = `git diff-index --check --cached 4b825dc642cb6eb9a060e54bf8d69288fbee4904 -- 2>&1`
|
10
|
-
return if $?.success?
|
11
|
-
end
|
12
|
-
|
13
|
-
errors
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
#!/usr/bin/env sh
|
2
|
-
|
3
|
-
cmd=`git config pre-commit.ruby 2>/dev/null`
|
4
|
-
if test -n "${cmd}"
|
5
|
-
then true
|
6
|
-
elif which rvm > /dev/null
|
7
|
-
then cmd="rvm default do ruby"
|
8
|
-
elif which rbenv > /dev/null
|
9
|
-
then cmd="rbenv exec ruby"
|
10
|
-
else cmd="ruby"
|
11
|
-
fi
|
12
|
-
|
13
|
-
export rvm_silence_path_mismatch_check_flag=1
|
14
|
-
|
15
|
-
${cmd} -rrubygems -e '
|
16
|
-
begin
|
17
|
-
require "pre-commit"
|
18
|
-
true
|
19
|
-
rescue LoadError
|
20
|
-
$stderr.puts "pre-commit: WARNING: Skipping checks because the pre-commit gem is not installed. (Did you change your Ruby version?)"
|
21
|
-
false
|
22
|
-
end and PreCommit.run
|
23
|
-
'
|