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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -5
  3. data/bin/pre-commit +7 -5
  4. data/lib/plugins/pre_commit/checks/before_all.rb +13 -0
  5. data/lib/plugins/pre_commit/checks/ci.rb +12 -0
  6. data/lib/plugins/pre_commit/checks/closure.rb +19 -0
  7. data/lib/plugins/pre_commit/checks/coffeelint.rb +19 -0
  8. data/lib/plugins/pre_commit/checks/console_log.rb +13 -0
  9. data/lib/plugins/pre_commit/checks/debugger.rb +21 -0
  10. data/lib/plugins/pre_commit/checks/gemfile_path.rb +14 -0
  11. data/lib/plugins/pre_commit/checks/js.rb +36 -0
  12. data/lib/plugins/pre_commit/checks/jshint.rb +24 -0
  13. data/lib/plugins/pre_commit/checks/jslint.rb +24 -0
  14. data/lib/plugins/pre_commit/checks/local.rb +13 -0
  15. data/lib/plugins/pre_commit/checks/merge_conflict.rb +12 -0
  16. data/lib/plugins/pre_commit/checks/migration.rb +38 -0
  17. data/lib/plugins/pre_commit/checks/nb_space.rb +23 -0
  18. data/lib/plugins/pre_commit/checks/php.rb +27 -0
  19. data/lib/plugins/pre_commit/checks/pry.rb +12 -0
  20. data/lib/plugins/pre_commit/checks/rspec_focus.rb +13 -0
  21. data/lib/plugins/pre_commit/checks/rubocop.rb +46 -0
  22. data/lib/plugins/pre_commit/checks/ruby_symbol_hashrockets.rb +16 -0
  23. data/lib/plugins/pre_commit/checks/tabs.rb +14 -0
  24. data/lib/plugins/pre_commit/checks/whitespace.rb +21 -0
  25. data/lib/pre-commit/checks.rb +66 -58
  26. data/lib/pre-commit/cli.rb +29 -2
  27. data/lib/pre-commit/support/templates/automatic_hook +35 -0
  28. data/lib/pre-commit/support/templates/default_hook +35 -0
  29. data/lib/pre-commit/support/templates/manual_hook +14 -0
  30. metadata +101 -26
  31. data/lib/pre-commit/checks/ci_check.rb +0 -10
  32. data/lib/pre-commit/checks/closure_check.rb +0 -13
  33. data/lib/pre-commit/checks/console_log_check.rb +0 -11
  34. data/lib/pre-commit/checks/debugger_check.rb +0 -19
  35. data/lib/pre-commit/checks/gemfile_path_check.rb +0 -12
  36. data/lib/pre-commit/checks/js_check.rb +0 -35
  37. data/lib/pre-commit/checks/jshint_check.rb +0 -26
  38. data/lib/pre-commit/checks/jslint_check.rb +0 -22
  39. data/lib/pre-commit/checks/local_check.rb +0 -11
  40. data/lib/pre-commit/checks/merge_conflict_check.rb +0 -10
  41. data/lib/pre-commit/checks/migration_check.rb +0 -33
  42. data/lib/pre-commit/checks/nb_space_check.rb +0 -21
  43. data/lib/pre-commit/checks/php_check.rb +0 -25
  44. data/lib/pre-commit/checks/pry_check.rb +0 -10
  45. data/lib/pre-commit/checks/rspec_focus_check.rb +0 -11
  46. data/lib/pre-commit/checks/rubocop_check.rb +0 -38
  47. data/lib/pre-commit/checks/ruby_symbol_hashrockets.rb +0 -14
  48. data/lib/pre-commit/checks/tabs_check.rb +0 -12
  49. data/lib/pre-commit/checks/whitespace_check.rb +0 -16
  50. data/lib/pre-commit/support/templates/pre-commit-hook +0 -23
