lib/pre-commit/checks.rb in pre-commit-0.11.0 vs lib/pre-commit/checks.rb in pre-commit-0.12.0
- old
+ new
@@ -1,51 +1,17 @@
+require 'pluginator'
require 'pre-commit/utils'
-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
- CHECKS = {
- :white_space => WhiteSpaceCheck,
- :console_log => ConsoleLogCheck,
- :js_lint => JslintCheck,
- :jshint => JshintCheck,
- :debugger => DebuggerCheck,
- :pry => PryCheck,
- :local => LocalCheck,
- :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,
- :gemfile_path => GemfilePathCheck
- }
- CHECKS[:rubocop] = RubocopCheck if defined?(Rubocop)
+ DEFAULT_CHECKS = [
+ :white_space, :console_log, :debugger, :pry, :tabs, :jshint,
+ :migrations, :merge_conflict, :local, :nb_space
+ ]
+ DEFAULT_WARNINGS = []
+
# 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
# pre 1.0.
@@ -54,29 +20,71 @@
# Actually, on the deprecation note. This method isn't really the problem.
# The problem is the default generated pre-commit hook. It shouldn't have
# logic in it. The we have freedom to change the gem implementation however
# 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)
+ @checks_to_run ||= find_plugins(configured_checks)
+ end
- if checks_to_run.empty?
- CHECKS.values_at(:white_space, :console_log, :debugger, :pry, :tabs, :jshint,
- :migrations, :merge_conflict, :local, :nb_space)
- else
- [: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
+ def self.warnings_to_run
+ @warnings_to_run ||= find_plugins(configured_warnings)
+ end
- CHECKS.values_at(*checks_to_run)
- end.compact
+ def self.configured_checks
+ @configured_checks ||= get_git_config('checks', DEFAULT_CHECKS)
end
+ def self.configured_warnings
+ @configured_warnings ||= get_git_config('warnings', DEFAULT_WARNINGS)
+ end
+
+ def self.get_git_config(name, default)
+ array_or_default(
+ `git config pre-commit.#{name}`.chomp.split(/,\s*/).map(&:to_sym),
+ default
+ )
+ end
+
+ def self.array_or_default(list, default)
+ list = default if list.nil? || list.empty?
+ list
+ end
+
+ def self.find_plugins(names)
+ names.map{|name| find_plugin(name) }.compact
+ end
+
+ def self.find_plugin(name)
+ pluginator.first_class('checks', name) ||
+ pluginator.first_ask('checks', 'supports', name) ||
+ begin
+ $stderr.puts "Could not find plugin supporting #{name}."
+ nil
+ end
+ end
+
+ def self.pluginator
+ @pluginator ||= Pluginator.find('pre_commit', :extends => [:first_ask, :first_class] )
+ end
+
+ def self.execute_list(list)
+ list.map { |cmd| cmd.call(@staged_files.dup) }.compact
+ end
+
def self.run
- staged_files = Utils.staged_files
- errors = checks_to_run.map { |cmd| cmd.call(staged_files.dup) }.compact
+ @staged_files = Utils.staged_files
+ show_warnings( execute_list(warnings_to_run) )
+ show_errors( execute_list(checks_to_run ) )
+ end
+
+ def self.show_warnings(warnings)
+ if warnings.any?
+ $stderr.puts "pre-commit: Some warnings were raised. These will not stop commit:"
+ $stderr.puts warnings.join("\n")
+ end
+ end
+
+ def self.show_errors(errors)
if errors.any?
$stderr.puts "pre-commit: Stopping commit because of errors."
$stderr.puts errors.join("\n")
$stderr.puts
$stderr.puts "pre-commit: You can bypass this check using `git commit -n`"