lib/pre-commit/checks.rb in pre-commit-0.8.1 vs lib/pre-commit/checks.rb in pre-commit-0.9.0
- old
+ new
@@ -1,64 +1,50 @@
-require 'support/all'
require 'pre-commit/utils'
-require 'pre-commit/checks/merge_conflict'
-require 'pre-commit/checks/tabs'
-require 'pre-commit/checks/console_log'
+require 'pre-commit/checks/merge_conflict_check'
+require 'pre-commit/checks/tabs_check'
+require 'pre-commit/checks/console_log_check'
require 'pre-commit/checks/debugger_check'
require 'pre-commit/checks/local_check'
+require 'pre-commit/checks/nb_space_check'
require 'pre-commit/checks/jslint_check'
require 'pre-commit/checks/jshint_check'
require 'pre-commit/checks/migration_check'
require 'pre-commit/checks/ci_check'
require 'pre-commit/checks/php_check'
require 'pre-commit/checks/pry_check'
require 'pre-commit/checks/rspec_focus_check'
require 'pre-commit/checks/ruby_symbol_hashrockets'
+require 'pre-commit/checks/whitespace_check'
+require 'pre-commit/checks/closure_check'
+require 'pre-commit/checks/gemfile_path_check'
begin
require 'pre-commit/checks/rubocop_check'
rescue LoadError # no rubocop
end
module PreCommit
-
- WhiteSpace = lambda {
- WhiteSpaceChecker.check
- }
-
- ClosureSyntaxCheck = lambda {
- if File.exists?('public/javascripts') && (args = Utils.staged_files('public/javascripts')).size > 0
- ClosureChecker.check(args.split(" "))
- else
- true
- end
- }
-
- Checks = {
- :white_space => WhiteSpace,
- :console_log => ConsoleLog,
- :js_lint_all => JslintCheck.new(:all),
- :js_lint_new => JslintCheck.new(:new),
- :jshint => JshintCheck.new,
+ CHECKS = {
+ :white_space => WhiteSpaceCheck,
+ :console_log => ConsoleLogCheck,
+ :js_lint => JslintCheck,
+ :jshint => JshintCheck,
:debugger => DebuggerCheck,
:pry => PryCheck,
:local => LocalCheck,
- :tabs => Tabs,
- :closure_syntax_check => ClosureSyntaxCheck,
- :merge_conflict => MergeConflict,
- :migrations => MigrationCheck.new,
+ :nb_space => NbSpaceCheck,
+ :tabs => TabsCheck,
+ :closure_syntax_check => ClosureCheck,
+ :merge_conflict => MergeConflictCheck,
+ :migrations => MigrationCheck,
:ci => CiCheck.new,
:php => PhpCheck.new,
:rspec_focus => RSpecFocusCheck,
- :ruby_symbol_hashrockets => RubySymbolHashrockets
+ :ruby_symbol_hashrockets => RubySymbolHashrockets,
+ :gemfile_path => GemfilePathCheck
}
- if defined?(Rubocop)
- Checks.merge!({
- :rubocop_new => RubocopCheck.new(:new),
- :rubocop_all => RubocopCheck.new(:all)
- })
- end
+ CHECKS[:rubocop] = RubocopCheck if defined?(Rubocop)
# Can not delete this method with out a deprecation strategy.
# It is refered to in the generated pre-commit hook in versions 0.0-0.1.1
#
# NOTE: The deprecation strategy *may* be just delete it since, we're still
@@ -71,20 +57,32 @@
# we want, and nobody is forced to update their pre-commit binary.
def self.checks_to_run
checks_to_run = `git config pre-commit.checks`.chomp.split(/,\s*/).map(&:to_sym)
if checks_to_run.empty?
- Checks.values_at(:white_space, :console_log, :debugger, :pry, :tabs, :jshint,
- :migrations, :merge_conflict, :local)
+ CHECKS.values_at(:white_space, :console_log, :debugger, :pry, :tabs, :jshint,
+ :migrations, :merge_conflict, :local, :nb_space)
else
- Checks.values_at(*checks_to_run)
+ [:js_lint, :rubocop].each do |check|
+ if checks_to_run.delete("#{check}_all".to_sym) || checks_to_run.delete("#{check}_new".to_sym)
+ $stderr.puts "please use just '#{check}' as check name"
+ checks_to_run << check
+ end
+ end
+
+ CHECKS.values_at(*checks_to_run)
end.compact
end
def self.run
- exit_status = self.checks_to_run.inject(true) do |acc, cmd|
- cmd.call && acc
+ staged_files = Utils.staged_files
+ errors = checks_to_run.map { |cmd| cmd.call(staged_files.dup) }.compact
+ if errors.any?
+ puts "pre-commit: Stopping commit because of errors."
+ puts errors.join("\n")
+ puts "pre-commit: You can bypass this check using `git commit -n`"
+ exit 1
+ else
+ exit 0
end
-
- exit(exit_status ? 0 : 1)
end
end