@@ -0,0 +1,16 @@
1
+ require 'pre-commit/utils'
2
+
3
+ module PreCommit
4
+ module Checks
5
+ class RubySymbolHashrockets
6
+ HASHROCKET_PATTERN = '[^:](:{1}(?:\$|@|@@|[_A-Za-z])?\w*[=!?]?\s*=>\s*)'
7
+
8
+ def self.call(staged_files)
9
+ return if staged_files.empty?
10
+ lines = `#{PreCommit::Utils.grep} '#{HASHROCKET_PATTERN}' #{staged_files.join(" ")}`.strip
11
+ return unless $?.success?
12
+ "detected :symbol => value hashrocket:\n#{lines}"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module PreCommit
2
+ module Checks
3
+ class Tabs
4
+ LEADING_TAB_PATTERN = '^ *\t'
5
+
6
+ def self.call(staged_files)
7
+ return if staged_files.empty?
8
+ errors = `#{PreCommit::Utils.grep} '#{LEADING_TAB_PATTERN}' #{staged_files.join(" ")}`.strip
9
+ return unless $?.success?
10
+ "detected tab before initial space:\n#{errors}"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ module PreCommit
2
+ module Checks
3
+ class Whitespace
4
+ def self.supports(name)
5
+ name == :white_space
6
+ end
7
+ def self.call(_)
8
+ errors = `git diff-index --check --cached HEAD -- 2>&1`
9
+ return if $?.success?
10
+
11
+ # Initial commit: diff against the empty tree object
12
+ if errors =~ /fatal: bad revision 'HEAD'/
13
+ errors = `git diff-index --check --cached 4b825dc642cb6eb9a060e54bf8d69288fbee4904 -- 2>&1`
14
+ return if $?.success?
15
+ end
16
+
17
+ errors
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,48 +1,14 @@
1
+ require 'pluginator'
1
2
  require 'pre-commit/utils'
2
- require 'pre-commit/checks/merge_conflict_check'
3
- require 'pre-commit/checks/tabs_check'
4
- require 'pre-commit/checks/console_log_check'
5
- require 'pre-commit/checks/debugger_check'
6
- require 'pre-commit/checks/local_check'
7
- require 'pre-commit/checks/nb_space_check'
8
- require 'pre-commit/checks/jslint_check'
9
- require 'pre-commit/checks/jshint_check'
10
- require 'pre-commit/checks/migration_check'
11
- require 'pre-commit/checks/ci_check'
12
- require 'pre-commit/checks/php_check'
13
- require 'pre-commit/checks/pry_check'
14
- require 'pre-commit/checks/rspec_focus_check'
15
- require 'pre-commit/checks/ruby_symbol_hashrockets'
16
- require 'pre-commit/checks/whitespace_check'
17
- require 'pre-commit/checks/closure_check'
18
- require 'pre-commit/checks/gemfile_path_check'
19
- begin
20
- require 'pre-commit/checks/rubocop_check'
21
- rescue LoadError # no rubocop
22
- end
23
3
 
24
4
  module PreCommit
25
- CHECKS = {
26
- :white_space => WhiteSpaceCheck,
27
- :console_log => ConsoleLogCheck,
28
- :js_lint => JslintCheck,
29
- :jshint => JshintCheck,
30
- :debugger => DebuggerCheck,
31
- :pry => PryCheck,
32
- :local => LocalCheck,
33
- :nb_space => NbSpaceCheck,
34
- :tabs => TabsCheck,
35
- :closure_syntax_check => ClosureCheck,
36
- :merge_conflict => MergeConflictCheck,
37
- :migrations => MigrationCheck,
38
- :ci => CiCheck.new,
39
- :php => PhpCheck.new,
40
- :rspec_focus => RSpecFocusCheck,
41
- :ruby_symbol_hashrockets => RubySymbolHashrockets,
42
- :gemfile_path => GemfilePathCheck
43
- }
44
-
45
- CHECKS[:rubocop] = RubocopCheck if defined?(Rubocop)
5
+
6
+ DEFAULT_CHECKS = [
7
+ :white_space, :console_log, :debugger, :pry, :tabs, :jshint,
8
+ :migrations, :merge_conflict, :local, :nb_space
9
+ ]
10
+
11
+ DEFAULT_WARNINGS = []
46
12
 
47
13
  # Can not delete this method with out a deprecation strategy.
48
14
  # It is refered to in the generated pre-commit hook in versions 0.0-0.1.1
@@ -56,26 +22,68 @@ module PreCommit
56
22
  # logic in it. The we have freedom to change the gem implementation however
57
23
  # we want, and nobody is forced to update their pre-commit binary.
58
24
  def self.checks_to_run
59
- checks_to_run = `git config pre-commit.checks`.chomp.split(/,\s*/).map(&:to_sym)
25
+ @checks_to_run ||= find_plugins(configured_checks)
26
+ end
60
27
 
61
- if checks_to_run.empty?
62
- CHECKS.values_at(:white_space, :console_log, :debugger, :pry, :tabs, :jshint,
63
- :migrations, :merge_conflict, :local, :nb_space)
64
- else
65
- [:js_lint, :rubocop].each do |check|
66
- if checks_to_run.delete("#{check}_all".to_sym) || checks_to_run.delete("#{check}_new".to_sym)
67
- $stderr.puts "please use just '#{check}' as check name"
68
- checks_to_run << check
69
- end
70
- end
71
-
72
- CHECKS.values_at(*checks_to_run)
73
- end.compact
28
+ def self.warnings_to_run
29
+ @warnings_to_run ||= find_plugins(configured_warnings)
30
+ end
31
+
32
+ def self.configured_checks
33
+ @configured_checks ||= get_git_config('checks', DEFAULT_CHECKS)
34
+ end
35
+
36
+ def self.configured_warnings
37
+ @configured_warnings ||= get_git_config('warnings', DEFAULT_WARNINGS)
38
+ end
39
+
40
+ def self.get_git_config(name, default)
41
+ array_or_default(
42
+ `git config pre-commit.#{name}`.chomp.split(/,\s*/).map(&:to_sym),
43
+ default
44
+ )
45
+ end
46
+
47
+ def self.array_or_default(list, default)
48
+ list = default if list.nil? || list.empty?
49
+ list
50
+ end
51
+
52
+ def self.find_plugins(names)
53
+ names.map{|name| find_plugin(name) }.compact
54
+ end
55
+
56
+ def self.find_plugin(name)
57
+ pluginator.first_class('checks', name) ||
58
+ pluginator.first_ask('checks', 'supports', name) ||
59
+ begin
60
+ $stderr.puts "Could not find plugin supporting #{name}."
61
+ nil
62
+ end
63
+ end
64
+
65
+ def self.pluginator
66
+ @pluginator ||= Pluginator.find('pre_commit', :extends => [:first_ask, :first_class] )
67
+ end
68
+
69
+ def self.execute_list(list)
70
+ list.map { |cmd| cmd.call(@staged_files.dup) }.compact
74
71
  end
75
72
 
76
73
  def self.run
77
- staged_files = Utils.staged_files
78
- errors = checks_to_run.map { |cmd| cmd.call(staged_files.dup) }.compact
74
+ @staged_files = Utils.staged_files
75
+ show_warnings( execute_list(warnings_to_run) )
76
+ show_errors( execute_list(checks_to_run ) )
77
+ end
78
+
79
+ def self.show_warnings(warnings)
80
+ if warnings.any?
81
+ $stderr.puts "pre-commit: Some warnings were raised. These will not stop commit:"
82
+ $stderr.puts warnings.join("\n")
83
+ end
84
+ end
85
+
86
+ def self.show_errors(errors)
79
87
  if errors.any?
80
88
  $stderr.puts "pre-commit: Stopping commit because of errors."
81
89
  $stderr.puts errors.join("\n")
@@ -1,15 +1,42 @@
1
1
  require 'fileutils'
2
2
 
3
3
  module PreCommit
4
+
5
+ TemplateNotFound = Class.new(StandardError)
6
+
4
7
  class Cli
5
8
 
6
9
  PRE_COMMIT_HOOK_PATH = '.git/hooks/pre-commit'
10
+ TEMPLATE_DIR = File.expand_path("../support/templates/", __FILE__)
11
+
12
+ attr_reader :templates
13
+
14
+ def initialize
15
+ @templates = load_templates
16
+ end
17
+
18
+ def install(key = nil)
19
+ key ||= "default"
20
+ hook = templates[key.sub(/^--/, "")]
21
+
22
+ raise TemplateNotFound.new("Could not find template #{key}") unless hook
7
23
 
8
- def install
9
- hook = File.expand_path("../support/templates/pre-commit-hook", __FILE__)
10
24
  FileUtils.cp(hook, PRE_COMMIT_HOOK_PATH)
11
25
  FileUtils.chmod(0755, PRE_COMMIT_HOOK_PATH)
12
26
  end
13
27
 
28
+ private
29
+
30
+ def load_templates
31
+ pattern = File.join(TEMPLATE_DIR, "*_hook")
32
+
33
+ Dir.glob(pattern).inject({}) do |hash, file|
34
+ key = file.match(/\/([^\/]+?)_hook$/)[1]
35
+ hash[key] = file
36
+
37
+ hash
38
+ end
39
+ end
40
+
14
41
  end
15
42
  end
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env sh
2
+
3
+ # This hook has a focus on portability.
4
+ # This hook will attempt to setup your environment before running checks.
5
+ #
6
+ # If you would like `pre-commit` to get out of your way and you are comfortable
7
+ # setting up your own environment, you can install the manual hook using:
8
+ #
9
+ # pre-commit install --manual
10
+ #
11
+
12
+ cmd=`git config pre-commit.ruby 2>/dev/null`
13
+ if test -n "${cmd}"
14
+ then true
15
+ elif which rvm > /dev/null
16
+ then cmd="rvm default do ruby"
17
+ elif which rbenv > /dev/null
18
+ then cmd="rbenv exec ruby"
19
+ else cmd="ruby"
20
+ fi
21
+
22
+ export rvm_silence_path_mismatch_check_flag=1
23
+
24
+ ${cmd} -rrubygems -e '
25
+ begin
26
+ require "pre-commit"
27
+ true
28
+ rescue LoadError => e
29
+ $stderr.puts <<-MESSAGE
30
+ pre-commit: WARNING: Skipping checks because: #{e}
31
+ pre-commit: Did you set your Ruby version?
32
+ MESSAGE
33
+ false
34
+ end and PreCommit.run
35
+ '
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env sh
2
+
3
+ # This hook has a focus on portability.
4
+ # This hook will attempt to setup your environment before running checks.
5
+ #
6
+ # If you would like `pre-commit` to get out of your way and you are comfortable
7
+ # setting up your own environment, you can install the manual hook using:
8
+ #
9
+ # pre-commit install --manual
10
+ #
11
+
12
+ cmd=`git config pre-commit.ruby 2>/dev/null`
13
+ if test -n "${cmd}"
14
+ then true
15
+ elif which rvm > /dev/null
16
+ then cmd="rvm default do ruby"
17
+ elif which rbenv > /dev/null
18
+ then cmd="rbenv exec ruby"
19
+ else cmd="ruby"
20
+ fi
21
+
22
+ export rvm_silence_path_mismatch_check_flag=1
23
+
24
+ ${cmd} -rrubygems -e '
25
+ begin
26
+ require "pre-commit"
27
+ true
28
+ rescue LoadError => e
29
+ $stderr.puts <<-MESSAGE
30
+ pre-commit: WARNING: Skipping checks because: #{e}
31
+ pre-commit: Did you set your Ruby version?
32
+ MESSAGE
33
+ false
34
+ end and PreCommit.run
35
+ '
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # This hook has a focus on simplicity.
4
+ # This hook puts the burden on you to set up your environment properly.
5
+ #
6
+ # If you would like `pre-commit` to attempt to setup your environment for you
7
+ # before the checks run, you can install the automatic environment hook using:
8
+ #
9
+ # pre-commit install --automatic
10
+ #
11
+
12
+ require 'pre-commit'
13
+
14
+ PreCommit.run
metadata CHANGED
@@ -1,24 +1,94 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pre-commit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shajith Chacko, Josh Lubaway
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-16 00:00:00.000000000 Z
11
+ date: 2013-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: execjs
14
+ name: pluginator
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - '>='
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '0.12'
20
20
  type: :runtime
21
21
  prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-reporters
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
22
92
  version_requirements: !ruby/object:Gem::Requirement
23
93
  requirements:
24
94
  - - '>='
@@ -32,38 +102,43 @@ extensions: []
32
102
  extra_rdoc_files:
33
103
  - README.md
34
104
  files:
35
- - lib/pre-commit/checks/ci_check.rb
36
- - lib/pre-commit/checks/closure_check.rb
37
- - lib/pre-commit/checks/console_log_check.rb
38
- - lib/pre-commit/checks/debugger_check.rb
39
- - lib/pre-commit/checks/gemfile_path_check.rb
40
- - lib/pre-commit/checks/js_check.rb
41
- - lib/pre-commit/checks/jshint_check.rb
42
- - lib/pre-commit/checks/jslint_check.rb
43
- - lib/pre-commit/checks/local_check.rb
44
- - lib/pre-commit/checks/merge_conflict_check.rb
45
- - lib/pre-commit/checks/migration_check.rb
46
- - lib/pre-commit/checks/nb_space_check.rb
47
- - lib/pre-commit/checks/php_check.rb
48
- - lib/pre-commit/checks/pry_check.rb
49
- - lib/pre-commit/checks/rspec_focus_check.rb
50
- - lib/pre-commit/checks/rubocop_check.rb
51
- - lib/pre-commit/checks/ruby_symbol_hashrockets.rb
52
- - lib/pre-commit/checks/tabs_check.rb
53
- - lib/pre-commit/checks/whitespace_check.rb
105
+ - lib/plugins/pre_commit/checks/before_all.rb
106
+ - lib/plugins/pre_commit/checks/ci.rb
107
+ - lib/plugins/pre_commit/checks/closure.rb
108
+ - lib/plugins/pre_commit/checks/coffeelint.rb
109
+ - lib/plugins/pre_commit/checks/console_log.rb
110
+ - lib/plugins/pre_commit/checks/debugger.rb
111
+ - lib/plugins/pre_commit/checks/gemfile_path.rb
112
+ - lib/plugins/pre_commit/checks/js.rb
113
+ - lib/plugins/pre_commit/checks/jshint.rb
114
+ - lib/plugins/pre_commit/checks/jslint.rb
115
+ - lib/plugins/pre_commit/checks/local.rb
116
+ - lib/plugins/pre_commit/checks/merge_conflict.rb
117
+ - lib/plugins/pre_commit/checks/migration.rb
118
+ - lib/plugins/pre_commit/checks/nb_space.rb
119
+ - lib/plugins/pre_commit/checks/php.rb
120
+ - lib/plugins/pre_commit/checks/pry.rb
121
+ - lib/plugins/pre_commit/checks/rspec_focus.rb
122
+ - lib/plugins/pre_commit/checks/rubocop.rb
123
+ - lib/plugins/pre_commit/checks/ruby_symbol_hashrockets.rb
124
+ - lib/plugins/pre_commit/checks/tabs.rb
125
+ - lib/plugins/pre_commit/checks/whitespace.rb
54
126
  - lib/pre-commit/checks.rb
55
127
  - lib/pre-commit/cli.rb
56
128
  - lib/pre-commit/runner.rb
57
129
  - lib/pre-commit/support/closure/compiler.jar
58
130
  - lib/pre-commit/support/jshint/jshint.js
59
131
  - lib/pre-commit/support/jslint/lint.js
60
- - lib/pre-commit/support/templates/pre-commit-hook
132
+ - lib/pre-commit/support/templates/automatic_hook
133
+ - lib/pre-commit/support/templates/default_hook
134
+ - lib/pre-commit/support/templates/manual_hook
61
135
  - lib/pre-commit/utils.rb
62
136
  - lib/pre-commit.rb
63
137
  - README.md
64
138
  - bin/pre-commit
65
139
  homepage: http://github.com/jish/pre-commit
66
- licenses: []
140
+ licenses:
141
+ - Apache 2.0
67
142
  metadata: {}
68
143
  post_install_message:
69
144
  rdoc_options:
@@ -83,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
158
  version: '0'
84
159
  requirements: []
85
160
  rubyforge_project:
86
- rubygems_version: 2.1.1
161
+ rubygems_version: 2.1.11
87
162
  signing_key:
88
163
  specification_version: 3
89
164
  summary: A slightly better git pre-commit hook